use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions in project elasticsearch by elastic.
the class AliasActionsTests method testValidate.
public void testValidate() {
AliasActions.Type type = randomFrom(AliasActions.Type.values());
if (type == AliasActions.Type.REMOVE_INDEX) {
Exception e = expectThrows(IllegalArgumentException.class, () -> new AliasActions(type).validate());
assertEquals("One of [index] or [indices] is required", e.getMessage());
} else {
Exception e = expectThrows(IllegalArgumentException.class, () -> new AliasActions(type).alias(randomAsciiOfLength(5)).validate());
assertEquals("One of [index] or [indices] is required", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> new AliasActions(type).index(randomAsciiOfLength(5)).validate());
assertEquals("One of [alias] or [aliases] is required", e.getMessage());
}
}
use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions in project elasticsearch by elastic.
the class TransportIndicesAliasesAction method masterOperation.
@Override
protected void masterOperation(final IndicesAliasesRequest request, final ClusterState state, final ActionListener<IndicesAliasesResponse> listener) {
//Expand the indices names
List<AliasActions> actions = request.aliasActions();
List<AliasAction> finalActions = new ArrayList<>();
// Resolve all the AliasActions into AliasAction instances and gather all the aliases
Set<String> aliases = new HashSet<>();
for (AliasActions action : actions) {
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request.indicesOptions(), action.indices());
Collections.addAll(aliases, action.aliases());
for (String index : concreteIndices) {
switch(action.actionType()) {
case ADD:
for (String alias : action.concreteAliases(state.metaData(), index)) {
finalActions.add(new AliasAction.Add(index, alias, action.filter(), action.indexRouting(), action.searchRouting()));
}
break;
case REMOVE:
for (String alias : action.concreteAliases(state.metaData(), index)) {
finalActions.add(new AliasAction.Remove(index, alias));
}
break;
case REMOVE_INDEX:
finalActions.add(new AliasAction.RemoveIndex(index));
break;
default:
throw new IllegalArgumentException("Unsupported action [" + action.actionType() + "]");
}
}
}
if (finalActions.isEmpty() && false == actions.isEmpty()) {
throw new AliasesNotFoundException(aliases.toArray(new String[aliases.size()]));
}
request.aliasActions().clear();
IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest(unmodifiableList(finalActions)).ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout());
indexAliasesService.indicesAliases(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {
@Override
public void onResponse(ClusterStateUpdateResponse response) {
listener.onResponse(new IndicesAliasesResponse(response.isAcknowledged()));
}
@Override
public void onFailure(Exception t) {
logger.debug("failed to perform aliases", t);
listener.onFailure(t);
}
});
}
use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions in project elasticsearch by elastic.
the class AliasActionsTests method testRoundTrip.
public void testRoundTrip() throws IOException {
AliasActions action = new AliasActions(randomFrom(AliasActions.Type.values()));
if (randomBoolean()) {
action.index(randomAsciiOfLength(5));
} else {
action.indices(generateRandomStringArray(5, 5, false, false));
}
if (action.actionType() != AliasActions.Type.REMOVE_INDEX) {
if (randomBoolean()) {
action.alias(randomAsciiOfLength(5));
} else {
action.aliases(generateRandomStringArray(5, 5, false, false));
}
}
if (action.actionType() == AliasActions.Type.ADD) {
if (randomBoolean()) {
action.filter(randomAsciiOfLength(10));
}
if (randomBoolean()) {
if (randomBoolean()) {
action.routing(randomAsciiOfLength(5));
} else {
action.searchRouting(randomAsciiOfLength(5));
action.indexRouting(randomAsciiOfLength(5));
}
}
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
action.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
AliasActions read = new AliasActions(in);
assertEquals(action, read);
}
}
}
use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions in project elasticsearch by elastic.
the class AliasActionsTests method testBadOptionsInNonIndex.
public void testBadOptionsInNonIndex() {
AliasActions action = randomBoolean() ? AliasActions.remove() : AliasActions.removeIndex();
Exception e = expectThrows(IllegalArgumentException.class, () -> action.routing("test"));
assertEquals("[routing] is unsupported for [" + action.actionType() + "]", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> action.searchRouting("test"));
assertEquals("[search_routing] is unsupported for [" + action.actionType() + "]", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> action.indexRouting("test"));
assertEquals("[index_routing] is unsupported for [" + action.actionType() + "]", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> action.filter("test"));
assertEquals("[filter] is unsupported for [" + action.actionType() + "]", e.getMessage());
}
use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions in project elasticsearch by elastic.
the class AliasActionsTests method testParseRemove.
public void testParseRemove() throws IOException {
String[] indices = generateRandomStringArray(10, 5, false, false);
String[] aliases = generateRandomStringArray(10, 5, false, false);
XContentBuilder b = XContentBuilder.builder(randomFrom(XContentType.values()).xContent());
b.startObject();
{
b.startObject("remove");
{
if (indices.length > 1 || randomBoolean()) {
b.array("indices", indices);
} else {
b.field("index", indices[0]);
}
if (aliases.length > 1 || randomBoolean()) {
b.array("aliases", aliases);
} else {
b.field("alias", aliases[0]);
}
}
b.endObject();
}
b.endObject();
b = shuffleXContent(b);
try (XContentParser parser = createParser(b)) {
AliasActions action = AliasActions.PARSER.apply(parser, null);
assertEquals(AliasActions.Type.REMOVE, action.actionType());
assertThat(action.indices(), equalTo(indices));
assertThat(action.aliases(), equalTo(aliases));
}
}
Aggregations