Skip to content

Commit

Permalink
Add LocalStackContainer#getEndpoint() (#7037)
Browse files Browse the repository at this point in the history
Most recent `localstack/localstack` image versions expose only one
port `4566` and services start lazily, no more `SERVICES` env var
is required. For that reason, `getEndpoint()` becomes handy.
  • Loading branch information
eddumelendez committed May 13, 2023
1 parent 5b9c639 commit e023920
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,35 @@ public URI getEndpointOverride(EnabledService service) {
}
}

/**
* Provides an endpoint to communicate with LocalStack service.
* The provided endpoint should be set in the AWS Java SDK v2 when building a client, e.g.:
* <pre><code>S3Client s3 = S3Client
.builder()
.endpointOverride(localstack.getEndpoint())
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
localstack.getAccessKey(), localstack.getSecretKey()
)))
.region(Region.of(localstack.getRegion()))
.build()
</code></pre>
* <p><strong>Please note that this method is only intended to be used for configuring AWS SDK clients
* that are running on the test host. If other containers need to call this one, they should be configured
* specifically to do so using a Docker network and appropriate addressing.</strong></p>
*
* @return an {@link URI} endpoint
*/
public URI getEndpoint() {
try {
final String address = getHost();
// resolve IP address and use that as the endpoint so that path-style access is automatically used for S3
String ipAddress = InetAddress.getByName(address).getHostAddress();
return new URI("http://" + ipAddress + ":" + getMappedPort(PORT));
} catch (UnknownHostException | URISyntaxException e) {
throw new IllegalStateException("Cannot obtain endpoint URL", e);
}
}

private int getServicePort(EnabledService service) {
return legacyMode ? service.getPort() : PORT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void s3TestOverBridgeNetwork() throws IOException {
.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getEndpoint().toString(),
localstack.getRegion()
)
)
Expand Down Expand Up @@ -115,7 +115,7 @@ public void s3TestUsingAwsSdkV2() {
// with_aws_sdk_v2 {
S3Client s3 = S3Client
.builder()
.endpointOverride(localstack.getEndpointOverride(Service.S3))
.endpointOverride(localstack.getEndpoint())
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey())
Expand All @@ -138,7 +138,7 @@ public void sqsTestOverBridgeNetwork() {
.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.SQS).toString(),
localstack.getEndpoint().toString(),
localstack.getRegion()
)
)
Expand Down Expand Up @@ -171,7 +171,7 @@ public void cloudWatchLogsTestOverBridgeNetwork() {
.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.CLOUDWATCHLOGS).toString(),
localstack.getEndpoint().toString(),
localstack.getRegion()
)
)
Expand All @@ -195,7 +195,7 @@ public void kmsKeyCreationTest() {
.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.KMS).toString(),
localstack.getEndpoint().toString(),
localstack.getRegion()
)
)
Expand Down Expand Up @@ -345,7 +345,7 @@ public static class WithRegion {
@Test
public void s3EndpointHasProperRegion() {
final AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getEndpoint().toString(),
localstack.getRegion()
);
assertThat(endpointConfiguration.getSigningRegion())
Expand All @@ -366,7 +366,7 @@ public void s3ServiceStartLazily() {
try (
S3Client s3 = S3Client
.builder()
.endpointOverride(localstack.getEndpointOverride(Service.S3))
.endpointOverride(localstack.getEndpoint())
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey())
Expand Down

0 comments on commit e023920

Please sign in to comment.