Skip to content

Commit

Permalink
expose host instead of endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeddada1 committed Oct 4, 2024
1 parent 061be96 commit 33872a6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 52 deletions.
2 changes: 2 additions & 0 deletions docs/src/main/asciidoc/storage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ The Spring Boot Starter for Google Cloud Storage provides the following configur
Base64-encoded contents of OAuth2 account private key for authenticating with the Google Cloud Storage API, if different from the ones in the <<spring-cloud-gcp-core,Spring Framework on Google Cloud Core Module>> | No |
| `spring.cloud.gcp.storage.credentials.scopes` |
https://developers.google.com/identity/protocols/googlescopes[OAuth2 scope] for Spring Framework on Google Cloud Storage credentials | No | https://www.googleapis.com/auth/devstorage.read_write
| `spring.cloud.gcp.bigquery.universe-domain` | Universe domain of the Storage service. The universe domain is a part of the host that is formatted as https://${service}.${universeDomain} | No | Relies on client library’s default universe domain which is googleapis.com
| `spring.cloud.gcp.bigquery.host` | Host of the Storage service which is formatted as https://${service}.${universeDomain}. | No | Relies on client library’s default host which is `https://storage.googleapis.com`
|===


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class GcpStorageAutoConfiguration { // NOSONAR squid:S1610 must be a clas

private final String universeDomain;

private final String endpoint;
private final String host;

public GcpStorageAutoConfiguration(
GcpProjectIdProvider coreProjectIdProvider,
Expand All @@ -73,31 +73,24 @@ public GcpStorageAutoConfiguration(
: credentialsProvider;

this.universeDomain = gcpStorageProperties.getUniverseDomain();
this.endpoint = gcpStorageProperties.getEndpoint();
this.host = gcpStorageProperties.getHost();
}

@Bean
@ConditionalOnMissingBean
public Storage storage() throws IOException {
StorageOptions.Builder storageOptionsBuilder = StorageOptions.newBuilder()
.setHeaderProvider(new UserAgentHeaderProvider(GcpStorageAutoConfiguration.class))
.setProjectId(this.gcpProjectIdProvider.getProjectId())
.setCredentials(this.credentialsProvider.getCredentials());
StorageOptions.Builder storageOptionsBuilder =
StorageOptions.newBuilder()
.setHeaderProvider(new UserAgentHeaderProvider(GcpStorageAutoConfiguration.class))
.setProjectId(this.gcpProjectIdProvider.getProjectId())
.setCredentials(this.credentialsProvider.getCredentials());

if (this.universeDomain != null){
if (this.universeDomain != null) {
storageOptionsBuilder.setUniverseDomain(this.universeDomain);
}
if (this.endpoint != null){
storageOptionsBuilder.setHost(resolveToHost(this.endpoint));
if (this.host != null) {
storageOptionsBuilder.setHost(this.host);
}
return storageOptionsBuilder.build().getService();
}

private String resolveToHost(String endpoint) {
int portIndex = endpoint.indexOf(":");
if (portIndex != -1) {
return "https://" + endpoint.substring(0, portIndex) + "/";
}
return "https://" + endpoint + "/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ public Credentials getCredentials() {
}

private String projectId;


/**
* Universe domain of the client which is part of the host that is formatted as
* `https://${service}.${universeDomain}.
*/
private String universeDomain;
private String endpoint;


/** Host of the Storage client that is formatted as `https://${service}.${universeDomain}. */
private String host;

public String getProjectId() {
return projectId;
Expand All @@ -59,11 +63,11 @@ public void setUniverseDomain(String universeDomain) {
this.universeDomain = universeDomain;
}

public String getEndpoint() {
return endpoint;
public String getHost() {
return host;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
public void setHost(String host) {
this.host = host;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,29 @@ class GcpStorageAutoConfigurationTests {
.withUserConfiguration(TestConfiguration.class);

@Test
void testValidObject() throws Exception {
void testValidObject() {
this.contextRunner
.withUserConfiguration(TestStorageConfiguration.class).run(
context -> {
Resource resource = context.getBean("mockResource", Resource.class);
assertThat(resource.contentLength()).isEqualTo(4096);
});
.withUserConfiguration(TestStorageConfiguration.class)
.run(
context -> {
Resource resource = context.getBean("mockResource", Resource.class);
assertThat(resource.contentLength()).isEqualTo(4096);
});
}

@Test
void testAutoCreateFilesTrueByDefault() throws IOException {
void testAutoCreateFilesTrueByDefault() {
this.contextRunner
.withUserConfiguration(TestStorageConfiguration.class).run(
context -> {
Resource resource = context.getBean("mockResource", Resource.class);
assertThat(((GoogleStorageResource) resource).isAutoCreateFiles()).isTrue();
});
.withUserConfiguration(TestStorageConfiguration.class)
.run(
context -> {
Resource resource = context.getBean("mockResource", Resource.class);
assertThat(((GoogleStorageResource) resource).isAutoCreateFiles()).isTrue();
});
}

@Test
void testAutoCreateFilesRespectsProperty() {

this.contextRunner
.withUserConfiguration(TestStorageConfiguration.class)
.withPropertyValues("spring.cloud.gcp.storage.auto-create-files=false")
Expand All @@ -93,9 +94,9 @@ void testUniverseDomain() {
}

@Test
void testEndpoint() {
void testHost() {
this.contextRunner
.withPropertyValues("spring.cloud.gcp.storage.endpoint=storage.example.com")
.withPropertyValues("spring.cloud.gcp.storage.host=https://storage.example.com/")
.run(
context -> {
Storage storage = context.getBean("storage", Storage.class);
Expand All @@ -104,10 +105,11 @@ void testEndpoint() {
}

@Test
void testUniverseDomainAndEndpointSet() {
void testUniverseDomainAndHostSet() {
this.contextRunner
.withPropertyValues("spring.cloud.gcp.storage.universe-domain=example.com",
"spring.cloud.gcp.storage.endpoint=storage.example.com")
.withPropertyValues(
"spring.cloud.gcp.storage.universe-domain=example.com",
"spring.cloud.gcp.storage.host=https://storage.example.com/")
.run(
context -> {
Storage storage = context.getBean("storage", Storage.class);
Expand All @@ -117,17 +119,15 @@ void testUniverseDomainAndEndpointSet() {
}

@Test
void testNoUniverseDomainAndEndpointSet_useDefaults() {
this.contextRunner
.run(
context -> {
Storage storage = context.getBean("storage", Storage.class);
assertThat(storage.getOptions().getUniverseDomain()).isNull();
assertThat(storage.getOptions().getHost()).isEqualTo("https://storage.googleapis.com/");
});
void testNoUniverseDomainOrHostSet_useDefaults() {
this.contextRunner.run(
context -> {
Storage storage = context.getBean("storage", Storage.class);
assertThat(storage.getOptions().getUniverseDomain()).isNull();
assertThat(storage.getOptions().getHost()).isEqualTo("https://storage.googleapis.com/");
});
}


@Configuration
static class TestConfiguration {

Expand Down

0 comments on commit 33872a6

Please sign in to comment.