From ab467cae8014d78aeecfc5782cd5174f53e6918a Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Tue, 27 Aug 2024 01:09:08 +1200 Subject: [PATCH] Generate ml.get_model Signed-off-by: Thomas Farr --- .../opensearch/ml/GetModelGroupRequest.java | 101 +++++++ .../opensearch/ml/GetModelGroupResponse.java | 258 ++++++++++++++++++ .../ml/OpenSearchMlAsyncClient.java | 26 ++ .../opensearch/ml/OpenSearchMlClient.java | 25 ++ .../client/codegen/CodeGenerator.java | 23 +- .../client/codegen/model/OperationGroup.java | 59 ---- .../codegen/model/OperationGroupMatcher.java | 61 +++++ .../client/codegen/model/SpecTransformer.java | 4 +- 8 files changed, 481 insertions(+), 76 deletions(-) create mode 100644 java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupRequest.java create mode 100644 java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupResponse.java create mode 100644 java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroupMatcher.java diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupRequest.java b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupRequest.java new file mode 100644 index 0000000000..92c6537dc9 --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupRequest.java @@ -0,0 +1,101 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch.ml; + +import java.util.function.Function; +import javax.annotation.Generated; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.opensearch._types.RequestBase; +import org.opensearch.client.transport.Endpoint; +import org.opensearch.client.transport.endpoints.SimpleEndpoint; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.ObjectBuilder; +import org.opensearch.client.util.ObjectBuilderBase; + +// typedef: ml.get_model_group.Request + +/** + * Retrieves a model group. + */ +@Generated("org.opensearch.client.codegen.CodeGenerator") +public class GetModelGroupRequest extends RequestBase { + + private final String modelGroupId; + + // --------------------------------------------------------------------------------------------- + + private GetModelGroupRequest(Builder builder) { + this.modelGroupId = ApiTypeHelper.requireNonNull(builder.modelGroupId, this, "modelGroupId"); + } + + public static GetModelGroupRequest of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * Required - API name: {@code model_group_id} + */ + public final String modelGroupId() { + return this.modelGroupId; + } + + // --------------------------------------------------------------------------------------------- + + /** + * Builder for {@link GetModelGroupRequest}. + */ + public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + private String modelGroupId; + + /** + * Required - API name: {@code model_group_id} + */ + public final Builder modelGroupId(String value) { + this.modelGroupId = value; + return this; + } + + /** + * Builds a {@link GetModelGroupRequest}. + * + * @throws NullPointerException if some of the required fields are null. + */ + public GetModelGroupRequest build() { + _checkSingleUse(); + + return new GetModelGroupRequest(this); + } + } + + // --------------------------------------------------------------------------------------------- + + /** + * Endpoint "{@code ml.get_model_group}". + */ + public static final Endpoint _ENDPOINT = new SimpleEndpoint<>( + // Request method + request -> "GET", + // Request path + request -> { + StringBuilder buf = new StringBuilder(); + buf.append("/_plugins/_ml/model_groups/"); + SimpleEndpoint.pathEncode(request.modelGroupId, buf); + return buf.toString(); + }, + // Request parameters + SimpleEndpoint.emptyMap(), + SimpleEndpoint.emptyMap(), + false, + GetModelGroupResponse._DESERIALIZER + ); +} diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupResponse.java b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupResponse.java new file mode 100644 index 0000000000..4a8321c607 --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/GetModelGroupResponse.java @@ -0,0 +1,258 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch.ml; + +import jakarta.json.stream.JsonGenerator; +import java.util.function.Function; +import javax.annotation.Generated; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.ObjectBuilderDeserializer; +import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.json.PlainJsonSerializable; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.ObjectBuilder; +import org.opensearch.client.util.ObjectBuilderBase; + +// typedef: ml.get_model_group.Response + +@JsonpDeserializable +@Generated("org.opensearch.client.codegen.CodeGenerator") +public class GetModelGroupResponse implements PlainJsonSerializable { + + private final String access; + + @Nullable + private final Long createdTime; + + private final String description; + + @Nullable + private final Long lastUpdatedTime; + + private final int latestVersion; + + private final String name; + + // --------------------------------------------------------------------------------------------- + + private GetModelGroupResponse(Builder builder) { + this.access = ApiTypeHelper.requireNonNull(builder.access, this, "access"); + this.createdTime = builder.createdTime; + this.description = ApiTypeHelper.requireNonNull(builder.description, this, "description"); + this.lastUpdatedTime = builder.lastUpdatedTime; + this.latestVersion = ApiTypeHelper.requireNonNull(builder.latestVersion, this, "latestVersion"); + this.name = ApiTypeHelper.requireNonNull(builder.name, this, "name"); + } + + public static GetModelGroupResponse of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * Required - The model group access. + *

+ * API name: {@code access} + *

+ */ + public final String access() { + return this.access; + } + + /** + * API name: {@code created_time} + */ + @Nullable + public final Long createdTime() { + return this.createdTime; + } + + /** + * Required - The model group description. + *

+ * API name: {@code description} + *

+ */ + public final String description() { + return this.description; + } + + /** + * API name: {@code last_updated_time} + */ + @Nullable + public final Long lastUpdatedTime() { + return this.lastUpdatedTime; + } + + /** + * Required - The latest version. + *

+ * API name: {@code latest_version} + *

+ */ + public final int latestVersion() { + return this.latestVersion; + } + + /** + * Required - The model group name. + *

+ * API name: {@code name} + *

+ */ + public final String name() { + return this.name; + } + + /** + * Serialize this object to JSON. + */ + @Override + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + serializeInternal(generator, mapper); + generator.writeEnd(); + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + generator.writeKey("access"); + generator.write(this.access); + + if (this.createdTime != null) { + generator.writeKey("created_time"); + generator.write(this.createdTime); + } + + generator.writeKey("description"); + generator.write(this.description); + + if (this.lastUpdatedTime != null) { + generator.writeKey("last_updated_time"); + generator.write(this.lastUpdatedTime); + } + + generator.writeKey("latest_version"); + generator.write(this.latestVersion); + + generator.writeKey("name"); + generator.write(this.name); + } + + // --------------------------------------------------------------------------------------------- + + /** + * Builder for {@link GetModelGroupResponse}. + */ + public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + private String access; + @Nullable + private Long createdTime; + private String description; + @Nullable + private Long lastUpdatedTime; + private Integer latestVersion; + private String name; + + /** + * Required - The model group access. + *

+ * API name: {@code access} + *

+ */ + public final Builder access(String value) { + this.access = value; + return this; + } + + /** + * API name: {@code created_time} + */ + public final Builder createdTime(@Nullable Long value) { + this.createdTime = value; + return this; + } + + /** + * Required - The model group description. + *

+ * API name: {@code description} + *

+ */ + public final Builder description(String value) { + this.description = value; + return this; + } + + /** + * API name: {@code last_updated_time} + */ + public final Builder lastUpdatedTime(@Nullable Long value) { + this.lastUpdatedTime = value; + return this; + } + + /** + * Required - The latest version. + *

+ * API name: {@code latest_version} + *

+ */ + public final Builder latestVersion(int value) { + this.latestVersion = value; + return this; + } + + /** + * Required - The model group name. + *

+ * API name: {@code name} + *

+ */ + public final Builder name(String value) { + this.name = value; + return this; + } + + /** + * Builds a {@link GetModelGroupResponse}. + * + * @throws NullPointerException if some of the required fields are null. + */ + public GetModelGroupResponse build() { + _checkSingleUse(); + + return new GetModelGroupResponse(this); + } + } + // --------------------------------------------------------------------------------------------- + + /** + * Json deserializer for {@link GetModelGroupResponse} + */ + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy( + Builder::new, + GetModelGroupResponse::setupGetModelGroupResponseDeserializer + ); + + protected static void setupGetModelGroupResponseDeserializer(ObjectDeserializer op) { + op.add(Builder::access, JsonpDeserializer.stringDeserializer(), "access"); + op.add(Builder::createdTime, JsonpDeserializer.longDeserializer(), "created_time"); + op.add(Builder::description, JsonpDeserializer.stringDeserializer(), "description"); + op.add(Builder::lastUpdatedTime, JsonpDeserializer.longDeserializer(), "last_updated_time"); + op.add(Builder::latestVersion, JsonpDeserializer.integerDeserializer(), "latest_version"); + op.add(Builder::name, JsonpDeserializer.stringDeserializer(), "name"); + } +} diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlAsyncClient.java b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlAsyncClient.java index 0d2fc6cb54..8f2ab231ba 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlAsyncClient.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlAsyncClient.java @@ -147,6 +147,32 @@ public final CompletableFuture deployModel( return deployModel(fn.apply(new DeployModelRequest.Builder()).build()); } + // ----- Endpoint: ml.get_model_group + + /** + * Retrieves a model group. + */ + public CompletableFuture getModelGroup(GetModelGroupRequest request) throws IOException, OpenSearchException { + @SuppressWarnings("unchecked") + JsonEndpoint endpoint = (JsonEndpoint< + GetModelGroupRequest, + GetModelGroupResponse, + ErrorResponse>) GetModelGroupRequest._ENDPOINT; + + return this.transport.performRequestAsync(request, endpoint, this.transportOptions); + } + + /** + * Retrieves a model group. + * + * @param fn a function that initializes a builder to create the {@link GetModelGroupRequest} + */ + public final CompletableFuture getModelGroup( + Function> fn + ) throws IOException, OpenSearchException { + return getModelGroup(fn.apply(new GetModelGroupRequest.Builder()).build()); + } + // ----- Endpoint: ml.get_task /** diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlClient.java b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlClient.java index d33ad14e0c..d3aba915be 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlClient.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/ml/OpenSearchMlClient.java @@ -143,6 +143,31 @@ public final DeployModelResponse deployModel(Function endpoint = (JsonEndpoint< + GetModelGroupRequest, + GetModelGroupResponse, + ErrorResponse>) GetModelGroupRequest._ENDPOINT; + + return this.transport.performRequest(request, endpoint, this.transportOptions); + } + + /** + * Retrieves a model group. + * + * @param fn a function that initializes a builder to create the {@link GetModelGroupRequest} + */ + public final GetModelGroupResponse getModelGroup(Function> fn) + throws IOException, OpenSearchException { + return getModelGroup(fn.apply(new GetModelGroupRequest.Builder()).build()); + } + // ----- Endpoint: ml.get_task /** diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java b/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java index 8c8506fad7..cf7333742a 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java @@ -26,27 +26,20 @@ import org.opensearch.client.codegen.exceptions.ApiSpecificationParseException; import org.opensearch.client.codegen.exceptions.RenderException; import org.opensearch.client.codegen.model.Namespace; -import org.opensearch.client.codegen.model.OperationGroup; import org.opensearch.client.codegen.model.ShapeRenderingContext; import org.opensearch.client.codegen.model.SpecTransformer; +import org.opensearch.client.codegen.model.OperationGroupMatcher; import org.opensearch.client.codegen.openapi.OpenApiSpecification; +import static org.opensearch.client.codegen.model.OperationGroupMatcher.*; + public class CodeGenerator { private static final Logger LOGGER = LogManager.getLogger(); - private static final OperationGroup.Matcher OPERATION_MATCHER = OperationGroup.matcher() - .add(null, "info") - .add("dangling_indices") - .add( - "ml", - "delete_model", - "delete_model_group", - "delete_task", - "deploy_model", - "get_task", - "register_model", - "register_model_group", - "undeploy_model" - ); + private static final OperationGroupMatcher OPERATION_MATCHER = or( + and(namespace(""), named("info")), + namespace("dangling_indices"), + and(namespace("ml"), not(named("search_models"))) + ); public static void main(String[] args) { var inputOpt = Option.builder("i") diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroup.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroup.java index 818a4acd40..0d8dd650d9 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroup.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroup.java @@ -8,12 +8,7 @@ package org.opensearch.client.codegen.model; -import java.util.Collection; -import java.util.HashSet; -import java.util.Objects; import java.util.Optional; -import java.util.Set; -import java.util.regex.Pattern; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.apache.commons.lang3.builder.EqualsBuilder; @@ -76,58 +71,4 @@ public boolean equals(Object o) { public int hashCode() { return new HashCodeBuilder(17, 37).append(namespace).append(name).toHashCode(); } - - @Nonnull - public static Matcher matcher() { - return new Matcher(); - } - - public static class Matcher { - private final Set namespaces = new HashSet<>(); - private final Set operations = new HashSet<>(); - private final Collection patterns = new HashSet<>(); - - private Matcher() {} - - @Nonnull - public Matcher add(@Nullable String namespace, @Nullable String... operations) { - if (operations == null || operations.length == 0) { - namespaces.add(Strings.requireNonBlank(namespace, "namespace must not be blank")); - } else { - for (String operation : operations) { - add(new OperationGroup(namespace, operation)); - } - } - return this; - } - - @Nonnull - public Matcher add(@Nonnull OperationGroup operation) { - operations.add(Objects.requireNonNull(operation, "operation must not be null")); - return this; - } - - @Nonnull - public Matcher add(@Nonnull Pattern pattern) { - patterns.add(Objects.requireNonNull(pattern, "pattern must not be null")); - return this; - } - - public boolean matches(@Nonnull OperationGroup operation) { - Objects.requireNonNull(operation, "operation must not be null"); - if (operation.getNamespace().map(namespaces::contains).orElse(false)) { - return true; - } - if (operations.contains(operation)) { - return true; - } - var str = operation.toString(); - for (Pattern pattern : patterns) { - if (pattern.matcher(str).matches()) { - return true; - } - } - return false; - } - } } diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroupMatcher.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroupMatcher.java new file mode 100644 index 0000000000..bf18015a71 --- /dev/null +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/OperationGroupMatcher.java @@ -0,0 +1,61 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.client.codegen.model; + +import java.util.Arrays; +import java.util.HashSet; +import javax.annotation.Nonnull; + +@FunctionalInterface +public interface OperationGroupMatcher { + boolean matches(OperationGroup group); + + static OperationGroupMatcher all() { + return group -> true; + } + + static OperationGroupMatcher none() { + return group -> false; + } + + static OperationGroupMatcher not(OperationGroupMatcher matcher) { + return group -> !matcher.matches(group); + } + + static OperationGroupMatcher or(OperationGroupMatcher... matchers) { + return group -> { + for (OperationGroupMatcher matcher : matchers) { + if (matcher.matches(group)) { + return true; + } + } + return false; + }; + } + + static OperationGroupMatcher and(OperationGroupMatcher... matchers) { + return group -> { + for (OperationGroupMatcher matcher : matchers) { + if (!matcher.matches(group)) { + return false; + } + } + return true; + }; + } + + static OperationGroupMatcher named(String... names) { + var set = new HashSet<>(Arrays.asList(names)); + return group -> set.contains(group.getName()); + } + + static OperationGroupMatcher namespace(@Nonnull String namespace) { + return group -> namespace.equals(group.getNamespace().orElse("")); + } +} diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java index 1579a89759..72527200ff 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java @@ -46,7 +46,7 @@ public class SpecTransformer { private static final Logger LOGGER = LogManager.getLogger(); @Nonnull - private final OperationGroup.Matcher matcher; + private final OperationGroupMatcher matcher; @Nonnull private final Namespace root = new Namespace(); @Nonnull @@ -54,7 +54,7 @@ public class SpecTransformer { @Nonnull private final Map schemaToType = new ConcurrentHashMap<>(); - public SpecTransformer(@Nonnull OperationGroup.Matcher matcher) { + public SpecTransformer(@Nonnull OperationGroupMatcher matcher) { this.matcher = Objects.requireNonNull(matcher, "matcher must not be null"); }