-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LocalstackAwsClientFactory to simplify creating AWS clients point…
- Loading branch information
1 parent
c826648
commit 7a4970d
Showing
6 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
28 changes: 28 additions & 0 deletions
28
...d-aws-testcontainers/src/main/java/io/awspring/cloud/testcontainers/AwsClientFactory.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,28 @@ | ||
/* | ||
* 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.testcontainers; | ||
|
||
import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; | ||
|
||
/** | ||
* Provides convenient way to construct AWS SDK clients. | ||
* | ||
* @author Maciej Walkowiak | ||
* @since 3.2.0 | ||
*/ | ||
public interface AwsClientFactory { | ||
<CLIENT, BUILDER extends AwsClientBuilder<?, CLIENT>> CLIENT create(BUILDER builder); | ||
} |
54 changes: 54 additions & 0 deletions
54
...containers/src/main/java/io/awspring/cloud/testcontainers/LocalstackAwsClientFactory.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,54 @@ | ||
/* | ||
* 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.testcontainers; | ||
|
||
import io.awspring.cloud.autoconfigure.core.AwsClientBuilderConfigurer; | ||
import io.awspring.cloud.autoconfigure.core.AwsProperties; | ||
import io.awspring.cloud.core.region.StaticRegionProvider; | ||
import org.testcontainers.containers.localstack.LocalStackContainer; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; | ||
|
||
/** | ||
* {@link AwsClientFactory} implementation that creates containers pointing to LocalStack instance running through | ||
* {@link LocalStackContainer}. | ||
* | ||
* @author Maciej Walkowiak | ||
* @since 3.2.0 | ||
*/ | ||
public class LocalstackAwsClientFactory implements AwsClientFactory { | ||
private final AwsClientBuilderConfigurer configurer; | ||
|
||
public LocalstackAwsClientFactory(LocalStackContainer localstack) { | ||
this.configurer = clientBuilderConfigurer(localstack); | ||
} | ||
|
||
@Override | ||
public <CLIENT, BUILDER extends AwsClientBuilder<?, CLIENT>> CLIENT create(BUILDER builder) { | ||
return configurer.configure(builder).build(); | ||
} | ||
|
||
private AwsClientBuilderConfigurer clientBuilderConfigurer(LocalStackContainer localstack) { | ||
AwsProperties properties = new AwsProperties(); | ||
properties.setEndpoint(localstack.getEndpoint()); | ||
|
||
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider | ||
.create(AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey())); | ||
StaticRegionProvider regionProvider = new StaticRegionProvider(localstack.getRegion()); | ||
return new AwsClientBuilderConfigurer(credentialsProvider, regionProvider, properties); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ainers/src/test/java/io/awspring/cloud/testcontainers/LocalstackAwsClientFactoryTest.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,47 @@ | ||
/* | ||
* 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.testcontainers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.containers.localstack.LocalStackContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
|
||
/** | ||
* Tests for {@link LocalstackAwsClientFactory}. | ||
* | ||
* @author Maciej Walkowiak | ||
*/ | ||
@Testcontainers | ||
class LocalstackAwsClientFactoryTest { | ||
|
||
@Container | ||
private LocalStackContainer localStackContainer = new LocalStackContainer( | ||
DockerImageName.parse("localstack/localstack:3.2.0")); | ||
|
||
@Test | ||
void createsClient() { | ||
var factory = new LocalstackAwsClientFactory(localStackContainer); | ||
try (var s3Client = factory.create(S3Client.builder())) { | ||
s3Client.createBucket(r -> r.bucket("my-bucket")); | ||
assertThat(s3Client.listBuckets().buckets()).hasSize(1); | ||
} | ||
} | ||
} |