Skip to content

Commit

Permalink
remove a couple of appContextService dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
hop-dev committed Dec 6, 2021
1 parent e772263 commit 5945b38
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
24 changes: 12 additions & 12 deletions x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
SavedObject,
SavedObjectsBulkCreateObject,
SavedObjectsClientContract,
SavedObjectsImporter,
Logger,
} from 'src/core/server';
import type { SavedObjectsImportSuccess, SavedObjectsImportFailure } from 'src/core/server/types';

Expand All @@ -21,8 +23,8 @@ import { KibanaAssetType, KibanaSavedObjectType } from '../../../../types';
import type { AssetType, AssetReference, AssetParts } from '../../../../types';
import { savedObjectTypes } from '../../packages';
import { indexPatternTypes, getIndexPatternSavedObjects } from '../index_pattern/install';
import { appContextService } from '../../../../services';

type SavedObjectsImporterContract = Pick<SavedObjectsImporter, 'import' | 'resolveImportErrors'>;
const formatImportErrorsForLog = (errors: SavedObjectsImportFailure[]) =>
JSON.stringify(
errors.map(({ type, id, error }) => ({ type, id, error })) // discard other fields
Expand Down Expand Up @@ -75,11 +77,12 @@ export function createSavedObjectKibanaAsset(asset: ArchiveAsset): SavedObjectTo
}

export async function installKibanaAssets(options: {
savedObjectsClient: SavedObjectsClientContract;
savedObjectsImporter: SavedObjectsImporterContract;
logger: Logger;
pkgName: string;
kibanaAssets: Record<KibanaAssetType, ArchiveAsset[]>;
}): Promise<SavedObjectsImportSuccess[]> {
const { savedObjectsClient, kibanaAssets } = options;
const { kibanaAssets, savedObjectsImporter, logger } = options;
const assetsToInstall = Object.entries(kibanaAssets).flatMap(([assetType, assets]) => {
if (!validKibanaAssetTypes.has(assetType as KibanaAssetType)) {
return [];
Expand Down Expand Up @@ -107,7 +110,8 @@ export async function installKibanaAssets(options: {
const indexPatternSavedObjects = getIndexPatternSavedObjects() as ArchiveAsset[];

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

Expand Down Expand Up @@ -163,11 +167,13 @@ export async function getKibanaAssets(
}

async function installKibanaSavedObjects({
savedObjectsClient,
savedObjectsImporter,
kibanaAssets,
logger,
}: {
savedObjectsClient: SavedObjectsClientContract;
kibanaAssets: ArchiveAsset[];
savedObjectsImporter: SavedObjectsImporterContract;
logger: Logger;
}) {
const toBeSavedObjects = await Promise.all(
kibanaAssets.map((asset) => createSavedObjectKibanaAsset(asset))
Expand All @@ -178,10 +184,6 @@ async function installKibanaSavedObjects({
if (toBeSavedObjects.length === 0) {
return [];
} else {
const savedObjectsImporter = appContextService
.getSavedObjects()
.createImporter(savedObjectsClient);

const { successResults: importSuccessResults = [], errors: importErrors = [] } =
await savedObjectsImporter.import({
overwrite: true,
Expand Down Expand Up @@ -210,8 +212,6 @@ async function installKibanaSavedObjects({
can still be installed, but if a warning is logged it should be reported to
the integrations team. */
if (referenceErrors.length) {
const logger = appContextService.getLogger();

logger.debug(
`Resolving ${
referenceErrors.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ describe('_installPackage', () => {

const installationPromise = _installPackage({
savedObjectsClient: soClient,
// @ts-ignore
savedObjectsImporter: jest.fn(),
esClient,
logger: loggerMock.create(),
paths: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
Logger,
SavedObject,
SavedObjectsClientContract,
SavedObjectsImporter,
} from 'src/core/server';

import {
Expand All @@ -36,7 +37,6 @@ import { installMlModel } from '../elasticsearch/ml_model/';
import { installIlmForDataStream } from '../elasticsearch/datastream_ilm/install';
import { saveArchiveEntries } from '../archive/storage';
import { ConcurrentInstallOperationError } from '../../../errors';

import { packagePolicyService } from '../..';

import { createInstallation, saveKibanaAssetsRefs, updateVersion } from './install';
Expand All @@ -48,6 +48,7 @@ import { deleteKibanaSavedObjectsAssets } from './remove';

export async function _installPackage({
savedObjectsClient,
savedObjectsImporter,
esClient,
logger,
installedPkg,
Expand All @@ -57,6 +58,7 @@ export async function _installPackage({
installSource,
}: {
savedObjectsClient: SavedObjectsClientContract;
savedObjectsImporter: Pick<SavedObjectsImporter, 'import' | 'resolveImportErrors'>;
esClient: ElasticsearchClient;
logger: Logger;
installedPkg?: SavedObject<Installation>;
Expand Down Expand Up @@ -112,8 +114,10 @@ export async function _installPackage({
pkgName,
kibanaAssets
);

await installKibanaAssets({
savedObjectsClient,
logger,
savedObjectsImporter,
pkgName,
kibanaAssets,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jest.mock('../../app_context', () => {
return { error: jest.fn(), debug: jest.fn(), warn: jest.fn() };
}),
getTelemetryEventsSender: jest.fn(),
getSavedObjects: jest.fn(() => ({
createImporter: jest.fn(),
})),
},
};
});
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,15 @@ async function installPackageFromRegistry({
return { error: err, installType };
}

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

// try installing the package, if there was an error, call error handler and rethrow
// @ts-expect-error status is string instead of InstallResult.status 'installed' | 'already_installed'
return _installPackage({
savedObjectsClient,
savedObjectsImporter,
esClient,
logger,
installedPkg,
Expand Down Expand Up @@ -406,9 +411,15 @@ async function installPackageByUpload({
version: packageInfo.version,
packageInfo,
});

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

// @ts-expect-error status is string instead of InstallResult.status 'installed' | 'already_installed'
return _installPackage({
savedObjectsClient,
savedObjectsImporter,
esClient,
logger,
installedPkg,
Expand Down

0 comments on commit 5945b38

Please sign in to comment.