Search in sources :

Example 6 with AliasActions

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());
    }
}
Also used : AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException)

Example 7 with AliasActions

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);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) AliasesNotFoundException(org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) AliasAction(org.elasticsearch.cluster.metadata.AliasAction) AliasesNotFoundException(org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) HashSet(java.util.HashSet)

Example 8 with AliasActions

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);
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 9 with AliasActions

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());
}
Also used : AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException)

Example 10 with AliasActions

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));
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)

Aggregations

AliasActions (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)10 IOException (java.io.IOException)4 ParsingException (org.elasticsearch.common.ParsingException)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ClusterStateUpdateResponse (org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)1 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)1 AliasAction (org.elasticsearch.cluster.metadata.AliasAction)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 AliasesNotFoundException (org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException)1