-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get autoscaling policy API (#54762)
This commit adds the get autoscaling policy API.
- Loading branch information
1 parent
bcc13ec
commit 48b314a
Showing
14 changed files
with
601 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
docs/reference/autoscaling/apis/get-autoscaling-policy.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
[role="xpack"] | ||
[testenv="platinum"] | ||
[[autoscaling-get-autoscaling-policy]] | ||
=== Get autoscaling policy API | ||
++++ | ||
<titleabbrev>Get autoscaling policy</titleabbrev> | ||
++++ | ||
|
||
Get autoscaling policy. | ||
|
||
[[autoscaling-get-autoscaling-policy-request]] | ||
==== {api-request-title} | ||
|
||
[source,console] | ||
-------------------------------------------------- | ||
PUT /_autoscaling/policy/my_autoscaling_policy | ||
{ | ||
"policy": { | ||
"deciders": { | ||
"always": { | ||
} | ||
} | ||
} | ||
} | ||
-------------------------------------------------- | ||
// TESTSETUP | ||
|
||
[source,console] | ||
-------------------------------------------------- | ||
GET /_autoscaling/policy/<name> | ||
-------------------------------------------------- | ||
// TEST[s/<name>/my_autoscaling_policy/] | ||
|
||
[[autoscaling-get-autoscaling-policy-prereqs]] | ||
==== {api-prereq-title} | ||
|
||
* If the {es} {security-features} are enabled, you must have | ||
`manage_autoscaling` cluster privileges. For more information, see | ||
<<security-privileges>>. | ||
|
||
[[autoscaling-get-autoscaling-policy-desc]] | ||
==== {api-description-title} | ||
|
||
This API gets an autoscaling policy with the provided name. | ||
|
||
[[autoscaling-get-autoscaling-policy-examples]] | ||
==== {api-examples-title} | ||
|
||
This example gets an autoscaling policy named `my_autosaling_policy`. | ||
|
||
[source,console] | ||
-------------------------------------------------- | ||
GET /_autoscaling/policy/my_autoscaling_policy | ||
-------------------------------------------------- | ||
// TEST | ||
|
||
The API returns the following result: | ||
|
||
[source,console-result] | ||
-------------------------------------------------- | ||
{ | ||
"policy": { | ||
"deciders": <deciders> | ||
} | ||
} | ||
-------------------------------------------------- | ||
// TEST[s/<deciders>/$body.policy.deciders/] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ling/qa/rest/src/test/resources/rest-api-spec/test/autoscaling/get_autoscaling_policy.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
"Test get autoscaling policy": | ||
- do: | ||
autoscaling.put_autoscaling_policy: | ||
name: my_autoscaling_policy | ||
body: | ||
policy: | ||
deciders: | ||
always: {} | ||
|
||
- match: { "acknowledged": true } | ||
|
||
- do: | ||
autoscaling.get_autoscaling_policy: | ||
name: my_autoscaling_policy | ||
|
||
- match: { policy.deciders.always: {} } | ||
|
||
# test cleanup | ||
- do: | ||
autoscaling.delete_autoscaling_policy: | ||
name: my_autoscaling_policy | ||
|
||
--- | ||
"Test get non-existent autoscaling policy": | ||
- do: | ||
catch: bad_request | ||
autoscaling.get_autoscaling_policy: | ||
name: does_not_exist | ||
|
||
- do: | ||
catch: /autoscaling policy with name \[does_not_exist\] does not exist/ | ||
autoscaling.get_autoscaling_policy: | ||
name: does_not_exist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
.../src/main/java/org/elasticsearch/xpack/autoscaling/action/GetAutoscalingPolicyAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.autoscaling.action; | ||
|
||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.action.support.master.MasterNodeReadRequest; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicy; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class GetAutoscalingPolicyAction extends ActionType<GetAutoscalingPolicyAction.Response> { | ||
|
||
public static final GetAutoscalingPolicyAction INSTANCE = new GetAutoscalingPolicyAction(); | ||
public static final String NAME = "cluster:admin/autoscaling/get_autoscaling_policy"; | ||
|
||
private GetAutoscalingPolicyAction() { | ||
super(NAME, Response::new); | ||
} | ||
|
||
public static class Request extends MasterNodeReadRequest<Request> { | ||
|
||
private final String name; | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public Request(final String name) { | ||
this.name = Objects.requireNonNull(name); | ||
} | ||
|
||
public Request(final StreamInput in) throws IOException { | ||
super(in); | ||
name = in.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(name); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
final Request request = (Request) o; | ||
return name.equals(request.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name); | ||
} | ||
|
||
} | ||
|
||
public static class Response extends ActionResponse implements ToXContentObject { | ||
|
||
private final AutoscalingPolicy policy; | ||
|
||
public AutoscalingPolicy policy() { | ||
return policy; | ||
} | ||
|
||
public Response(final AutoscalingPolicy policy) { | ||
this.policy = Objects.requireNonNull(policy); | ||
} | ||
|
||
public Response(final StreamInput in) throws IOException { | ||
policy = new AutoscalingPolicy(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
policy.writeTo(out); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { | ||
builder.startObject(); | ||
{ | ||
builder.field("policy", policy); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
final Response response = (Response) o; | ||
return policy.equals(response.policy); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(policy); | ||
} | ||
|
||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
.../java/org/elasticsearch/xpack/autoscaling/action/TransportGetAutoscalingPolicyAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.autoscaling.action; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.master.TransportMasterNodeAction; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.block.ClusterBlockException; | ||
import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.xpack.autoscaling.AutoscalingMetadata; | ||
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicy; | ||
|
||
import java.io.IOException; | ||
|
||
public class TransportGetAutoscalingPolicyAction extends TransportMasterNodeAction< | ||
GetAutoscalingPolicyAction.Request, | ||
GetAutoscalingPolicyAction.Response> { | ||
|
||
@Inject | ||
public TransportGetAutoscalingPolicyAction( | ||
final TransportService transportService, | ||
final ClusterService clusterService, | ||
final ThreadPool threadPool, | ||
final ActionFilters actionFilters, | ||
final IndexNameExpressionResolver indexNameExpressionResolver | ||
) { | ||
super( | ||
GetAutoscalingPolicyAction.NAME, | ||
transportService, | ||
clusterService, | ||
threadPool, | ||
actionFilters, | ||
GetAutoscalingPolicyAction.Request::new, | ||
indexNameExpressionResolver | ||
); | ||
} | ||
|
||
@Override | ||
protected String executor() { | ||
return ThreadPool.Names.SAME; | ||
} | ||
|
||
@Override | ||
protected GetAutoscalingPolicyAction.Response read(final StreamInput in) throws IOException { | ||
return new GetAutoscalingPolicyAction.Response(in); | ||
} | ||
|
||
@Override | ||
protected void masterOperation( | ||
final Task task, | ||
final GetAutoscalingPolicyAction.Request request, | ||
final ClusterState state, | ||
final ActionListener<GetAutoscalingPolicyAction.Response> listener | ||
) { | ||
listener.onResponse(new GetAutoscalingPolicyAction.Response(getAutoscalingPolicy(state, request.name()))); | ||
} | ||
|
||
static AutoscalingPolicy getAutoscalingPolicy(final ClusterState state, final String name) { | ||
final AutoscalingMetadata metadata; | ||
if (state.metadata().custom(AutoscalingMetadata.NAME) != null) { | ||
metadata = state.metadata().custom(AutoscalingMetadata.NAME); | ||
} else { | ||
// we will reject the request below when we try to look up the policy by name | ||
metadata = AutoscalingMetadata.EMPTY; | ||
} | ||
if (metadata.policies().containsKey(name) == false) { | ||
throw new IllegalArgumentException("autoscaling policy with name [" + name + "] does not exist"); | ||
} | ||
return metadata.policies().get(name).policy(); | ||
} | ||
|
||
@Override | ||
protected ClusterBlockException checkBlock(final GetAutoscalingPolicyAction.Request request, final ClusterState state) { | ||
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); | ||
} | ||
|
||
} |
Oops, something went wrong.