Skip to content

Commit

Permalink
Rename members and methods related to parent project id
Browse files Browse the repository at this point in the history
  • Loading branch information
pajaks committed Aug 16, 2024
1 parent af646cb commit 0542332
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ public class CredentialsOptionsConfigurer
implements BigQueryOptionsConfigurer
{
private final BigQueryCredentialsSupplier credentialsSupplier;
private final Optional<String> parentProjectId;
private final Optional<String> configParentProjectId;

@Inject
public CredentialsOptionsConfigurer(BigQueryConfig bigQueryConfig, BigQueryCredentialsSupplier credentialsSupplier)
{
this.parentProjectId = bigQueryConfig.getParentProjectId();
this.configParentProjectId = bigQueryConfig.getParentProjectId();
this.credentialsSupplier = requireNonNull(credentialsSupplier, "credentialsSupplier is null");
}

@Override
public BigQueryOptions.Builder configure(BigQueryOptions.Builder builder, ConnectorSession session)
{
Optional<Credentials> credentials = credentialsSupplier.getCredentials(session);
String billingProjectId = calculateBillingProjectId(parentProjectId, credentials);
String parentProjectId = resolveProjectId(configParentProjectId, credentials);
credentials.ifPresent(builder::setCredentials);
builder.setProjectId(billingProjectId);
builder.setProjectId(parentProjectId);
return builder;
}

Expand All @@ -69,10 +69,10 @@ public BigQueryWriteSettings.Builder configure(BigQueryWriteSettings.Builder bui

// Note that at this point the config has been validated, which means that option 2 or option 3 will always be valid
@VisibleForTesting
static String calculateBillingProjectId(Optional<String> configParentProjectId, Optional<Credentials> credentials)
static String resolveProjectId(Optional<String> configProjectId, Optional<Credentials> credentials)
{
// 1. Get from configuration
return configParentProjectId
return configProjectId
// 2. Get from the provided credentials, but only ServiceAccountCredentials contains the project id.
// All other credentials types (User, AppEngine, GCE, CloudShell, etc.) take it from the environment
.orElseGet(() -> credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@
import java.io.IOException;
import java.util.Optional;

import static io.trino.plugin.bigquery.CredentialsOptionsConfigurer.calculateBillingProjectId;
import static io.trino.plugin.bigquery.CredentialsOptionsConfigurer.resolveProjectId;
import static org.assertj.core.api.Assertions.assertThat;

public class TestCredentialsOptionsConfigurer
{
@Test
public void testConfigurationOnly()
{
String projectId = calculateBillingProjectId(Optional.of("pid"), Optional.empty());
String projectId = resolveProjectId(Optional.of("pid"), Optional.empty());
assertThat(projectId).isEqualTo("pid");
}

@Test
public void testCredentialsOnly()
throws Exception
{
String projectId = calculateBillingProjectId(Optional.empty(), credentials());
String projectId = resolveProjectId(Optional.empty(), credentials());
assertThat(projectId).isEqualTo("presto-bq-credentials-test");
}

@Test
public void testBothConfigurationAndCredentials()
throws Exception
{
String projectId = calculateBillingProjectId(Optional.of("pid"), credentials());
String projectId = resolveProjectId(Optional.of("pid"), credentials());
assertThat(projectId).isEqualTo("pid");
}

Expand Down

0 comments on commit 0542332

Please sign in to comment.