migrate allocate action pojo/xcontent to xpack.protocol (#32853)
This commit is contained in:
parent
9406aa5af9
commit
238eb93893
|
@ -452,7 +452,11 @@ public class XPackClientPlugin extends Plugin implements ActionPlugin, NetworkPl
|
|||
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse),
|
||||
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse),
|
||||
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse),
|
||||
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)
|
||||
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse),
|
||||
// ILM - Client - Lifecycle Actions
|
||||
new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.LifecycleAction.class,
|
||||
new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.AllocateAction.NAME),
|
||||
org.elasticsearch.protocol.xpack.indexlifecycle.AllocateAction::parse)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.ToXContentObject;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AllocateAction extends LifecycleAction implements ToXContentObject {
|
||||
|
||||
public static final String NAME = "allocate";
|
||||
static final ParseField NUMBER_OF_REPLICAS_FIELD = new ParseField("number_of_replicas");
|
||||
static final ParseField INCLUDE_FIELD = new ParseField("include");
|
||||
static final ParseField EXCLUDE_FIELD = new ParseField("exclude");
|
||||
static final ParseField REQUIRE_FIELD = new ParseField("require");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final ConstructingObjectParser<AllocateAction, Void> PARSER = new ConstructingObjectParser<>(NAME,
|
||||
a -> new AllocateAction((Integer) a[0], (Map<String, String>) a[1], (Map<String, String>) a[2], (Map<String, String>) a[3]));
|
||||
|
||||
static {
|
||||
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUMBER_OF_REPLICAS_FIELD);
|
||||
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.mapStrings(), INCLUDE_FIELD);
|
||||
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.mapStrings(), EXCLUDE_FIELD);
|
||||
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.mapStrings(), REQUIRE_FIELD);
|
||||
}
|
||||
|
||||
private final Integer numberOfReplicas;
|
||||
private final Map<String, String> include;
|
||||
private final Map<String, String> exclude;
|
||||
private final Map<String, String> require;
|
||||
|
||||
public static AllocateAction parse(XContentParser parser) {
|
||||
return PARSER.apply(parser, null);
|
||||
}
|
||||
|
||||
public AllocateAction(Integer numberOfReplicas, Map<String, String> include, Map<String, String> exclude, Map<String, String> require) {
|
||||
if (include == null) {
|
||||
this.include = Collections.emptyMap();
|
||||
} else {
|
||||
this.include = include;
|
||||
}
|
||||
if (exclude == null) {
|
||||
this.exclude = Collections.emptyMap();
|
||||
} else {
|
||||
this.exclude = exclude;
|
||||
}
|
||||
if (require == null) {
|
||||
this.require = Collections.emptyMap();
|
||||
} else {
|
||||
this.require = require;
|
||||
}
|
||||
if (this.include.isEmpty() && this.exclude.isEmpty() && this.require.isEmpty() && numberOfReplicas == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"At least one of " + INCLUDE_FIELD.getPreferredName() + ", " + EXCLUDE_FIELD.getPreferredName() + " or "
|
||||
+ REQUIRE_FIELD.getPreferredName() + "must contain attributes for action " + NAME);
|
||||
}
|
||||
if (numberOfReplicas != null && numberOfReplicas < 0) {
|
||||
throw new IllegalArgumentException("[" + NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0");
|
||||
}
|
||||
this.numberOfReplicas = numberOfReplicas;
|
||||
}
|
||||
|
||||
public Integer getNumberOfReplicas() {
|
||||
return numberOfReplicas;
|
||||
}
|
||||
|
||||
public Map<String, String> getInclude() {
|
||||
return include;
|
||||
}
|
||||
|
||||
public Map<String, String> getExclude() {
|
||||
return exclude;
|
||||
}
|
||||
|
||||
public Map<String, String> getRequire() {
|
||||
return require;
|
||||
}
|
||||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
|
||||
builder.startObject();
|
||||
if (numberOfReplicas != null) {
|
||||
builder.field(NUMBER_OF_REPLICAS_FIELD.getPreferredName(), numberOfReplicas);
|
||||
}
|
||||
builder.field(INCLUDE_FIELD.getPreferredName(), include);
|
||||
builder.field(EXCLUDE_FIELD.getPreferredName(), exclude);
|
||||
builder.field(REQUIRE_FIELD.getPreferredName(), require);
|
||||
builder.endObject();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(numberOfReplicas, include, exclude, require);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (obj.getClass() != getClass()) {
|
||||
return false;
|
||||
}
|
||||
AllocateAction other = (AllocateAction) obj;
|
||||
return Objects.equals(numberOfReplicas, other.numberOfReplicas) &&
|
||||
Objects.equals(include, other.include) &&
|
||||
Objects.equals(exclude, other.exclude) &&
|
||||
Objects.equals(require, other.require);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Strings.toString(this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
|
||||
/**
|
||||
* Marker interface for index lifecycle management actions
|
||||
*/
|
||||
public class LifecycleAction {
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractXContentTestCase;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AllocateActionTests extends AbstractXContentTestCase<AllocateAction> {
|
||||
|
||||
@Override
|
||||
protected AllocateAction createTestInstance() {
|
||||
boolean hasAtLeastOneMap = false;
|
||||
Map<String, String> includes;
|
||||
if (randomBoolean()) {
|
||||
includes = randomMap(1, 100);
|
||||
hasAtLeastOneMap = true;
|
||||
} else {
|
||||
includes = randomBoolean() ? null : Collections.emptyMap();
|
||||
}
|
||||
Map<String, String> excludes;
|
||||
if (randomBoolean()) {
|
||||
hasAtLeastOneMap = true;
|
||||
excludes = randomMap(1, 100);
|
||||
} else {
|
||||
excludes = randomBoolean() ? null : Collections.emptyMap();
|
||||
}
|
||||
Map<String, String> requires;
|
||||
if (hasAtLeastOneMap == false || randomBoolean()) {
|
||||
requires = randomMap(1, 100);
|
||||
} else {
|
||||
requires = randomBoolean() ? null : Collections.emptyMap();
|
||||
}
|
||||
Integer numberOfReplicas = randomBoolean() ? null : randomIntBetween(0, 10);
|
||||
return new AllocateAction(numberOfReplicas, includes, excludes, requires);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AllocateAction doParseInstance(XContentParser parser) {
|
||||
return AllocateAction.parse(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean supportsUnknownFields() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void testAllMapsNullOrEmpty() {
|
||||
Map<String, String> include = randomBoolean() ? null : Collections.emptyMap();
|
||||
Map<String, String> exclude = randomBoolean() ? null : Collections.emptyMap();
|
||||
Map<String, String> require = randomBoolean() ? null : Collections.emptyMap();
|
||||
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
|
||||
() -> new AllocateAction(null, include, exclude, require));
|
||||
assertEquals("At least one of " + AllocateAction.INCLUDE_FIELD.getPreferredName() + ", "
|
||||
+ AllocateAction.EXCLUDE_FIELD.getPreferredName() + " or " + AllocateAction.REQUIRE_FIELD.getPreferredName()
|
||||
+ "must contain attributes for action " + AllocateAction.NAME, exception.getMessage());
|
||||
}
|
||||
|
||||
public void testInvalidNumberOfReplicas() {
|
||||
Map<String, String> include = randomMap(1, 5);
|
||||
Map<String, String> exclude = randomBoolean() ? null : Collections.emptyMap();
|
||||
Map<String, String> require = randomBoolean() ? null : Collections.emptyMap();
|
||||
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
|
||||
() -> new AllocateAction(randomIntBetween(-1000, -1), include, exclude, require));
|
||||
assertEquals("[" + AllocateAction.NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0", exception.getMessage());
|
||||
}
|
||||
|
||||
public static Map<String, String> randomMap(int minEntries, int maxEntries) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
int numIncludes = randomIntBetween(minEntries, maxEntries);
|
||||
for (int i = 0; i < numIncludes; i++) {
|
||||
map.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue