Skip to content

Commit

Permalink
Refactor service names as constants
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Sep 13, 2024
1 parent cdfa8eb commit f2abd4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
19 changes: 13 additions & 6 deletions common/src/main/java/org/opensearch/sdk/SdkClientSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Setting.Property;

import java.util.Set;

/** Settings applicable to the SdkClient */
public class SdkClientSettings {

/** The key for remote metadata type. */
public static final String REMOTE_METADATA_TYPE_KEY = "plugins.ml_commons.remote_metadata_type";

/** The value for remote metadata type for a remote cluster. */
/** The value for remote metadata type for a remote OpenSearch cluster compatible with OpenSearch Java Client. */
public static final String REMOTE_OPENSEARCH = "RemoteOpenSearch";
/** The value for remote metadata type for a remote cluster on AWS OpenSearch Service. */
/** The value for remote metadata type for a remote cluster on AWS OpenSearch Service using AWS SDK v2. */
public static final String AWS_OPENSEARCH_SERVICE = "AWSOpenSearchService";
/** The value for remote metadata type for a remote cluster on AWS Dynamo DB. */
private static final String AOS_SERVICE_NAME = "es";
private static final String AOSS_SERVICE_NAME = "aoss";
/** Service Names compatible with AWS SDK v2. */
public static final Set<String> VALID_AWS_OPENSEARCH_SERVICE_NAMES = Set.of(AOS_SERVICE_NAME, AOSS_SERVICE_NAME);

/** The value for remote metadata type for a remote cluster on AWS Dynamo DB and Zero-ETL replication to OpenSearch */
public static final String AWS_DYNAMO_DB = "AWSDynamoDB";

/** The key for remote metadata endpoint, applicable to remote clusters or DynamoDB. */
/** The key for remote metadata endpoint, applicable to remote clusters or Zero-ETL DynamoDB sinks */
public static final String REMOTE_METADATA_ENDPOINT_KEY = "plugins.ml_commons.remote_metadata_endpoint";
/** The key for remote metadata region, applicable to AWS remote clusters or DynamoDB. */
/** The key for remote metadata region, applicable for AWS SDK v2 connections */
public static final String REMOTE_METADATA_REGION_KEY = "plugins.ml_commons.remote_metadata_region";
/** The key for remote metadata AWS service, either "es" for cluster or "aoss" for OpenSearch Serverless */
/** The key for remote metadata service name used by service-specific SDKs */
public static final String REMOTE_METADATA_SERVICE_NAME_KEY = "plugins.ml_commons.remote_metadata_service_name";

public static final Setting<String> REMOTE_METADATA_TYPE = Setting.simpleString(REMOTE_METADATA_TYPE_KEY, Property.NodeScope, Property.Final);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
*/
package org.opensearch.ml.sdkclient;

import static org.opensearch.sdk.SdkClientSettings.AWS_DYNAMO_DB;
import static org.opensearch.sdk.SdkClientSettings.AWS_OPENSEARCH_SERVICE;
import static org.opensearch.sdk.SdkClientSettings.REMOTE_METADATA_ENDPOINT;
import static org.opensearch.sdk.SdkClientSettings.REMOTE_METADATA_REGION;
import static org.opensearch.sdk.SdkClientSettings.REMOTE_METADATA_SERVICE_NAME;
import static org.opensearch.sdk.SdkClientSettings.REMOTE_METADATA_TYPE;
import static org.opensearch.sdk.SdkClientSettings.REMOTE_OPENSEARCH;
import static org.opensearch.sdk.SdkClientSettings.VALID_AWS_OPENSEARCH_SERVICE_NAMES;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
Expand Down Expand Up @@ -39,7 +48,6 @@
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.sdk.SdkClient;
import org.opensearch.sdk.SdkClientDelegate;
import org.opensearch.sdk.SdkClientSettings;
import org.opensearch.sdk.client.LocalClusterIndicesClient;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -72,25 +80,25 @@ public class SdkClientFactory {
* @return An instance of SdkClient which delegates to an implementation based on Remote Metadata Type
*/
public static SdkClient createSdkClient(Client client, NamedXContentRegistry xContentRegistry, Settings settings) {
String remoteMetadataType = SdkClientSettings.REMOTE_METADATA_TYPE.get(settings);
String remoteMetadataEndpoint = SdkClientSettings.REMOTE_METADATA_ENDPOINT.get(settings);
String region = SdkClientSettings.REMOTE_METADATA_REGION.get(settings);
String serviceName = SdkClientSettings.REMOTE_METADATA_SERVICE_NAME.get(settings);
String remoteMetadataType = REMOTE_METADATA_TYPE.get(settings);
String remoteMetadataEndpoint = REMOTE_METADATA_ENDPOINT.get(settings);
String region = REMOTE_METADATA_REGION.get(settings);
String serviceName = REMOTE_METADATA_SERVICE_NAME.get(settings);

switch (remoteMetadataType) {
case SdkClientSettings.REMOTE_OPENSEARCH:
case REMOTE_OPENSEARCH:
if (Strings.isBlank(remoteMetadataEndpoint)) {
throw new OpenSearchException("Remote Opensearch client requires a metadata endpoint.");
}
log.info("Using remote opensearch cluster as metadata store");
return new SdkClient(new RemoteClusterIndicesClient(createOpenSearchClient(remoteMetadataEndpoint)));
case SdkClientSettings.AWS_OPENSEARCH_SERVICE:
case AWS_OPENSEARCH_SERVICE:
validateAwsParams(remoteMetadataType, remoteMetadataEndpoint, region, serviceName);
log.info("Using remote AWS Opensearch Service cluster as metadata store");
return new SdkClient(
new RemoteClusterIndicesClient(createAwsOpenSearchServiceClient(remoteMetadataEndpoint, region, serviceName))
);
case SdkClientSettings.AWS_DYNAMO_DB:
case AWS_DYNAMO_DB:
validateAwsParams(remoteMetadataType, remoteMetadataEndpoint, region, serviceName);
log.info("Using dynamo DB as metadata store");
return new SdkClient(
Expand All @@ -109,8 +117,8 @@ private static void validateAwsParams(String clientType, String remoteMetadataEn
if (Strings.isBlank(remoteMetadataEndpoint) || Strings.isBlank(region)) {
throw new OpenSearchException(clientType + " client requires a metadata endpoint and region.");
}
if (!"es".equals(serviceName) && !"aoss".equals(serviceName)) {
throw new OpenSearchException(clientType + " client requires a signing service of 'es' or 'aoss'.");
if (!VALID_AWS_OPENSEARCH_SERVICE_NAMES.contains(serviceName)) {
throw new OpenSearchException(clientType + " client only supports service names " + VALID_AWS_OPENSEARCH_SERVICE_NAMES);
}
}

Expand Down

0 comments on commit f2abd4e

Please sign in to comment.