Skip to content

Commit

Permalink
Add get autoscaling policy API (#54762)
Browse files Browse the repository at this point in the history
This commit adds the get autoscaling policy API.
  • Loading branch information
jasontedor authored Apr 4, 2020
1 parent bcc13ec commit 48b314a
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/reference/autoscaling/apis/autoscaling-apis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ You can use the following APIs to perform autoscaling operations.

* <<autoscaling-get-autoscaling-decision,Get autoscaling decision>>
* <<autoscaling-delete-autoscaling-policy,Delete autoscaling policy>>
* <<autoscaling-get-autoscaling-policy,Get autoscaling policy>>
* <<autoscaling-put-autoscaling-policy,Put autoscaling policy>>

// top-level
include::get-autoscaling-decision.asciidoc[]
include::delete-autoscaling-policy.asciidoc[]
include::get-autoscaling-policy.asciidoc[]
include::put-autoscaling-policy.asciidoc[]
67 changes: 67 additions & 0 deletions docs/reference/autoscaling/apis/get-autoscaling-policy.asciidoc
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/]
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
autoscaling.delete_autoscaling_policy:
name: my_autoscaling_policy

# TODO: add validation that the policy is removed after we have a get policy API
# validate the policy does not exist
- do:
catch: /autoscaling policy with name \[my_autoscaling_policy\] does not exist/
autoscaling.get_autoscaling_policy:
name: my_autoscaling_policy

---
"Test delete non-existent policy":
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.xpack.autoscaling.action.DeleteAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.action.GetAutoscalingDecisionAction;
import org.elasticsearch.xpack.autoscaling.action.GetAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.action.PutAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.action.TransportDeleteAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.action.TransportGetAutoscalingDecisionAction;
import org.elasticsearch.xpack.autoscaling.action.TransportGetAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.action.TransportPutAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.decision.AlwaysAutoscalingDecider;
import org.elasticsearch.xpack.autoscaling.decision.AutoscalingDecider;
import org.elasticsearch.xpack.autoscaling.rest.RestDeleteAutoscalingPolicyHandler;
import org.elasticsearch.xpack.autoscaling.rest.RestGetAutoscalingDecisionHandler;
import org.elasticsearch.xpack.autoscaling.rest.RestGetAutoscalingPolicyHandler;
import org.elasticsearch.xpack.autoscaling.rest.RestPutAutoscalingPolicyHandler;
import org.elasticsearch.xpack.core.XPackPlugin;

Expand Down Expand Up @@ -103,6 +106,7 @@ boolean isSnapshot() {
return List.of(
new ActionHandler<>(GetAutoscalingDecisionAction.INSTANCE, TransportGetAutoscalingDecisionAction.class),
new ActionHandler<>(DeleteAutoscalingPolicyAction.INSTANCE, TransportDeleteAutoscalingPolicyAction.class),
new ActionHandler<>(GetAutoscalingPolicyAction.INSTANCE, TransportGetAutoscalingPolicyAction.class),
new ActionHandler<>(PutAutoscalingPolicyAction.INSTANCE, TransportPutAutoscalingPolicyAction.class)
);
} else {
Expand All @@ -124,6 +128,7 @@ public List<RestHandler> getRestHandlers(
return List.of(
new RestGetAutoscalingDecisionHandler(),
new RestDeleteAutoscalingPolicyHandler(),
new RestGetAutoscalingPolicyHandler(),
new RestPutAutoscalingPolicyHandler()
);
} else {
Expand Down
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);
}

}

}
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);
}

}
Loading

0 comments on commit 48b314a

Please sign in to comment.