Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EPM] Reduce usage of epm-package SavedObject #52576

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions x-pack/legacy/plugins/epm/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,15 @@ export type PackageListItem = Installable<RegistrySearchResult & PackageAddition
export type PackagesGroupedByStatus = Record<InstallationStatus, PackageList>;
export type PackageInfo = Installable<RegistryPackage & PackageAdditions>;

export type Installation = SavedObject<InstallationAttributes>;
export interface InstallationAttributes extends SavedObjectAttributes {
export interface Installation extends SavedObjectAttributes {
installed: AssetReference[];
}

export type Installable<T> = Installed<T> | NotInstalled<T>;

export type Installed<T = {}> = T & {
status: InstallationStatus.installed;
savedObject: Installation;
savedObject: SavedObject<Installation>;
};

export type NotInstalled<T = {}> = T & {
Expand Down
16 changes: 12 additions & 4 deletions x-pack/legacy/plugins/epm/server/packages/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -35,7 +35,7 @@ export async function getPackages(
type: SAVED_OBJECT_TYPE_PACKAGES,
id: `${name}-${version}`,
}));
const results = await savedObjectsClient.bulkGet<InstallationAttributes>(searchObjects);
const results = await savedObjectsClient.bulkGet<Installation>(searchObjects);
const savedObjects = results.saved_objects.filter(o => !o.error); // ignore errors for now
const packageList = registryItems
.map(item =>
Expand Down Expand Up @@ -74,13 +74,21 @@ export async function getPackageInfo(options: {
export async function getInstallationObject(options: {
savedObjectsClient: SavedObjectsClientContract;
pkgkey: string;
}): Promise<Installation | undefined> {
}) {
const { savedObjectsClient, pkgkey } = options;
return savedObjectsClient
.get<InstallationAttributes>(SAVED_OBJECT_TYPE_PACKAGES, pkgkey)
.get<Installation>(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;
Expand Down
6 changes: 5 additions & 1 deletion x-pack/legacy/plugins/epm/server/packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,7 +22,10 @@ export * from './remove';
// only Kibana Assets use Saved Objects at this point
export const savedObjectTypes: AssetType[] = Object.values(KibanaAssetType);

export function createInstallableFrom<T>(from: T, savedObject?: Installation): Installable<T> {
export function createInstallableFrom<T>(
from: T,
savedObject?: SavedObject<Installation>
): Installable<T> {
return savedObject
? {
...from,
Expand Down
12 changes: 6 additions & 6 deletions x-pack/legacy/plugins/epm/server/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InstallationAttributes>(
await savedObjectsClient.create<Installation>(
SAVED_OBJECT_TYPE_PACKAGES,
{ installed: toInstall },
{ id: pkgkey, overwrite: true }
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