"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultResourceInstaller = void 0; var _slo_mappings_template = require("../../assets/component_templates/slo_mappings_template"); var _slo_settings_template = require("../../assets/component_templates/slo_settings_template"); var _slo_summary_mappings_template = require("../../assets/component_templates/slo_summary_mappings_template"); var _slo_summary_settings_template = require("../../assets/component_templates/slo_summary_settings_template"); var _constants = require("../../assets/constants"); var _slo_index_templates = require("../../assets/index_templates/slo_index_templates"); var _slo_summary_index_templates = require("../../assets/index_templates/slo_summary_index_templates"); var _slo_pipeline_template = require("../../assets/ingest_templates/slo_pipeline_template"); var _slo_summary_pipeline_template = require("../../assets/ingest_templates/slo_summary_pipeline_template"); var _retry = require("../../utils/retry"); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ class DefaultResourceInstaller { constructor(esClient, logger) { this.esClient = esClient; this.logger = logger; } async ensureCommonResourcesInstalled() { const alreadyInstalled = await this.areResourcesAlreadyInstalled(); if (alreadyInstalled) { this.logger.info('SLO resources already installed - skipping'); return; } try { this.logger.info('Installing SLO shared resources'); await Promise.all([this.createOrUpdateComponentTemplate((0, _slo_mappings_template.getSLOMappingsTemplate)(_constants.SLO_COMPONENT_TEMPLATE_MAPPINGS_NAME)), this.createOrUpdateComponentTemplate((0, _slo_settings_template.getSLOSettingsTemplate)(_constants.SLO_COMPONENT_TEMPLATE_SETTINGS_NAME)), this.createOrUpdateComponentTemplate((0, _slo_summary_mappings_template.getSLOSummaryMappingsTemplate)(_constants.SLO_SUMMARY_COMPONENT_TEMPLATE_MAPPINGS_NAME)), this.createOrUpdateComponentTemplate((0, _slo_summary_settings_template.getSLOSummarySettingsTemplate)(_constants.SLO_SUMMARY_COMPONENT_TEMPLATE_SETTINGS_NAME))]); await this.createOrUpdateIndexTemplate((0, _slo_index_templates.getSLOIndexTemplate)(_constants.SLO_INDEX_TEMPLATE_NAME, _constants.SLO_INDEX_TEMPLATE_PATTERN, [_constants.SLO_COMPONENT_TEMPLATE_MAPPINGS_NAME, _constants.SLO_COMPONENT_TEMPLATE_SETTINGS_NAME])); await this.createOrUpdateIndexTemplate((0, _slo_summary_index_templates.getSLOSummaryIndexTemplate)(_constants.SLO_SUMMARY_INDEX_TEMPLATE_NAME, _constants.SLO_SUMMARY_INDEX_TEMPLATE_PATTERN, [_constants.SLO_SUMMARY_COMPONENT_TEMPLATE_MAPPINGS_NAME, _constants.SLO_SUMMARY_COMPONENT_TEMPLATE_SETTINGS_NAME])); await this.createIndex(_constants.SLO_DESTINATION_INDEX_NAME); await this.createIndex(_constants.SLO_SUMMARY_DESTINATION_INDEX_NAME); await this.createIndex(_constants.SLO_SUMMARY_TEMP_INDEX_NAME); await this.createOrUpdateIngestPipelineTemplate((0, _slo_pipeline_template.getSLOPipelineTemplate)(_constants.SLO_INGEST_PIPELINE_NAME, _constants.SLO_INGEST_PIPELINE_INDEX_NAME_PREFIX)); await this.createOrUpdateIngestPipelineTemplate((0, _slo_summary_pipeline_template.getSLOSummaryPipelineTemplate)(_constants.SLO_SUMMARY_INGEST_PIPELINE_NAME)); } catch (err) { this.logger.error(`Error installing resources shared for SLO: ${err.message}`); throw err; } } async areResourcesAlreadyInstalled() { let indexTemplateExists = false; try { var _sloIndexTemplate$ind; const { index_templates: indexTemplates } = await this.execute(() => this.esClient.indices.getIndexTemplate({ name: _constants.SLO_INDEX_TEMPLATE_NAME })); const sloIndexTemplate = indexTemplates.find(template => template.name === _constants.SLO_INDEX_TEMPLATE_NAME); indexTemplateExists = !!sloIndexTemplate && ((_sloIndexTemplate$ind = sloIndexTemplate.index_template._meta) === null || _sloIndexTemplate$ind === void 0 ? void 0 : _sloIndexTemplate$ind.version) === _constants.SLO_RESOURCES_VERSION; } catch (err) { return false; } let summaryIndexTemplateExists = false; try { var _sloSummaryIndexTempl; const { index_templates: indexTemplates } = await this.execute(() => this.esClient.indices.getIndexTemplate({ name: _constants.SLO_SUMMARY_INDEX_TEMPLATE_NAME })); const sloSummaryIndexTemplate = indexTemplates.find(template => template.name === _constants.SLO_SUMMARY_INDEX_TEMPLATE_NAME); summaryIndexTemplateExists = !!sloSummaryIndexTemplate && ((_sloSummaryIndexTempl = sloSummaryIndexTemplate.index_template._meta) === null || _sloSummaryIndexTempl === void 0 ? void 0 : _sloSummaryIndexTempl.version) === _constants.SLO_RESOURCES_VERSION; } catch (err) { return false; } let ingestPipelineExists = false; try { const pipeline = await this.execute(() => this.esClient.ingest.getPipeline({ id: _constants.SLO_INGEST_PIPELINE_NAME })); ingestPipelineExists = // @ts-ignore _meta is not defined on the type pipeline && pipeline[_constants.SLO_INGEST_PIPELINE_NAME]._meta.version === _constants.SLO_RESOURCES_VERSION; } catch (err) { return false; } let summaryIngestPipelineExists = false; try { const pipeline = await this.execute(() => this.esClient.ingest.getPipeline({ id: _constants.SLO_SUMMARY_INGEST_PIPELINE_NAME })); summaryIngestPipelineExists = pipeline && // @ts-ignore _meta is not defined on the type pipeline[_constants.SLO_SUMMARY_INGEST_PIPELINE_NAME]._meta.version === _constants.SLO_RESOURCES_VERSION; } catch (err) { return false; } return indexTemplateExists && summaryIndexTemplateExists && ingestPipelineExists && summaryIngestPipelineExists; } async createOrUpdateComponentTemplate(template) { this.logger.info(`Installing SLO component template [${template.name}]`); return this.execute(() => this.esClient.cluster.putComponentTemplate(template)); } async createOrUpdateIndexTemplate(template) { this.logger.info(`Installing SLO index template [${template.name}]`); return this.execute(() => this.esClient.indices.putIndexTemplate(template)); } async createOrUpdateIngestPipelineTemplate(template) { this.logger.info(`Installing SLO ingest pipeline [${template.id}]`); return this.execute(() => this.esClient.ingest.putPipeline(template)); } async createIndex(indexName) { try { await this.execute(() => this.esClient.indices.create({ index: indexName })); } catch (err) { var _err$meta, _err$meta$body, _err$meta$body$error; if ((err === null || err === void 0 ? void 0 : (_err$meta = err.meta) === null || _err$meta === void 0 ? void 0 : (_err$meta$body = _err$meta.body) === null || _err$meta$body === void 0 ? void 0 : (_err$meta$body$error = _err$meta$body.error) === null || _err$meta$body$error === void 0 ? void 0 : _err$meta$body$error.type) !== 'resource_already_exists_exception') { throw err; } } } async execute(esCall) { return await (0, _retry.retryTransientEsErrors)(esCall, { logger: this.logger }); } } exports.DefaultResourceInstaller = DefaultResourceInstaller;