Skip to content

Commit

Permalink
added resolving index template config; changed GetIndexMappings API t…
Browse files Browse the repository at this point in the history
…o return passed indexName in response (#283) (#285)

Signed-off-by: Petar Dzepina <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] authored Jan 11, 2023
1 parent 0559053 commit f6bfcb0
Show file tree
Hide file tree
Showing 4 changed files with 576 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,47 @@ public void onResponse(AcknowledgedResponse acknowledgedResponse) {
if (acknowledgedResponse.isAcknowledged() == false) {
log.warn("Upserting component template not ack'd!");
}
boolean updateConflictingTemplate = false;
// Find template which matches input index best
String templateName =
MetadataIndexTemplateService.findV2Template(
state.metadata(),
normalizeIndexName(indexName),
false
);
// If we find conflicting templates(regardless of priority) and that template was created by us,
// we will silently update index_pattern of that template.
// Otherwise, we will fail since we don't want to change index_pattern of user created index template
Map<String, List<String>> conflictingTemplates =
MetadataIndexTemplateService.findConflictingV2Templates(
state,
computeIndexTemplateName(indexName),
List.of(computeIndexPattern(indexName))
);

// If there is 1 conflict with our own template, we will update that template's index_pattern field
if (conflictingTemplates.size() == 1) {
String conflictingTemplateName = conflictingTemplates.keySet().iterator().next();
if (conflictingTemplateName.startsWith(OPENSEARCH_SAP_INDEX_TEMPLATE_PREFIX)) {
templateName = conflictingTemplateName;
updateConflictingTemplate = true;
}
}

if (templateName == null && conflictingTemplates.size() > 0) {
String errorMessage = "Found conflicting templates: [" +
String.join(", ", conflictingTemplates.keySet()) + "]";
log.error(errorMessage);
actionListener.onFailure(SecurityAnalyticsException.wrap(new IllegalStateException(errorMessage)));
return;
}

String componentName = computeComponentTemplateName(indexName);

ComposableIndexTemplate template;
if (templateName == null) {
template = new ComposableIndexTemplate(
List.of(indexName.endsWith("*") == false ? indexName + "*": indexName),
List.of(computeIndexPattern(indexName)),
null,
List.of(componentName),
null,
Expand All @@ -123,10 +151,18 @@ public void onResponse(AcknowledgedResponse acknowledgedResponse) {
template = state.metadata().templatesV2().get(templateName);
// Check if we need to append our component to composedOf list
if (template.composedOf().contains(componentName) == false) {
List<String> newComposedOf = new ArrayList<>(template.composedOf());
newComposedOf.add(componentName);
List<String> newComposedOf;
List<String> indexPatterns;
if (updateConflictingTemplate) {
newComposedOf = new ArrayList<>(template.composedOf());
newComposedOf.add(componentName);
indexPatterns = List.of(computeIndexPattern(indexName));
} else {
newComposedOf = List.of(componentName);
indexPatterns = template.indexPatterns();
}
template = new ComposableIndexTemplate(
template.indexPatterns(),
indexPatterns,
template.template(),
newComposedOf,
template.priority(),
Expand Down Expand Up @@ -155,6 +191,10 @@ public void onFailure(Exception e) {

}

private String computeIndexPattern(String indexName) {
return indexName.endsWith("*") == false ? indexName + "*" : indexName;
}

private void upsertIndexTemplate(
IndicesAdminClient indicesClient,
boolean create,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void getMappingAction(String indexName, ActionListener<GetIndexMappingsRe
resolveConcreteIndex(indexName, new ActionListener<>() {
@Override
public void onResponse(String concreteIndex) {
doGetMappingAction(concreteIndex, actionListener);
doGetMappingAction(indexName, concreteIndex, actionListener);
}

@Override
Expand All @@ -263,14 +263,13 @@ public void onFailure(Exception e) {
}
}

public void doGetMappingAction(String indexName, ActionListener<GetIndexMappingsResponse> actionListener) {
GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices(indexName);
public void doGetMappingAction(String indexName, String concreteIndexName, ActionListener<GetIndexMappingsResponse> actionListener) {
GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices(concreteIndexName);
indicesClient.getMappings(getMappingsRequest, new ActionListener<>() {
@Override
public void onResponse(GetMappingsResponse getMappingsResponse) {
try {
// Extract indexName and MappingMetadata
String indexName = getMappingsResponse.mappings().iterator().next().key;
// Extract MappingMetadata
MappingMetadata mappingMetadata = getMappingsResponse.mappings().iterator().next().value;
// List of all found applied aliases on index
List<String> appliedAliases = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1386,14 +1386,19 @@ protected void createComponentTemplateWithMappings(String componentTemplateName,
}

protected void createComposableIndexTemplate(String templateName, List<String> indexPatterns, String componentTemplateName, boolean isDatastream) throws IOException {
createComposableIndexTemplate(templateName, indexPatterns, componentTemplateName, isDatastream, 0);
}

protected void createComposableIndexTemplate(String templateName, List<String> indexPatterns, String componentTemplateName, boolean isDatastream, int priority) throws IOException {

String body = "{\n" +
(isDatastream ? "\"data_stream\": { }," : "") +
" \"index_patterns\": [" +
indexPatterns.stream().collect(
Collectors.joining(",", "\"", "\"")) +
" ]," +
"\"composed_of\": [\"" + componentTemplateName + "\"]" +
"\"composed_of\": [\"" + componentTemplateName + "\"]," +
"\"priority\":" + priority +
"}";
Response response = makeRequest(
client(),
Expand Down
Loading

0 comments on commit f6bfcb0

Please sign in to comment.