forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HLRC support for simulate index template api (elastic#55936)
(cherry picked from commit 475790c) Signed-off-by: Andrei Dan <[email protected]>
- Loading branch information
Showing
9 changed files
with
320 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...gh-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client.indices; | ||
|
||
import org.elasticsearch.client.TimedRequest; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.Strings; | ||
|
||
/** | ||
* A request to simulate matching a provided index name and an optional new index template against the existing index templates. | ||
*/ | ||
public class SimulateIndexTemplateRequest extends TimedRequest { | ||
|
||
private String indexName; | ||
|
||
@Nullable | ||
private PutIndexTemplateV2Request indexTemplateV2Request; | ||
|
||
public SimulateIndexTemplateRequest(String indexName) { | ||
if (Strings.isNullOrEmpty(indexName)) { | ||
throw new IllegalArgumentException("index name cannot be null or empty"); | ||
} | ||
this.indexName = indexName; | ||
} | ||
|
||
/** | ||
* Return the index name for which we simulate the index template matching. | ||
*/ | ||
public String indexName() { | ||
return indexName; | ||
} | ||
|
||
/** | ||
* Set the index name to simulate template matching against the index templates in the system. | ||
*/ | ||
public SimulateIndexTemplateRequest indexName(String indexName) { | ||
if (Strings.isNullOrEmpty(indexName)) { | ||
throw new IllegalArgumentException("index name cannot be null or empty"); | ||
} | ||
this.indexName = indexName; | ||
return this; | ||
} | ||
|
||
/** | ||
* An optional new template request will be part of the index template simulation. | ||
*/ | ||
@Nullable | ||
public PutIndexTemplateV2Request indexTemplateV2Request() { | ||
return indexTemplateV2Request; | ||
} | ||
|
||
/** | ||
* Optionally, define a new template request which will included in the index simulation as if it was an index template stored in the | ||
* system. The new template will be validated just as a regular, standalone, live, new index template request. | ||
*/ | ||
public SimulateIndexTemplateRequest indexTemplateV2Request(@Nullable PutIndexTemplateV2Request indexTemplateV2Request) { | ||
this.indexTemplateV2Request = indexTemplateV2Request; | ||
return this; | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...h-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.indices; | ||
|
||
import org.elasticsearch.cluster.metadata.Template; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class SimulateIndexTemplateResponse { | ||
|
||
private static final ParseField TEMPLATE = new ParseField("template"); | ||
private static final ParseField OVERLAPPING = new ParseField("overlapping"); | ||
private static final ParseField NAME = new ParseField("name"); | ||
private static final ParseField INDEX_PATTERNS = new ParseField("index_patterns"); | ||
|
||
@SuppressWarnings("unchecked") | ||
private static final ConstructingObjectParser<SimulateIndexTemplateResponse, Void> PARSER = | ||
new ConstructingObjectParser<>("simulate_index_templates_response", false, | ||
a -> new SimulateIndexTemplateResponse( | ||
a[0] != null ? (Template) a[0] : null, | ||
a[1] != null ? | ||
((List<IndexTemplateAndPatterns>) a[1]).stream() | ||
.collect(Collectors.toMap(IndexTemplateAndPatterns::name, IndexTemplateAndPatterns::indexPatterns)) : null | ||
) | ||
); | ||
|
||
@SuppressWarnings("unchecked") | ||
private static final ConstructingObjectParser<IndexTemplateAndPatterns, Void> INNER_PARSER = | ||
new ConstructingObjectParser<>("index_template_and_patterns", false, | ||
a -> new IndexTemplateAndPatterns((String) a[0], (List<String>) a[1])); | ||
|
||
private static class IndexTemplateAndPatterns { | ||
String name; | ||
List<String> indexPatterns; | ||
|
||
IndexTemplateAndPatterns(String name, List<String> indexPatterns) { | ||
this.name = name; | ||
this.indexPatterns = indexPatterns; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public List<String> indexPatterns() { | ||
return indexPatterns; | ||
} | ||
} | ||
|
||
static { | ||
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Template.PARSER, TEMPLATE); | ||
INNER_PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); | ||
INNER_PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), INDEX_PATTERNS); | ||
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), INNER_PARSER, OVERLAPPING); | ||
} | ||
|
||
@Nullable | ||
// the resolved settings, mappings and aliases for the matched templates, if any | ||
private Template resolvedTemplate; | ||
|
||
@Nullable | ||
// a map of template names and their index patterns that would overlap when matching the given index name | ||
private Map<String, List<String>> overlappingTemplates; | ||
|
||
SimulateIndexTemplateResponse(@Nullable Template resolvedTemplate, @Nullable Map<String, List<String>> overlappingTemplates) { | ||
this.resolvedTemplate = resolvedTemplate; | ||
this.overlappingTemplates = overlappingTemplates; | ||
} | ||
|
||
public static SimulateIndexTemplateResponse fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, null); | ||
} | ||
|
||
public Template resolvedTemplate() { | ||
return resolvedTemplate; | ||
} | ||
|
||
public Map<String, List<String>> overlappingTemplates() { | ||
return overlappingTemplates; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
SimulateIndexTemplateResponse that = (SimulateIndexTemplateResponse) o; | ||
return Objects.equals(resolvedTemplate, that.resolvedTemplate) | ||
&& Objects.deepEquals(overlappingTemplates, that.overlappingTemplates); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(resolvedTemplate, overlappingTemplates); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SimulateIndexTemplateResponse{" + "resolved template=" + resolvedTemplate + ", overlapping templates=" | ||
+ String.join("|", overlappingTemplates.keySet()) + "}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.