Skip to content

Commit

Permalink
Reduce usage of EPM SO. Add getInstallation().
Browse files Browse the repository at this point in the history
Replaced two calls of getInstallationObject() with getInstallation().

Two less places with knowledge of SO internals.

Lots of potential improvements for EPM TS types remain (refactoring/removing Installable, etc), but this is a good incremental step, IMO
  • Loading branch information
John Schulz committed Dec 9, 2019
1 parent c21723f commit 9ecaf44
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
8 changes: 8 additions & 0 deletions x-pack/legacy/plugins/epm/server/packages/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export async function getInstallationObject(options: {
.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;
Expand Down
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/epm/server/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AssetReference, Installation, KibanaAssetType } from '../../common/type
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;
Expand Down Expand Up @@ -64,15 +64,15 @@ 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<Installation>(
SAVED_OBJECT_TYPE_PACKAGES,
Expand Down
6 changes: 3 additions & 3 deletions x-pack/legacy/plugins/epm/server/packages/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ 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;
pkgkey: string;
callCluster: CallESAsCurrentUser;
}): Promise<AssetReference[]> {
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
Expand Down

0 comments on commit 9ecaf44

Please sign in to comment.