-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HLRC support for query API key API (#76520)
- Loading branch information
Showing
12 changed files
with
774 additions
and
5 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
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
158 changes: 158 additions & 0 deletions
158
...t/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyRequest.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,158 @@ | ||
/* | ||
* 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.client.security; | ||
|
||
import org.elasticsearch.client.Validatable; | ||
import org.elasticsearch.client.ValidationException; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.search.searchafter.SearchAfterBuilder; | ||
import org.elasticsearch.search.sort.FieldSortBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public final class QueryApiKeyRequest implements Validatable, ToXContentObject { | ||
|
||
@Nullable | ||
private QueryBuilder queryBuilder; | ||
private Integer from; | ||
private Integer size; | ||
@Nullable | ||
private List<FieldSortBuilder> fieldSortBuilders; | ||
@Nullable | ||
private SearchAfterBuilder searchAfterBuilder; | ||
|
||
public QueryApiKeyRequest() { | ||
this(null, null, null, null, null); | ||
} | ||
|
||
public QueryApiKeyRequest( | ||
@Nullable QueryBuilder queryBuilder, | ||
@Nullable Integer from, | ||
@Nullable Integer size, | ||
@Nullable List<FieldSortBuilder> fieldSortBuilders, | ||
@Nullable SearchAfterBuilder searchAfterBuilder) { | ||
this.queryBuilder = queryBuilder; | ||
this.from = from; | ||
this.size = size; | ||
this.fieldSortBuilders = fieldSortBuilders; | ||
this.searchAfterBuilder = searchAfterBuilder; | ||
} | ||
|
||
public QueryBuilder getQueryBuilder() { | ||
return queryBuilder; | ||
} | ||
|
||
public int getFrom() { | ||
return from; | ||
} | ||
|
||
public int getSize() { | ||
return size; | ||
} | ||
|
||
public List<FieldSortBuilder> getFieldSortBuilders() { | ||
return fieldSortBuilders; | ||
} | ||
|
||
public SearchAfterBuilder getSearchAfterBuilder() { | ||
return searchAfterBuilder; | ||
} | ||
|
||
public QueryApiKeyRequest queryBuilder(QueryBuilder queryBuilder) { | ||
this.queryBuilder = queryBuilder; | ||
return this; | ||
} | ||
|
||
public QueryApiKeyRequest from(int from) { | ||
this.from = from; | ||
return this; | ||
} | ||
|
||
public QueryApiKeyRequest size(int size) { | ||
this.size = size; | ||
return this; | ||
} | ||
|
||
public QueryApiKeyRequest fieldSortBuilders(List<FieldSortBuilder> fieldSortBuilders) { | ||
this.fieldSortBuilders = fieldSortBuilders; | ||
return this; | ||
} | ||
|
||
public QueryApiKeyRequest searchAfterBuilder(SearchAfterBuilder searchAfterBuilder) { | ||
this.searchAfterBuilder = searchAfterBuilder; | ||
return this; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
if (queryBuilder != null) { | ||
builder.field("query"); | ||
queryBuilder.toXContent(builder, params); | ||
} | ||
if (from != null) { | ||
builder.field("from", from); | ||
} | ||
if (size != null) { | ||
builder.field("size", size); | ||
} | ||
if (fieldSortBuilders != null && false == fieldSortBuilders.isEmpty()) { | ||
builder.field("sort", fieldSortBuilders); | ||
} | ||
if (searchAfterBuilder != null) { | ||
builder.array(SearchAfterBuilder.SEARCH_AFTER.getPreferredName(), searchAfterBuilder.getSortValues()); | ||
} | ||
return builder.endObject(); | ||
} | ||
|
||
@Override | ||
public Optional<ValidationException> validate() { | ||
ValidationException validationException = null; | ||
if (from != null && from < 0) { | ||
validationException = addValidationError(validationException, "from must be non-negative"); | ||
} | ||
if (size != null && size < 0) { | ||
validationException = addValidationError(validationException, "size must be non-negative"); | ||
} | ||
return validationException == null ? Optional.empty() : Optional.of(validationException); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
QueryApiKeyRequest that = (QueryApiKeyRequest) o; | ||
return Objects.equals(queryBuilder, that.queryBuilder) && Objects.equals(from, that.from) && Objects.equals( | ||
size, | ||
that.size) && Objects.equals(fieldSortBuilders, that.fieldSortBuilders) && Objects.equals( | ||
searchAfterBuilder, | ||
that.searchAfterBuilder); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(queryBuilder, from, size, fieldSortBuilders, searchAfterBuilder); | ||
} | ||
|
||
private ValidationException addValidationError(ValidationException validationException, String message) { | ||
if (validationException == null) { | ||
validationException = new ValidationException(); | ||
} | ||
validationException.addValidationError(message); | ||
return validationException; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
.../rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyResponse.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,68 @@ | ||
/* | ||
* 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.client.security; | ||
|
||
import org.elasticsearch.client.security.support.ApiKey; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ParseField; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
||
public final class QueryApiKeyResponse { | ||
|
||
private final long total; | ||
private final List<ApiKey> apiKeys; | ||
|
||
public QueryApiKeyResponse(long total, List<ApiKey> apiKeys) { | ||
this.total = total; | ||
this.apiKeys = apiKeys; | ||
} | ||
|
||
public long getTotal() { | ||
return total; | ||
} | ||
|
||
public int getCount() { | ||
return apiKeys.size(); | ||
} | ||
|
||
public List<ApiKey> getApiKeys() { | ||
return apiKeys; | ||
} | ||
|
||
public static QueryApiKeyResponse fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, null); | ||
} | ||
|
||
static final ConstructingObjectParser<QueryApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>( | ||
"query_api_key_response", | ||
args -> { | ||
final long total = (long) args[0]; | ||
final int count = (int) args[1]; | ||
@SuppressWarnings("unchecked") | ||
final List<ApiKey> items = (List<ApiKey>) args[2]; | ||
if (count != items.size()) { | ||
throw new IllegalArgumentException("count [" + count + "] is not equal to number of items [" | ||
+ items.size() + "]"); | ||
} | ||
return new QueryApiKeyResponse(total, items); | ||
} | ||
); | ||
|
||
static { | ||
PARSER.declareLong(constructorArg(), new ParseField("total")); | ||
PARSER.declareInt(constructorArg(), new ParseField("count")); | ||
PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> ApiKey.fromXContent(p), new ParseField("api_keys")); | ||
} | ||
} |
Oops, something went wrong.