|
| 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 | +package org.elasticsearch.xpack.core.rollup.action; |
| 7 | + |
| 8 | + |
| 9 | +import org.elasticsearch.action.Action; |
| 10 | +import org.elasticsearch.action.ActionRequest; |
| 11 | +import org.elasticsearch.action.ActionRequestBuilder; |
| 12 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 13 | +import org.elasticsearch.action.ActionResponse; |
| 14 | +import org.elasticsearch.action.IndicesRequest; |
| 15 | +import org.elasticsearch.action.support.IndicesOptions; |
| 16 | +import org.elasticsearch.client.ElasticsearchClient; |
| 17 | +import org.elasticsearch.common.ParseField; |
| 18 | +import org.elasticsearch.common.Strings; |
| 19 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 20 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 21 | +import org.elasticsearch.common.io.stream.Writeable; |
| 22 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 23 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | +import org.elasticsearch.xpack.core.rollup.RollupField; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +public class GetRollupIndexCapsAction extends Action<GetRollupIndexCapsAction.Request, |
| 34 | + GetRollupIndexCapsAction.Response, GetRollupIndexCapsAction.RequestBuilder> { |
| 35 | + |
| 36 | + public static final GetRollupIndexCapsAction INSTANCE = new GetRollupIndexCapsAction(); |
| 37 | + public static final String NAME = "indices:data/read/xpack/rollup/get/index/caps"; |
| 38 | + public static final ParseField CONFIG = new ParseField("config"); |
| 39 | + public static final ParseField STATUS = new ParseField("status"); |
| 40 | + private static final ParseField INDICES_OPTIONS = new ParseField("indices_options"); |
| 41 | + |
| 42 | + private GetRollupIndexCapsAction() { |
| 43 | + super(NAME); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Response newResponse() { |
| 48 | + return new Response(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public RequestBuilder newRequestBuilder(ElasticsearchClient client) { |
| 53 | + return new RequestBuilder(client, INSTANCE); |
| 54 | + } |
| 55 | + |
| 56 | + public static class Request extends ActionRequest implements IndicesRequest.Replaceable, ToXContent { |
| 57 | + private String[] indices; |
| 58 | + private IndicesOptions options; |
| 59 | + |
| 60 | + public Request(String[] indices) { |
| 61 | + this(indices, IndicesOptions.STRICT_EXPAND_OPEN_FORBID_CLOSED); |
| 62 | + } |
| 63 | + |
| 64 | + public Request(String[] indices, IndicesOptions options) { |
| 65 | + this.indices = indices; |
| 66 | + this.options = options; |
| 67 | + } |
| 68 | + |
| 69 | + public Request() {} |
| 70 | + |
| 71 | + @Override |
| 72 | + public IndicesOptions indicesOptions() { |
| 73 | + return options; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String[] indices() { |
| 78 | + return indices; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public IndicesRequest indices(String... indices) { |
| 83 | + Objects.requireNonNull(indices, "indices must not be null"); |
| 84 | + for (String index : indices) { |
| 85 | + Objects.requireNonNull(index, "index must not be null"); |
| 86 | + } |
| 87 | + this.indices = indices; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void readFrom(StreamInput in) throws IOException { |
| 93 | + super.readFrom(in); |
| 94 | + this.indices = in.readStringArray(); |
| 95 | + this.options = IndicesOptions.readIndicesOptions(in); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void writeTo(StreamOutput out) throws IOException { |
| 100 | + super.writeTo(out); |
| 101 | + out.writeStringArray(indices); |
| 102 | + options.writeIndicesOptions(out); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public ActionRequestValidationException validate() { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 112 | + builder.array(RollupField.ID.getPreferredName(), indices); |
| 113 | + builder.field(INDICES_OPTIONS.getPreferredName(), options); |
| 114 | + return builder; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public int hashCode() { |
| 119 | + return Objects.hash(Arrays.hashCode(indices), options); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean equals(Object obj) { |
| 124 | + if (obj == null) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + if (getClass() != obj.getClass()) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + Request other = (Request) obj; |
| 131 | + return Arrays.equals(indices, other.indices) |
| 132 | + && Objects.equals(options, other.options); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public static class RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder> { |
| 137 | + |
| 138 | + protected RequestBuilder(ElasticsearchClient client, GetRollupIndexCapsAction action) { |
| 139 | + super(client, action, new Request()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public static class Response extends ActionResponse implements Writeable, ToXContentObject { |
| 144 | + |
| 145 | + private Map<String, RollableIndexCaps> jobs = Collections.emptyMap(); |
| 146 | + |
| 147 | + public Response() { |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + public Response(Map<String, RollableIndexCaps> jobs) { |
| 152 | + this.jobs = Objects.requireNonNull(jobs); |
| 153 | + } |
| 154 | + |
| 155 | + Response(StreamInput in) throws IOException { |
| 156 | + jobs = in.readMap(StreamInput::readString, RollableIndexCaps::new); |
| 157 | + } |
| 158 | + |
| 159 | + public Map<String, RollableIndexCaps> getJobs() { |
| 160 | + return jobs; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void writeTo(StreamOutput out) throws IOException { |
| 165 | + super.writeTo(out); |
| 166 | + out.writeMap(jobs, StreamOutput::writeString, (out1, value) -> value.writeTo(out1)); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 171 | + builder.startObject(); |
| 172 | + for (Map.Entry<String, RollableIndexCaps> entry : jobs.entrySet()) { |
| 173 | + entry.getValue().toXContent(builder, params); |
| 174 | + } |
| 175 | + builder.endObject(); |
| 176 | + return builder; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public int hashCode() { |
| 181 | + return Objects.hash(jobs); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean equals(Object obj) { |
| 186 | + if (obj == null) { |
| 187 | + return false; |
| 188 | + } |
| 189 | + if (getClass() != obj.getClass()) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + Response other = (Response) obj; |
| 193 | + return Objects.equals(jobs, other.jobs); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public final String toString() { |
| 198 | + return Strings.toString(this); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments