-
-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autoconfigure SsmClient even if config import is not used
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...o/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2013-2024 the original author or 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.awspring.cloud.autoconfigure.config.parameterstore; | ||
|
||
import io.awspring.cloud.autoconfigure.core.AwsClientBuilderConfigurer; | ||
import io.awspring.cloud.autoconfigure.core.AwsClientCustomizer; | ||
import io.awspring.cloud.autoconfigure.core.AwsConnectionDetails; | ||
import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration; | ||
import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import software.amazon.awssdk.services.ssm.SsmClient; | ||
import software.amazon.awssdk.services.ssm.SsmClientBuilder; | ||
|
||
/** | ||
* {@link AutoConfiguration Auto-Configuration} for AWS Parameter Store integration. | ||
* | ||
* @author Oleh Onufryk | ||
* @since 3.3.0 | ||
*/ | ||
@AutoConfiguration | ||
@EnableConfigurationProperties(ParameterStoreProperties.class) | ||
@ConditionalOnClass({ SsmClient.class }) | ||
@AutoConfigureAfter({ CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class }) | ||
@ConditionalOnProperty(name = "spring.cloud.aws.parameterstore.enabled", havingValue = "true", matchIfMissing = true) | ||
public class ParameterStoreAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public SsmClient ssmClient(ParameterStoreProperties properties, | ||
AwsClientBuilderConfigurer awsClientBuilderConfigurer, | ||
ObjectProvider<AwsClientCustomizer<SsmClientBuilder>> customizers, | ||
ObjectProvider<AwsConnectionDetails> connectionDetails) { | ||
return awsClientBuilderConfigurer.configure(SsmClient.builder(), properties, connectionDetails.getIfAvailable(), | ||
customizers.getIfAvailable()).build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...spring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.awspring.cloud.autoconfigure.config.parameterstore; | ||
|
||
import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration; | ||
import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration; | ||
import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.internal.util.MockUtil; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import software.amazon.awssdk.services.ssm.SsmClient; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Tests for {@link ParameterStoreAutoConfiguration} | ||
*/ | ||
class ParameterStoreAutoConfigurationTest { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withPropertyValues("spring.cloud.aws.region.static:eu-west-1") | ||
.withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class, | ||
CredentialsProviderAutoConfiguration.class, ParameterStoreAutoConfiguration.class, | ||
AwsAutoConfiguration.class)); | ||
|
||
@Test | ||
void parameterStoreAutoConfigurationIsDisabled() { | ||
this.contextRunner.withPropertyValues("spring.cloud.aws.parameterstore.enabled:false") | ||
.run(context -> assertThat(context).doesNotHaveBean(SsmClient.class)); | ||
} | ||
|
||
@Test | ||
void parameterStoreAutoConfigurationIsEnabled() { | ||
this.contextRunner.withPropertyValues("spring.cloud.aws.parameterstore.enabled:true") | ||
.run(context -> assertThat(context).hasSingleBean(SsmClient.class)); | ||
} | ||
|
||
@Test | ||
void createsClientBeanByDefault() { | ||
this.contextRunner.run(context -> assertThat(context).hasSingleBean(SsmClient.class)); | ||
} | ||
|
||
@Test | ||
void usesCustomBeanWhenProvided() { | ||
this.contextRunner.withUserConfiguration(CustomParameterStoreConfiguration.class).run(context -> { | ||
assertThat(context).hasSingleBean(SsmClient.class); | ||
assertThat(MockUtil.isMock(context.getBean(SsmClient.class))).isTrue(); | ||
}); | ||
} | ||
|
||
@TestConfiguration | ||
static class CustomParameterStoreConfiguration { | ||
|
||
@Bean | ||
SsmClient ssmClient() { | ||
return mock(SsmClient.class); | ||
} | ||
|
||
} | ||
|
||
} |