update endpoint to restrict removing with datasources (#64978)

This commit is contained in:
Sandra Gonzales 2020-05-04 08:33:02 -04:00 committed by GitHub
parent 5bcf2c8b89
commit 3356a19294
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
@ -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);