Skip to content

Commit

Permalink
wip: Configure domain name in ConnectionConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
hessjcg committed Aug 8, 2024
1 parent b5c6b97 commit a5f924b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
24 changes: 20 additions & 4 deletions core/src/main/java/com/google/cloud/sql/ConnectorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.base.Objects;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand All @@ -33,6 +34,7 @@ public class ConnectorConfig {
private final String adminRootUrl;
private final String adminServicePath;
private final Supplier<GoogleCredentials> googleCredentialsSupplier;
private final Function<String,String> instanceNameResolver;
private final GoogleCredentials googleCredentials;
private final String googleCredentialsPath;
private final String adminQuotaProject;
Expand All @@ -50,7 +52,8 @@ private ConnectorConfig(
String googleCredentialsPath,
String adminQuotaProject,
String universeDomain,
RefreshStrategy refreshStrategy) {
RefreshStrategy refreshStrategy,
Function<String,String> instanceNameResolver) {
this.targetPrincipal = targetPrincipal;
this.delegates = delegates;
this.adminRootUrl = adminRootUrl;
Expand All @@ -61,6 +64,7 @@ private ConnectorConfig(
this.adminQuotaProject = adminQuotaProject;
this.universeDomain = universeDomain;
this.refreshStrategy = refreshStrategy;
this.instanceNameResolver = instanceNameResolver;
}

@Override
Expand All @@ -81,7 +85,8 @@ public boolean equals(Object o) {
&& Objects.equal(googleCredentialsPath, that.googleCredentialsPath)
&& Objects.equal(adminQuotaProject, that.adminQuotaProject)
&& Objects.equal(universeDomain, that.universeDomain)
&& Objects.equal(refreshStrategy, that.refreshStrategy);
&& Objects.equal(refreshStrategy, that.refreshStrategy)
&& Objects.equal(instanceNameResolver, that.instanceNameResolver);
}

@Override
Expand All @@ -96,7 +101,8 @@ public int hashCode() {
googleCredentialsPath,
adminQuotaProject,
universeDomain,
refreshStrategy);
refreshStrategy,
instanceNameResolver);
}

public String getTargetPrincipal() {
Expand Down Expand Up @@ -139,6 +145,10 @@ public RefreshStrategy getRefreshStrategy() {
return refreshStrategy;
}

public Function<String, String> getInstanceNameResolver() {
return instanceNameResolver;
}

/** The builder for the ConnectionConfig. */
public static class Builder {

Expand All @@ -152,6 +162,7 @@ public static class Builder {
private String adminQuotaProject;
private String universeDomain;
private RefreshStrategy refreshStrategy = RefreshStrategy.BACKGROUND;
private Function<String,String> instanceNameResolver;

public Builder withTargetPrincipal(String targetPrincipal) {
this.targetPrincipal = targetPrincipal;
Expand Down Expand Up @@ -203,6 +214,10 @@ public Builder withRefreshStrategy(RefreshStrategy refreshStrategy) {
this.refreshStrategy = refreshStrategy;
return this;
}
public Builder withInstanceNameResolver(Function<String,String> instanceNameResolver) {
this.instanceNameResolver = instanceNameResolver;
return this;
}

/** Builds a new instance of {@code ConnectionConfig}. */
public ConnectorConfig build() {
Expand Down Expand Up @@ -238,7 +253,8 @@ public ConnectorConfig build() {
googleCredentialsPath,
adminQuotaProject,
universeDomain,
refreshStrategy);
refreshStrategy,
instanceNameResolver);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.google.cloud.sql;

public interface InstanceNameResolver {
String resolve(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,19 @@ public class ConnectionConfig {

private final AuthType authType;
private final String unixSocketPathSuffix;
private final String domainName;

/** Create a new ConnectionConfig from the well known JDBC Connection properties. */
public static ConnectionConfig fromConnectionProperties(Properties props) {
// TODO convert internal uses to fromConnectionProperties(props, domainName)
return fromConnectionProperties(props, null);
}

/**
* Create a new ConnectionConfig from the well known JDBC Connection properties, also setting
* database domain name.
*/
public static ConnectionConfig fromConnectionProperties(Properties props, String domainName) {
final String csqlInstanceName = props.getProperty(ConnectionConfig.CLOUD_SQL_INSTANCE_PROPERTY);
final String namedConnection =
props.getProperty(ConnectionConfig.CLOUD_SQL_NAMED_CONNECTOR_PROPERTY);
Expand Down Expand Up @@ -113,6 +123,7 @@ public static ConnectionConfig fromConnectionProperties(Properties props) {
ipTypes,
authType,
unixSocketPathSuffix,
domainName,
new ConnectorConfig.Builder()
.withTargetPrincipal(targetPrincipal)
.withDelegates(delegates)
Expand Down Expand Up @@ -162,13 +173,20 @@ public boolean equals(Object o) {
&& Objects.equals(unixSocketPath, config.unixSocketPath)
&& Objects.equals(ipTypes, config.ipTypes)
&& Objects.equals(authType, config.authType)
&& Objects.equals(domainName, config.domainName)
&& Objects.equals(connectorConfig, config.connectorConfig);
}

@Override
public int hashCode() {
return Objects.hash(
cloudSqlInstance, namedConnector, unixSocketPath, ipTypes, authType, connectorConfig);
cloudSqlInstance,
namedConnector,
unixSocketPath,
ipTypes,
authType,
domainName,
connectorConfig);
}

private ConnectionConfig(
Expand All @@ -178,6 +196,7 @@ private ConnectionConfig(
List<IpType> ipTypes,
AuthType authType,
String unixSocketPathSuffix,
String domainName,
ConnectorConfig connectorConfig) {
this.cloudSqlInstance = cloudSqlInstance;
this.namedConnector = namedConnector;
Expand All @@ -186,6 +205,7 @@ private ConnectionConfig(
this.unixSocketPathSuffix = unixSocketPathSuffix;
this.connectorConfig = connectorConfig;
this.authType = authType;
this.domainName = domainName;
}

/** Creates a new instance of the ConnectionConfig with an updated connectorConfig. */
Expand All @@ -197,9 +217,36 @@ public ConnectionConfig withConnectorConfig(ConnectorConfig config) {
ipTypes,
authType,
unixSocketPathSuffix,
domainName,
config);
}

/** Creates a new instance of the ConnectionConfig with an updated cloudSqlInstance. */
public ConnectionConfig withCloudSqlInstance(String newCloudSqlInstance) {
return new ConnectionConfig(
newCloudSqlInstance,
namedConnector,
unixSocketPath,
ipTypes,
authType,
unixSocketPathSuffix,
domainName,
connectorConfig);
}

/** Creates a new instance of the ConnectionConfig with an updated cloudSqlInstance. */
public ConnectionConfig withDomainName(String domainName) {
return new ConnectionConfig(
cloudSqlInstance,
namedConnector,
unixSocketPath,
ipTypes,
authType,
unixSocketPathSuffix,
domainName,
connectorConfig);
}

public String getNamedConnector() {
return namedConnector;
}
Expand Down Expand Up @@ -228,6 +275,10 @@ public AuthType getAuthType() {
return authType;
}

public String getDomainName() {
return domainName;
}

/** The builder for the ConnectionConfig. */
public static class Builder {

Expand All @@ -238,6 +289,7 @@ public static class Builder {
private String unixSocketPathSuffix;
private ConnectorConfig connectorConfig = new ConnectorConfig.Builder().build();
private AuthType authType = DEFAULT_AUTH_TYPE;
private String domainName;

public Builder withCloudSqlInstance(String cloudSqlInstance) {
this.cloudSqlInstance = cloudSqlInstance;
Expand Down Expand Up @@ -275,6 +327,11 @@ public Builder withIpTypes(List<IpType> ipTypes) {
this.ipTypes = ipTypes;
return this;
}
/** Set domainName as. */
public Builder withDomainName(String domainName) {
this.domainName = domainName;
return this;
}

public Builder withUnixSocketPathSuffix(String unixSocketPathSuffix) {
this.unixSocketPathSuffix = unixSocketPathSuffix;
Expand All @@ -290,6 +347,7 @@ public ConnectionConfig build() {
ipTypes,
authType,
unixSocketPathSuffix,
domainName,
connectorConfig);
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/com/google/cloud/sql/core/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.cloud.sql.ConnectorConfig;
import com.google.cloud.sql.CredentialFactory;
import com.google.cloud.sql.RefreshStrategy;
import com.google.common.base.Strings;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import java.io.File;
Expand All @@ -28,6 +29,7 @@
import java.security.KeyPair;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import javax.net.ssl.SSLSocket;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void testConfigFromProps() {
final String wantAdminQuotaProject = "myNewProject";
final String propRefreshStrategy = "Lazy";
final RefreshStrategy wantRefreshStrategy = RefreshStrategy.LAZY;
final String wantDomainName = "db.example.com";

Properties props = new Properties();
props.setProperty(ConnectionConfig.CLOUD_SQL_INSTANCE_PROPERTY, wantCsqlInstance);
Expand All @@ -66,7 +67,7 @@ public void testConfigFromProps() {
ConnectionConfig.CLOUD_SQL_ADMIN_QUOTA_PROJECT_PROPERTY, wantAdminQuotaProject);
props.setProperty(ConnectionConfig.CLOUD_SQL_REFRESH_STRATEGY_PROPERTY, propRefreshStrategy);

ConnectionConfig c = ConnectionConfig.fromConnectionProperties(props);
ConnectionConfig c = ConnectionConfig.fromConnectionProperties(props, wantDomainName);

assertThat(c.getCloudSqlInstance()).isEqualTo(wantCsqlInstance);
assertThat(c.getNamedConnector()).isEqualTo(wantNamedConnector);
Expand All @@ -81,6 +82,7 @@ public void testConfigFromProps() {
assertThat(c.getConnectorConfig().getAdminQuotaProject()).isEqualTo(wantAdminQuotaProject);
assertThat(c.getUnixSocketPathSuffix()).isEqualTo(wantUnixSuffix);
assertThat(c.getConnectorConfig().getRefreshStrategy()).isEqualTo(wantRefreshStrategy);
assertThat(c.getDomainName()).isEqualTo(wantDomainName);
}

@Test
Expand All @@ -96,6 +98,7 @@ public void testConfigFromBuilder() {
final String wantAdminServicePath = "sqladmin/";
final String wantUnixSuffix = ".psql.5432";
final String wantAdminQuotaProject = "myNewProject";
final String wantDomainName = "db.example.com";

ConnectorConfig cc =
new ConnectorConfig.Builder()
Expand All @@ -115,6 +118,7 @@ public void testConfigFromBuilder() {
.withUnixSocketPath(wantUnixSocket)
.withUnixSocketPathSuffix(wantUnixSuffix)
.withConnectorConfig(cc)
.withDomainName(wantDomainName)
.build();

assertThat(c.getCloudSqlInstance()).isEqualTo(wantCsqlInstance);
Expand All @@ -128,6 +132,7 @@ public void testConfigFromBuilder() {
assertThat(c.getConnectorConfig().getAdminServicePath()).isEqualTo(wantAdminServicePath);
assertThat(c.getConnectorConfig().getAdminQuotaProject()).isEqualTo(wantAdminQuotaProject);
assertThat(c.getUnixSocketPathSuffix()).isEqualTo(wantUnixSuffix);
assertThat(c.getDomainName()).isEqualTo(wantDomainName);
}

@Test
Expand Down

0 comments on commit a5f924b

Please sign in to comment.