From 21f882a96207f151645b271d86f3890e2ae06163 Mon Sep 17 00:00:00 2001 From: mgodwan Date: Fri, 5 Jul 2024 17:16:03 +0530 Subject: [PATCH] SPI for loading ABC templates Signed-off-by: mgodwan --- .../service/ClusterManagerService.java | 8 +- .../cluster/service/ClusterService.java | 9 +- .../cluster/service/MasterService.java | 10 +- .../ClusterStateComponentTemplateLoader.java | 65 +++++++++++ .../applicationtemplates/SystemTemplate.java | 41 +++++++ .../SystemTemplateInfo.java | 53 +++++++++ .../SystemTemplatesPlugin.java | 28 +++++ .../SystemTemplatesService.java | 104 ++++++++++++++++++ .../applicationtemplates/TemplateLoader.java | 24 ++++ .../TemplateRepository.java | 35 ++++++ .../TemplateRepositoryInfo.java | 31 ++++++ .../main/java/org/opensearch/node/Node.java | 4 +- .../TestSystemTemplatesRepositoryPlugin.java | 62 +++++++++++ .../test/OpenSearchIntegTestCase.java | 2 + 14 files changed, 468 insertions(+), 8 deletions(-) create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/ClusterStateComponentTemplateLoader.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplate.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplateInfo.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesPlugin.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateLoader.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepository.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepositoryInfo.java create mode 100644 test/framework/src/main/java/org/opensearch/cluster/service/applicationtemplates/TestSystemTemplatesRepositoryPlugin.java diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java index fa8c965b4d538..4f3f5ecb86d1f 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java @@ -9,11 +9,14 @@ package org.opensearch.cluster.service; import org.opensearch.cluster.ClusterManagerMetrics; +import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; import org.opensearch.common.annotation.PublicApi; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.threadpool.ThreadPool; +import java.util.List; + /** * Main Cluster Manager Node Service * @@ -30,8 +33,9 @@ public ClusterManagerService( Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool, - ClusterManagerMetrics clusterManagerMetrics + ClusterManagerMetrics clusterManagerMetrics, + List systemTemplatesPlugins ) { - super(settings, clusterSettings, threadPool, clusterManagerMetrics); + super(settings, clusterSettings, threadPool, clusterManagerMetrics, systemTemplatesPlugins); } } diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index c3c48dd8b87ef..d3282bb158774 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -46,6 +46,7 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.routing.OperationRouting; import org.opensearch.cluster.routing.RerouteService; +import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; import org.opensearch.common.annotation.PublicApi; import org.opensearch.common.lifecycle.AbstractLifecycleComponent; import org.opensearch.common.settings.ClusterSettings; @@ -58,6 +59,7 @@ import org.opensearch.threadpool.ThreadPool; import java.util.Collections; +import java.util.List; import java.util.Map; /** @@ -94,19 +96,20 @@ public class ClusterService extends AbstractLifecycleComponent { private IndexingPressureService indexingPressureService; public ClusterService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { - this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE)); + this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE), null); } public ClusterService( Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool, - ClusterManagerMetrics clusterManagerMetrics + ClusterManagerMetrics clusterManagerMetrics, + List systemTemplatesPlugins ) { this( settings, clusterSettings, - new ClusterManagerService(settings, clusterSettings, threadPool, clusterManagerMetrics), + new ClusterManagerService(settings, clusterSettings, threadPool, clusterManagerMetrics, systemTemplatesPlugins), new ClusterApplierService(Node.NODE_NAME_SETTING.get(settings), settings, clusterSettings, threadPool, clusterManagerMetrics) ); } diff --git a/server/src/main/java/org/opensearch/cluster/service/MasterService.java b/server/src/main/java/org/opensearch/cluster/service/MasterService.java index 686e9793a8fd3..de420b420dbad 100644 --- a/server/src/main/java/org/opensearch/cluster/service/MasterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/MasterService.java @@ -53,6 +53,8 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.routing.RoutingTable; +import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; +import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesService; import org.opensearch.common.Nullable; import org.opensearch.common.Priority; import org.opensearch.common.annotation.DeprecatedApi; @@ -140,16 +142,18 @@ public class MasterService extends AbstractLifecycleComponent { private final ClusterManagerThrottlingStats throttlingStats; private final ClusterStateStats stateStats; private final ClusterManagerMetrics clusterManagerMetrics; + private final SystemTemplatesService systemTemplatesService; public MasterService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { - this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE)); + this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE), null); } public MasterService( Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool, - ClusterManagerMetrics clusterManagerMetrics + ClusterManagerMetrics clusterManagerMetrics, + List systemTemplatesPlugins ) { this.nodeName = Objects.requireNonNull(Node.NODE_NAME_SETTING.get(settings)); @@ -169,6 +173,7 @@ public MasterService( this.stateStats = new ClusterStateStats(); this.threadPool = threadPool; this.clusterManagerMetrics = clusterManagerMetrics; + this.systemTemplatesService = new SystemTemplatesService(systemTemplatesPlugins, clusterSettings, settings); } private void setSlowTaskLoggingThreshold(TimeValue slowTaskLoggingThreshold) { @@ -395,6 +400,7 @@ void onPublicationSuccess(ClusterChangedEvent clusterChangedEvent, TaskOutputs t try { taskOutputs.clusterStatePublished(clusterChangedEvent); + threadPool.executor(ThreadPool.Names.GENERIC).submit(() -> systemTemplatesService.refreshTemplates()); } catch (Exception e) { logger.error( () -> new ParameterizedMessage( diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/ClusterStateComponentTemplateLoader.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/ClusterStateComponentTemplateLoader.java new file mode 100644 index 0000000000000..75b5ba8ee09f8 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/ClusterStateComponentTemplateLoader.java @@ -0,0 +1,65 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.action.admin.indices.template.put.PutComponentTemplateAction; +import org.opensearch.client.Client; +import org.opensearch.client.OriginSettingClient; +import org.opensearch.cluster.ClusterState; +import org.opensearch.cluster.metadata.ComponentTemplate; +import org.opensearch.common.unit.TimeValue; +import org.opensearch.common.xcontent.json.JsonXContent; +import org.opensearch.core.xcontent.DeprecationHandler; +import org.opensearch.core.xcontent.NamedXContentRegistry; +import org.opensearch.core.xcontent.XContentParser; +import org.opensearch.threadpool.ThreadPool; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; + +public class ClusterStateComponentTemplateLoader implements TemplateLoader { + + private Client client; + + private Supplier clusterStateSupplier; + + private static final Logger logger = LogManager.getLogger(TemplateLoader.class); + + public static final String TEMPLATE_LOADER_IDENTIFIER = "system_template_loader"; + + public ClusterStateComponentTemplateLoader(Client client, + ThreadPool threadPool, + Supplier clusterStateSupplier) { + this.client = new OriginSettingClient(client, TEMPLATE_LOADER_IDENTIFIER); + this.clusterStateSupplier = clusterStateSupplier; + } + + @Override + public void loadTemplate(SystemTemplate template) throws IOException { + ComponentTemplate existingTemplate = clusterStateSupplier.get().metadata().componentTemplates().get(template.templateInfo().fullyQualifiedName()); + + XContentParser contentParser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, + template.templateContent().utf8ToString()); + ComponentTemplate newTemplate = ComponentTemplate.parse(contentParser); + + if (existingTemplate != null && existingTemplate.version() >= newTemplate.version()) { + logger.debug("Skipping putting template {} as its existing version [{}] is >= fetched version [{}]", template.templateInfo().fullyQualifiedName(), + existingTemplate.version(), + newTemplate.version()); + } + + PutComponentTemplateAction.Request request = new PutComponentTemplateAction.Request(template.templateInfo().fullyQualifiedName()) + .componentTemplate(newTemplate); + + client.admin().indices().execute(PutComponentTemplateAction.INSTANCE, request).actionGet(TimeValue.timeValueMillis(30000)); + } +} diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplate.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplate.java new file mode 100644 index 0000000000000..1baf452768f69 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplate.java @@ -0,0 +1,41 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +import org.opensearch.core.common.bytes.BytesReference; + +/** + * Encapsulates the information and content about a system template available within a repository. + */ +public class SystemTemplate { + + private final BytesReference templateContent; + + private final SystemTemplateInfo templateInfo; + + private final TemplateRepositoryInfo repositoryInfo; + + public SystemTemplate(BytesReference templateContent, SystemTemplateInfo templateInfo, TemplateRepositoryInfo repositoryInfo) { + this.templateContent = templateContent; + this.templateInfo = templateInfo; + this.repositoryInfo = repositoryInfo; + } + + public BytesReference templateContent() { + return templateContent; + } + + public SystemTemplateInfo templateInfo() { + return templateInfo; + } + + public TemplateRepositoryInfo repositoryInfo() { + return repositoryInfo; + } +} diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplateInfo.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplateInfo.java new file mode 100644 index 0000000000000..209fcc885d250 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplateInfo.java @@ -0,0 +1,53 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +/** + * Metadata information about a template available in a template repository. + */ +public class SystemTemplateInfo { + + private final long version; + private final String type; + private final String name; + + private static final String DELIMITER = "@"; + + public static final String COMPONENT_TEMPLATE_TYPE = "@abc_template"; + + public SystemTemplateInfo(long version, String type, String name) { + this.version = version; + this.type = type; + this.name = name; + } + + public String type() { + return type; + } + + public String name() { + return name; + } + + public long version() { + return version; + } + + public static SystemTemplateInfo fromComponentTemplate(String fullyQualifiedName) { + return new SystemTemplateInfo(Long.parseLong(fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(DELIMITER))), COMPONENT_TEMPLATE_TYPE, fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf(DELIMITER))); + } + + public static SystemTemplateInfo createComponentTemplateInfo(String name, long version) { + return new SystemTemplateInfo(version, COMPONENT_TEMPLATE_TYPE, name); + } + + public final String fullyQualifiedName() { + return name + DELIMITER + version; + } +} diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesPlugin.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesPlugin.java new file mode 100644 index 0000000000000..3d4d662beff18 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesPlugin.java @@ -0,0 +1,28 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +import java.io.IOException; + +/** + * Plugin interface to expose the template maintaining logic. + */ +public interface SystemTemplatesPlugin { + + /** + * @return repository implementation from which templates are to be fetched. + */ + TemplateRepository loadRepository() throws IOException; + + /** + * @param templateInfo Metadata about the template to load + * @return Implementation of TemplateLoader which determines how to make the template available at runtime. + */ + TemplateLoader loaderFor(SystemTemplateInfo templateInfo); +} diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java new file mode 100644 index 0000000000000..c9f1c4de477e0 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java @@ -0,0 +1,104 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Setting; +import org.opensearch.common.settings.Settings; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Service class to orchestrate execution around available templates' management. + */ +public class SystemTemplatesService { + + public static final int APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD_DEFAULT_COUNT = 50; + + public static final Setting SETTING_APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD = Setting.intSetting( + "cluster.application_templates.load", + APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD_DEFAULT_COUNT, + 0, + 1000, + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); + + private final List systemTemplatesPluginList; + + private final Settings settings; + + private final AtomicBoolean loaded = new AtomicBoolean(false); + + private volatile int templatesToLoad = APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD_DEFAULT_COUNT; + + private static final Logger logger = LogManager.getLogger(SystemTemplatesService.class); + + public SystemTemplatesService(List systemTemplatesPluginList, + ClusterSettings clusterSettings, + Settings settings) { + this.systemTemplatesPluginList = systemTemplatesPluginList; + clusterSettings.addSettingsUpdateConsumer(SETTING_APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD, this::setTemplatesToLoad); + this.settings = settings; + } + + public void refreshTemplates() { + if (loaded.compareAndSet(false, true)) { + if (SETTING_APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD.get(settings) > 0) { + int countOfTemplatesToLoad = SETTING_APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD.get(settings); + int templatesLoaded = 0; + int failedLoadingTemplates = 0; + int failedLoadingRepositories = 0; + + for (SystemTemplatesPlugin plugin : systemTemplatesPluginList) { + try (TemplateRepository repository = plugin.loadRepository()) { + TemplateRepositoryInfo repositoryInfo = repository.info(); + logger.debug("Loading templates from repository: {} at version {}", repositoryInfo.id(), repositoryInfo.version()); + for (SystemTemplateInfo templateInfo : repository.listTemplates()) { + + if (templatesLoaded == countOfTemplatesToLoad) { + logger.debug("Not loading template: {} from repository: {} as we've breached the max count [{}] allowed", + templateInfo.fullyQualifiedName(), repositoryInfo.id(), countOfTemplatesToLoad); + break; + } + try { + SystemTemplate template = repository.fetchTemplate(templateInfo); + plugin.loaderFor(templateInfo).loadTemplate(template); + countOfTemplatesToLoad--; + } catch (Exception ex) { + logger.error("Failed loading template {} from repository: {}", + templateInfo.fullyQualifiedName(), repositoryInfo.id(), ex); + failedLoadingTemplates++; + } + } + + if (templatesLoaded == countOfTemplatesToLoad) { + logger.debug("Loaded maximum permitted templates"); + break; + } + } catch (Exception ex) { + failedLoadingRepositories++; + logger.error("Failed loading repository from plugin: {}", plugin.getClass().getName(), ex); + } + } + + logger.debug("Stats: Total Loaded Templates: [{}], Failed Loading Templates: [{}], Failed Loading Repositories: [{}]", + templatesLoaded, failedLoadingTemplates, failedLoadingRepositories); + } + } + } + + private void setTemplatesToLoad(int templatesToLoad) { + this.templatesToLoad = templatesToLoad; + } +} diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateLoader.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateLoader.java new file mode 100644 index 0000000000000..7c43d19ae2526 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateLoader.java @@ -0,0 +1,24 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +import java.io.IOException; + + +/** + * Interface to load template into the OpenSearch runtime. + */ +public interface TemplateLoader { + + /** + * @param template Templated to be loaded + * @throws IOException If an exceptional situation is encountered while parsing/loading the template + */ + void loadTemplate(SystemTemplate template) throws IOException; +} diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepository.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepository.java new file mode 100644 index 0000000000000..6e968a29e0dfb --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepository.java @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +import java.io.IOException; +import java.util.List; + +/** + * Repository interface around the templates provided by a store (e.g. code repo, remote file store, etc) + */ +public interface TemplateRepository extends AutoCloseable { + + /** + * @return Metadata about the repository + */ + TemplateRepositoryInfo info(); + + /** + * @return Metadata for all available templates + */ + Iterable listTemplates() throws IOException; + + /** + * + * @param template + * @return + */ + SystemTemplate fetchTemplate(SystemTemplateInfo template) throws IOException; +} diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepositoryInfo.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepositoryInfo.java new file mode 100644 index 0000000000000..9328cd24c3981 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/TemplateRepositoryInfo.java @@ -0,0 +1,31 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +/** + * The information to uniquely identify a template repository. + */ +public class TemplateRepositoryInfo { + + private final String id; + private final long version; + + public TemplateRepositoryInfo(String id, long version) { + this.id = id; + this.version = version; + } + + public String id() { + return id; + } + + public long version() { + return version; + } +} diff --git a/server/src/main/java/org/opensearch/node/Node.java b/server/src/main/java/org/opensearch/node/Node.java index 85ef547e27787..95dcebfc463f7 100644 --- a/server/src/main/java/org/opensearch/node/Node.java +++ b/server/src/main/java/org/opensearch/node/Node.java @@ -84,6 +84,7 @@ import org.opensearch.cluster.routing.allocation.AwarenessReplicaBalance; import org.opensearch.cluster.routing.allocation.DiskThresholdMonitor; import org.opensearch.cluster.service.ClusterService; +import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; import org.opensearch.common.SetOnce; import org.opensearch.common.StopWatch; import org.opensearch.common.cache.module.CacheModule; @@ -663,7 +664,8 @@ protected Node( settings, settingsModule.getClusterSettings(), threadPool, - clusterManagerMetrics + clusterManagerMetrics, + pluginsService.filterPlugins(SystemTemplatesPlugin.class) ); clusterService.addStateApplier(scriptService); resourcesToClose.add(clusterService); diff --git a/test/framework/src/main/java/org/opensearch/cluster/service/applicationtemplates/TestSystemTemplatesRepositoryPlugin.java b/test/framework/src/main/java/org/opensearch/cluster/service/applicationtemplates/TestSystemTemplatesRepositoryPlugin.java new file mode 100644 index 0000000000000..bb04136adb6da --- /dev/null +++ b/test/framework/src/main/java/org/opensearch/cluster/service/applicationtemplates/TestSystemTemplatesRepositoryPlugin.java @@ -0,0 +1,62 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service.applicationtemplates; + +import org.opensearch.core.common.bytes.BytesReference; +import org.opensearch.plugins.Plugin; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.List; + +public class TestSystemTemplatesRepositoryPlugin extends Plugin implements SystemTemplatesPlugin { + + private SystemTemplateInfo info = SystemTemplateInfo.createComponentTemplateInfo("dummy", 1); + + private TemplateRepositoryInfo repoInfo = new TemplateRepositoryInfo("test", 1); + + private SystemTemplate systemTemplate = new SystemTemplate(BytesReference.fromByteBuffer(ByteBuffer.wrap("content".getBytes(StandardCharsets.UTF_8))), info, repoInfo); + + @Override + public TemplateRepository loadRepository() throws IOException { + return new TemplateRepository() { + @Override + public TemplateRepositoryInfo info() { + return repoInfo; + } + + @Override + public List listTemplates() throws IOException { + return List.of(info); + } + + @Override + public SystemTemplate fetchTemplate(SystemTemplateInfo template) throws IOException { + return systemTemplate; + } + + @Override + public void close() throws Exception { + } + }; + } + + @Override + public TemplateLoader loaderFor(SystemTemplateInfo templateInfo) { + return new TemplateLoader() { // Asserting Loader + @Override + public void loadTemplate(SystemTemplate template) throws IOException { + assert template.templateInfo() == info; + assert template.repositoryInfo() == repoInfo; + assert template.templateContent() == systemTemplate; + } + }; + } +} diff --git a/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java b/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java index ca5ddf21710af..2955c988a12c6 100644 --- a/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java +++ b/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java @@ -90,6 +90,7 @@ import org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider; import org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider; import org.opensearch.cluster.service.ClusterService; +import org.opensearch.cluster.service.applicationtemplates.TestSystemTemplatesRepositoryPlugin; import org.opensearch.common.Nullable; import org.opensearch.common.Priority; import org.opensearch.common.collect.Tuple; @@ -2168,6 +2169,7 @@ protected Collection> getMockPlugins() { if (addMockTelemetryPlugin()) { mocks.add(MockTelemetryPlugin.class); } + mocks.add(TestSystemTemplatesRepositoryPlugin.class); return Collections.unmodifiableList(mocks); }