|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.cluster.applicationtemplates; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.opensearch.OpenSearchCorruptionException; |
| 14 | +import org.opensearch.action.admin.indices.template.put.PutComponentTemplateAction; |
| 15 | +import org.opensearch.client.Client; |
| 16 | +import org.opensearch.client.OriginSettingClient; |
| 17 | +import org.opensearch.cluster.ClusterState; |
| 18 | +import org.opensearch.cluster.metadata.ComponentTemplate; |
| 19 | +import org.opensearch.common.annotation.ExperimentalApi; |
| 20 | +import org.opensearch.common.unit.TimeValue; |
| 21 | +import org.opensearch.common.xcontent.json.JsonXContent; |
| 22 | +import org.opensearch.core.xcontent.DeprecationHandler; |
| 23 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 24 | +import org.opensearch.core.xcontent.XContentParser; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.function.Supplier; |
| 29 | + |
| 30 | +/** |
| 31 | + * Class responsible for loading the component templates provided by a repository into the cluster state. |
| 32 | + */ |
| 33 | +@ExperimentalApi |
| 34 | +public class ClusterStateSystemTemplateLoader implements SystemTemplateLoader { |
| 35 | + |
| 36 | + private final Client client; |
| 37 | + |
| 38 | + private final Supplier<ClusterState> clusterStateSupplier; |
| 39 | + |
| 40 | + private static final Logger logger = LogManager.getLogger(SystemTemplateLoader.class); |
| 41 | + |
| 42 | + public static final String TEMPLATE_LOADER_IDENTIFIER = "system_template_loader"; |
| 43 | + public static final String TEMPLATE_TYPE_KEY = "_type"; |
| 44 | + |
| 45 | + public ClusterStateSystemTemplateLoader(Client client, Supplier<ClusterState> clusterStateSupplier) { |
| 46 | + this.client = new OriginSettingClient(client, TEMPLATE_LOADER_IDENTIFIER); |
| 47 | + this.clusterStateSupplier = clusterStateSupplier; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean loadTemplate(SystemTemplate template) throws IOException { |
| 52 | + final ComponentTemplate existingTemplate = clusterStateSupplier.get() |
| 53 | + .metadata() |
| 54 | + .componentTemplates() |
| 55 | + .get(template.templateMetadata().fullyQualifiedName()); |
| 56 | + |
| 57 | + if (existingTemplate != null |
| 58 | + && !SystemTemplateMetadata.COMPONENT_TEMPLATE_TYPE.equals( |
| 59 | + Objects.toString(existingTemplate.metadata().get(TEMPLATE_TYPE_KEY)) |
| 60 | + )) { |
| 61 | + throw new OpenSearchCorruptionException( |
| 62 | + "Attempting to create " + template.templateMetadata().name() + " which has already been created through some other source." |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (existingTemplate != null && existingTemplate.version() >= template.templateMetadata().version()) { |
| 67 | + logger.debug( |
| 68 | + "Skipping putting template {} as its existing version [{}] is >= fetched version [{}]", |
| 69 | + template.templateMetadata().fullyQualifiedName(), |
| 70 | + existingTemplate.version(), |
| 71 | + template.templateMetadata().version() |
| 72 | + ); |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + ComponentTemplate newTemplate = null; |
| 77 | + try ( |
| 78 | + XContentParser contentParser = JsonXContent.jsonXContent.createParser( |
| 79 | + NamedXContentRegistry.EMPTY, |
| 80 | + DeprecationHandler.IGNORE_DEPRECATIONS, |
| 81 | + template.templateContent().utf8ToString() |
| 82 | + ) |
| 83 | + ) { |
| 84 | + newTemplate = ComponentTemplate.parse(contentParser); |
| 85 | + } |
| 86 | + |
| 87 | + if (!Objects.equals(newTemplate.version(), template.templateMetadata().version())) { |
| 88 | + throw new OpenSearchCorruptionException( |
| 89 | + "Template version mismatch for " |
| 90 | + + template.templateMetadata().name() |
| 91 | + + ". Version in metadata: " |
| 92 | + + template.templateMetadata().version() |
| 93 | + + " , Version in content: " |
| 94 | + + newTemplate.version() |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + final PutComponentTemplateAction.Request request = new PutComponentTemplateAction.Request( |
| 99 | + template.templateMetadata().fullyQualifiedName() |
| 100 | + ).componentTemplate(newTemplate); |
| 101 | + |
| 102 | + return client.admin() |
| 103 | + .indices() |
| 104 | + .execute(PutComponentTemplateAction.INSTANCE, request) |
| 105 | + .actionGet(TimeValue.timeValueMillis(30000)) |
| 106 | + .isAcknowledged(); |
| 107 | + } |
| 108 | +} |
0 commit comments