diff --git a/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/WazuhIndexerSetupPlugin.java b/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/WazuhIndexerSetupPlugin.java index 2cba1c4..a119e15 100644 --- a/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/WazuhIndexerSetupPlugin.java +++ b/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/WazuhIndexerSetupPlugin.java @@ -10,6 +10,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.action.admin.indices.create.CreateIndexResponse; +import org.opensearch.action.support.master.AcknowledgedResponse; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.node.DiscoveryNode; @@ -62,6 +63,18 @@ public Collection createComponents( public void onNodeStarted(DiscoveryNode localNode) { try { + this.indices.putTemplate(new ActionListener<>() { + @Override + public void onResponse(AcknowledgedResponse acknowledgedResponse) { + log.info("template created"); + } + + @Override + public void onFailure(Exception e) { + log.error("template creation failed"); + } + }); + this.indices.create(new ActionListener<>() { @Override public void onResponse(CreateIndexResponse createIndexResponse) { diff --git a/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/index/WazuhIndices.java b/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/index/WazuhIndices.java index 4eca0b3..a5b529f 100644 --- a/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/index/WazuhIndices.java +++ b/plugins/wazuh-indexer-setup/src/main/java/org/wazuh/setup/index/WazuhIndices.java @@ -11,6 +11,8 @@ import org.apache.logging.log4j.Logger; import org.opensearch.action.admin.indices.create.CreateIndexRequest; import org.opensearch.action.admin.indices.create.CreateIndexResponse; +import org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest; +import org.opensearch.action.support.master.AcknowledgedResponse; import org.opensearch.client.Client; import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.service.ClusterService; @@ -18,24 +20,24 @@ import org.opensearch.common.xcontent.XContentType; import org.opensearch.core.action.ActionListener; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.text.MessageFormat; +import java.util.List; import java.util.Locale; public class WazuhIndices { private static final Logger log = LogManager.getLogger(WazuhIndices.class); - private Client client; - private ClusterService clusterService; + private final Client client; + private final ClusterService clusterService; - public static String INDEX_NAME = "wazuh-indexer-setup-plugin"; - private static String INDEX_MAPPING_FILE_NAME = "index-mapping.yml"; - private static String INDEX_SETTING_FILE_NAME = "index-settings.yml"; + public static final String INDEX_NAME = "wazuh-indexer-setup-plugin"; + private static final String INDEX_MAPPING_FILE_NAME = "index-mapping.yml"; + private static final String INDEX_SETTING_FILE_NAME = "index-settings.yml"; /** * Constructor @@ -85,13 +87,29 @@ public String getIndexSettings() { } } + public void putTemplate(ActionListener actionListener) { + String indexTemplate = "wazuh"; + PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest() + .name(indexTemplate) + .patterns(List.of("wazuh-*")); + try { + client.admin().indices().putTemplate(putRequest, actionListener); + + } catch (Exception e) { + String errorMessage = new MessageFormat( + "failed to create index template [{0}]", + Locale.ROOT + ).format(indexTemplate); + log.error(errorMessage, e); + throw new IllegalStateException(errorMessage, e); + } + } /** * Create Wazuh's Indices. */ public void create(ActionListener actionListener) throws IOException { - if (!indexExists(WazuhIndices.INDEX_NAME)) { CreateIndexRequest indexRequest = new CreateIndexRequest(WazuhIndices.INDEX_NAME) .mapping(getIndexMapping(), XContentType.YAML) @@ -106,6 +124,6 @@ public void create(ActionListener actionListener) throws IO */ public boolean indexExists(String indexName) { ClusterState clusterState = clusterService.state(); - return clusterState.getRoutingTable().hasIndex(WazuhIndices.INDEX_NAME); + return clusterState.getRoutingTable().hasIndex(indexName); } }