Skip to content

Commit

Permalink
Use default region provider chain (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
singhbaljit authored Aug 28, 2023
1 parent b21541f commit f93442a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.EnumUtils;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
import software.amazon.awssdk.services.glue.model.Compatibility;

import java.net.URI;
Expand Down Expand Up @@ -150,7 +152,15 @@ private void validateAndSetAWSRegion(Map<String, ?> configs) {
if (isPresent(configs, AWSSchemaRegistryConstants.AWS_REGION)) {
this.region = String.valueOf(configs.get(AWSSchemaRegistryConstants.AWS_REGION));
} else {
throw new AWSSchemaRegistryException("Region is not defined in the properties");
try {
this.region = DefaultAwsRegionProviderChain
.builder()
.build()
.getRegion()
.id();
} catch (SdkClientException ex) {
throw new AWSSchemaRegistryException("Region is not defined in the properties", ex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -103,12 +104,28 @@ public void testBuildConfig_noRegionConfigsSupplied_throwsException() {
assertEquals("Region is not defined in the properties", exception.getMessage());
}

/**
* Tests configuration for region value via default AWS region provider chain
*/
@Test
public void testBuildConfig_regionConfigsSuppliedUsingAwsProvider_thenUseDefaultAwsRegionProviderChain() {
Map<String, Object> configWithoutRegion = new HashMap<>();
configWithoutRegion.put(AWSSchemaRegistryConstants.AWS_ENDPOINT, "https://test/");
System.setProperty("aws.region", "us-west-2");

GlueSchemaRegistryConfiguration glueSchemaRegistryConfiguration = new GlueSchemaRegistryConfiguration(configWithoutRegion);

assertEquals("us-west-2", glueSchemaRegistryConfiguration.getRegion());

System.clearProperty("aws.region");
}

/**
* Tests configuration for region value
*/
@Test
public void testBuildConfig_withRegionConfig_Instantiates() {
assertNotNull(new GlueSchemaRegistryConfiguration("us-west-1"));
assertDoesNotThrow(() -> new GlueSchemaRegistryConfiguration("us-west-1"));
}

/**
Expand Down

0 comments on commit f93442a

Please sign in to comment.