Skip to content

Commit

Permalink
Await installation of prebuilt component templates
Browse files Browse the repository at this point in the history
  • Loading branch information
skh committed May 18, 2020
1 parent dcf36dc commit 0092b2f
Showing 1 changed file with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Boom from 'boom';
import { Dataset, RegistryPackage, ElasticsearchAssetType, TemplateRef } from '../../../../types';
import { CallESAsCurrentUser } from '../../../../types';
import { Field, loadFieldsFromYaml, processFields } from '../../fields/field';
Expand All @@ -20,8 +21,8 @@ export const installTemplates = async (
// install any pre-built index template assets,
// atm, this is only the base package's global index templates
// Install component templates first, as they are used by the index templates
installPreBuiltComponentTemplates(pkgName, pkgVersion, callCluster);
installPreBuiltTemplates(pkgName, pkgVersion, callCluster);
await installPreBuiltComponentTemplates(pkgName, pkgVersion, callCluster);
await installPreBuiltTemplates(pkgName, pkgVersion, callCluster);

// build templates per dataset from yml files
const datasets = registryPackage.datasets;
Expand Down Expand Up @@ -53,16 +54,7 @@ const installPreBuiltTemplates = async (
pkgVersion,
(entry: Registry.ArchiveEntry) => isTemplate(entry)
);
// templatePaths.forEach(async path => {
// const { file } = Registry.pathParts(path);
// const templateName = file.substr(0, file.lastIndexOf('.'));
// const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
// await callCluster('indices.putTemplate', {
// name: templateName,
// body: content,
// });
// });
templatePaths.forEach(async path => {
const templateInstallPromises = templatePaths.map(async path => {
const { file } = Registry.pathParts(path);
const templateName = file.substr(0, file.lastIndexOf('.'));
const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
Expand Down Expand Up @@ -91,8 +83,15 @@ const installPreBuiltTemplates = async (
// The existing convenience endpoint `indices.putTemplate` only sends to _template,
// which does not support v2 templates.
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
await callCluster('transport.request', callClusterParams);
return await callCluster('transport.request', callClusterParams);
});
try {
return await Promise.all(templateInstallPromises);
} catch (e) {
throw new Boom(`Error installing prebuilt index templates ${e.message}`, {
statusCode: 400,
});
}
};

const installPreBuiltComponentTemplates = async (
Expand All @@ -105,7 +104,7 @@ const installPreBuiltComponentTemplates = async (
pkgVersion,
(entry: Registry.ArchiveEntry) => isComponentTemplate(entry)
);
templatePaths.forEach(async path => {
const templateInstallPromises = templatePaths.map(async path => {
const { file } = Registry.pathParts(path);
const templateName = file.substr(0, file.lastIndexOf('.'));
const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
Expand All @@ -124,8 +123,15 @@ const installPreBuiltComponentTemplates = async (
// This uses the catch-all endpoint 'transport.request' because there is no
// convenience endpoint for component templates yet.
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
await callCluster('transport.request', callClusterParams);
return await callCluster('transport.request', callClusterParams);
});
try {
return await Promise.all(templateInstallPromises);
} catch (e) {
throw new Boom(`Error installing prebuilt component templates ${e.message}`, {
statusCode: 400,
});
}
};

const isTemplate = ({ path }: Registry.ArchiveEntry) => {
Expand Down

0 comments on commit 0092b2f

Please sign in to comment.