Skip to content

Commit b0077b0

Browse files
talevyjasontedor
authored andcommitted
migrate allocate action pojo/xcontent to xpack.protocol (#32853)
1 parent 266f4ba commit b0077b0

File tree

4 files changed

+267
-1
lines changed

4 files changed

+267
-1
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ public List<NamedXContentRegistry.Entry> getNamedXContent() {
449449
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse),
450450
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse),
451451
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse),
452-
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)
452+
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse),
453+
// ILM - Client - Lifecycle Actions
454+
new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.LifecycleAction.class,
455+
new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.AllocateAction.NAME),
456+
org.elasticsearch.protocol.xpack.indexlifecycle.AllocateAction::parse)
453457
);
454458
}
455459

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
/**
22+
* Marker interface for index lifecycle management actions
23+
*/
24+
public class LifecycleAction {
25+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.xcontent.XContentParser;
22+
import org.elasticsearch.test.AbstractXContentTestCase;
23+
24+
import java.util.Collections;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
public class AllocateActionTests extends AbstractXContentTestCase<AllocateAction> {
29+
30+
@Override
31+
protected AllocateAction createTestInstance() {
32+
boolean hasAtLeastOneMap = false;
33+
Map<String, String> includes;
34+
if (randomBoolean()) {
35+
includes = randomMap(1, 100);
36+
hasAtLeastOneMap = true;
37+
} else {
38+
includes = randomBoolean() ? null : Collections.emptyMap();
39+
}
40+
Map<String, String> excludes;
41+
if (randomBoolean()) {
42+
hasAtLeastOneMap = true;
43+
excludes = randomMap(1, 100);
44+
} else {
45+
excludes = randomBoolean() ? null : Collections.emptyMap();
46+
}
47+
Map<String, String> requires;
48+
if (hasAtLeastOneMap == false || randomBoolean()) {
49+
requires = randomMap(1, 100);
50+
} else {
51+
requires = randomBoolean() ? null : Collections.emptyMap();
52+
}
53+
Integer numberOfReplicas = randomBoolean() ? null : randomIntBetween(0, 10);
54+
return new AllocateAction(numberOfReplicas, includes, excludes, requires);
55+
}
56+
57+
@Override
58+
protected AllocateAction doParseInstance(XContentParser parser) {
59+
return AllocateAction.parse(parser);
60+
}
61+
62+
@Override
63+
protected boolean supportsUnknownFields() {
64+
return false;
65+
}
66+
67+
public void testAllMapsNullOrEmpty() {
68+
Map<String, String> include = randomBoolean() ? null : Collections.emptyMap();
69+
Map<String, String> exclude = randomBoolean() ? null : Collections.emptyMap();
70+
Map<String, String> require = randomBoolean() ? null : Collections.emptyMap();
71+
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
72+
() -> new AllocateAction(null, include, exclude, require));
73+
assertEquals("At least one of " + AllocateAction.INCLUDE_FIELD.getPreferredName() + ", "
74+
+ AllocateAction.EXCLUDE_FIELD.getPreferredName() + " or " + AllocateAction.REQUIRE_FIELD.getPreferredName()
75+
+ "must contain attributes for action " + AllocateAction.NAME, exception.getMessage());
76+
}
77+
78+
public void testInvalidNumberOfReplicas() {
79+
Map<String, String> include = randomMap(1, 5);
80+
Map<String, String> exclude = randomBoolean() ? null : Collections.emptyMap();
81+
Map<String, String> require = randomBoolean() ? null : Collections.emptyMap();
82+
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
83+
() -> new AllocateAction(randomIntBetween(-1000, -1), include, exclude, require));
84+
assertEquals("[" + AllocateAction.NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0", exception.getMessage());
85+
}
86+
87+
public static Map<String, String> randomMap(int minEntries, int maxEntries) {
88+
Map<String, String> map = new HashMap<>();
89+
int numIncludes = randomIntBetween(minEntries, maxEntries);
90+
for (int i = 0; i < numIncludes; i++) {
91+
map.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
92+
}
93+
return map;
94+
}
95+
}

0 commit comments

Comments
 (0)