|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.protocol.xpack.indexlifecycle; |
| 20 | + |
| 21 | +import org.elasticsearch.common.ParseField; |
| 22 | +import org.elasticsearch.common.Strings; |
| 23 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 24 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 25 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 26 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Objects; |
| 33 | + |
| 34 | +public class AllocateAction extends LifecycleAction implements ToXContentObject { |
| 35 | + |
| 36 | + public static final String NAME = "allocate"; |
| 37 | + static final ParseField NUMBER_OF_REPLICAS_FIELD = new ParseField("number_of_replicas"); |
| 38 | + static final ParseField INCLUDE_FIELD = new ParseField("include"); |
| 39 | + static final ParseField EXCLUDE_FIELD = new ParseField("exclude"); |
| 40 | + static final ParseField REQUIRE_FIELD = new ParseField("require"); |
| 41 | + |
| 42 | + @SuppressWarnings("unchecked") |
| 43 | + private static final ConstructingObjectParser<AllocateAction, Void> PARSER = new ConstructingObjectParser<>(NAME, |
| 44 | + a -> new AllocateAction((Integer) a[0], (Map<String, String>) a[1], (Map<String, String>) a[2], (Map<String, String>) a[3])); |
| 45 | + |
| 46 | + static { |
| 47 | + PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUMBER_OF_REPLICAS_FIELD); |
| 48 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.mapStrings(), INCLUDE_FIELD); |
| 49 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.mapStrings(), EXCLUDE_FIELD); |
| 50 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.mapStrings(), REQUIRE_FIELD); |
| 51 | + } |
| 52 | + |
| 53 | + private final Integer numberOfReplicas; |
| 54 | + private final Map<String, String> include; |
| 55 | + private final Map<String, String> exclude; |
| 56 | + private final Map<String, String> require; |
| 57 | + |
| 58 | + public static AllocateAction parse(XContentParser parser) { |
| 59 | + return PARSER.apply(parser, null); |
| 60 | + } |
| 61 | + |
| 62 | + public AllocateAction(Integer numberOfReplicas, Map<String, String> include, Map<String, String> exclude, Map<String, String> require) { |
| 63 | + if (include == null) { |
| 64 | + this.include = Collections.emptyMap(); |
| 65 | + } else { |
| 66 | + this.include = include; |
| 67 | + } |
| 68 | + if (exclude == null) { |
| 69 | + this.exclude = Collections.emptyMap(); |
| 70 | + } else { |
| 71 | + this.exclude = exclude; |
| 72 | + } |
| 73 | + if (require == null) { |
| 74 | + this.require = Collections.emptyMap(); |
| 75 | + } else { |
| 76 | + this.require = require; |
| 77 | + } |
| 78 | + if (this.include.isEmpty() && this.exclude.isEmpty() && this.require.isEmpty() && numberOfReplicas == null) { |
| 79 | + throw new IllegalArgumentException( |
| 80 | + "At least one of " + INCLUDE_FIELD.getPreferredName() + ", " + EXCLUDE_FIELD.getPreferredName() + " or " |
| 81 | + + REQUIRE_FIELD.getPreferredName() + "must contain attributes for action " + NAME); |
| 82 | + } |
| 83 | + if (numberOfReplicas != null && numberOfReplicas < 0) { |
| 84 | + throw new IllegalArgumentException("[" + NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0"); |
| 85 | + } |
| 86 | + this.numberOfReplicas = numberOfReplicas; |
| 87 | + } |
| 88 | + |
| 89 | + public Integer getNumberOfReplicas() { |
| 90 | + return numberOfReplicas; |
| 91 | + } |
| 92 | + |
| 93 | + public Map<String, String> getInclude() { |
| 94 | + return include; |
| 95 | + } |
| 96 | + |
| 97 | + public Map<String, String> getExclude() { |
| 98 | + return exclude; |
| 99 | + } |
| 100 | + |
| 101 | + public Map<String, String> getRequire() { |
| 102 | + return require; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 107 | + builder.startObject(); |
| 108 | + if (numberOfReplicas != null) { |
| 109 | + builder.field(NUMBER_OF_REPLICAS_FIELD.getPreferredName(), numberOfReplicas); |
| 110 | + } |
| 111 | + builder.field(INCLUDE_FIELD.getPreferredName(), include); |
| 112 | + builder.field(EXCLUDE_FIELD.getPreferredName(), exclude); |
| 113 | + builder.field(REQUIRE_FIELD.getPreferredName(), require); |
| 114 | + builder.endObject(); |
| 115 | + return builder; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public int hashCode() { |
| 120 | + return Objects.hash(numberOfReplicas, include, exclude, require); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean equals(Object obj) { |
| 125 | + if (obj == null) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + if (obj.getClass() != getClass()) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + AllocateAction other = (AllocateAction) obj; |
| 132 | + return Objects.equals(numberOfReplicas, other.numberOfReplicas) && |
| 133 | + Objects.equals(include, other.include) && |
| 134 | + Objects.equals(exclude, other.exclude) && |
| 135 | + Objects.equals(require, other.require); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + return Strings.toString(this); |
| 141 | + } |
| 142 | +} |
0 commit comments