use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class GetIndexTemplatesResponseTests method testParsingFromEsResponse.
public void testParsingFromEsResponse() throws IOException {
for (int runs = 0; runs < 20; runs++) {
org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse esResponse = new org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse(new ArrayList<>());
XContentType xContentType = randomFrom(XContentType.values());
int numTemplates = randomIntBetween(0, 32);
for (int i = 0; i < numTemplates; i++) {
org.opensearch.cluster.metadata.IndexTemplateMetadata.Builder esIMD = new org.opensearch.cluster.metadata.IndexTemplateMetadata.Builder(String.format(Locale.ROOT, "%02d ", i) + randomAlphaOfLength(4));
esIMD.patterns(Arrays.asList(generateRandomStringArray(32, 4, false, false)));
esIMD.settings(randomIndexSettings());
esIMD.putMapping("_doc", new CompressedXContent(BytesReference.bytes(randomMapping("_doc", xContentType))));
int numAliases = randomIntBetween(0, 8);
for (int j = 0; j < numAliases; j++) {
esIMD.putAlias(randomAliasMetadata(String.format(Locale.ROOT, "%02d ", j) + randomAlphaOfLength(4)));
}
esIMD.order(randomIntBetween(0, Integer.MAX_VALUE));
esIMD.version(randomIntBetween(0, Integer.MAX_VALUE));
esResponse.getIndexTemplates().add(esIMD.build());
}
XContentBuilder xContentBuilder = XContentBuilder.builder(xContentType.xContent());
esResponse.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(xContentBuilder), xContentType)) {
GetIndexTemplatesResponse response = GetIndexTemplatesResponse.fromXContent(parser);
assertThat(response.getIndexTemplates().size(), equalTo(numTemplates));
response.getIndexTemplates().sort(Comparator.comparing(IndexTemplateMetadata::name));
for (int i = 0; i < numTemplates; i++) {
org.opensearch.cluster.metadata.IndexTemplateMetadata esIMD = esResponse.getIndexTemplates().get(i);
IndexTemplateMetadata result = response.getIndexTemplates().get(i);
assertThat(result.patterns(), equalTo(esIMD.patterns()));
assertThat(result.settings(), equalTo(esIMD.settings()));
assertThat(result.order(), equalTo(esIMD.order()));
assertThat(result.version(), equalTo(esIMD.version()));
assertThat(esIMD.mappings().size(), equalTo(1));
BytesReference mappingSource = esIMD.mappings().valuesIt().next().uncompressed();
Map<String, Object> expectedMapping = XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType()).v2();
assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc")));
assertThat(result.aliases().size(), equalTo(esIMD.aliases().size()));
List<AliasMetadata> expectedAliases = Arrays.stream(esIMD.aliases().values().toArray(AliasMetadata.class)).sorted(Comparator.comparing(AliasMetadata::alias)).collect(Collectors.toList());
List<AliasMetadata> actualAliases = Arrays.stream(result.aliases().values().toArray(AliasMetadata.class)).sorted(Comparator.comparing(AliasMetadata::alias)).collect(Collectors.toList());
for (int j = 0; j < result.aliases().size(); j++) {
assertThat(actualAliases.get(j), equalTo(expectedAliases.get(j)));
}
}
}
}
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class GetAliasesResponseTests method mutateAliases.
private static ImmutableOpenMap<String, List<AliasMetadata>> mutateAliases(ImmutableOpenMap<String, List<AliasMetadata>> aliases) {
if (aliases.isEmpty()) {
return createIndicesAliasesMap(1, 3).build();
}
if (randomBoolean()) {
ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder(aliases);
ImmutableOpenMap<String, List<AliasMetadata>> list = createIndicesAliasesMap(1, 2).build();
list.forEach(e -> builder.put(e.key, e.value));
return builder.build();
}
Set<String> indices = new HashSet<>();
Iterator<String> keys = aliases.keysIt();
while (keys.hasNext()) {
indices.add(keys.next());
}
List<String> indicesToBeModified = randomSubsetOf(randomIntBetween(1, indices.size()), indices);
ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder();
for (String index : indices) {
List<AliasMetadata> list = new ArrayList<>(aliases.get(index));
if (indicesToBeModified.contains(index)) {
if (randomBoolean() || list.isEmpty()) {
list.add(createAliasMetadata());
} else {
int aliasIndex = randomInt(list.size() - 1);
AliasMetadata aliasMetadata = list.get(aliasIndex);
list.add(aliasIndex, mutateAliasMetadata(aliasMetadata));
}
}
builder.put(index, list);
}
return builder.build();
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class TransportGetAliasesActionTests method testDeprecationWarningEmittedForTotalWildcard.
public void testDeprecationWarningEmittedForTotalWildcard() {
ClusterState state = systemIndexTestClusterState();
GetAliasesRequest request = new GetAliasesRequest();
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder().fPut(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build())).fPut("c", Collections.singletonList(new AliasMetadata.Builder("d").build())).build();
final String[] concreteIndices = { "a", ".b", "c" };
assertEquals(state.metadata().findAliases(request, concreteIndices), aliases);
ImmutableOpenMap<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(request, concreteIndices, aliases, state, false, EMPTY_SYSTEM_INDICES);
assertThat(result.size(), equalTo(3));
assertThat(result.get("a").size(), equalTo(0));
assertThat(result.get(".b").size(), equalTo(1));
assertThat(result.get("c").size(), equalTo(1));
assertWarnings("this request accesses system indices: [.b], but in a future major version, direct access to system " + "indices will be prevented by default");
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class TransportGetAliasesActionTests method testDeprecationWarningEmittedWhenRequestingNonExistingAliasInSystemPattern.
public void testDeprecationWarningEmittedWhenRequestingNonExistingAliasInSystemPattern() {
ClusterState state = systemIndexTestClusterState();
SystemIndices systemIndices = new SystemIndices(Collections.singletonMap(this.getTestName(), Collections.singletonList(new SystemIndexDescriptor(".y", "an index that doesn't exist"))));
GetAliasesRequest request = new GetAliasesRequest(".y");
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder().build();
final String[] concreteIndices = {};
assertEquals(state.metadata().findAliases(request, concreteIndices), aliases);
ImmutableOpenMap<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(request, concreteIndices, aliases, state, false, systemIndices);
assertThat(result.size(), equalTo(0));
assertWarnings("this request accesses aliases with names reserved for system indices: [.y], but in a future major version, direct" + "access to system indices and their aliases will not be allowed");
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class RestAliasAction method buildTable.
private Table buildTable(RestRequest request, GetAliasesResponse response) {
Table table = getTableWithHeader(request);
for (ObjectObjectCursor<String, List<AliasMetadata>> cursor : response.getAliases()) {
String indexName = cursor.key;
for (AliasMetadata aliasMetadata : cursor.value) {
table.startRow();
table.addCell(aliasMetadata.alias());
table.addCell(indexName);
table.addCell(aliasMetadata.filteringRequired() ? "*" : "-");
String indexRouting = Strings.hasLength(aliasMetadata.indexRouting()) ? aliasMetadata.indexRouting() : "-";
table.addCell(indexRouting);
String searchRouting = Strings.hasLength(aliasMetadata.searchRouting()) ? aliasMetadata.searchRouting() : "-";
table.addCell(searchRouting);
String isWriteIndex = aliasMetadata.writeIndex() == null ? "-" : aliasMetadata.writeIndex().toString();
table.addCell(isWriteIndex);
table.endRow();
}
}
return table;
}
Aggregations