From fd3fd01910aa4f51d499dda7fa4785e50b13fd9a Mon Sep 17 00:00:00 2001 From: Jonathan Buttner Date: Thu, 8 Oct 2020 17:01:56 -0400 Subject: [PATCH] Correctly removing index pattern fields --- .../server/services/epm/packages/remove.ts | 8 +++-- .../apis/epm/install_remove_assets.ts | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts index 417f2871a6cbf5..8d5995f92c95f2 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts @@ -44,9 +44,6 @@ export async function removeInstallation(options: { `unable to remove package with existing package policy(s) in use by agent(s)` ); - // recreate or delete index patterns when a package is uninstalled - await installIndexPatterns(savedObjectsClient); - // Delete the installed assets const installedAssets = [...installation.installed_kibana, ...installation.installed_es]; await deleteAssets(installedAssets, savedObjectsClient, callCluster); @@ -55,6 +52,11 @@ export async function removeInstallation(options: { // could also update with [] or some other state await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName); + // recreate or delete index patterns when a package is uninstalled + // this must be done after deleting the saved object for the current package otherwise it will retrieve the package + // from the registry again and reinstall the index patterns + await installIndexPatterns(savedObjectsClient); + // remove the package archive and its contents from the cache so that a reinstall fetches // a fresh copy from the registry deletePackageCache(pkgName, pkgVersion); diff --git a/x-pack/test/ingest_manager_api_integration/apis/epm/install_remove_assets.ts b/x-pack/test/ingest_manager_api_integration/apis/epm/install_remove_assets.ts index 96663f1b3fb122..d58d81c8231e86 100644 --- a/x-pack/test/ingest_manager_api_integration/apis/epm/install_remove_assets.ts +++ b/x-pack/test/ingest_manager_api_integration/apis/epm/install_remove_assets.ts @@ -35,6 +35,9 @@ export default function (providerContext: FtrProviderContext) { before(async () => { await installPackage(pkgKey); }); + after(async () => { + await uninstallPackage(pkgKey); + }); it('should have installed the ILM policy', async function () { const resPolicy = await es.transport.request({ method: 'GET', @@ -200,6 +203,7 @@ export default function (providerContext: FtrProviderContext) { describe('uninstalls all assets when uninstalling a package', async () => { skipIfNoDockerRegistry(providerContext); before(async () => { + await installPackage(pkgKey); await uninstallPackage(pkgKey); }); it('should have uninstalled the index templates', async function () { @@ -324,6 +328,35 @@ export default function (providerContext: FtrProviderContext) { } expect(resSearch.response.data.statusCode).equal(404); }); + it('should have removed the fields from the index patterns', async () => { + try { + const resIndexPatternLogs = await kibanaServer.savedObjects.get({ + type: 'index-pattern', + id: 'logs-*', + }); + const fields = JSON.parse(resIndexPatternLogs.attributes.fields); + const exists = fields.find((field: { name: string }) => field.name === 'logs_test_name'); + expect(exists).to.be(undefined); + } catch (err) { + // if all packages are uninstalled there won't be a logs-* index pattern + expect(err.response.data.statusCode).equal(404); + } + + try { + const resIndexPatternMetrics = await kibanaServer.savedObjects.get({ + type: 'index-pattern', + id: 'metrics-*', + }); + const fieldsMetrics = JSON.parse(resIndexPatternMetrics.attributes.fields); + const existsMetrics = fieldsMetrics.find( + (field: { name: string }) => field.name === 'metrics_test_name' + ); + expect(existsMetrics).to.be(undefined); + } catch (err) { + // if all packages are uninstalled there won't be a metrics-* index pattern + expect(err.response.data.statusCode).equal(404); + } + }); it('should have removed the saved object', async function () { let res; try {