Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for configuring admin service endpoint #251

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ class AlloyDBAdminClientFactory {

private static final String DEFAULT_ENDPOINT = "alloydb.googleapis.com:443";

static AlloyDBAdminClient create(FixedCredentialsProvider credentialsProvider)
static AlloyDBAdminClient create(FixedCredentialsProvider credentialsProvider, String endpoint)
throws IOException {
AlloyDBAdminSettings.Builder settingsBuilder = AlloyDBAdminSettings.newBuilder();

if (endpoint == null || endpoint.isEmpty()) {
endpoint = DEFAULT_ENDPOINT;
}

Map<String, String> headers =
ImmutableMap.<String, String>builder()
.put("user-agent", "alloydb-java-connector/" + Version.VERSION)
.build();

AlloyDBAdminSettings alloyDBAdminSettings =
settingsBuilder
.setEndpoint(DEFAULT_ENDPOINT)
.setEndpoint(endpoint)
.setHeaderProvider(FixedHeaderProvider.create(headers))
.setCredentialsProvider(credentialsProvider)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public class ConnectionConfig {
public static final String ALLOYDB_TARGET_PRINCIPAL = "alloydbTargetPrincipal";
public static final String ALLOYDB_DELEGATES = "alloydbDelegates";
public static final String ALLOYDB_NAMED_CONNECTION = "alloydbNamedConnection";
public static final String ALLOYDB_ADMIN_SERVICE_ENDPOINT = "alloydbAdminServiceEndpoint";
public static final String DEFAULT_NAMED_CONNECTION = "default";
private final InstanceName instanceName;
private final String namedConnection;
private final String adminServiceEndpoint;
private final String targetPrincipal;
private final List<String> delegates;

Expand All @@ -38,6 +40,8 @@ public static ConnectionConfig fromConnectionProperties(Properties props) {
final String instanceNameStr = props.getProperty(ALLOYDB_INSTANCE_NAME, "");
final InstanceName instanceName = InstanceName.parse(instanceNameStr);
final String namedConnection = props.getProperty(ConnectionConfig.ALLOYDB_NAMED_CONNECTION);
final String adminServiceEndpoint =
props.getProperty(ConnectionConfig.ALLOYDB_ADMIN_SERVICE_ENDPOINT);
final String targetPrincipal = props.getProperty(ConnectionConfig.ALLOYDB_TARGET_PRINCIPAL);
final String delegatesStr = props.getProperty(ConnectionConfig.ALLOYDB_DELEGATES);
final List<String> delegates;
Expand All @@ -47,18 +51,21 @@ public static ConnectionConfig fromConnectionProperties(Properties props) {
delegates = Collections.emptyList();
}

return new ConnectionConfig(instanceName, namedConnection, targetPrincipal, delegates);
return new ConnectionConfig(
instanceName, namedConnection, targetPrincipal, delegates, adminServiceEndpoint);
}

private ConnectionConfig(
InstanceName instanceName,
String namedConnection,
String targetPrincipal,
List<String> delegates) {
List<String> delegates,
String adminServiceEndpoint) {
this.instanceName = instanceName;
this.namedConnection = namedConnection;
this.targetPrincipal = targetPrincipal;
this.delegates = delegates;
this.adminServiceEndpoint = adminServiceEndpoint;
}

public InstanceName getInstanceName() {
Expand All @@ -80,10 +87,15 @@ public List<String> getDelegates() {
return delegates;
}

public String getAdminServiceEndpoint() {
return adminServiceEndpoint;
}

/** The builder for the ConnectionConfig. */
public static class Builder {
private InstanceName instanceName;
private String namedConnection;
private String adminServiceEndpoint;
private String targetPrincipal;
private List<String> delegates;

Expand All @@ -107,8 +119,14 @@ public Builder withDelegates(List<String> delegates) {
return this;
}

public Builder withAdminServiceEndpoint(String adminServiceEndpoint) {
this.adminServiceEndpoint = adminServiceEndpoint;
return this;
}

public ConnectionConfig build() {
return new ConnectionConfig(instanceName, namedConnection, targetPrincipal, delegates);
return new ConnectionConfig(
instanceName, namedConnection, targetPrincipal, delegates, adminServiceEndpoint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Connector getConnector(ConnectionConfig config) {
FixedCredentialsProvider credentialsProvider =
CredentialsProviderFactory.create(config);
AlloyDBAdminClient alloyDBAdminClient =
AlloyDBAdminClientFactory.create(credentialsProvider);
AlloyDBAdminClientFactory.create(
credentialsProvider, config.getAdminServiceEndpoint());

return new Connector(
executor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ public void testConfigFromProps() {
final String wantTargetPrincipal = "[email protected]";
final List<String> wantDelegates = Arrays.asList("[email protected]", "[email protected]");
final String delegates = wantDelegates.stream().collect(Collectors.joining(","));
final String wantAdminServiceEndpoint = "alloydb.googleapis.com:443";

Properties props = new Properties();
props.setProperty(ConnectionConfig.ALLOYDB_INSTANCE_NAME, INSTANCE_NAME);
props.setProperty(ConnectionConfig.ALLOYDB_NAMED_CONNECTION, wantNamedConnection);
props.setProperty(ConnectionConfig.ALLOYDB_TARGET_PRINCIPAL, wantTargetPrincipal);
props.setProperty(ConnectionConfig.ALLOYDB_DELEGATES, delegates);
props.setProperty(ConnectionConfig.ALLOYDB_ADMIN_SERVICE_ENDPOINT, wantAdminServiceEndpoint);

ConnectionConfig config = ConnectionConfig.fromConnectionProperties(props);

assertThat(config.getInstanceName().toString()).isEqualTo(INSTANCE_NAME);
assertThat(config.getNamedConnection()).isEqualTo(wantNamedConnection);
assertThat(config.getTargetPrincipal()).isEqualTo(wantTargetPrincipal);
assertThat(config.getDelegates()).isEqualTo(wantDelegates);
assertThat(config.getAdminServiceEndpoint()).isEqualTo(wantAdminServiceEndpoint);
}

@Test
Expand All @@ -56,18 +59,21 @@ public void testConfigFromBuilder() {
final String wantNamedConnection = "my-connection";
final String wantTargetPrincipal = "[email protected]";
final List<String> wantDelegates = Arrays.asList("[email protected]", "[email protected]");
final String wantAdminServiceEndpoint = "alloydb.googleapis.com:443";

ConnectionConfig config =
new ConnectionConfig.Builder()
.withInstanceName(wantInstance)
.withNamedConnection(wantNamedConnection)
.withTargetPrincipal(wantTargetPrincipal)
.withDelegates(wantDelegates)
.withAdminServiceEndpoint(wantAdminServiceEndpoint)
.build();

assertThat(config.getInstanceName()).isEqualTo(wantInstance);
assertThat(config.getNamedConnection()).isEqualTo(wantNamedConnection);
assertThat(config.getTargetPrincipal()).isEqualTo(wantTargetPrincipal);
assertThat(config.getDelegates()).isEqualTo(wantDelegates);
assertThat(config.getAdminServiceEndpoint()).isEqualTo(wantAdminServiceEndpoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void setUp() throws IOException {
ConnectionConfig config =
new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(instanceName)).build();
FixedCredentialsProvider credentialsProvider = CredentialsProviderFactory.create(config);
alloydbAdminClient = AlloyDBAdminClientFactory.create(credentialsProvider);
alloydbAdminClient =
AlloyDBAdminClientFactory.create(credentialsProvider, config.getAdminServiceEndpoint());
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public void setUp() throws Exception {
ConnectionConfig config =
new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(instanceUri)).build();
FixedCredentialsProvider credentialsProvider = CredentialsProviderFactory.create(config);
alloyDBAdminClient = AlloyDBAdminClientFactory.create(credentialsProvider);
alloyDBAdminClient =
AlloyDBAdminClientFactory.create(credentialsProvider, config.getAdminServiceEndpoint());

defaultConnectionInfoRepository =
new DefaultConnectionInfoRepository(executor, alloyDBAdminClient);
Expand Down
Loading