use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.
the class IndicesClientIT method testSimulateIndexTemplate.
public void testSimulateIndexTemplate() throws Exception {
String templateName = "my-template";
Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
Template template = new Template(settings, mappings, org.opensearch.common.collect.Map.of("alias", alias));
List<String> pattern = org.opensearch.common.collect.List.of("pattern");
ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null);
PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate);
AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
SimulateIndexTemplateRequest simulateIndexTemplateRequest = new SimulateIndexTemplateRequest("pattern");
AliasMetadata simulationAlias = AliasMetadata.builder("simulation-alias").writeIndex(true).build();
ComposableIndexTemplate simulationTemplate = new ComposableIndexTemplate(pattern, new Template(null, null, org.opensearch.common.collect.Map.of("simulation-alias", simulationAlias)), Collections.emptyList(), 2L, 1L, new HashMap<>(), null);
PutComposableIndexTemplateRequest newIndexTemplateReq = new PutComposableIndexTemplateRequest().name("used-for-simulation").create(true).indexTemplate(indexTemplate);
newIndexTemplateReq.indexTemplate(simulationTemplate);
simulateIndexTemplateRequest.indexTemplateV2Request(newIndexTemplateReq);
SimulateIndexTemplateResponse simulateResponse = execute(simulateIndexTemplateRequest, highLevelClient().indices()::simulateIndexTemplate, highLevelClient().indices()::simulateIndexTemplateAsync);
Map<String, AliasMetadata> aliases = simulateResponse.resolvedTemplate().aliases();
assertThat(aliases, is(notNullValue()));
assertThat("the template we provided for the simulation has a higher priority than the one in the system", aliases.get("simulation-alias"), is(notNullValue()));
assertThat(aliases.get("simulation-alias").getAlias(), is("simulation-alias"));
assertThat("existing template overlaps the higher priority template we provided for the simulation", simulateResponse.overlappingTemplates().get("my-template").get(0), is("pattern"));
}
use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.
the class IndicesClientIT method testIndexTemplates.
public void testIndexTemplates() throws Exception {
String templateName = "my-template";
Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias));
List<String> pattern = Collections.singletonList("pattern");
ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null);
PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate);
AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
ComposableIndexTemplateExistRequest composableIndexTemplateExistRequest = new ComposableIndexTemplateExistRequest(templateName);
boolean exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
assertTrue(exist);
GetComposableIndexTemplateRequest getComposableIndexTemplateRequest = new GetComposableIndexTemplateRequest(templateName);
GetComposableIndexTemplatesResponse getResponse = execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync);
assertThat(getResponse.getIndexTemplates().size(), equalTo(1));
assertThat(getResponse.getIndexTemplates().containsKey(templateName), equalTo(true));
assertThat(getResponse.getIndexTemplates().get(templateName), equalTo(indexTemplate));
DeleteComposableIndexTemplateRequest deleteComposableIndexTemplateRequest = new DeleteComposableIndexTemplateRequest(templateName);
response = execute(deleteComposableIndexTemplateRequest, highLevelClient().indices()::deleteIndexTemplate, highLevelClient().indices()::deleteIndexTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync));
assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
assertFalse(exist);
}
use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testSimulateIndexTemplate.
public void testSimulateIndexTemplate() throws Exception {
RestHighLevelClient client = highLevelClient();
{
// <1>
PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest().name("my-template");
Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null);
ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(Arrays.asList("pattern-1", "log-*"), template, null, null, null, null);
request.indexTemplate(composableIndexTemplate);
assertTrue(client.indices().putIndexTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::simulate-index-template-request
// <1>
SimulateIndexTemplateRequest simulateRequest = new SimulateIndexTemplateRequest("log-000001");
PutComposableIndexTemplateRequest newIndexTemplateRequest = new PutComposableIndexTemplateRequest().name("used-for-simulation");
Settings settings = Settings.builder().put("index.number_of_shards", 6).build();
// <2>
Template template = new Template(settings, null, null);
ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(Arrays.asList("log-*"), template, null, 90L, null, null);
newIndexTemplateRequest.indexTemplate(composableIndexTemplate);
// <2>
simulateRequest.indexTemplateV2Request(newIndexTemplateRequest);
// end::simulate-index-template-request
// tag::simulate-index-template-response
SimulateIndexTemplateResponse simulateIndexTemplateResponse = client.indices().simulateIndexTemplate(simulateRequest, RequestOptions.DEFAULT);
// <1>
assertThat(simulateIndexTemplateResponse.resolvedTemplate().settings().get("index.number_of_shards"), is("6"));
assertThat(simulateIndexTemplateResponse.overlappingTemplates().get("my-template"), // <2>
containsInAnyOrder("pattern-1", "log-*"));
// end::simulate-index-template-response
// tag::simulate-index-template-execute-listener
ActionListener<SimulateIndexTemplateResponse> listener = new ActionListener<SimulateIndexTemplateResponse>() {
@Override
public void onResponse(SimulateIndexTemplateResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::simulate-index-template-execute-listener
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::simulate-index-template-execute-async
// <1>
client.indices().simulateIndexTemplateAsync(simulateRequest, RequestOptions.DEFAULT, listener);
// end::simulate-index-template-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.
the class TransportGetComposableIndexTemplateAction method masterOperation.
@Override
protected void masterOperation(GetComposableIndexTemplateAction.Request request, ClusterState state, ActionListener<GetComposableIndexTemplateAction.Response> listener) {
Map<String, ComposableIndexTemplate> allTemplates = state.metadata().templatesV2();
// If we did not ask for a specific name, then we return all templates
if (request.name() == null) {
listener.onResponse(new GetComposableIndexTemplateAction.Response(allTemplates));
return;
}
final Map<String, ComposableIndexTemplate> results = new HashMap<>();
String name = request.name();
if (Regex.isSimpleMatchPattern(name)) {
for (Map.Entry<String, ComposableIndexTemplate> entry : allTemplates.entrySet()) {
if (Regex.simpleMatch(name, entry.getKey())) {
results.put(entry.getKey(), entry.getValue());
}
}
} else if (allTemplates.containsKey(name)) {
results.put(name, allTemplates.get(name));
} else {
throw new ResourceNotFoundException("index template matching [" + request.name() + "] not found");
}
listener.onResponse(new GetComposableIndexTemplateAction.Response(results));
}
use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.
the class TransportSimulateIndexTemplateAction method masterOperation.
@Override
protected void masterOperation(SimulateIndexTemplateRequest request, ClusterState state, ActionListener<SimulateIndexTemplateResponse> listener) throws Exception {
final ClusterState stateWithTemplate;
if (request.getIndexTemplateRequest() != null) {
// we'll "locally" add the template defined by the user in the cluster state (as if it existed in the system)
String simulateTemplateToAdd = "simulate_index_template_" + UUIDs.randomBase64UUID().toLowerCase(Locale.ROOT);
// Perform validation for things like typos in component template names
MetadataIndexTemplateService.validateV2TemplateRequest(state.metadata(), simulateTemplateToAdd, request.getIndexTemplateRequest().indexTemplate());
stateWithTemplate = indexTemplateService.addIndexTemplateV2(state, request.getIndexTemplateRequest().create(), simulateTemplateToAdd, request.getIndexTemplateRequest().indexTemplate());
} else {
stateWithTemplate = state;
}
String matchingTemplate = findV2Template(stateWithTemplate.metadata(), request.getIndexName(), false);
if (matchingTemplate == null) {
listener.onResponse(new SimulateIndexTemplateResponse(null, null));
return;
}
final ClusterState tempClusterState = resolveTemporaryState(matchingTemplate, request.getIndexName(), stateWithTemplate);
ComposableIndexTemplate templateV2 = tempClusterState.metadata().templatesV2().get(matchingTemplate);
assert templateV2 != null : "the matched template must exist";
final Template template = resolveTemplate(matchingTemplate, request.getIndexName(), stateWithTemplate, xContentRegistry, indicesService, aliasValidator);
final Map<String, List<String>> overlapping = new HashMap<>();
overlapping.putAll(findConflictingV1Templates(tempClusterState, matchingTemplate, templateV2.indexPatterns()));
overlapping.putAll(findConflictingV2Templates(tempClusterState, matchingTemplate, templateV2.indexPatterns()));
listener.onResponse(new SimulateIndexTemplateResponse(template, overlapping));
}
Aggregations