From 510a79642240deab9c746538cc903e7e938db44b Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Tue, 1 Jun 2021 15:14:06 +0200 Subject: [PATCH] Factory for Service Discovery Client and Builder (#1086) Co-authored-by: Denis Stepanov --- aws-sdk-v2/build.gradle | 2 + .../ServiceDiscoveryAsyncClientFactory.java | 93 +++++++++++++++++++ .../servicediscovery/package-info.java | 28 ++++++ .../service/ServiceDiscoveryClientSpec.groovy | 36 +++++++ .../docs/guide/sdkv2/servicediscovery.adoc | 15 +++ src/main/docs/guide/toc.yml | 1 + 6 files changed, 175 insertions(+) create mode 100644 aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/ServiceDiscoveryAsyncClientFactory.java create mode 100644 aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/package-info.java create mode 100644 aws-sdk-v2/src/test/groovy/io/micronaut/aws/sdk/v2/service/ServiceDiscoveryClientSpec.groovy create mode 100644 src/main/docs/guide/sdkv2/servicediscovery.adoc diff --git a/aws-sdk-v2/build.gradle b/aws-sdk-v2/build.gradle index bfb87ea4dc..4107c182b4 100644 --- a/aws-sdk-v2/build.gradle +++ b/aws-sdk-v2/build.gradle @@ -17,8 +17,10 @@ dependencies { compileOnly "software.amazon.awssdk:sqs" compileOnly "software.amazon.awssdk:ssm" compileOnly 'software.amazon.awssdk:secretsmanager' + compileOnly 'software.amazon.awssdk:servicediscovery' // Tests + testImplementation 'software.amazon.awssdk:servicediscovery' testImplementation "software.amazon.awssdk:url-connection-client" testImplementation "software.amazon.awssdk:netty-nio-client" testImplementation "software.amazon.awssdk:apache-client" diff --git a/aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/ServiceDiscoveryAsyncClientFactory.java b/aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/ServiceDiscoveryAsyncClientFactory.java new file mode 100644 index 0000000000..2bc9c9ce42 --- /dev/null +++ b/aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/ServiceDiscoveryAsyncClientFactory.java @@ -0,0 +1,93 @@ +/* + * Copyright 2017-2020 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micronaut.aws.sdk.v2.service.servicediscovery; + +import io.micronaut.aws.sdk.v2.service.AwsClientFactory; +import io.micronaut.context.annotation.Bean; +import io.micronaut.context.annotation.Factory; +import io.micronaut.context.annotation.Requires; +import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain; +import software.amazon.awssdk.http.SdkHttpClient; +import software.amazon.awssdk.http.async.SdkAsyncHttpClient; +import software.amazon.awssdk.regions.providers.AwsRegionProviderChain; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClientBuilder; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClientBuilder; + +import javax.inject.Singleton; + +/** + * Factory that creates service discovery clients. + * + * @author Denis Stepanov + */ +@Factory +public class ServiceDiscoveryAsyncClientFactory extends AwsClientFactory { + + /** + * Constructor. + * + * @param credentialsProvider The credentials provider + * @param regionProvider The region provider + */ + public ServiceDiscoveryAsyncClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) { + super(credentialsProvider, regionProvider); + } + + @Override + protected ServiceDiscoveryClientBuilder createSyncBuilder() { + return ServiceDiscoveryClient.builder(); + } + + @Override + protected ServiceDiscoveryAsyncClientBuilder createAsyncBuilder() { + return ServiceDiscoveryAsyncClient.builder(); + } + + @Requires(missingBeans = ServiceDiscoveryClientBuilder.class) + @Override + @Singleton + public ServiceDiscoveryClientBuilder syncBuilder(SdkHttpClient httpClient) { + return super.syncBuilder(httpClient); + } + + @Requires(missingBeans = ServiceDiscoveryClient.class) + @Override + @Bean(preDestroy = "close") + @Singleton + public ServiceDiscoveryClient syncClient(ServiceDiscoveryClientBuilder builder) { + return super.syncClient(builder); + } + + @Requires(missingBeans = ServiceDiscoveryAsyncClientBuilder.class) + @Override + @Singleton + @Requires(beans = SdkAsyncHttpClient.class) + public ServiceDiscoveryAsyncClientBuilder asyncBuilder(SdkAsyncHttpClient httpClient) { + return super.asyncBuilder(httpClient); + } + + @Requires(missingBeans = ServiceDiscoveryAsyncClient.class) + @Override + @Bean(preDestroy = "close") + @Singleton + @Requires(beans = SdkAsyncHttpClient.class) + public ServiceDiscoveryAsyncClient asyncClient(ServiceDiscoveryAsyncClientBuilder builder) { + return super.asyncClient(builder); + } +} diff --git a/aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/package-info.java b/aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/package-info.java new file mode 100644 index 0000000000..0270bb9922 --- /dev/null +++ b/aws-sdk-v2/src/main/java/io/micronaut/aws/sdk/v2/service/servicediscovery/package-info.java @@ -0,0 +1,28 @@ +/* + * Copyright 2017-2020 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Service discovery client configuration and factory. + * + * @author Denis Stepanov + */ +@Requires(classes = {ServiceDiscoveryClient.class, ServiceDiscoveryAsyncClient.class}) +@Configuration +package io.micronaut.aws.sdk.v2.service.servicediscovery; + +import io.micronaut.context.annotation.Configuration; +import io.micronaut.context.annotation.Requires; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient; diff --git a/aws-sdk-v2/src/test/groovy/io/micronaut/aws/sdk/v2/service/ServiceDiscoveryClientSpec.groovy b/aws-sdk-v2/src/test/groovy/io/micronaut/aws/sdk/v2/service/ServiceDiscoveryClientSpec.groovy new file mode 100644 index 0000000000..bbebe91e75 --- /dev/null +++ b/aws-sdk-v2/src/test/groovy/io/micronaut/aws/sdk/v2/service/ServiceDiscoveryClientSpec.groovy @@ -0,0 +1,36 @@ +package io.micronaut.aws.sdk.v2.service; + +import io.micronaut.aws.sdk.v2.ApplicationContextSpecification; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClientBuilder; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClientBuilder; + +class ServiceDiscoveryClientSpec extends ApplicationContextSpecification { + + void "it can configure a service discovery client"() { + when: + ServiceDiscoveryClient client = applicationContext.getBean(ServiceDiscoveryClient) + + then: + client.serviceName() == ServiceDiscoveryClient.SERVICE_NAME + } + + void "it can configure an async service discovery client"() { + when: + ServiceDiscoveryAsyncClient client = applicationContext.getBean(ServiceDiscoveryAsyncClient) + + then: + client.serviceName() == ServiceDiscoveryClient.SERVICE_NAME + } + + void "it can configure an async service discovery client Builder"() { + expect: + applicationContext.getBean(ServiceDiscoveryAsyncClientBuilder) + } + + void "it can configure a service discovery client Builder"() { + expect: + applicationContext.getBean(ServiceDiscoveryClientBuilder) + } +} diff --git a/src/main/docs/guide/sdkv2/servicediscovery.adoc b/src/main/docs/guide/sdkv2/servicediscovery.adoc new file mode 100644 index 0000000000..064723e4c2 --- /dev/null +++ b/src/main/docs/guide/sdkv2/servicediscovery.adoc @@ -0,0 +1,15 @@ +To use Service discovery, the AWS Java SDK for Amazon Route 53 Auto Naming module, add the following dependency: + +dependency:servicediscovery[groupId="software.amazon.awssdk"] + +Then, the following beans will be created: + +* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClientBuilder` +* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient` + +And: + +* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient` +* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClientBuilder`. + +The HTTP client, credentials and region will be configured as per described in the <>. \ No newline at end of file diff --git a/src/main/docs/guide/toc.yml b/src/main/docs/guide/toc.yml index b61fa6fbb5..f0e7836840 100644 --- a/src/main/docs/guide/toc.yml +++ b/src/main/docs/guide/toc.yml @@ -21,6 +21,7 @@ sdkv2: sqs: SQS ssm: SSM secretsmanager: Secrets Manager + servicediscovery: Service discovery advancedConfiguration: Advanced configuration otherServices: Other services lambda: