/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.action.support; import org.opensearch.OpenSearchParseException; import org.opensearch.core.ParseField; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.ToXContentFragment; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; import org.opensearch.core.xcontent.XContentParser.Token; import org.opensearch.rest.RestRequest; import java.io.IOException; import java.util.EnumSet; import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; import static org.opensearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; import static org.opensearch.common.xcontent.support.XContentMapValues.nodeStringArrayValue; /** * Controls how to deal with unavailable concrete indices (closed or missing), how wildcard expressions are expanded * to actual indices (all, closed or open indices) and how to deal with wildcard expressions that resolve to no indices. * * @opensearch.internal */ public class IndicesOptions implements ToXContentFragment { /** * The wildcard states. * * @opensearch.internal */ public enum WildcardStates { OPEN, CLOSED, HIDDEN; public static final EnumSet NONE = EnumSet.noneOf(WildcardStates.class); public static EnumSet parseParameter(Object value, EnumSet defaultStates) { if (value == null) { return defaultStates; } EnumSet states = EnumSet.noneOf(WildcardStates.class); String[] wildcards = nodeStringArrayValue(value); // TODO why do we let patterns like "none,all" or "open,none,closed" get used. The location of 'none' in the array changes the // meaning of the resulting value for (String wildcard : wildcards) { updateSetForValue(states, wildcard); } return states; } public static XContentBuilder toXContent(EnumSet states, XContentBuilder builder) throws IOException { if (states.isEmpty()) { builder.field("expand_wildcards", "none"); } else if (states.containsAll(EnumSet.allOf(WildcardStates.class))) { builder.field("expand_wildcards", "all"); } else { builder.field( "expand_wildcards", states.stream().map(state -> state.toString().toLowerCase(Locale.ROOT)).collect(Collectors.joining(",")) ); } return builder; } private static void updateSetForValue(EnumSet states, String wildcard) { switch (wildcard) { case "open": states.add(OPEN); break; case "closed": states.add(CLOSED); break; case "hidden": states.add(HIDDEN); break; case "none": states.clear(); break; case "all": states.addAll(EnumSet.allOf(WildcardStates.class)); break; default: throw new IllegalArgumentException("No valid expand wildcard value [" + wildcard + "]"); } } } /** * The options. * * @opensearch.internal */ public enum Option { IGNORE_UNAVAILABLE, IGNORE_ALIASES, ALLOW_NO_INDICES, FORBID_ALIASES_TO_MULTIPLE_INDICES, FORBID_CLOSED_INDICES, IGNORE_THROTTLED; public static final EnumSet