Search in sources :

Example 1 with IndicesRequest

use of org.opensearch.action.IndicesRequest in project OpenSearch by opensearch-project.

the class IndexNameExpressionResolverTests method testConcreteWriteIndexWithInvalidIndicesRequest.

public void testConcreteWriteIndexWithInvalidIndicesRequest() {
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias")));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    Function<String[], IndicesRequest> requestGen = (indices) -> new IndicesRequest() {

        @Override
        public String[] indices() {
            return indices;
        }

        @Override
        public IndicesOptions indicesOptions() {
            return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
        }

        @Override
        public boolean includeDataStreams() {
            return false;
        }
    };
    IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, requestGen.apply(null)));
    assertThat(exception.getMessage(), equalTo("indices request must specify a single index expression"));
    exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, requestGen.apply(new String[] { "too", "many" })));
    assertThat(exception.getMessage(), equalTo("indices request must specify a single index expression"));
}
Also used : Matchers.arrayContaining(org.hamcrest.Matchers.arrayContaining) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) Arrays(java.util.Arrays) State(org.opensearch.cluster.metadata.IndexMetadata.State) Matchers.emptyArray(org.hamcrest.Matchers.emptyArray) DocWriteRequest(org.opensearch.action.DocWriteRequest) Version(org.opensearch.Version) IndicesOptions(org.opensearch.action.support.IndicesOptions) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Function(java.util.function.Function) INDEX_HIDDEN_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_HIDDEN_SETTING) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) Sets.newHashSet(org.opensearch.common.util.set.Sets.newHashSet) IndicesRequest(org.opensearch.action.IndicesRequest) ClusterState(org.opensearch.cluster.ClusterState) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) IndexClosedException(org.opensearch.indices.IndexClosedException) DataStreamTestHelper.createBackingIndex(org.opensearch.cluster.DataStreamTestHelper.createBackingIndex) SearchRequest(org.opensearch.action.search.SearchRequest) EnumSet(java.util.EnumSet) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) ClusterName(org.opensearch.cluster.ClusterName) UpdateRequest(org.opensearch.action.update.UpdateRequest) Matchers.is(org.hamcrest.Matchers.is) IndexRequest(org.opensearch.action.index.IndexRequest) Comparator(java.util.Comparator) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) DataStreamTestHelper.createTimestampField(org.opensearch.cluster.DataStreamTestHelper.createTimestampField) SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY(org.opensearch.cluster.metadata.IndexNameExpressionResolver.SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY) ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) IndicesRequest(org.opensearch.action.IndicesRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with IndicesRequest

use of org.opensearch.action.IndicesRequest in project OpenSearch by opensearch-project.

the class IndexNameExpressionResolverTests method testConcreteWriteIndexSuccessful.

public void testConcreteWriteIndexSuccessful() {
    boolean testZeroWriteIndex = randomBoolean();
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(testZeroWriteIndex ? true : null)));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    String[] strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, new HashSet<>(Arrays.asList("test-0", "test-alias")));
    Arrays.sort(strings);
    assertArrayEquals(new String[] { "test-alias" }, strings);
    IndicesRequest request = new IndicesRequest() {

        @Override
        public String[] indices() {
            return new String[] { "test-alias" };
        }

        @Override
        public IndicesOptions indicesOptions() {
            return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
        }

        @Override
        public boolean includeDataStreams() {
            return false;
        }
    };
    Index writeIndex = indexNameExpressionResolver.concreteWriteIndex(state, request);
    assertThat(writeIndex.getName(), equalTo("test-0"));
    state = ClusterState.builder(state).metadata(Metadata.builder(state.metadata()).put(indexBuilder("test-1").putAlias(AliasMetadata.builder("test-alias").writeIndex(testZeroWriteIndex ? randomFrom(false, null) : true)))).build();
    writeIndex = indexNameExpressionResolver.concreteWriteIndex(state, request);
    assertThat(writeIndex.getName(), equalTo(testZeroWriteIndex ? "test-0" : "test-1"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) IndicesRequest(org.opensearch.action.IndicesRequest) DataStreamTestHelper.createBackingIndex(org.opensearch.cluster.DataStreamTestHelper.createBackingIndex) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with IndicesRequest

use of org.opensearch.action.IndicesRequest in project OpenSearch by opensearch-project.

the class IndexNameExpressionResolverTests method testConcreteWriteIndexWithWildcardExpansion.

public void testConcreteWriteIndexWithWildcardExpansion() {
    boolean testZeroWriteIndex = randomBoolean();
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-1").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(testZeroWriteIndex ? true : null))).put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(testZeroWriteIndex ? randomFrom(false, null) : true)));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    String[] strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, new HashSet<>(Arrays.asList("test-0", "test-1", "test-alias")));
    Arrays.sort(strings);
    assertArrayEquals(new String[] { "test-alias" }, strings);
    IndicesRequest request = new IndicesRequest() {

        @Override
        public String[] indices() {
            return new String[] { "test-*" };
        }

        @Override
        public IndicesOptions indicesOptions() {
            return IndicesOptions.strictExpandOpenAndForbidClosed();
        }

        @Override
        public boolean includeDataStreams() {
            return false;
        }
    };
    IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, request));
    assertThat(exception.getMessage(), equalTo("The index expression [test-*] and options provided did not point to a single write-index"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) IndicesRequest(org.opensearch.action.IndicesRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)3 IndicesRequest (org.opensearch.action.IndicesRequest)3 ClusterName (org.opensearch.cluster.ClusterName)3 ClusterState (org.opensearch.cluster.ClusterState)3 DataStreamTestHelper.createBackingIndex (org.opensearch.cluster.DataStreamTestHelper.createBackingIndex)2 Index (org.opensearch.index.Index)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumSet (java.util.EnumSet)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Matchers.arrayContaining (org.hamcrest.Matchers.arrayContaining)1 Matchers.arrayContainingInAnyOrder (org.hamcrest.Matchers.arrayContainingInAnyOrder)1 Matchers.arrayWithSize (org.hamcrest.Matchers.arrayWithSize)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 Matchers.empty (org.hamcrest.Matchers.empty)1