|
9 | 9 | import org.apache.logging.log4j.LogManager;
|
10 | 10 | import org.apache.logging.log4j.Logger;
|
11 | 11 | import org.elasticsearch.action.ActionListener;
|
| 12 | +import org.elasticsearch.action.ActionRequestValidationException; |
12 | 13 | import org.elasticsearch.action.support.ActionFilters;
|
13 | 14 | import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
14 | 15 | import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
|
|
18 | 19 | import org.elasticsearch.cluster.block.ClusterBlockLevel;
|
19 | 20 | import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
20 | 21 | import org.elasticsearch.cluster.metadata.Metadata;
|
| 22 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
21 | 23 | import org.elasticsearch.cluster.service.ClusterService;
|
22 | 24 | import org.elasticsearch.common.inject.Inject;
|
23 | 25 | import org.elasticsearch.threadpool.ThreadPool;
|
|
28 | 30 | import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicyMetadata;
|
29 | 31 |
|
30 | 32 | import java.util.Collections;
|
| 33 | +import java.util.List; |
31 | 34 | import java.util.SortedMap;
|
| 35 | +import java.util.SortedSet; |
32 | 36 | import java.util.TreeMap;
|
| 37 | +import java.util.function.Predicate; |
| 38 | +import java.util.stream.Collectors; |
33 | 39 |
|
34 | 40 | public class TransportPutAutoscalingPolicyAction extends AcknowledgedTransportMasterNodeAction<PutAutoscalingPolicyAction.Request> {
|
35 | 41 |
|
@@ -76,6 +82,17 @@ protected void masterOperation(
|
76 | 82 | final ClusterState state,
|
77 | 83 | ActionListener<AcknowledgedResponse> listener
|
78 | 84 | ) {
|
| 85 | + SortedSet<String> roles = request.roles(); |
| 86 | + if (roles != null) { |
| 87 | + List<String> errors = roles.stream().filter(not(DiscoveryNode.getPossibleRoleNames()::contains)).collect(Collectors.toList()); |
| 88 | + if (errors.isEmpty() == false) { |
| 89 | + ActionRequestValidationException exception = new ActionRequestValidationException(); |
| 90 | + exception.addValidationErrors(errors); |
| 91 | + listener.onFailure(exception); |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + |
79 | 96 | clusterService.submitStateUpdateTask("put-autoscaling-policy", new AckedClusterStateUpdateTask(request, listener) {
|
80 | 97 | @Override
|
81 | 98 | public ClusterState execute(final ClusterState currentState) {
|
@@ -144,4 +161,9 @@ static ClusterState putAutoscalingPolicy(
|
144 | 161 | builder.metadata(Metadata.builder(currentState.getMetadata()).putCustom(AutoscalingMetadata.NAME, newMetadata).build());
|
145 | 162 | return builder.build();
|
146 | 163 | }
|
| 164 | + |
| 165 | + // java 11 forward compatibility |
| 166 | + private static <T> Predicate<T> not(Predicate<T> predicate) { |
| 167 | + return predicate.negate(); |
| 168 | + } |
147 | 169 | }
|
0 commit comments