diff --git a/x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/install.ts index 2aa28d23cf857..4804701df23a8 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/install.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/install.ts @@ -85,7 +85,6 @@ export async function installIndexPatterns( savedObjectsClient, InstallationStatus.installed ); - // TODO: move to install package // cache all installed packages if they don't exist const packagePromises = installedPackages.map((pkg) => @@ -95,26 +94,32 @@ export async function installIndexPatterns( ); await Promise.all(packagePromises); + const packageVersionsToFetch = [...installedPackages]; if (pkgName && pkgVersion) { - // add this package to the array if it doesn't already exist - const foundPkg = installedPackages.find((pkg) => pkg.pkgName === pkgName); - // this may be removed if we add the packged to saved objects before installing index patterns - // otherwise this is a first time install - // TODO: handle update case when versions are different - if (!foundPkg) { - installedPackages.push({ pkgName, pkgVersion }); + const packageToInstall = packageVersionsToFetch.find((pkg) => pkg.pkgName === pkgName); + + if (packageToInstall) { + // set the version to the one we want to install + // if we're installing for the first time the number will be the same + // if this is an upgrade then we'll be modifying the version number to the upgrade version + packageToInstall.pkgVersion = pkgVersion; + } else { + // this will likely not happen because the saved objects should already have the package we're trying + // install which means that it should have been found in the case above + packageVersionsToFetch.push({ pkgName, pkgVersion }); } } // get each package's registry info - const installedPackagesFetchInfoPromise = installedPackages.map((pkg) => + const packageVersionsFetchInfoPromise = packageVersionsToFetch.map((pkg) => Registry.fetchInfo(pkg.pkgName, pkg.pkgVersion) ); - const installedPackagesInfo = await Promise.all(installedPackagesFetchInfoPromise); + + const packageVersionsInfo = await Promise.all(packageVersionsFetchInfoPromise); // for each index pattern type, create an index pattern const indexPatternTypes = [IndexPatternType.logs, IndexPatternType.metrics]; indexPatternTypes.forEach(async (indexPatternType) => { - // if this is an update because a package is being unisntalled (no pkgkey argument passed) and no other packages are installed, remove the index pattern + // if this is an update because a package is being uninstalled (no pkgkey argument passed) and no other packages are installed, remove the index pattern if (!pkgName && installedPackages.length === 0) { try { await savedObjectsClient.delete(INDEX_PATTERN_SAVED_OBJECT_TYPE, `${indexPatternType}-*`); @@ -125,8 +130,7 @@ export async function installIndexPatterns( } // get all data stream fields from all installed packages - const fields = await getAllDataStreamFieldsByType(installedPackagesInfo, indexPatternType); - + const fields = await getAllDataStreamFieldsByType(packageVersionsInfo, indexPatternType); const kibanaIndexPattern = createIndexPattern(indexPatternType, fields); // create or overwrite the index pattern await savedObjectsClient.create(INDEX_PATTERN_SAVED_OBJECT_TYPE, kibanaIndexPattern, { diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/get.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/get.ts index 74ee25eace736..2cf94e9c16079 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/get.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/get.ts @@ -89,8 +89,15 @@ export async function getPackageKeysByStatus( const allPackages = await getPackages({ savedObjectsClient }); return allPackages.reduce>((acc, pkg) => { if (pkg.status === status) { - acc.push({ pkgName: pkg.name, pkgVersion: pkg.version }); + if (pkg.status === InstallationStatus.installed) { + // if we're looking for installed packages grab the version from the saved object because `getPackages` will + // return the latest package information from the registry + acc.push({ pkgName: pkg.name, pkgVersion: pkg.savedObject.attributes.version }); + } else { + acc.push({ pkgName: pkg.name, pkgVersion: pkg.version }); + } } + return acc; }, []); } 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 96663f1b3fb12..16a500ddf85ee 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 @@ -131,6 +131,24 @@ export default function (providerContext: FtrProviderContext) { }); expect(resSearch.id).equal('sample_search'); }); + it('should create an index pattern with the package fields', async () => { + 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).not.to.be(undefined); + const resIndexPatternMetrics = await kibanaServer.savedObjects.get({ + type: 'index-pattern', + id: 'metrics-*', + }); + const fieldsMetrics = JSON.parse(resIndexPatternMetrics.attributes.fields); + const metricsExists = fieldsMetrics.find( + (field: { name: string }) => field.name === 'metrics_test_name' + ); + expect(metricsExists).not.to.be(undefined); + }); it('should have created the correct saved object', async function () { const res = await kibanaServer.savedObjects.get({ type: 'epm-packages',