From f124ecafa946fb9c9b389f367b4ce8a848f6bf23 Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Thu, 12 Dec 2024 16:43:09 +0100 Subject: [PATCH 1/3] Region provider (#1040) * consider region provder in client builders when possible --- .../core/sdk/AbstractSdkClientFactory.java | 8 +++-- .../factory/ObjectStorageFactorySpec.groovy | 25 ++++++++++++-- .../httpclient/netty/NettyManagedTest.java | 10 ++++++ .../OracleCloudRawMeterRegistry.java | 2 +- .../sdk/MonitorRegionProviderSpec.groovy | 34 +++++++++++++++++++ .../sdk/VaultRegionProviderMock.java | 21 ++++++++++++ .../processor/OracleCloudSdkProcessor.java | 12 +++++-- .../vault/OracleCloudVaultConfiguration.java | 1 - 8 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/MonitorRegionProviderSpec.groovy create mode 100644 oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/VaultRegionProviderMock.java diff --git a/oraclecloud-common/src/main/java/io/micronaut/oraclecloud/core/sdk/AbstractSdkClientFactory.java b/oraclecloud-common/src/main/java/io/micronaut/oraclecloud/core/sdk/AbstractSdkClientFactory.java index b48bd5c2f..c291099af 100644 --- a/oraclecloud-common/src/main/java/io/micronaut/oraclecloud/core/sdk/AbstractSdkClientFactory.java +++ b/oraclecloud-common/src/main/java/io/micronaut/oraclecloud/core/sdk/AbstractSdkClientFactory.java @@ -17,6 +17,7 @@ import com.oracle.bmc.ClientConfiguration; import com.oracle.bmc.auth.AbstractAuthenticationDetailsProvider; +import com.oracle.bmc.auth.RegionProvider; import com.oracle.bmc.common.ClientBuilderBase; import com.oracle.bmc.http.ClientConfigurator; import com.oracle.bmc.http.client.HttpProvider; @@ -33,6 +34,7 @@ * @param The builder type * @param The client type */ +@Internal public abstract class AbstractSdkClientFactory, T> { private final B builder; @@ -71,12 +73,14 @@ 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 AbstractAuthenticationDetailsProvider authenticationDetailsProvider, + @NonNull RegionProvider regionProvider + ); /** * Set the HTTP provider for this client. This is injected by the application context, in order diff --git a/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/factory/ObjectStorageFactorySpec.groovy b/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/factory/ObjectStorageFactorySpec.groovy index 0da874533..159fd565a 100644 --- a/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/factory/ObjectStorageFactorySpec.groovy +++ b/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/factory/ObjectStorageFactorySpec.groovy @@ -1,7 +1,14 @@ -package io.micronaut.oraclecloud.factory; +package io.micronaut.oraclecloud.factory +import com.oracle.bmc.Region +import com.oracle.bmc.auth.RegionProvider; import com.oracle.bmc.objectstorage.ObjectStorageAsyncClient import com.oracle.bmc.objectstorage.ObjectStorageClient +import io.micronaut.context.annotation.BootstrapContextCompatible +import io.micronaut.context.annotation.Primary +import io.micronaut.context.annotation.Property +import io.micronaut.context.annotation.Replaces +import io.micronaut.context.annotation.Requires import io.micronaut.context.event.BeanCreatedEvent import io.micronaut.context.event.BeanCreatedEventListener import io.micronaut.core.annotation.NonNull @@ -10,7 +17,7 @@ import jakarta.inject.Inject import jakarta.inject.Singleton import spock.lang.Specification - +@Property(name = "spec.name", value = "ObjectStorageFactorySpec") @MicronautTest class ObjectStorageFactorySpec extends Specification { @@ -41,6 +48,7 @@ class ObjectStorageFactorySpec extends Specification { asyncClient.endpoint == ASYNC_ENDPOINT } + @Requires(property = "spec.name", value = "ObjectStorageFactorySpec") @Singleton static class DatabaseClientBuilderListener implements BeanCreatedEventListener { @@ -54,6 +62,7 @@ class ObjectStorageFactorySpec extends Specification { } } + @Requires(property = "spec.name", value = "ObjectStorageFactorySpec") @Singleton static class DatabaseAsyncClientBuilderListener implements BeanCreatedEventListener { @@ -67,4 +76,16 @@ class ObjectStorageFactorySpec extends Specification { } } + @Requires(property = "spec.name", value = "ObjectStorageFactorySpec") + @Singleton + @BootstrapContextCompatible + @Replaces(RegionProvider.class) + static class RegionProviderReplacement implements RegionProvider { + + @Override + Region getRegion() { + return null + } + } + } diff --git a/oraclecloud-httpclient-netty/src/test/java/io/micronaut/oraclecloud/httpclient/netty/NettyManagedTest.java b/oraclecloud-httpclient-netty/src/test/java/io/micronaut/oraclecloud/httpclient/netty/NettyManagedTest.java index 135368754..17f3031d3 100644 --- a/oraclecloud-httpclient-netty/src/test/java/io/micronaut/oraclecloud/httpclient/netty/NettyManagedTest.java +++ b/oraclecloud-httpclient-netty/src/test/java/io/micronaut/oraclecloud/httpclient/netty/NettyManagedTest.java @@ -2,8 +2,11 @@ import com.oracle.bmc.http.client.HttpProvider; import io.micronaut.context.ApplicationContext; +import io.micronaut.core.util.StringUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; public class NettyManagedTest extends NettyUnmanagedTest { ApplicationContext ctx; @@ -22,4 +25,11 @@ public void tearDown() { HttpProvider provider() { return ctx.getBean(HttpProvider.class); } + + @DisabledIfEnvironmentVariable(named = "CI", matches = StringUtils.TRUE, + disabledReason = "It is flaky https://ge.micronaut.io/scans/tests?tests.container=io.micronaut.oraclecloud.httpclient.netty.NettyManagedTest&tests.test=connectionReuse()") + @Test + public void connectionReuse() throws Exception { + super.connectionReuse(); + } } diff --git a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudRawMeterRegistry.java b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudRawMeterRegistry.java index 3d78de9cf..3a50d522e 100644 --- a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudRawMeterRegistry.java +++ b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudRawMeterRegistry.java @@ -212,7 +212,7 @@ Stream trackRawData(Meter meter) { if (meter instanceof OracleCloudDatapointProducer oracleCloudDatapointProducer) { return Stream.of(metricDataDetails(meter.getId(), null, oracleCloudDatapointProducer.getDatapoints())); } - logger.error("Metrics name: {}. Haven't publish metrics for class: {}",meter.getId(), meter.getClass()); + logger.error("Metrics name: {}. Haven't publish metrics for class: {}", meter.getId(), meter.getClass()); return Stream.empty(); } diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/MonitorRegionProviderSpec.groovy b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/MonitorRegionProviderSpec.groovy new file mode 100644 index 000000000..d59b480c0 --- /dev/null +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/MonitorRegionProviderSpec.groovy @@ -0,0 +1,34 @@ +package io.micronaut.oraclecloud.monitoring.sdk + +import com.oracle.bmc.Region +import com.oracle.bmc.auth.AbstractAuthenticationDetailsProvider +import com.oracle.bmc.monitoring.Monitoring +import com.oracle.bmc.monitoring.MonitoringClient +import io.micronaut.context.annotation.Property +import io.micronaut.context.annotation.Requires +import io.micronaut.test.extensions.spock.annotation.MicronautTest +import jakarta.inject.Inject +import org.junit.jupiter.api.Assertions +import spock.lang.Specification + +@MicronautTest(startApplication = false) +@Property(name = "spec.name", value = "MonitorRegionProviderSpec") +@Property(name = "micronaut.metrics.export.oraclecloud.enabled", value = "false") +@Property(name = "oci.vault.config.enabled", value = "false") +@Property(name = "micronaut.config-client.enabled", value = "false") +@Property(name = "datasources.enabled", value = "false") +@Requires(bean = AbstractAuthenticationDetailsProvider.class) +class MonitorRegionProviderSpec extends Specification { + + @Inject + Monitoring monitoring + + void 'test the' () { + when: + String endpoint = monitoring.getEndpoint() + + then: + Region.EU_JOVANOVAC_1.getEndpoint(MonitoringClient.SERVICE).get() == endpoint + } + +} diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/VaultRegionProviderMock.java b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/VaultRegionProviderMock.java new file mode 100644 index 000000000..30fcad34e --- /dev/null +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/sdk/VaultRegionProviderMock.java @@ -0,0 +1,21 @@ +package io.micronaut.oraclecloud.monitoring.sdk; + +import com.oracle.bmc.Region; +import com.oracle.bmc.auth.RegionProvider; +import io.micronaut.context.annotation.BootstrapContextCompatible; +import io.micronaut.context.annotation.Primary; +import io.micronaut.context.annotation.Property; +import io.micronaut.context.annotation.Replaces; +import io.micronaut.context.annotation.Requires; +import jakarta.inject.Singleton; + +@Replaces(RegionProvider.class) +@BootstrapContextCompatible +@Singleton +@Requires(property = "spec.name", value = "MonitorRegionProviderSpec") +public class VaultRegionProviderMock implements RegionProvider { + @Override + public Region getRegion() { + return Region.EU_JOVANOVAC_1; + } +} diff --git a/oraclecloud-sdk-processor/src/main/java/io/micronaut/oraclecloud/clients/processor/OracleCloudSdkProcessor.java b/oraclecloud-sdk-processor/src/main/java/io/micronaut/oraclecloud/clients/processor/OracleCloudSdkProcessor.java index f15d3957e..8513fec10 100644 --- a/oraclecloud-sdk-processor/src/main/java/io/micronaut/oraclecloud/clients/processor/OracleCloudSdkProcessor.java +++ b/oraclecloud-sdk-processor/src/main/java/io/micronaut/oraclecloud/clients/processor/OracleCloudSdkProcessor.java @@ -92,6 +92,11 @@ public class OracleCloudSdkProcessor extends AbstractProcessor { * Processing environment option used to specify the client classes to process. */ 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 FACTORIES_THAT_DOESNT_SUPPORT_REGION = List.of("KmsCrypto", "KmsManagement", "IdentityDomains", "Stream"); + private Filer filer; private Messager messager; private Elements elements; @@ -353,11 +358,13 @@ private String writeClientFactory(Element e, String packageName, String simpleNa final String factoryName = simpleName + "Factory"; final String factoryPackageName = packageName.replace("com.oracle.bmc", CLIENT_PACKAGE); final TypeSpec.Builder builder = defineSuperclass(packageName, simpleName, factoryName); + final MethodSpec.Builder constructor = buildConstructor(simpleName, builder); ClassName builderType = getBuilderClassNameForClient(packageName, simpleName); 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"); @@ -389,11 +396,12 @@ private String writeClientFactory(Element e, String packageName, String simpleNa buildMethod.returns(ClassName.get(packageName, simpleName)) .addParameter(builderType, "clientBuilder") .addParameter(authProviderType, "authenticationDetailsProvider") - .addAnnotation(Singleton.class) + .addParameter(regionProvider, "regionProvider") + .addAnnotation(Singleton.class) .addAnnotation(requiresSpec.build()) .addAnnotation(preDestroy.build()) .addModifiers(Modifier.PROTECTED) - .addCode("return clientBuilder.build(authenticationDetailsProvider);"); + .addStatement(FACTORIES_THAT_DOESNT_SUPPORT_REGION.stream().noneMatch(factoryName::startsWith) ? RETURN_BUILDER_STATEMENT_WITH_REGION : RETURN_BUILDER_STATEMENT_WITHOUT_REGION); if (isBootstrapCompatible) { buildMethod.addAnnotation(BootstrapContextCompatible.class); } diff --git a/oraclecloud-vault/src/main/java/io/micronaut/oraclecloud/discovery/vault/OracleCloudVaultConfiguration.java b/oraclecloud-vault/src/main/java/io/micronaut/oraclecloud/discovery/vault/OracleCloudVaultConfiguration.java index 330d8a3c1..f06c266a3 100644 --- a/oraclecloud-vault/src/main/java/io/micronaut/oraclecloud/discovery/vault/OracleCloudVaultConfiguration.java +++ b/oraclecloud-vault/src/main/java/io/micronaut/oraclecloud/discovery/vault/OracleCloudVaultConfiguration.java @@ -25,7 +25,6 @@ import io.micronaut.context.annotation.BootstrapContextCompatible; import io.micronaut.context.annotation.ConfigurationProperties; import io.micronaut.context.annotation.EachProperty; -import io.micronaut.context.annotation.Property; import io.micronaut.core.annotation.NonNull; import io.micronaut.core.util.StringUtils; import io.micronaut.discovery.config.ConfigDiscoveryConfiguration; From fdd9fb20f80495a68848bc3fe4df6270c414713d Mon Sep 17 00:00:00 2001 From: micronaut-build Date: Thu, 12 Dec 2024 15:44:54 +0000 Subject: [PATCH 2/3] [skip ci] Release v4.3.4 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index d990b7a24..b4318a440 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -projectVersion=4.3.4-SNAPSHOT +projectVersion=4.3.4 projectGroup=io.micronaut.oraclecloud title=Micronaut Oracle Cloud From c858304117fc5c55c76be95993e6cbd08c14d097 Mon Sep 17 00:00:00 2001 From: micronaut-build Date: Thu, 12 Dec 2024 16:11:51 +0000 Subject: [PATCH 3/3] chore: Bump version to 4.3.5-SNAPSHOT --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index b4318a440..b953cb88d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -projectVersion=4.3.4 +projectVersion=4.3.5-SNAPSHOT projectGroup=io.micronaut.oraclecloud title=Micronaut Oracle Cloud