Skip to content

Commit

Permalink
Move the region provider to the sdk client factory constructor and ma…
Browse files Browse the repository at this point in the history
…ke it optional (#1043)

Move the region provider to the sdk client factory constructor and make it optional.
This will avoid overwriting endpoint if one was set by user
  • Loading branch information
andriy-dmytruk authored Dec 20, 2024
1 parent 36f4e01 commit b4da6fd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public class OracleCloudCoreFactory {

public static final String OKE_WORKLOAD_IDENTITY_PREFIX = OracleCloudCoreFactory.ORACLE_CLOUD + ".config.oke-workload-identity";

private OracleCloudConfigFileConfigurationProperties ociConfigFileConfiguration;

@Inject
@Nullable
SessionKeySupplier sessionKeySupplier;

private OracleCloudConfigFileConfigurationProperties ociConfigFileConfiguration;

/**
* @param profile The configured profile
* @param configPath The configuration file path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.oracle.bmc.auth.AbstractAuthenticationDetailsProvider;
import com.oracle.bmc.auth.RegionProvider;
import com.oracle.bmc.common.ClientBuilderBase;
import com.oracle.bmc.common.RegionalClientBuilder;
import com.oracle.bmc.http.ClientConfigurator;
import com.oracle.bmc.http.client.HttpProvider;
import com.oracle.bmc.http.signing.RequestSignerFactory;
Expand All @@ -44,14 +45,23 @@ public abstract class AbstractSdkClientFactory<B extends ClientBuilderBase<B, T>
* @param clientConfiguration The client config
* @param clientConfigurator The client configurator (optional)
* @param requestSignerFactory The request signer factory (optional)
* @param regionProvider The region provider (optional)
*/
protected AbstractSdkClientFactory(
B builder,
ClientConfiguration clientConfiguration,
@Nullable ClientConfigurator clientConfigurator,
@Nullable RequestSignerFactory requestSignerFactory) {
@Nullable RequestSignerFactory requestSignerFactory,
@Nullable RegionProvider regionProvider
) {
this.builder = Objects.requireNonNull(builder, "Builder cannot be null");
builder.configuration(Objects.requireNonNull(clientConfiguration, "Client configuration cannot be null"));
if (regionProvider != null && regionProvider.getRegion() != null
&& !(regionProvider instanceof AbstractAuthenticationDetailsProvider)
&& builder instanceof RegionalClientBuilder<?, ?> regional
) {
regional.region(regionProvider.getRegion());
}
if (clientConfigurator != null) {
builder.clientConfigurator(clientConfigurator);
}
Expand All @@ -73,14 +83,12 @@ protected AbstractSdkClientFactory(
*
* @param clientBuilder The builder for client
* @param authenticationDetailsProvider The authentication details provider
* @param regionProvider The region provider
* @return The client to build
*/
protected abstract @NonNull T build(
@NonNull B clientBuilder,
@NonNull AbstractAuthenticationDetailsProvider authenticationDetailsProvider,
@NonNull RegionProvider regionProvider
);
@NonNull AbstractAuthenticationDetailsProvider authenticationDetailsProvider
);

/**
* Set the HTTP provider for this client. This is injected by the application context, in order
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.micronaut.oraclecloud.client

import com.oracle.bmc.Service
import com.oracle.bmc.Services
import com.oracle.bmc.auth.AuthenticationDetailsProvider
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider
import com.oracle.bmc.auth.RegionProvider
Expand Down Expand Up @@ -92,7 +94,11 @@ class OciMonitoringSpec extends Specification {
}

MonitoringClient createTelemetryClient() {
String ingestionEndpoint = regionProvider.getRegion().getEndpoint(MonitoringClient.SERVICE).orElseThrow()
Service service = Services.serviceBuilder().serviceName("MONITORING")
.serviceEndpointPrefix("telemetry-ingestion")
.serviceEndpointTemplate("https://telemetry-ingestion.{region}.{secondLevelDomain}")
.build()
String ingestionEndpoint = regionProvider.getRegion().getEndpoint(service).orElseThrow()
return MonitoringClient.builder().endpoint(ingestionEndpoint).build(authenticationDetailsProvider)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ public class OracleCloudSdkProcessor extends AbstractProcessor {
*/
public static final String OCI_SDK_CLIENT_CLASSES_OPTION = "ociSdkClientClasses";

private static final String RETURN_BUILDER_STATEMENT_WITH_REGION = "return regionProvider.getRegion() != null ? clientBuilder.region(regionProvider.getRegion()).build(authenticationDetailsProvider) : clientBuilder.build(authenticationDetailsProvider)";
private static final String RETURN_BUILDER_STATEMENT_WITHOUT_REGION = "return clientBuilder.build(authenticationDetailsProvider)";
private static final List<String> FACTORIES_THAT_DOESNT_SUPPORT_REGION = List.of("KmsCrypto", "KmsManagement", "IdentityDomains", "Stream");
private static final String RETURN_BUILDER = "return clientBuilder.build(authenticationDetailsProvider)";

private Filer filer;
private Messager messager;
Expand Down Expand Up @@ -364,7 +362,6 @@ private String writeClientFactory(Element e, String packageName, String simpleNa
builder.addField(FieldSpec.builder(builderType, "builder", Modifier.PRIVATE).build());
builder.addAnnotation(Factory.class);
final ClassName authProviderType = ClassName.get("com.oracle.bmc.auth", "AbstractAuthenticationDetailsProvider");
final ClassName regionProvider = ClassName.get("com.oracle.bmc.auth", "RegionProvider");
final AnnotationSpec.Builder requiresSpec = AnnotationSpec.builder(Requires.class)
.addMember("classes", simpleName + ".class")
.addMember("beans", authProviderType.canonicalName() + ".class");
Expand Down Expand Up @@ -396,12 +393,11 @@ private String writeClientFactory(Element e, String packageName, String simpleNa
buildMethod.returns(ClassName.get(packageName, simpleName))
.addParameter(builderType, "clientBuilder")
.addParameter(authProviderType, "authenticationDetailsProvider")
.addParameter(regionProvider, "regionProvider")
.addAnnotation(Singleton.class)
.addAnnotation(Singleton.class)
.addAnnotation(requiresSpec.build())
.addAnnotation(preDestroy.build())
.addModifiers(Modifier.PROTECTED)
.addStatement(FACTORIES_THAT_DOESNT_SUPPORT_REGION.stream().noneMatch(factoryName::startsWith) ? RETURN_BUILDER_STATEMENT_WITH_REGION : RETURN_BUILDER_STATEMENT_WITHOUT_REGION);
.addStatement(RETURN_BUILDER);
if (isBootstrapCompatible) {
buildMethod.addAnnotation(BootstrapContextCompatible.class);
}
Expand Down Expand Up @@ -440,8 +436,10 @@ private MethodSpec.Builder buildConstructor(String simpleName, TypeSpec.Builder
.addAnnotation(Nullable.class).build())
.addParameter(ParameterSpec.builder(ClassName.get("com.oracle.bmc.http.signing", "RequestSignerFactory"), "requestSignerFactory")
.addAnnotation(Nullable.class).build())
.addParameter(ParameterSpec.builder(ClassName.get("com.oracle.bmc.auth", "RegionProvider"), "regionProvider")
.addAnnotation(Nullable.class).build())
.addCode(CodeBlock.builder()
.addStatement("super(" + simpleName + ".builder(), clientConfiguration, clientConfigurator, requestSignerFactory)")
.addStatement("super(" + simpleName + ".builder(), clientConfiguration, clientConfigurator, requestSignerFactory, regionProvider)")
.addStatement("builder = super.getBuilder()").build());
builder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
return constructor;
Expand Down

0 comments on commit b4da6fd

Please sign in to comment.