diff --git a/x-pack/legacy/plugins/epm/common/types.ts b/x-pack/legacy/plugins/epm/common/types.ts index ec1fe98d1a1a4..6c1a34ae834a5 100644 --- a/x-pack/legacy/plugins/epm/common/types.ts +++ b/x-pack/legacy/plugins/epm/common/types.ts @@ -156,8 +156,7 @@ export type PackageListItem = Installable; export type PackageInfo = Installable; -export type Installation = SavedObject; -export interface InstallationAttributes extends SavedObjectAttributes { +export interface Installation extends SavedObjectAttributes { installed: AssetReference[]; } @@ -165,7 +164,7 @@ export type Installable = Installed | NotInstalled; export type Installed = T & { status: InstallationStatus.installed; - savedObject: Installation; + savedObject: SavedObject; }; export type NotInstalled = T & { diff --git a/x-pack/legacy/plugins/epm/server/packages/get.ts b/x-pack/legacy/plugins/epm/server/packages/get.ts index 7b8d258b9e5bd..2617fa46b4e7e 100644 --- a/x-pack/legacy/plugins/epm/server/packages/get.ts +++ b/x-pack/legacy/plugins/epm/server/packages/get.ts @@ -6,7 +6,7 @@ import { SavedObjectsClientContract } from 'src/core/server/'; import { SAVED_OBJECT_TYPE_PACKAGES } from '../../common/constants'; -import { Installation, InstallationAttributes, Installed, NotInstalled } from '../../common/types'; +import { Installation, Installed, NotInstalled } from '../../common/types'; import * as Registry from '../registry'; import { createInstallableFrom } from './index'; @@ -35,7 +35,7 @@ export async function getPackages( type: SAVED_OBJECT_TYPE_PACKAGES, id: `${name}-${version}`, })); - const results = await savedObjectsClient.bulkGet(searchObjects); + const results = await savedObjectsClient.bulkGet(searchObjects); const savedObjects = results.saved_objects.filter(o => !o.error); // ignore errors for now const packageList = registryItems .map(item => @@ -74,13 +74,21 @@ export async function getPackageInfo(options: { export async function getInstallationObject(options: { savedObjectsClient: SavedObjectsClientContract; pkgkey: string; -}): Promise { +}) { const { savedObjectsClient, pkgkey } = options; return savedObjectsClient - .get(SAVED_OBJECT_TYPE_PACKAGES, pkgkey) + .get(SAVED_OBJECT_TYPE_PACKAGES, pkgkey) .catch(e => undefined); } +export async function getInstallation(options: { + savedObjectsClient: SavedObjectsClientContract; + pkgkey: string; +}) { + const savedObject = await getInstallationObject(options); + return savedObject?.attributes; +} + function sortByName(a: { name: string }, b: { name: string }) { if (a.name > b.name) { return 1; diff --git a/x-pack/legacy/plugins/epm/server/packages/index.ts b/x-pack/legacy/plugins/epm/server/packages/index.ts index 1b1851540de48..2286938a5b91a 100644 --- a/x-pack/legacy/plugins/epm/server/packages/index.ts +++ b/x-pack/legacy/plugins/epm/server/packages/index.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { SavedObject } from '../../../../../../src/core/server'; import { AssetType, // ElasticsearchAssetType, @@ -41,7 +42,10 @@ export class PackageNotInstalledError extends Error { // only Kibana Assets use Saved Objects at this point export const savedObjectTypes: AssetType[] = Object.values(KibanaAssetType); -export function createInstallableFrom(from: T, savedObject?: Installation): Installable { +export function createInstallableFrom( + from: T, + savedObject?: SavedObject +): Installable { return savedObject ? { ...from, diff --git a/x-pack/legacy/plugins/epm/server/packages/install.ts b/x-pack/legacy/plugins/epm/server/packages/install.ts index 5159afa0ff816..748c9590f8d8f 100644 --- a/x-pack/legacy/plugins/epm/server/packages/install.ts +++ b/x-pack/legacy/plugins/epm/server/packages/install.ts @@ -6,11 +6,11 @@ import { SavedObject, SavedObjectsClientContract } from 'src/core/server/'; import { SAVED_OBJECT_TYPE_PACKAGES } from '../../common/constants'; -import { AssetReference, InstallationAttributes, KibanaAssetType } from '../../common/types'; +import { AssetReference, Installation, KibanaAssetType } from '../../common/types'; import { installIndexPattern } from '../lib/kibana/index_pattern/install'; import * as Registry from '../registry'; import { getObject } from './get_objects'; -import { getInstallationObject } from './index'; +import { getInstallation } from './index'; export async function installPackage(options: { savedObjectsClient: SavedObjectsClientContract; @@ -64,17 +64,17 @@ export async function saveInstallationReferences(options: { toSave: AssetReference[]; }) { const { savedObjectsClient, pkgkey, toSave } = options; - const savedObject = await getInstallationObject({ savedObjectsClient, pkgkey }); - const savedRefs = savedObject && savedObject.attributes.installed; + const installation = await getInstallation({ savedObjectsClient, pkgkey }); + const savedRefs = installation?.installed || []; const mergeRefsReducer = (current: AssetReference[], pending: AssetReference) => { const hasRef = current.find(c => c.id === pending.id && c.type === pending.type); if (!hasRef) current.push(pending); return current; }; - const toInstall = toSave.reduce(mergeRefsReducer, savedRefs || []); + const toInstall = toSave.reduce(mergeRefsReducer, savedRefs); - await savedObjectsClient.create( + await savedObjectsClient.create( SAVED_OBJECT_TYPE_PACKAGES, { installed: toInstall }, { id: pkgkey, overwrite: true } diff --git a/x-pack/legacy/plugins/epm/server/packages/remove.ts b/x-pack/legacy/plugins/epm/server/packages/remove.ts index b8e02e89d33a3..9e55eebfe5261 100644 --- a/x-pack/legacy/plugins/epm/server/packages/remove.ts +++ b/x-pack/legacy/plugins/epm/server/packages/remove.ts @@ -8,7 +8,7 @@ import { SavedObjectsClientContract } from 'src/core/server/'; import { SAVED_OBJECT_TYPE_PACKAGES } from '../../common/constants'; import { AssetReference, AssetType, ElasticsearchAssetType } from '../../common/types'; import { CallESAsCurrentUser } from '../lib/cluster_access'; -import { getInstallationObject, savedObjectTypes } from './index'; +import { getInstallation, savedObjectTypes } from './index'; export async function removeInstallation(options: { savedObjectsClient: SavedObjectsClientContract; @@ -16,8 +16,8 @@ export async function removeInstallation(options: { callCluster: CallESAsCurrentUser; }): Promise { const { savedObjectsClient, pkgkey, callCluster } = options; - const installation = await getInstallationObject({ savedObjectsClient, pkgkey }); - const installedObjects = (installation && installation.attributes.installed) || []; + const installation = await getInstallation({ savedObjectsClient, pkgkey }); + const installedObjects = installation?.installed || []; // Delete the manager saved object with references to the asset objects // could also update with [] or some other state