Skip to content

Commit

Permalink
[Ingest Manager] Index pattern installation use requested package ver…
Browse files Browse the repository at this point in the history
…sion (elastic#80079) (elastic#80178)

* Install the requested package version

* Add test for correct package fields

* Addressing feedback
  • Loading branch information
jonathan-buttner authored Oct 12, 2020
1 parent 90d3122 commit f3a5dcb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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}-*`);
Expand All @@ -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, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,15 @@ export async function getPackageKeysByStatus(
const allPackages = await getPackages({ savedObjectsClient });
return allPackages.reduce<Array<{ pkgName: string; pkgVersion: string }>>((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;
}, []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit f3a5dcb

Please sign in to comment.