forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a capabilities API to check node and cluster capabilities (elasti…
…c#106820) This adds a /_capabilities rest endpoint for checking the capabilities of a cluster - what endpoints, parameters, and endpoint capabilities the cluster supports
- Loading branch information
Showing
15 changed files
with
504 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 106820 | ||
summary: Add a capabilities API to check node and cluster capabilities | ||
area: Infra/REST API | ||
type: feature | ||
issues: [] |
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
55 changes: 55 additions & 0 deletions
55
...ternalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.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,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.nodescapabilities; | ||
|
||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; | ||
import org.elasticsearch.action.admin.cluster.node.capabilities.NodesCapabilitiesRequest; | ||
import org.elasticsearch.action.admin.cluster.node.capabilities.NodesCapabilitiesResponse; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) | ||
public class SimpleNodesCapabilitiesIT extends ESIntegTestCase { | ||
|
||
public void testNodesCapabilities() throws IOException { | ||
internalCluster().startNodes(2); | ||
|
||
ClusterHealthResponse clusterHealth = clusterAdmin().prepareHealth().setWaitForGreenStatus().setWaitForNodes("2").get(); | ||
logger.info("--> done cluster_health, status {}", clusterHealth.getStatus()); | ||
|
||
// check we support the capabilities API itself. Which we do. | ||
NodesCapabilitiesResponse response = clusterAdmin().nodesCapabilities(new NodesCapabilitiesRequest().path("_capabilities")) | ||
.actionGet(); | ||
assertThat(response.getNodes(), hasSize(2)); | ||
assertThat(response.isSupported(), is(true)); | ||
|
||
// check we support some parameters of the capabilities API | ||
response = clusterAdmin().nodesCapabilities(new NodesCapabilitiesRequest().path("_capabilities").parameters("method", "path")) | ||
.actionGet(); | ||
assertThat(response.getNodes(), hasSize(2)); | ||
assertThat(response.isSupported(), is(true)); | ||
|
||
// check we don't support some other parameters of the capabilities API | ||
response = clusterAdmin().nodesCapabilities(new NodesCapabilitiesRequest().path("_capabilities").parameters("method", "invalid")) | ||
.actionGet(); | ||
assertThat(response.getNodes(), hasSize(2)); | ||
assertThat(response.isSupported(), is(false)); | ||
|
||
// check we don't support a random invalid api | ||
// TODO this is not working yet - see https://github.com/elastic/elasticsearch/issues/107425 | ||
/*response = clusterAdmin().nodesCapabilities(new NodesCapabilitiesRequest().path("_invalid")) | ||
.actionGet(); | ||
assertThat(response.getNodes(), hasSize(2)); | ||
assertThat(response.isSupported(), is(false));*/ | ||
} | ||
} |
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
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
43 changes: 43 additions & 0 deletions
43
...rc/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/NodeCapability.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,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.cluster.node.capabilities; | ||
|
||
import org.elasticsearch.action.support.nodes.BaseNodeResponse; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
public class NodeCapability extends BaseNodeResponse { | ||
|
||
private final boolean supported; | ||
|
||
public NodeCapability(StreamInput in) throws IOException { | ||
super(in); | ||
|
||
supported = in.readBoolean(); | ||
} | ||
|
||
public NodeCapability(boolean supported, DiscoveryNode node) { | ||
super(node); | ||
this.supported = supported; | ||
} | ||
|
||
public boolean isSupported() { | ||
return supported; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
|
||
out.writeBoolean(supported); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...va/org/elasticsearch/action/admin/cluster/node/capabilities/NodesCapabilitiesRequest.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,75 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.cluster.node.capabilities; | ||
|
||
import org.elasticsearch.action.support.nodes.BaseNodesRequest; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.core.RestApiVersion; | ||
import org.elasticsearch.rest.RestRequest; | ||
|
||
import java.util.Set; | ||
|
||
public class NodesCapabilitiesRequest extends BaseNodesRequest<NodesCapabilitiesRequest> { | ||
|
||
private RestRequest.Method method = RestRequest.Method.GET; | ||
private String path = "/"; | ||
private Set<String> parameters = Set.of(); | ||
private Set<String> capabilities = Set.of(); | ||
private RestApiVersion restApiVersion = RestApiVersion.current(); | ||
|
||
public NodesCapabilitiesRequest() { | ||
// always send to all nodes | ||
super(Strings.EMPTY_ARRAY); | ||
} | ||
|
||
public NodesCapabilitiesRequest path(String path) { | ||
this.path = path; | ||
return this; | ||
} | ||
|
||
public String path() { | ||
return path; | ||
} | ||
|
||
public NodesCapabilitiesRequest method(RestRequest.Method method) { | ||
this.method = method; | ||
return this; | ||
} | ||
|
||
public RestRequest.Method method() { | ||
return method; | ||
} | ||
|
||
public NodesCapabilitiesRequest parameters(String... parameters) { | ||
this.parameters = Set.of(parameters); | ||
return this; | ||
} | ||
|
||
public Set<String> parameters() { | ||
return parameters; | ||
} | ||
|
||
public NodesCapabilitiesRequest capabilities(String... capabilities) { | ||
this.capabilities = Set.of(capabilities); | ||
return this; | ||
} | ||
|
||
public Set<String> capabilities() { | ||
return capabilities; | ||
} | ||
|
||
public NodesCapabilitiesRequest restApiVersion(RestApiVersion restApiVersion) { | ||
this.restApiVersion = restApiVersion; | ||
return this; | ||
} | ||
|
||
public RestApiVersion restApiVersion() { | ||
return restApiVersion; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...a/org/elasticsearch/action/admin/cluster/node/capabilities/NodesCapabilitiesResponse.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,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.cluster.node.capabilities; | ||
|
||
import org.elasticsearch.action.FailedNodeException; | ||
import org.elasticsearch.action.support.TransportAction; | ||
import org.elasticsearch.action.support.nodes.BaseNodesResponse; | ||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xcontent.ToXContentFragment; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class NodesCapabilitiesResponse extends BaseNodesResponse<NodeCapability> implements ToXContentFragment { | ||
protected NodesCapabilitiesResponse(ClusterName clusterName, List<NodeCapability> nodes, List<FailedNodeException> failures) { | ||
super(clusterName, nodes, failures); | ||
} | ||
|
||
@Override | ||
protected List<NodeCapability> readNodesFrom(StreamInput in) throws IOException { | ||
return TransportAction.localOnly(); | ||
} | ||
|
||
@Override | ||
protected void writeNodesTo(StreamOutput out, List<NodeCapability> nodes) throws IOException { | ||
TransportAction.localOnly(); | ||
} | ||
|
||
public boolean isSupported() { | ||
return getNodes().isEmpty() == false && getNodes().stream().allMatch(NodeCapability::isSupported); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
return builder.field("supported", isSupported()); | ||
} | ||
} |
Oops, something went wrong.