|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.admin.cluster.remotestore.restore; |
| 10 | + |
| 11 | +import org.opensearch.action.ActionRequestValidationException; |
| 12 | +import org.opensearch.action.support.IndicesOptions; |
| 13 | +import org.opensearch.action.support.clustermanager.ClusterManagerNodeRequest; |
| 14 | +import org.opensearch.common.Strings; |
| 15 | +import org.opensearch.common.io.stream.StreamInput; |
| 16 | +import org.opensearch.common.io.stream.StreamOutput; |
| 17 | +import org.opensearch.common.xcontent.ToXContentObject; |
| 18 | +import org.opensearch.common.xcontent.XContentBuilder; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +import static org.opensearch.action.ValidateActions.addValidationError; |
| 28 | + |
| 29 | +/** |
| 30 | + * Restore remote store request |
| 31 | + * |
| 32 | + * @opensearch.internal |
| 33 | + */ |
| 34 | +public class RestoreRemoteStoreRequest extends ClusterManagerNodeRequest<RestoreRemoteStoreRequest> implements ToXContentObject { |
| 35 | + |
| 36 | + private String[] indices = Strings.EMPTY_ARRAY; |
| 37 | + private Boolean waitForCompletion; |
| 38 | + |
| 39 | + public RestoreRemoteStoreRequest() {} |
| 40 | + |
| 41 | + public RestoreRemoteStoreRequest(StreamInput in) throws IOException { |
| 42 | + super(in); |
| 43 | + indices = in.readStringArray(); |
| 44 | + waitForCompletion = in.readOptionalBoolean(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void writeTo(StreamOutput out) throws IOException { |
| 49 | + super.writeTo(out); |
| 50 | + out.writeStringArray(indices); |
| 51 | + out.writeOptionalBoolean(waitForCompletion); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public ActionRequestValidationException validate() { |
| 56 | + ActionRequestValidationException validationException = null; |
| 57 | + if (indices == null || indices.length == 0) { |
| 58 | + validationException = addValidationError("indices are missing", validationException); |
| 59 | + } |
| 60 | + return validationException; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Sets the list of indices that should be restored from the remote store |
| 65 | + * <p> |
| 66 | + * The list of indices supports multi-index syntax. For example: "+test*" ,"-test42" will index all indices with |
| 67 | + * prefix "test" except index "test42". Aliases are not supported. An empty list or {"_all"} will restore all open |
| 68 | + * indices in the cluster. |
| 69 | + * |
| 70 | + * @param indices list of indices |
| 71 | + * @return this request |
| 72 | + */ |
| 73 | + public RestoreRemoteStoreRequest indices(String... indices) { |
| 74 | + this.indices = indices; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Sets the list of indices that should be restored from the remote store |
| 80 | + * <p> |
| 81 | + * The list of indices supports multi-index syntax. For example: "+test*" ,"-test42" will index all indices with |
| 82 | + * prefix "test" except index "test42". Aliases are not supported. An empty list or {"_all"} will restore all open |
| 83 | + * indices in the cluster. |
| 84 | + * |
| 85 | + * @param indices list of indices |
| 86 | + * @return this request |
| 87 | + */ |
| 88 | + public RestoreRemoteStoreRequest indices(List<String> indices) { |
| 89 | + this.indices = indices.toArray(new String[indices.size()]); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns list of indices that should be restored from the remote store |
| 95 | + */ |
| 96 | + public String[] indices() { |
| 97 | + return indices; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * If this parameter is set to true the operation will wait for completion of restore process before returning. |
| 102 | + * |
| 103 | + * @param waitForCompletion if true the operation will wait for completion |
| 104 | + * @return this request |
| 105 | + */ |
| 106 | + public RestoreRemoteStoreRequest waitForCompletion(boolean waitForCompletion) { |
| 107 | + this.waitForCompletion = waitForCompletion; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns wait for completion setting |
| 113 | + * |
| 114 | + * @return true if the operation will wait for completion |
| 115 | + */ |
| 116 | + public boolean waitForCompletion() { |
| 117 | + return waitForCompletion; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Parses restore definition |
| 122 | + * |
| 123 | + * @param source restore definition |
| 124 | + * @return this request |
| 125 | + */ |
| 126 | + @SuppressWarnings("unchecked") |
| 127 | + public RestoreRemoteStoreRequest source(Map<String, Object> source) { |
| 128 | + for (Map.Entry<String, Object> entry : source.entrySet()) { |
| 129 | + String name = entry.getKey(); |
| 130 | + if (name.equals("indices")) { |
| 131 | + if (entry.getValue() instanceof String) { |
| 132 | + indices(Strings.splitStringByCommaToArray((String) entry.getValue())); |
| 133 | + } else if (entry.getValue() instanceof ArrayList) { |
| 134 | + indices((ArrayList<String>) entry.getValue()); |
| 135 | + } else { |
| 136 | + throw new IllegalArgumentException("malformed indices section, should be an array of strings"); |
| 137 | + } |
| 138 | + } else { |
| 139 | + if (IndicesOptions.isIndicesOptions(name) == false) { |
| 140 | + throw new IllegalArgumentException("Unknown parameter " + name); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 149 | + builder.startObject(); |
| 150 | + builder.startArray("indices"); |
| 151 | + for (String index : indices) { |
| 152 | + builder.value(index); |
| 153 | + } |
| 154 | + builder.endArray(); |
| 155 | + builder.endObject(); |
| 156 | + return builder; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public String getDescription() { |
| 161 | + return "remote_store"; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public boolean equals(Object o) { |
| 166 | + if (this == o) return true; |
| 167 | + if (o == null || getClass() != o.getClass()) return false; |
| 168 | + RestoreRemoteStoreRequest that = (RestoreRemoteStoreRequest) o; |
| 169 | + return waitForCompletion == that.waitForCompletion && Arrays.equals(indices, that.indices); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int hashCode() { |
| 174 | + int result = Objects.hash(waitForCompletion); |
| 175 | + result = 31 * result + Arrays.hashCode(indices); |
| 176 | + return result; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public String toString() { |
| 181 | + return Strings.toString(this); |
| 182 | + } |
| 183 | +} |
0 commit comments