diff --git a/src/plugins/console/public/lib/autocomplete/components/component_template_autocomplete_component.js b/src/plugins/console/public/lib/autocomplete/components/component_template_autocomplete_component.js new file mode 100644 index 000000000000..ca59e077116e --- /dev/null +++ b/src/plugins/console/public/lib/autocomplete/components/component_template_autocomplete_component.js @@ -0,0 +1,20 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { getComponentTemplates } from '../../mappings/mappings'; +import { ListComponent } from './list_component'; + +export class ComponentTemplateAutocompleteComponent extends ListComponent { + constructor(name, parent) { + super(name, getComponentTemplates, parent, true, true); + } + + getContextKey() { + return 'component_template'; + } +} diff --git a/src/plugins/console/public/lib/autocomplete/components/index.js b/src/plugins/console/public/lib/autocomplete/components/index.js index 0e651aefa167..32078ee2c151 100644 --- a/src/plugins/console/public/lib/autocomplete/components/index.js +++ b/src/plugins/console/public/lib/autocomplete/components/index.js @@ -20,5 +20,7 @@ export { IndexAutocompleteComponent } from './index_autocomplete_component'; export { FieldAutocompleteComponent } from './field_autocomplete_component'; export { TypeAutocompleteComponent } from './type_autocomplete_component'; export { IdAutocompleteComponent } from './id_autocomplete_component'; -export { TemplateAutocompleteComponent } from './template_autocomplete_component'; export { UsernameAutocompleteComponent } from './username_autocomplete_component'; +export { IndexTemplateAutocompleteComponent } from './index_template_autocomplete_component'; +export { ComponentTemplateAutocompleteComponent } from './component_template_autocomplete_component'; +export * from './legacy'; diff --git a/src/plugins/console/public/lib/autocomplete/components/template_autocomplete_component.js b/src/plugins/console/public/lib/autocomplete/components/index_template_autocomplete_component.js similarity index 68% rename from src/plugins/console/public/lib/autocomplete/components/template_autocomplete_component.js rename to src/plugins/console/public/lib/autocomplete/components/index_template_autocomplete_component.js index 40ebd6b4c55f..444e40e756f7 100644 --- a/src/plugins/console/public/lib/autocomplete/components/template_autocomplete_component.js +++ b/src/plugins/console/public/lib/autocomplete/components/index_template_autocomplete_component.js @@ -6,14 +6,15 @@ * Side Public License, v 1. */ -import { getTemplates } from '../../mappings/mappings'; +import { getIndexTemplates } from '../../mappings/mappings'; import { ListComponent } from './list_component'; -export class TemplateAutocompleteComponent extends ListComponent { +export class IndexTemplateAutocompleteComponent extends ListComponent { constructor(name, parent) { - super(name, getTemplates, parent, true, true); + super(name, getIndexTemplates, parent, true, true); } + getContextKey() { - return 'template'; + return 'index_template'; } } diff --git a/src/plugins/console/public/lib/autocomplete/components/legacy/index.js b/src/plugins/console/public/lib/autocomplete/components/legacy/index.js new file mode 100644 index 000000000000..1e84cb05f5b8 --- /dev/null +++ b/src/plugins/console/public/lib/autocomplete/components/legacy/index.js @@ -0,0 +1,9 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +export { LegacyTemplateAutocompleteComponent } from './legacy_template_autocomplete_component'; diff --git a/src/plugins/console/public/lib/autocomplete/components/legacy/legacy_template_autocomplete_component.js b/src/plugins/console/public/lib/autocomplete/components/legacy/legacy_template_autocomplete_component.js new file mode 100644 index 000000000000..b68ae952702f --- /dev/null +++ b/src/plugins/console/public/lib/autocomplete/components/legacy/legacy_template_autocomplete_component.js @@ -0,0 +1,19 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { getLegacyTemplates } from '../../../mappings/mappings'; +import { ListComponent } from '../list_component'; + +export class LegacyTemplateAutocompleteComponent extends ListComponent { + constructor(name, parent) { + super(name, getLegacyTemplates, parent, true, true); + } + getContextKey() { + return 'template'; + } +} diff --git a/src/plugins/console/public/lib/kb/kb.js b/src/plugins/console/public/lib/kb/kb.js index 199440bf6197..5f02365a48fd 100644 --- a/src/plugins/console/public/lib/kb/kb.js +++ b/src/plugins/console/public/lib/kb/kb.js @@ -12,8 +12,10 @@ import { IndexAutocompleteComponent, FieldAutocompleteComponent, ListComponent, - TemplateAutocompleteComponent, + LegacyTemplateAutocompleteComponent, UsernameAutocompleteComponent, + IndexTemplateAutocompleteComponent, + ComponentTemplateAutocompleteComponent, } from '../autocomplete/components'; import $ from 'jquery'; @@ -62,7 +64,7 @@ const parametrizedComponentFactories = { return new UsernameAutocompleteComponent(name, parent); }, template: function (name, parent) { - return new TemplateAutocompleteComponent(name, parent); + return new LegacyTemplateAutocompleteComponent(name, parent); }, task_id: function (name, parent) { return idAutocompleteComponentFactory(name, parent); @@ -86,6 +88,12 @@ const parametrizedComponentFactories = { node: function (name, parent) { return new ListComponent(name, [], parent, false); }, + index_template: function (name, parent) { + return new IndexTemplateAutocompleteComponent(name, parent); + }, + component_template: function (name, parent) { + return new ComponentTemplateAutocompleteComponent(name, parent); + }, }; export function getUnmatchedEndpointComponents() { diff --git a/src/plugins/console/public/lib/mappings/mapping.test.js b/src/plugins/console/public/lib/mappings/mapping.test.js index b694b8c3936f..9191eb736be3 100644 --- a/src/plugins/console/public/lib/mappings/mapping.test.js +++ b/src/plugins/console/public/lib/mappings/mapping.test.js @@ -240,4 +240,30 @@ describe('Mappings', () => { ]); expect(mappings.expandAliases('alias2')).toEqual('test_index2'); }); + + test('Templates', function () { + mappings.loadLegacyTemplates({ + test_index1: { order: 0 }, + test_index2: { order: 0 }, + test_index3: { order: 0 }, + }); + + mappings.loadIndexTemplates({ + index_templates: [{ name: 'test_index1' }, { name: 'test_index2' }, { name: 'test_index3' }], + }); + + mappings.loadComponentTemplates({ + component_templates: [ + { name: 'test_index1' }, + { name: 'test_index2' }, + { name: 'test_index3' }, + ], + }); + + const expectedResult = ['test_index1', 'test_index2', 'test_index3']; + + expect(mappings.getLegacyTemplates()).toEqual(expectedResult); + expect(mappings.getIndexTemplates()).toEqual(expectedResult); + expect(mappings.getComponentTemplates()).toEqual(expectedResult); + }); }); diff --git a/src/plugins/console/public/lib/mappings/mappings.js b/src/plugins/console/public/lib/mappings/mappings.js index 84e818f177d6..75b8a263e869 100644 --- a/src/plugins/console/public/lib/mappings/mappings.js +++ b/src/plugins/console/public/lib/mappings/mappings.js @@ -14,7 +14,9 @@ let pollTimeoutId; let perIndexTypes = {}; let perAliasIndexes = []; -let templates = []; +let legacyTemplates = []; +let indexTemplates = []; +let componentTemplates = []; const mappingObj = {}; @@ -46,8 +48,16 @@ export function expandAliases(indicesOrAliases) { return ret.length > 1 ? ret : ret[0]; } -export function getTemplates() { - return [...templates]; +export function getLegacyTemplates() { + return [...legacyTemplates]; +} + +export function getIndexTemplates() { + return [...indexTemplates]; +} + +export function getComponentTemplates() { + return [...componentTemplates]; } export function getFields(indices, types) { @@ -182,8 +192,16 @@ function getFieldNamesFromProperties(properties = {}) { }); } -function loadTemplates(templatesObject = {}) { - templates = Object.keys(templatesObject); +export function loadLegacyTemplates(templatesObject = {}) { + legacyTemplates = Object.keys(templatesObject); +} + +export function loadIndexTemplates(data) { + indexTemplates = (data.index_templates ?? []).map(({ name }) => name); +} + +export function loadComponentTemplates(data) { + componentTemplates = (data.component_templates ?? []).map(({ name }) => name); } export function loadMappings(mappings) { @@ -235,14 +253,18 @@ export function loadAliases(aliases) { export function clear() { perIndexTypes = {}; perAliasIndexes = {}; - templates = []; + legacyTemplates = []; + indexTemplates = []; + componentTemplates = []; } function retrieveSettings(settingsKey, settingsToRetrieve) { const settingKeyToPathMap = { fields: '_mapping', indices: '_aliases', - templates: '_template', + legacyTemplates: '_template', + indexTemplates: '_index_template', + componentTemplates: '_component_template', }; // Fetch autocomplete info if setting is set to true, and if user has made changes. @@ -289,36 +311,66 @@ export function clearSubscriptions() { export function retrieveAutoCompleteInfo(settings, settingsToRetrieve) { clearSubscriptions(); + const templatesSettingToRetrieve = { + ...settingsToRetrieve, + legacyTemplates: settingsToRetrieve.templates, + indexTemplates: settingsToRetrieve.templates, + componentTemplates: settingsToRetrieve.templates, + }; + const mappingPromise = retrieveSettings('fields', settingsToRetrieve); const aliasesPromise = retrieveSettings('indices', settingsToRetrieve); - const templatesPromise = retrieveSettings('templates', settingsToRetrieve); - - $.when(mappingPromise, aliasesPromise, templatesPromise).done((mappings, aliases, templates) => { + const legacyTemplatesPromise = retrieveSettings('legacyTemplates', templatesSettingToRetrieve); + const indexTemplatesPromise = retrieveSettings('indexTemplates', templatesSettingToRetrieve); + const componentTemplatesPromise = retrieveSettings( + 'componentTemplates', + templatesSettingToRetrieve + ); + + $.when( + mappingPromise, + aliasesPromise, + legacyTemplatesPromise, + indexTemplatesPromise, + componentTemplatesPromise + ).done((mappings, aliases, legacyTemplates, indexTemplates, componentTemplates) => { let mappingsResponse; - if (mappings) { - const maxMappingSize = mappings[0].length > 10 * 1024 * 1024; - if (maxMappingSize) { - console.warn( - `Mapping size is larger than 10MB (${mappings[0].length / 1024 / 1024} MB). Ignoring...` - ); - mappingsResponse = '[{}]'; - } else { - mappingsResponse = mappings[0]; + try { + if (mappings && mappings.length) { + const maxMappingSize = mappings[0].length > 10 * 1024 * 1024; + if (maxMappingSize) { + console.warn( + `Mapping size is larger than 10MB (${mappings[0].length / 1024 / 1024} MB). Ignoring...` + ); + mappingsResponse = '[{}]'; + } else { + mappingsResponse = mappings[0]; + } + loadMappings(JSON.parse(mappingsResponse)); } - loadMappings(JSON.parse(mappingsResponse)); - } - if (aliases) { - loadAliases(JSON.parse(aliases[0])); - } + if (aliases) { + loadAliases(JSON.parse(aliases[0])); + } - if (templates) { - loadTemplates(JSON.parse(templates[0])); - } + if (legacyTemplates) { + loadLegacyTemplates(JSON.parse(legacyTemplates[0])); + } - if (mappings && aliases) { - // Trigger an update event with the mappings, aliases - $(mappingObj).trigger('update', [mappingsResponse, aliases[0]]); + if (indexTemplates) { + loadIndexTemplates(JSON.parse(indexTemplates[0])); + } + + if (componentTemplates) { + loadComponentTemplates(JSON.parse(componentTemplates[0])); + } + + if (mappings && aliases) { + // Trigger an update event with the mappings, aliases + $(mappingObj).trigger('update', [mappingsResponse, aliases[0]]); + } + } catch (error) { + console.error(error); } // Schedule next request. diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.delete_component_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.delete_component_template.json index 24255f723189..400e064c3de9 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.delete_component_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.delete_component_template.json @@ -8,7 +8,7 @@ "DELETE" ], "patterns": [ - "_component_template/{name}" + "_component_template/{component_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-component-template.html" } diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.exists_component_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.exists_component_template.json index 24dcbeb006e6..3157e1b8ccc7 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.exists_component_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.exists_component_template.json @@ -8,7 +8,7 @@ "HEAD" ], "patterns": [ - "_component_template/{name}" + "_component_template/{component_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-component-template.html" } diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.get_component_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.get_component_template.json index cbfed6741f8a..e491ccf94bb6 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.get_component_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.get_component_template.json @@ -10,7 +10,7 @@ ], "patterns": [ "_component_template", - "_component_template/{name}" + "_component_template/{component_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/getting-component-templates.html" } diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.put_component_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.put_component_template.json index 999ff0c149fe..31a94d098f60 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.put_component_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/cluster.put_component_template.json @@ -9,7 +9,7 @@ "POST" ], "patterns": [ - "_component_template/{name}" + "_component_template/{component_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-component-template.html" } diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.delete_index_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.delete_index_template.json index ef3f836207f1..5d6d53e1d1d6 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.delete_index_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.delete_index_template.json @@ -8,7 +8,7 @@ "DELETE" ], "patterns": [ - "_index_template/{name}" + "_index_template/{index_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html" } diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.exists_index_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.exists_index_template.json index 97fa8cf55576..d1c5d9d617f8 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.exists_index_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.exists_index_template.json @@ -9,7 +9,7 @@ "HEAD" ], "patterns": [ - "_index_template/{name}" + "_index_template/{index_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html" } diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.get_index_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.get_index_template.json index 142b75f22c2a..3d91424f4ce3 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.get_index_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.get_index_template.json @@ -10,7 +10,7 @@ ], "patterns": [ "_index_template", - "_index_template/{name}" + "_index_template/{index_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html" } diff --git a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.put_index_template.json b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.put_index_template.json index 0ce27c1d9d21..fcae7af55b4e 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/generated/indices.put_index_template.json +++ b/src/plugins/console/server/lib/spec_definitions/json/generated/indices.put_index_template.json @@ -10,7 +10,7 @@ "POST" ], "patterns": [ - "_index_template/{name}" + "_index_template/{index_template}" ], "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html" }