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] update endpoint to restrict removing with datasources #64978

Merged
Merged
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
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