Skip to content

Commit

Permalink
create index patterns at the same time as other assets
Browse files Browse the repository at this point in the history
  • Loading branch information
hop-dev committed Nov 29, 2021
1 parent b3c5c1b commit 5b3c3bc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getAsset, getPathParts } from '../../archive';
import { KibanaAssetType, KibanaSavedObjectType } from '../../../../types';
import type { AssetType, AssetReference, AssetParts } from '../../../../types';
import { savedObjectTypes } from '../../packages';
import { indexPatternTypes } from '../index_pattern/install';
import { indexPatternTypes, getIndexPatternSavedObjects } from '../index_pattern/install';
import { appContextService } from '../../../../services';

const validKibanaAssetTypes = new Set(Object.values(KibanaAssetType));
Expand Down Expand Up @@ -97,9 +97,14 @@ export async function installKibanaAssets(options: {
return [];
}

// As we use `import` to create our saved objects, we have to install
// their references (the index patterns) at the same time
// to prevent a reference error
const indexPatternSavedObjects = getIndexPatternSavedObjects() as ArchiveAsset[];

const installedAssets = await installKibanaSavedObjects({
savedObjectsClient,
kibanaAssets: assetsToInstall,
kibanaAssets: [...indexPatternSavedObjects, ...assetsToInstall],
});

return installedAssets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,23 @@

import type { SavedObjectsClientContract } from 'src/core/server';

import { createListStream } from '@kbn/utils';

import { dataTypes, installationStatuses } from '../../../../../common/constants';
import { appContextService } from '../../../../services';
import { getPackageSavedObjects } from '../../packages/get';
const INDEX_PATTERN_SAVED_OBJECT_TYPE = 'index-pattern';

export const indexPatternTypes = Object.values(dataTypes);

export async function installIndexPatterns(savedObjectsClient: SavedObjectsClientContract) {
const logger = appContextService.getLogger();

const indexPatterns = indexPatternTypes.map((indexPatternType) => `${indexPatternType}-*`);
logger.debug(`creating index patterns ${indexPatterns}`);
const kibanaIndexPatterns = indexPatterns.map((indexPattern) => ({
id: indexPattern,
export function getIndexPatternSavedObjects() {
return indexPatternTypes.map((indexPatternType) => ({
id: `${indexPatternType}-*`,
type: INDEX_PATTERN_SAVED_OBJECT_TYPE,
attributes: {
title: indexPattern,
title: `${indexPatternType}-*`,
timeFieldName: '@timestamp',
allowNoIndex: true,
},
}));

const savedObjectsImporter = appContextService
.getSavedObjects()
.createImporter(savedObjectsClient);

const { successResults, errors } = await savedObjectsImporter.import({
overwrite: true,
readStream: createListStream(kibanaIndexPatterns),
createNewCopies: false,
});

if (errors?.length) {
throw new Error(
`Encountered ${errors.length} creating index patterns: ${JSON.stringify(
errors.map(({ id, error }) => ({ id, error })) // discard other fields
)}`
);
}

return successResults || [];
}

export async function removeUnusedIndexPatterns(savedObjectsClient: SavedObjectsClientContract) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jest.mock('./get');

import { updateCurrentWriteIndices } from '../elasticsearch/template/template';
import { installKibanaAssets } from '../kibana/assets/install';
import { installIndexPatterns } from '../kibana/index_pattern/install';

import { _installPackage } from './_install_package';

Expand All @@ -29,9 +28,6 @@ const mockedUpdateCurrentWriteIndices = updateCurrentWriteIndices as jest.Mocked
const mockedGetKibanaAssets = installKibanaAssets as jest.MockedFunction<
typeof installKibanaAssets
>;
const mockedInstallIndexPatterns = installIndexPatterns as jest.MockedFunction<
typeof installIndexPatterns
>;

function sleep(millis: number) {
return new Promise((resolve) => setTimeout(resolve, millis));
Expand All @@ -49,14 +45,11 @@ describe('_installPackage', () => {
afterEach(async () => {
appContextService.stop();
});
it('handles errors from installIndexPatterns or installKibanaAssets', async () => {
// force errors from either/both these functions
it('handles errors from installKibanaAssets', async () => {
// force errors from this function
mockedGetKibanaAssets.mockImplementation(async () => {
throw new Error('mocked async error A: should be caught');
});
mockedInstallIndexPatterns.mockImplementation(async () => {
throw new Error('mocked async error B: should be caught');
});

// pick any function between when those are called and when await Promise.all is defined later
// and force it to take long enough for the errors to occur
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { ElasticsearchClient, SavedObjectsClientContract } from 'src/core/s

import { appContextService } from '../../app_context';
import * as Registry from '../registry';
import { installIndexPatterns } from '../kibana/index_pattern/install';

import type { InstallResult } from '../../../types';

Expand All @@ -31,7 +30,6 @@ export async function bulkInstallPackages({
}: BulkInstallPackagesParams): Promise<BulkInstallResponse[]> {
const logger = appContextService.getLogger();
const installSource = 'registry';
await installIndexPatterns(savedObjectsClient);

const packagesResults = await Promise.allSettled(
packagesToInstall.map((pkg) => {
Expand Down Expand Up @@ -73,7 +71,6 @@ export async function bulkInstallPackages({
esClient,
pkgkey: Registry.pkgToPkgKey(pkgKeyProps),
installSource,
skipIndexPatternCreation: true,
force,
});
if (installResult.error) {
Expand Down
14 changes: 3 additions & 11 deletions x-pack/plugins/fleet/server/services/epm/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import * as Registry from '../registry';
import { setPackageInfo, parseAndVerifyArchiveEntries, unpackBufferToCache } from '../archive';
import { toAssetReference } from '../kibana/assets/install';
import type { ArchiveAsset } from '../kibana/assets/install';
import { installIndexPatterns } from '../kibana/index_pattern/install';

import type { PackageUpdateEvent } from '../../upgrade_sender';
import { sendTelemetryEvents, UpdateEventType } from '../../upgrade_sender';
Expand Down Expand Up @@ -438,23 +437,16 @@ async function installPackageByUpload({
}
}

export type InstallPackageParams = {
skipIndexPatternCreation?: boolean;
} & (
export type InstallPackageParams =
| ({ installSource: Extract<InstallSource, 'registry'> } & InstallRegistryPackageParams)
| ({ installSource: Extract<InstallSource, 'upload'> } & InstallUploadedArchiveParams)
);
| ({ installSource: Extract<InstallSource, 'upload'> } & InstallUploadedArchiveParams);

export async function installPackage(args: InstallPackageParams) {
if (!('installSource' in args)) {
throw new Error('installSource is required');
}
const logger = appContextService.getLogger();
const { savedObjectsClient, esClient, skipIndexPatternCreation = false } = args;

if (!skipIndexPatternCreation) {
await installIndexPatterns(savedObjectsClient);
}
const { savedObjectsClient, esClient } = args;

if (args.installSource === 'registry') {
const { pkgkey, force } = args;
Expand Down

0 comments on commit 5b3c3bc

Please sign in to comment.