Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public TransformState transform(ProjectId projectId, ComponentsAndComposables so

// 4. validate for v2 composable template overlaps
for (var request : composables) {
MetadataIndexTemplateService.v2TemplateOverlaps(project, request.name(), request.indexTemplate(), true);
MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), request.name(), request.indexTemplate(), true);
}

Set<String> componentEntities = components.stream().map(r -> reservedComponentName(r.name())).collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,16 @@ public ProjectMetadata addComponentTemplate(
return ProjectMetadata.builder(project).put(name, finalComponentTemplate).build();
}

/**
* Mappings in templates don't have to include <code>_doc</code>, so update the mappings to include this single type if necessary
*
* @param mappings mappings from a template
* @param xContentRegistry the xcontent registry used for parsing
* @return a normalized form of the mapping provided
* @throws IOException if reading or writing the mapping encounters a problem
*/
@Nullable
private static CompressedXContent wrapMappingsIfNecessary(@Nullable CompressedXContent mappings, NamedXContentRegistry xContentRegistry)
public static CompressedXContent wrapMappingsIfNecessary(@Nullable CompressedXContent mappings, NamedXContentRegistry xContentRegistry)
throws IOException {
// Mappings in templates don't have to include _doc, so update
// the mappings to include this single type if necessary
Expand Down Expand Up @@ -605,14 +613,22 @@ public ProjectMetadata execute(ProjectMetadata currentProject) throws Exception
}

public static void validateV2TemplateRequest(ProjectMetadata metadata, String name, ComposableIndexTemplate template) {
validateV2TemplateRequest(metadata.componentTemplates(), name, template);
}

public static void validateV2TemplateRequest(
Map<String, ComponentTemplate> componentTemplates,
String name,
ComposableIndexTemplate template
) {
if (template.createdDateMillis().isPresent()) {
throw new InvalidIndexTemplateException(name, "provided a template property which is managed by the system: created_date");
}
if (template.modifiedDateMillis().isPresent()) {
throw new InvalidIndexTemplateException(name, "provided a template property which is managed by the system: modified_date");
}
if (template.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)) {
Settings mergedSettings = resolveSettings(template, metadata.componentTemplates());
Settings mergedSettings = resolveSettings(template, componentTemplates);
if (IndexMetadata.INDEX_HIDDEN_SETTING.exists(mergedSettings)) {
throw new InvalidIndexTemplateException(
name,
Expand All @@ -621,7 +637,6 @@ public static void validateV2TemplateRequest(ProjectMetadata metadata, String na
}
}

final Map<String, ComponentTemplate> componentTemplates = metadata.componentTemplates();
final List<String> ignoreMissingComponentTemplates = (template.getIgnoreMissingComponentTemplates() == null
? List.of()
: template.getIgnoreMissingComponentTemplates());
Expand Down Expand Up @@ -673,7 +688,7 @@ public ProjectMetadata addIndexTemplateV2(
throw new IllegalArgumentException("index template [" + name + "] already exists");
}

Map<String, List<String>> overlaps = v2TemplateOverlaps(project, name, template, validateV2Overlaps);
Map<String, List<String>> overlaps = v2TemplateOverlaps(project.templatesV2(), name, template, validateV2Overlaps);

overlaps = findConflictingV1Templates(project, name, template.indexPatterns());
if (overlaps.size() > 0) {
Expand Down Expand Up @@ -737,20 +752,20 @@ public ProjectMetadata addIndexTemplateV2(
* we throw an {@link IllegalArgumentException} with information about the conflicting templates.
* <p>
* This method doesn't check for conflicting overlaps with v1 templates.
* @param project the current project
* @param templatesV2 the templates to check overlap against
* @param name the composable index template name
* @param template the full composable index template object we check for overlaps
* @param validate should we throw {@link IllegalArgumentException} if conflicts are found or just compute them
* @return a map of v2 template names to their index patterns for v2 templates that would overlap with the given template
*/
public static Map<String, List<String>> v2TemplateOverlaps(
final ProjectMetadata project,
Map<String, ComposableIndexTemplate> templatesV2,
String name,
final ComposableIndexTemplate template,
boolean validate
) {
Map<String, List<String>> overlaps = findConflictingV2Templates(
project,
templatesV2,
name,
template.indexPatterns(),
true,
Expand Down Expand Up @@ -1040,7 +1055,7 @@ public static Map<String, List<String>> findConflictingV2Templates(
final String candidateName,
final List<String> indexPatterns
) {
return findConflictingV2Templates(project, candidateName, indexPatterns, false, 0L);
return findConflictingV2Templates(project.templatesV2(), candidateName, indexPatterns, false, 0L);
}

/**
Expand All @@ -1054,15 +1069,15 @@ public static Map<String, List<String>> findConflictingV2Templates(
* index templates with the same priority).
*/
static Map<String, List<String>> findConflictingV2Templates(
final ProjectMetadata project,
final Map<String, ComposableIndexTemplate> templatesV2,
final String candidateName,
final List<String> indexPatterns,
boolean checkPriority,
long priority
) {
Automaton v1automaton = Regex.simpleMatchToAutomaton(indexPatterns.toArray(Strings.EMPTY_ARRAY));
Map<String, List<String>> overlappingTemplates = new TreeMap<>();
for (Map.Entry<String, ComposableIndexTemplate> entry : project.templatesV2().entrySet()) {
for (Map.Entry<String, ComposableIndexTemplate> entry : templatesV2.entrySet()) {
String name = entry.getKey();
ComposableIndexTemplate template = entry.getValue();
Automaton v2automaton = Regex.simpleMatchToAutomaton(template.indexPatterns().toArray(Strings.EMPTY_ARRAY));
Expand Down Expand Up @@ -2005,7 +2020,8 @@ private static void validateCompositeTemplate(
});
}

static void validateTemplate(Settings validateSettings, CompressedXContent mappings, IndicesService indicesService) throws Exception {
public static void validateTemplate(Settings validateSettings, CompressedXContent mappings, IndicesService indicesService)
throws Exception {
// Hard to validate settings if they're non-existent, so used empty ones if none were provided
Settings settings = validateSettings;
if (settings == null) {
Expand Down Expand Up @@ -2036,7 +2052,7 @@ static void validateTemplate(Settings validateSettings, CompressedXContent mappi
});
}

private void validate(String name, ComponentTemplate template) {
public void validate(String name, ComponentTemplate template) {
validate(name, template.template(), Collections.emptyList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2476,14 +2476,14 @@ public void testV2TemplateOverlaps() throws Exception {
.build();

// when validating is false, we return the conflicts instead of throwing an exception
var overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project, "foo2", newTemplate, false);
var overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "foo2", newTemplate, false);

assertThat(overlaps, allOf(aMapWithSize(1), hasKey("foo")));

// try now the same thing with validation on
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project, "foo2", newTemplate, true)
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "foo2", newTemplate, true)
);
assertThat(
e.getMessage(),
Expand All @@ -2499,7 +2499,7 @@ public void testV2TemplateOverlaps() throws Exception {
.priority(1L)
.build();

overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project, "no-conflict", nonConflict, true);
overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "no-conflict", nonConflict, true);
assertTrue(overlaps.isEmpty());
}

Expand All @@ -2513,7 +2513,7 @@ public void testV2TemplateOverlaps() throws Exception {
.build();
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project, "foo2", newTemplate, true)
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "foo2", newTemplate, true)
);
assertThat(
e.getMessage(),
Expand Down