From 45fbb57601b6048456982347773ebf33848f6e04 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Wed, 11 Nov 2020 15:20:04 -0500 Subject: [PATCH] Don't make the caller do work the function can do (#83180) --- .../services/epm/packages/_install_package.ts | 26 ++++------- .../server/services/epm/packages/install.ts | 45 +++++++++---------- 2 files changed, 28 insertions(+), 43 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts b/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts index a83d9428b7c93..4d36c3919198c 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts @@ -16,12 +16,10 @@ import { } from '../../../types'; import { installIndexPatterns } from '../kibana/index_pattern/install'; import { installTemplates } from '../elasticsearch/template/install'; -import { generateESIndexPatterns } from '../elasticsearch/template/template'; import { installPipelines, deletePreviousPipelines } from '../elasticsearch/ingest_pipeline/'; import { installILMPolicy } from '../elasticsearch/ilm/install'; import { installKibanaAssets, getKibanaAssets } from '../kibana/assets/install'; import { updateCurrentWriteIndices } from '../elasticsearch/template/template'; -import { isRequiredPackage } from './index'; import { deleteKibanaSavedObjectsAssets } from './remove'; import { installTransform } from '../elasticsearch/transform/install'; import { createInstallation, saveKibanaAssetsRefs, updateVersion } from './install'; @@ -47,30 +45,22 @@ export async function _installPackage({ installType: InstallType; installSource: InstallSource; }): Promise { - const { internal = false, name: pkgName, version: pkgVersion } = packageInfo; - const removable = !isRequiredPackage(pkgName); - const toSaveESIndexPatterns = generateESIndexPatterns(packageInfo.data_streams); + const { name: pkgName, version: pkgVersion } = packageInfo; // add the package installation to the saved object. // if some installation already exists, just update install info - if (!installedPkg) { - await createInstallation({ - savedObjectsClient, - pkgName, - pkgVersion, - internal, - removable, - installed_kibana: [], - installed_es: [], - toSaveESIndexPatterns, - installSource, - }); - } else { + if (installedPkg) { await savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { install_version: pkgVersion, install_status: 'installing', install_started_at: new Date().toISOString(), install_source: installSource, }); + } else { + await createInstallation({ + savedObjectsClient, + packageInfo, + installSource, + }); } // kick off `installIndexPatterns` & `installKibanaAssets` as early as possible because they're the longest running operations diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install.ts b/x-pack/plugins/fleet/server/services/epm/packages/install.ts index e49aa4848809c..023691cad1913 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.ts @@ -4,18 +4,24 @@ * you may not use this file except in compliance with the Elastic License. */ -import { SavedObject, SavedObjectsClientContract } from 'src/core/server'; import semver from 'semver'; import Boom from '@hapi/boom'; import { UnwrapPromise } from '@kbn/utility-types'; -import { BulkInstallPackageInfo, InstallSource, defaultPackages } from '../../../../common'; +import { SavedObject, SavedObjectsClientContract } from 'src/core/server'; +import { generateESIndexPatterns } from '../elasticsearch/template/template'; +import { isRequiredPackage } from './index'; +import { + BulkInstallPackageInfo, + InstallablePackage, + InstallSource, + defaultPackages, +} from '../../../../common'; import { PACKAGES_SAVED_OBJECT_TYPE, MAX_TIME_COMPLETE_INSTALL } from '../../../constants'; import { AssetReference, Installation, CallESAsCurrentUser, AssetType, - KibanaAssetReference, EsAssetReference, InstallType, KibanaAssetType, @@ -346,31 +352,19 @@ export const updateVersion = async ( }; export async function createInstallation(options: { savedObjectsClient: SavedObjectsClientContract; - pkgName: string; - pkgVersion: string; - internal: boolean; - removable: boolean; - installed_kibana: KibanaAssetReference[]; - installed_es: EsAssetReference[]; - toSaveESIndexPatterns: Record; + packageInfo: InstallablePackage; installSource: InstallSource; }) { - const { - savedObjectsClient, - pkgName, - pkgVersion, - internal, - removable, - installed_kibana: installedKibana, - installed_es: installedEs, - toSaveESIndexPatterns, - installSource, - } = options; - await savedObjectsClient.create( + const { savedObjectsClient, packageInfo, installSource } = options; + const { internal = false, name: pkgName, version: pkgVersion } = packageInfo; + const removable = !isRequiredPackage(pkgName); + const toSaveESIndexPatterns = generateESIndexPatterns(packageInfo.data_streams); + + const created = await savedObjectsClient.create( PACKAGES_SAVED_OBJECT_TYPE, { - installed_kibana: installedKibana, - installed_es: installedEs, + installed_kibana: [], + installed_es: [], es_index_patterns: toSaveESIndexPatterns, name: pkgName, version: pkgVersion, @@ -383,7 +377,8 @@ export async function createInstallation(options: { }, { id: pkgName, overwrite: true } ); - return [...installedKibana, ...installedEs]; + + return created; } export const saveKibanaAssetsRefs = async (