Skip to content

Commit

Permalink
Make default AWS region configurable, address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders Leung committed Feb 3, 2021
1 parent 598bf6f commit e2e4663
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/ga4gh/drs/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import org.ga4gh.drs.configuration.DataSourceRegistry;
import org.ga4gh.drs.configuration.DrsConfig;
import org.ga4gh.drs.configuration.DrsConfigContainer;
import org.ga4gh.drs.configuration.S3;
import org.ga4gh.drs.configuration.ServerProps;
import org.ga4gh.drs.constant.DataSourceDefaults;
import org.ga4gh.drs.constant.S3Defaults;
import org.ga4gh.drs.constant.ServerPropsDefaults;
import org.ga4gh.drs.constant.ServiceInfoDefaults;
import org.ga4gh.drs.model.ServiceInfo;
Expand All @@ -31,6 +33,7 @@
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import software.amazon.awssdk.regions.Region;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -85,6 +88,10 @@ public DrsConfigContainer defaultDrsConfigContainer(

DataSourceRegistry dataSourceRegistry = drsConfigContainer.getDrsConfig().getDataSourceRegistry();
dataSourceRegistry.setDataSources(DataSourceDefaults.REGISTRY);

S3 s3 = drsConfigContainer.getDrsConfig().getS3();
s3.setRegion(S3Defaults.S3_REGION);

return drsConfigContainer;
}

Expand Down Expand Up @@ -183,4 +190,11 @@ public DataSourceLookup dataSourceLookup() {
public DrsObjectLoaderFactory drsObjectLoaderFactory() {
return new DrsObjectLoaderFactory();
}

@Bean
public Region defaultRegion(
@Qualifier(AppConfigConstants.MERGED_DRS_CONFIG_CONTAINER) DrsConfigContainer drsConfigContainer
) {
return Region.of(drsConfigContainer.getDrsConfig().getS3().getRegion());
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/ga4gh/drs/configuration/DrsConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ga4gh.drs.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.ga4gh.drs.model.ServiceInfo;

public class DrsConfig {
Expand All @@ -8,10 +9,14 @@ public class DrsConfig {
private ServiceInfo serviceInfo;
private DataSourceRegistry dataSourceRegistry;

@JsonProperty("S3")
private S3 s3;

public DrsConfig() {
serverProps = new ServerProps();
serviceInfo = new ServiceInfo();
dataSourceRegistry = new DataSourceRegistry();
s3 = new S3();
}

public void setServerProps(ServerProps serverProps) {
Expand All @@ -37,4 +42,12 @@ public void setDataSourceRegistry(DataSourceRegistry dataSourceRegistry) {
public DataSourceRegistry getDataSourceRegistry() {
return dataSourceRegistry;
}

public S3 getS3() {
return s3;
}

public void setS3(S3 S3) {
this.s3 = S3;
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/ga4gh/drs/configuration/S3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.ga4gh.drs.configuration;

public class S3 {
private String region;

public S3() {

}

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/ga4gh/drs/constant/S3Defaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.ga4gh.drs.constant;

public class S3Defaults {

public static final String S3_REGION = "us-east-1";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ga4gh.drs.utils;

import org.springframework.beans.factory.annotation.Autowired;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
Expand All @@ -10,12 +11,15 @@

public class S3ClientRegionBasedProvider {

@Autowired
private static Region DEFAULT_REGION;

public S3ClientRegionBasedProvider() {

}

private static S3Client mainClient = S3Client.builder()
.region(Region.US_EAST_1)
.region(DEFAULT_REGION)
.build();

private static Map<Region, S3Client> clients = new HashMap<>();
Expand Down Expand Up @@ -47,6 +51,6 @@ private static Region getRegion(String bucket) {
}
}
// Default US_EAST_1
return Region.US_EAST_1;
return DEFAULT_REGION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public S3DrsObjectLoader(String objectId, String objectPath) {
URI uri = URI.create(objectPath);
bucket = uri.getHost();
// Strip leading /
key = uri.getPath().substring(1);
key = uri.getPath().replaceAll("^/+$", "");
// Get client based on region
client = S3ClientRegionBasedProvider.getClient(bucket);

Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/config/test-config-00.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ drsConfig:
dataSources:
- drsIdPattern: ^testdata\.(?<acc1>.+?)\.(?<acc2>.+?)\.(?<acc3>.+?)$
objectPathTemplate: ./src/test/resources/data/{acc1}/{acc2}/{acc3}
protocol: FILE
protocol: FILE
S3:
region: us-east-1

0 comments on commit e2e4663

Please sign in to comment.