|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core.indexlifecycle.action; |
| 8 | + |
| 9 | +import org.elasticsearch.action.Action; |
| 10 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 11 | +import org.elasticsearch.action.ActionResponse; |
| 12 | +import org.elasticsearch.action.IndicesRequest; |
| 13 | +import org.elasticsearch.action.support.IndicesOptions; |
| 14 | +import org.elasticsearch.action.support.master.AcknowledgedRequest; |
| 15 | +import org.elasticsearch.common.ParseField; |
| 16 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 17 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 18 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 19 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 20 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +public class SetPolicyForIndexAction extends Action<SetPolicyForIndexAction.Request, SetPolicyForIndexAction.Response> { |
| 28 | + public static final SetPolicyForIndexAction INSTANCE = new SetPolicyForIndexAction(); |
| 29 | + public static final String NAME = "indices:admin/xpack/index_lifecycle/set_policy"; |
| 30 | + |
| 31 | + protected SetPolicyForIndexAction() { |
| 32 | + super(NAME); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public SetPolicyForIndexAction.Response newResponse() { |
| 37 | + return new Response(); |
| 38 | + } |
| 39 | + |
| 40 | + public static class Response extends ActionResponse implements ToXContentObject { |
| 41 | + |
| 42 | + public static final ParseField HAS_FAILURES_FIELD = new ParseField("has_failures"); |
| 43 | + public static final ParseField FAILED_INDEXES_FIELD = new ParseField("failed_indexes"); |
| 44 | + @SuppressWarnings("unchecked") |
| 45 | + public static final ConstructingObjectParser<Response, Void> PARSER = new ConstructingObjectParser<>( |
| 46 | + "change_policy_for_index_response", a -> new Response((List<String>) a[0])); |
| 47 | + static { |
| 48 | + PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), FAILED_INDEXES_FIELD); |
| 49 | + // Needs to be declared but not used in constructing the response object |
| 50 | + PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), HAS_FAILURES_FIELD); |
| 51 | + } |
| 52 | + |
| 53 | + private List<String> failedIndexes; |
| 54 | + |
| 55 | + public Response() { |
| 56 | + } |
| 57 | + |
| 58 | + public Response(List<String> failedIndexes) { |
| 59 | + if (failedIndexes == null) { |
| 60 | + throw new IllegalArgumentException(FAILED_INDEXES_FIELD.getPreferredName() + " cannot be null"); |
| 61 | + } |
| 62 | + this.failedIndexes = failedIndexes; |
| 63 | + } |
| 64 | + |
| 65 | + public List<String> getFailedIndexes() { |
| 66 | + return failedIndexes; |
| 67 | + } |
| 68 | + |
| 69 | + public boolean hasFailures() { |
| 70 | + return failedIndexes.isEmpty() == false; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 75 | + builder.startObject(); |
| 76 | + builder.field(HAS_FAILURES_FIELD.getPreferredName(), hasFailures()); |
| 77 | + builder.field(FAILED_INDEXES_FIELD.getPreferredName(), failedIndexes); |
| 78 | + builder.endObject(); |
| 79 | + return builder; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void readFrom(StreamInput in) throws IOException { |
| 84 | + super.readFrom(in); |
| 85 | + failedIndexes = in.readList(StreamInput::readString); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void writeTo(StreamOutput out) throws IOException { |
| 90 | + super.writeTo(out); |
| 91 | + out.writeStringList(failedIndexes); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public int hashCode() { |
| 96 | + return Objects.hash(failedIndexes); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean equals(Object obj) { |
| 101 | + if (obj == null) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + if (getClass() != obj.getClass()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + Response other = (Response) obj; |
| 108 | + return Objects.equals(failedIndexes, other.failedIndexes); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + public static class Request extends AcknowledgedRequest<Request> implements IndicesRequest.Replaceable { |
| 114 | + |
| 115 | + private String[] indices; |
| 116 | + private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen(); |
| 117 | + private String policy; |
| 118 | + |
| 119 | + public Request() { |
| 120 | + } |
| 121 | + |
| 122 | + public Request(String policy, String... indices) { |
| 123 | + if (indices == null) { |
| 124 | + throw new IllegalArgumentException("indices cannot be null"); |
| 125 | + } |
| 126 | + if (policy == null) { |
| 127 | + throw new IllegalArgumentException("policy cannot be null"); |
| 128 | + } |
| 129 | + this.indices = indices; |
| 130 | + this.policy = policy; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Request indices(String... indices) { |
| 135 | + this.indices = indices; |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public String[] indices() { |
| 141 | + return indices; |
| 142 | + } |
| 143 | + |
| 144 | + public String policy() { |
| 145 | + return policy; |
| 146 | + } |
| 147 | + |
| 148 | + public void indicesOptions(IndicesOptions indicesOptions) { |
| 149 | + this.indicesOptions = indicesOptions; |
| 150 | + } |
| 151 | + |
| 152 | + public IndicesOptions indicesOptions() { |
| 153 | + return indicesOptions; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public ActionRequestValidationException validate() { |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void readFrom(StreamInput in) throws IOException { |
| 163 | + super.readFrom(in); |
| 164 | + indices = in.readStringArray(); |
| 165 | + indicesOptions = IndicesOptions.readIndicesOptions(in); |
| 166 | + policy = in.readString(); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void writeTo(StreamOutput out) throws IOException { |
| 171 | + super.writeTo(out); |
| 172 | + out.writeStringArray(indices); |
| 173 | + indicesOptions.writeIndicesOptions(out); |
| 174 | + out.writeString(policy); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public int hashCode() { |
| 179 | + return Objects.hash(Arrays.hashCode(indices), indicesOptions, policy); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public boolean equals(Object obj) { |
| 184 | + if (obj == null) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + if (getClass() != obj.getClass()) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + Request other = (Request) obj; |
| 191 | + return Objects.deepEquals(indices, other.indices) && |
| 192 | + Objects.equals(indicesOptions, other.indicesOptions) && |
| 193 | + Objects.equals(policy, other.policy); |
| 194 | + } |
| 195 | + |
| 196 | + } |
| 197 | +} |
0 commit comments