Skip to content

Commit

Permalink
Add code to create index templates on start.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRuiz7 committed Jul 12, 2024
1 parent 5b49a46 commit d137d6c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +63,18 @@ public Collection<Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,33 @@
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;
import org.opensearch.common.util.io.Streams;
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
Expand Down Expand Up @@ -85,13 +87,29 @@ public String getIndexSettings() {
}
}

public void putTemplate(ActionListener<AcknowledgedResponse> 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<CreateIndexResponse> actionListener) throws IOException {


if (!indexExists(WazuhIndices.INDEX_NAME)) {
CreateIndexRequest indexRequest = new CreateIndexRequest(WazuhIndices.INDEX_NAME)
.mapping(getIndexMapping(), XContentType.YAML)
Expand All @@ -106,6 +124,6 @@ public void create(ActionListener<CreateIndexResponse> actionListener) throws IO
*/
public boolean indexExists(String indexName) {
ClusterState clusterState = clusterService.state();
return clusterState.getRoutingTable().hasIndex(WazuhIndices.INDEX_NAME);
return clusterState.getRoutingTable().hasIndex(indexName);
}
}

0 comments on commit d137d6c

Please sign in to comment.