Skip to content

Commit ea3e745

Browse files
[7.x] Add REST scaffolding for node shutdown API (#70697) (#70787)
Co-authored-by: Elastic Machine <[email protected]>
1 parent 03f736f commit ea3e745

17 files changed

+695
-0
lines changed

docs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ testClusters.integTest {
4444
setting 'xpack.license.self_generated.type', 'trial'
4545
setting 'indices.lifecycle.history_index_enabled', 'false'
4646
systemProperty 'es.rollup_v2_feature_flag_enabled', 'true'
47+
systemProperty 'es.shutdown_feature_flag_enabled', 'true'
4748
keystorePassword 'keystore-password'
4849
}
4950

gradle/run.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ testClusters {
2525
setting 'xpack.security.enabled', 'true'
2626
keystore 'bootstrap.password', 'password'
2727
user username: 'elastic-admin', password: 'elastic-password', role: 'superuser'
28+
systemProperty 'es.shutdown_feature_flag_enabled', 'true'
2829
}
2930
}
3031
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apply plugin: 'elasticsearch.esplugin'
2+
3+
esplugin {
4+
name 'x-pack-shutdown'
5+
description 'Elasticsearch Expanded Pack Plugin - Shutdown'
6+
classname 'org.elasticsearch.xpack.shutdown.ShutdownPlugin'
7+
extendedPlugins = ['x-pack-core']
8+
}
9+
archivesBaseName = 'x-pack-shutdown'
10+
11+
dependencies {
12+
compileOnly project(path: xpackModule('core'))
13+
testImplementation(testArtifact(project(xpackModule('core'))))
14+
}
15+
16+
addQaCheckDependencies()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.shutdown;
9+
10+
import org.elasticsearch.action.ActionRequestValidationException;
11+
import org.elasticsearch.action.ActionType;
12+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
13+
import org.elasticsearch.action.support.master.MasterNodeRequest;
14+
import org.elasticsearch.common.Strings;
15+
import org.elasticsearch.common.io.stream.StreamInput;
16+
import org.elasticsearch.common.io.stream.StreamOutput;
17+
18+
import java.io.IOException;
19+
20+
public class DeleteShutdownNodeAction extends ActionType<AcknowledgedResponse> {
21+
22+
public static final DeleteShutdownNodeAction INSTANCE = new DeleteShutdownNodeAction();
23+
public static final String NAME = "cluster:admin/shutdown/delete";
24+
25+
public DeleteShutdownNodeAction() {
26+
super(NAME, AcknowledgedResponse::readFrom);
27+
}
28+
29+
public static class Request extends MasterNodeRequest<DeleteShutdownNodeAction.Request> {
30+
31+
private final String nodeId;
32+
33+
public Request(String nodeId) {
34+
this.nodeId = nodeId;
35+
}
36+
37+
public Request(StreamInput in) throws IOException {
38+
this.nodeId = in.readString();
39+
}
40+
41+
@Override
42+
public void writeTo(StreamOutput out) throws IOException {
43+
out.writeString(this.nodeId);
44+
}
45+
46+
public String getNodeId() {
47+
return nodeId;
48+
}
49+
50+
@Override
51+
public ActionRequestValidationException validate() {
52+
if (Strings.hasText(nodeId) == false) {
53+
ActionRequestValidationException arve = new ActionRequestValidationException();
54+
arve.addValidationError("the node id to remove from shutdown is required");
55+
return arve;
56+
}
57+
return null;
58+
}
59+
}
60+
61+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.shutdown;
9+
10+
import org.elasticsearch.action.ActionRequestValidationException;
11+
import org.elasticsearch.action.ActionResponse;
12+
import org.elasticsearch.action.ActionType;
13+
import org.elasticsearch.action.support.master.MasterNodeRequest;
14+
import org.elasticsearch.common.io.stream.StreamInput;
15+
import org.elasticsearch.common.io.stream.StreamOutput;
16+
import org.elasticsearch.common.xcontent.ToXContentObject;
17+
import org.elasticsearch.common.xcontent.XContentBuilder;
18+
19+
import java.io.IOException;
20+
21+
public class GetShutdownStatusAction extends ActionType<GetShutdownStatusAction.Response> {
22+
23+
public static final GetShutdownStatusAction INSTANCE = new GetShutdownStatusAction();
24+
public static final String NAME = "cluster:admin/shutdown/get";
25+
26+
public GetShutdownStatusAction() {
27+
super(NAME, Response::new);
28+
}
29+
30+
public static class Request extends MasterNodeRequest<Request> {
31+
32+
private final String[] nodeIds;
33+
34+
public Request(String... nodeIds) {
35+
this.nodeIds = nodeIds;
36+
}
37+
38+
public static Request readFrom(StreamInput in) throws IOException {
39+
return new Request(in.readStringArray());
40+
}
41+
42+
@Override
43+
public void writeTo(StreamOutput out) throws IOException {
44+
out.writeStringArray(this.nodeIds);
45+
}
46+
47+
public String[] getNodeIds() {
48+
return nodeIds;
49+
}
50+
51+
@Override
52+
public ActionRequestValidationException validate() {
53+
return null;
54+
}
55+
}
56+
57+
public static class Response extends ActionResponse implements ToXContentObject {
58+
59+
public Response(StreamInput in) throws IOException {
60+
61+
}
62+
63+
@Override
64+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
65+
builder.startObject();
66+
builder.endObject();
67+
return builder;
68+
}
69+
70+
@Override
71+
public void writeTo(StreamOutput out) throws IOException {
72+
73+
}
74+
}
75+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.shutdown;
9+
10+
import org.elasticsearch.action.ActionRequestValidationException;
11+
import org.elasticsearch.action.ActionType;
12+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
13+
import org.elasticsearch.action.support.master.MasterNodeRequest;
14+
import org.elasticsearch.common.Strings;
15+
import org.elasticsearch.common.io.stream.StreamInput;
16+
import org.elasticsearch.common.io.stream.StreamOutput;
17+
18+
import java.io.IOException;
19+
20+
public class PutShutdownNodeAction extends ActionType<AcknowledgedResponse> {
21+
22+
public static final PutShutdownNodeAction INSTANCE = new PutShutdownNodeAction();
23+
public static final String NAME = "cluster:admin/shutdown/create";
24+
25+
public PutShutdownNodeAction() {
26+
super(NAME, AcknowledgedResponse::readFrom);
27+
}
28+
29+
public static class Request extends MasterNodeRequest<Request> {
30+
31+
private final String nodeId;
32+
33+
public Request(String nodeId) {
34+
this.nodeId = nodeId;
35+
}
36+
37+
public Request(StreamInput in) throws IOException {
38+
this.nodeId = in.readString();
39+
}
40+
41+
@Override
42+
public void writeTo(StreamOutput out) throws IOException {
43+
out.writeString(this.nodeId);
44+
}
45+
46+
public String getNodeId() {
47+
return nodeId;
48+
}
49+
50+
@Override
51+
public ActionRequestValidationException validate() {
52+
if (Strings.hasText(nodeId) == false) {
53+
ActionRequestValidationException arve = new ActionRequestValidationException();
54+
arve.addValidationError("the node id to shutdown is required");
55+
return arve;
56+
}
57+
return null;
58+
}
59+
}
60+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.shutdown;
9+
10+
import org.elasticsearch.client.node.NodeClient;
11+
import org.elasticsearch.rest.BaseRestHandler;
12+
import org.elasticsearch.rest.RestRequest;
13+
import org.elasticsearch.rest.action.RestToXContentListener;
14+
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
public class RestDeleteShutdownNodeAction extends BaseRestHandler {
19+
20+
@Override
21+
public String getName() {
22+
return "delete_shutdown_node";
23+
}
24+
25+
@Override
26+
public List<Route> routes() {
27+
return Collections.singletonList(new Route(RestRequest.Method.DELETE, "/_nodes/{nodeId}/shutdown"));
28+
}
29+
30+
@Override
31+
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
32+
String nodeId = request.param("nodeId");
33+
return channel -> client.execute(
34+
DeleteShutdownNodeAction.INSTANCE,
35+
new DeleteShutdownNodeAction.Request(nodeId),
36+
new RestToXContentListener<>(channel)
37+
);
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.shutdown;
9+
10+
import org.elasticsearch.client.node.NodeClient;
11+
import org.elasticsearch.common.Strings;
12+
import org.elasticsearch.rest.BaseRestHandler;
13+
import org.elasticsearch.rest.RestRequest;
14+
import org.elasticsearch.rest.action.RestToXContentListener;
15+
16+
import java.util.Arrays;
17+
import java.util.List;
18+
19+
public class RestGetShutdownStatusAction extends BaseRestHandler {
20+
21+
@Override
22+
public String getName() {
23+
return "get_shutdown_status";
24+
}
25+
26+
@Override
27+
public List<Route> routes() {
28+
return Arrays.asList(
29+
new Route(RestRequest.Method.GET, "/_nodes/{nodeId}/shutdown"),
30+
new Route(RestRequest.Method.GET, "/_nodes/shutdown")
31+
);
32+
}
33+
34+
@Override
35+
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
36+
String[] nodeIds = Strings.commaDelimitedListToStringArray(request.param("nodeId"));
37+
return channel -> client.execute(
38+
GetShutdownStatusAction.INSTANCE,
39+
new GetShutdownStatusAction.Request(nodeIds),
40+
new RestToXContentListener<>(channel)
41+
);
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.shutdown;
9+
10+
import org.elasticsearch.client.node.NodeClient;
11+
import org.elasticsearch.rest.BaseRestHandler;
12+
import org.elasticsearch.rest.RestRequest;
13+
import org.elasticsearch.rest.action.RestToXContentListener;
14+
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
public class RestPutShutdownNodeAction extends BaseRestHandler {
19+
20+
@Override
21+
public String getName() {
22+
return "put_shutdown_node";
23+
}
24+
25+
@Override
26+
public List<Route> routes() {
27+
return Collections.singletonList(new Route(RestRequest.Method.PUT, "/_nodes/{nodeId}/shutdown"));
28+
}
29+
30+
@Override
31+
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
32+
String nodeId = request.param("nodeId");
33+
return channel -> client.execute(
34+
PutShutdownNodeAction.INSTANCE,
35+
new PutShutdownNodeAction.Request(nodeId),
36+
new RestToXContentListener<>(channel)
37+
);
38+
}
39+
}

0 commit comments

Comments
 (0)