use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class RestGetAliasesAction method buildRestResponse.
static RestResponse buildRestResponse(boolean aliasesExplicitlyRequested, String[] requestedAliases, ImmutableOpenMap<String, List<AliasMetadata>> responseAliasMap, XContentBuilder builder) throws Exception {
final Set<String> indicesToDisplay = new HashSet<>();
final Set<String> returnedAliasNames = new HashSet<>();
for (final ObjectObjectCursor<String, List<AliasMetadata>> cursor : responseAliasMap) {
for (final AliasMetadata aliasMetadata : cursor.value) {
if (aliasesExplicitlyRequested) {
// only display indices that have aliases
indicesToDisplay.add(cursor.key);
}
returnedAliasNames.add(aliasMetadata.alias());
}
}
// compute explicitly requested aliases that have are not returned in the result
final SortedSet<String> missingAliases = new TreeSet<>();
// first wildcard index, leading "-" as an alias name after this index means
// that it is an exclusion
int firstWildcardIndex = requestedAliases.length;
for (int i = 0; i < requestedAliases.length; i++) {
if (Regex.isSimpleMatchPattern(requestedAliases[i])) {
firstWildcardIndex = i;
break;
}
}
for (int i = 0; i < requestedAliases.length; i++) {
if (Metadata.ALL.equals(requestedAliases[i]) || Regex.isSimpleMatchPattern(requestedAliases[i]) || (i > firstWildcardIndex && requestedAliases[i].charAt(0) == '-')) {
// only explicitly requested aliases will be called out as missing (404)
continue;
}
// check if aliases[i] is subsequently excluded
int j = Math.max(i + 1, firstWildcardIndex);
for (; j < requestedAliases.length; j++) {
if (requestedAliases[j].charAt(0) == '-') {
// this is an exclude pattern
if (Regex.simpleMatch(requestedAliases[j].substring(1), requestedAliases[i]) || Metadata.ALL.equals(requestedAliases[j].substring(1))) {
// aliases[i] is excluded by aliases[j]
break;
}
}
}
if (j == requestedAliases.length) {
// explicitly requested aliases[i] is not excluded by any subsequent "-" wildcard in expression
if (false == returnedAliasNames.contains(requestedAliases[i])) {
// aliases[i] is not in the result set
missingAliases.add(requestedAliases[i]);
}
}
}
final RestStatus status;
builder.startObject();
{
if (missingAliases.isEmpty()) {
status = RestStatus.OK;
} else {
status = RestStatus.NOT_FOUND;
final String message;
if (missingAliases.size() == 1) {
message = String.format(Locale.ROOT, "alias [%s] missing", Strings.collectionToCommaDelimitedString(missingAliases));
} else {
message = String.format(Locale.ROOT, "aliases [%s] missing", Strings.collectionToCommaDelimitedString(missingAliases));
}
builder.field("error", message);
builder.field("status", status.getStatus());
}
for (final ObjectObjectCursor<String, List<AliasMetadata>> entry : responseAliasMap) {
if (aliasesExplicitlyRequested == false || (aliasesExplicitlyRequested && indicesToDisplay.contains(entry.key))) {
builder.startObject(entry.key);
{
builder.startObject("aliases");
{
for (final AliasMetadata alias : entry.value) {
AliasMetadata.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
}
}
builder.endObject();
}
builder.endObject();
}
}
}
builder.endObject();
return new BytesRestResponse(status, builder);
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class PutMappingRequestTests method addAliases.
/**
* Adds aliases to the supplied ClusterState instance. The aliases parameter takes of list of tuples of aliasName
* to the alias's indices. The alias's indices are a tuple of index name and a flag indicating whether the alias
* is a write alias for that index. See usage examples above.
*/
private static ClusterState addAliases(ClusterState cs, List<Tuple<String, List<Tuple<String, Boolean>>>> aliases) {
Metadata.Builder builder = Metadata.builder(cs.metadata());
for (Tuple<String, List<Tuple<String, Boolean>>> alias : aliases) {
for (Tuple<String, Boolean> index : alias.v2()) {
IndexMetadata im = builder.get(index.v1());
AliasMetadata newAliasMd = AliasMetadata.newAliasMetadataBuilder(alias.v1()).writeIndex(index.v2()).build();
builder.put(IndexMetadata.builder(im).putAlias(newAliasMd));
}
}
return ClusterState.builder(cs).metadata(builder.build()).build();
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class MetadataRolloverServiceTests method testHiddenAffectsResolvedV2ComponentTemplates.
public void testHiddenAffectsResolvedV2ComponentTemplates() {
Map<String, AliasMetadata> aliases = new HashMap<>();
aliases.put("foo-write", AliasMetadata.builder("foo-write").build());
aliases.put("bar-write", AliasMetadata.builder("bar-write").writeIndex(randomBoolean()).build());
final ComponentTemplate ct = new ComponentTemplate(new Template(null, null, aliases), null, null);
final ComposableIndexTemplate template = new ComposableIndexTemplate(Collections.singletonList("*"), null, Collections.singletonList("ct"), null, null, null, null);
final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false).put("ct", ct).put("test-template", template).build();
String indexName = randomFrom("foo-123", "bar-xyz");
String aliasName = randomFrom("foo-write", "bar-write");
// hidden shouldn't throw
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE);
// not hidden will throw
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomFrom(Boolean.FALSE, null)));
assertThat(ex.getMessage(), containsString("index template [test-template]"));
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class MetadataRolloverServiceTests method testRejectDuplicateAliasV2UsingComponentTemplates.
public void testRejectDuplicateAliasV2UsingComponentTemplates() {
Map<String, AliasMetadata> aliases = new HashMap<>();
aliases.put("foo-write", AliasMetadata.builder("foo-write").build());
aliases.put("bar-write", AliasMetadata.builder("bar-write").writeIndex(randomBoolean()).build());
final ComponentTemplate ct = new ComponentTemplate(new Template(null, null, aliases), null, null);
final ComposableIndexTemplate template = new ComposableIndexTemplate(Arrays.asList("foo-*", "bar-*"), null, Collections.singletonList("ct"), null, null, null, null);
final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false).put("ct", ct).put("test-template", template).build();
String indexName = randomFrom("foo-123", "bar-xyz");
String aliasName = randomFrom("foo-write", "bar-write");
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomBoolean()));
assertThat(ex.getMessage(), containsString("index template [test-template]"));
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class ValidateIndicesAliasesRequestIT method testAllowed.
public void testAllowed() {
final Settings settings = Settings.builder().putList(IndicesAliasesPlugin.ALLOWED_ORIGINS_SETTING.getKey(), Collections.singletonList("allowed")).build();
createIndex("index", settings);
final IndicesAliasesRequest request = new IndicesAliasesRequest().origin("allowed");
request.addAliasAction(IndicesAliasesRequest.AliasActions.add().index("index").alias("alias"));
assertAcked(client().admin().indices().aliases(request).actionGet());
final GetAliasesResponse response = client().admin().indices().getAliases(new GetAliasesRequest("alias")).actionGet();
assertThat(response.getAliases().keys().size(), equalTo(1));
assertThat(response.getAliases().keys().iterator().next().value, equalTo("index"));
final List<AliasMetadata> aliasMetadata = response.getAliases().get("index");
assertThat(aliasMetadata, hasSize(1));
assertThat(aliasMetadata.get(0).alias(), equalTo("alias"));
}
Aggregations