-
Notifications
You must be signed in to change notification settings - Fork 25.2k
HLREST: Add Clear Roles Cache API #34187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fc8c4c2
add clear roles cache request to high level client
bleskes c5f6be8
supress
bleskes e966203
lint
bleskes 5bb06b1
fix parsing
bleskes 01fd7fd
Merge remote-tracking branch 'upstream/master' into clear_cache
bleskes 7f9659a
Merge remote-tracking branch 'upstream/master' into clear_cache
bleskes 6521201
Merge branch 'master' into clear_cache
jaymode 80bba1c
address review feedback
jaymode c3ba5b9
Merge branch 'master' into clear_cache
jaymode 1598d16
Merge branch 'master' into clear_cache
jaymode 780a76b
add docs
jaymode ae20137
fix checkstyle
jaymode d63d921
Merge branch 'master' into clear_cache
jaymode 382cb79
Merge branch 'master' into clear_cache
jaymode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponseHeader.java
This file contains hidden or 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,136 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.action.support.nodes.BaseNodesResponse; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.rest.action.RestActions; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A utility class to parse the Nodes Header returned by | ||
* {@link RestActions#buildNodesHeader(XContentBuilder, ToXContent.Params, BaseNodesResponse)}. | ||
*/ | ||
public final class NodesResponseHeader { | ||
|
||
public static final ParseField TOTAL = new ParseField("total"); | ||
public static final ParseField SUCCESSFUL = new ParseField("successful"); | ||
public static final ParseField FAILED = new ParseField("failed"); | ||
public static final ParseField FAILURES = new ParseField("failures"); | ||
|
||
@SuppressWarnings("unchecked") | ||
public static final ConstructingObjectParser<NodesResponseHeader, Void> PARSER = | ||
new ConstructingObjectParser<>("nodes_response_header", true, | ||
(a) -> { | ||
int i = 0; | ||
int total = (Integer) a[i++]; | ||
int successful = (Integer) a[i++]; | ||
int failed = (Integer) a[i++]; | ||
List<ElasticsearchException> failures = (List<ElasticsearchException>) a[i++]; | ||
return new NodesResponseHeader(total, successful, failed, failures); | ||
}); | ||
|
||
static { | ||
PARSER.declareInt(ConstructingObjectParser.constructorArg(), TOTAL); | ||
PARSER.declareInt(ConstructingObjectParser.constructorArg(), SUCCESSFUL); | ||
PARSER.declareInt(ConstructingObjectParser.constructorArg(), FAILED); | ||
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), | ||
(p, c) -> ElasticsearchException.fromXContent(p), FAILURES); | ||
} | ||
|
||
private final int total; | ||
private final int successful; | ||
private final int failed; | ||
private final List<ElasticsearchException> failures; | ||
|
||
public NodesResponseHeader(int total, int successful, int failed, @Nullable List<ElasticsearchException> failures) { | ||
this.total = total; | ||
this.successful = successful; | ||
this.failed = failed; | ||
this.failures = failures == null ? Collections.emptyList() : failures; | ||
} | ||
|
||
public static NodesResponseHeader fromXContent(XContentParser parser, Void context) throws IOException { | ||
return PARSER.parse(parser, context); | ||
} | ||
|
||
/** the total number of nodes that the operation was carried on */ | ||
public int getTotal() { | ||
return total; | ||
} | ||
|
||
/** the number of nodes that the operation has failed on */ | ||
public int getFailed() { | ||
return failed; | ||
} | ||
|
||
/** the number of nodes that the operation was successful on */ | ||
public int getSuccessful() { | ||
return successful; | ||
} | ||
|
||
/** | ||
* Get the failed node exceptions. | ||
* | ||
* @return Never {@code null}. Can be empty. | ||
*/ | ||
public List<ElasticsearchException> getFailures() { | ||
return failures; | ||
} | ||
|
||
/** | ||
* Determine if there are any node failures in {@link #failures}. | ||
* | ||
* @return {@code true} if {@link #failures} contains at least 1 exception. | ||
*/ | ||
public boolean hasFailures() { | ||
return failures.isEmpty() == false; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
NodesResponseHeader that = (NodesResponseHeader) o; | ||
return total == that.total && | ||
successful == that.successful && | ||
failed == that.failed && | ||
Objects.equals(failures, that.failures); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(total, successful, failed, failures); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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
...st-high-level/src/main/java/org/elasticsearch/client/security/ClearRolesCacheRequest.java
This file contains hidden or 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 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client.security; | ||
|
||
import org.elasticsearch.client.Validatable; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* The request used to clear the cache for native roles stored in an index. | ||
*/ | ||
public final class ClearRolesCacheRequest implements Validatable { | ||
|
||
private final String[] names; | ||
|
||
/** | ||
* Sets the roles for which caches will be evicted. When not set all the roles will be evicted from the cache. | ||
* | ||
* @param names The role names | ||
*/ | ||
public ClearRolesCacheRequest(String... names) { | ||
this.names = names; | ||
} | ||
|
||
/** | ||
* @return an array of role names that will have the cache evicted or <code>null</code> if all | ||
*/ | ||
public String[] names() { | ||
return names; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ClearRolesCacheRequest that = (ClearRolesCacheRequest) o; | ||
return Arrays.equals(names, that.names); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(names); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...t-high-level/src/main/java/org/elasticsearch/client/security/ClearRolesCacheResponse.java
This file contains hidden or 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,109 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client.security; | ||
|
||
import org.elasticsearch.client.NodesResponseHeader; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* The response object that will be returned when clearing the cache of native roles | ||
*/ | ||
public final class ClearRolesCacheResponse { | ||
|
||
@SuppressWarnings("unchecked") | ||
private static final ConstructingObjectParser<ClearRolesCacheResponse, Void> PARSER = | ||
new ConstructingObjectParser<>("clear_roles_cache_response", false, | ||
args -> new ClearRolesCacheResponse((List<Node>)args[0], (NodesResponseHeader) args[1], (String) args[2])); | ||
|
||
static { | ||
PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> Node.PARSER.apply(p, n), | ||
new ParseField("nodes")); | ||
PARSER.declareObject(ConstructingObjectParser.constructorArg(), NodesResponseHeader::fromXContent, new ParseField("_nodes")); | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("cluster_name")); | ||
} | ||
|
||
private final List<Node> nodes; | ||
private final NodesResponseHeader header; | ||
private final String clusterName; | ||
|
||
public ClearRolesCacheResponse(List<Node> nodes, NodesResponseHeader header, String clusterName) { | ||
this.nodes = nodes; | ||
this.header = header; | ||
this.clusterName = Objects.requireNonNull(clusterName, "cluster name must be provided"); | ||
} | ||
|
||
/** returns a list of nodes in which the cache was cleared */ | ||
public List<Node> getNodes() { | ||
return nodes; | ||
} | ||
|
||
/** | ||
* Get the cluster name associated with all of the nodes. | ||
* | ||
* @return Never {@code null}. | ||
*/ | ||
public String getClusterName() { | ||
return clusterName; | ||
} | ||
|
||
/** | ||
* Gets information about the number of total, successful and failed nodes the request was run on. | ||
* Also includes exceptions if relevant. | ||
*/ | ||
public NodesResponseHeader getHeader() { | ||
return header; | ||
} | ||
|
||
public static class Node { | ||
|
||
private static final ConstructingObjectParser<Node, String> PARSER = | ||
new ConstructingObjectParser<>("clear_roles_cache_response_node", false, (args, id) -> new Node(id, (String) args[0])); | ||
|
||
static { | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("name")); | ||
} | ||
|
||
private final String id; | ||
private final String name; | ||
|
||
public Node(String id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
public static ClearRolesCacheResponse fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, null); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should enforce this in the constructor of the class with
Objects.requireNonNull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, ive been telling ppl to remove the validation method and put all the validation into the constructor. the old school validation is a relic of the old actions.