Skip to content

Commit

Permalink
update endpoint to restrict removing with datasources (elastic#64978)
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed May 4, 2020
1 parent 445d703 commit a539a0d
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import { SavedObjectsClientContract } from 'src/core/server';
import Boom from 'boom';
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants';
import { PACKAGES_SAVED_OBJECT_TYPE, DATASOURCE_SAVED_OBJECT_TYPE } from '../../../constants';
import { AssetReference, AssetType, ElasticsearchAssetType } from '../../../types';
import { CallESAsCurrentUser } from '../../../types';
import { getInstallation, savedObjectTypes } from './index';
import { installIndexPatterns } from '../kibana/index_pattern/install';
import { datasourceService } from '../..';

export async function removeInstallation(options: {
savedObjectsClient: SavedObjectsClientContract;
Expand All @@ -26,6 +27,17 @@ export async function removeInstallation(options: {
throw Boom.badRequest(`${pkgName} is installed by default and cannot be removed`);
const installedObjects = installation.installed || [];

const { total } = await datasourceService.list(savedObjectsClient, {
kuery: `${DATASOURCE_SAVED_OBJECT_TYPE}.package.name:${pkgName}`,
page: 0,
perPage: 0,
});

if (total > 0)
throw Boom.badRequest(
`unable to remove package with existing datasource(s) in use by agent(s)`
);

// Delete the manager saved object with references to the asset objects
// could also update with [] or some other state
await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName);
Expand Down

0 comments on commit a539a0d

Please sign in to comment.