diff --git a/core/src/main/java/com/google/cloud/sql/ConnectorConfig.java b/core/src/main/java/com/google/cloud/sql/ConnectorConfig.java index 1f5e3e456..30c5e8fe5 100644 --- a/core/src/main/java/com/google/cloud/sql/ConnectorConfig.java +++ b/core/src/main/java/com/google/cloud/sql/ConnectorConfig.java @@ -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; /** @@ -33,6 +34,7 @@ public class ConnectorConfig { private final String adminRootUrl; private final String adminServicePath; private final Supplier googleCredentialsSupplier; + private final Function instanceNameResolver; private final GoogleCredentials googleCredentials; private final String googleCredentialsPath; private final String adminQuotaProject; @@ -50,7 +52,8 @@ private ConnectorConfig( String googleCredentialsPath, String adminQuotaProject, String universeDomain, - RefreshStrategy refreshStrategy) { + RefreshStrategy refreshStrategy, + Function instanceNameResolver) { this.targetPrincipal = targetPrincipal; this.delegates = delegates; this.adminRootUrl = adminRootUrl; @@ -61,6 +64,7 @@ private ConnectorConfig( this.adminQuotaProject = adminQuotaProject; this.universeDomain = universeDomain; this.refreshStrategy = refreshStrategy; + this.instanceNameResolver = instanceNameResolver; } @Override @@ -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 @@ -96,7 +101,8 @@ public int hashCode() { googleCredentialsPath, adminQuotaProject, universeDomain, - refreshStrategy); + refreshStrategy, + instanceNameResolver); } public String getTargetPrincipal() { @@ -139,6 +145,10 @@ public RefreshStrategy getRefreshStrategy() { return refreshStrategy; } + public Function getInstanceNameResolver() { + return instanceNameResolver; + } + /** The builder for the ConnectionConfig. */ public static class Builder { @@ -152,6 +162,7 @@ public static class Builder { private String adminQuotaProject; private String universeDomain; private RefreshStrategy refreshStrategy = RefreshStrategy.BACKGROUND; + private Function instanceNameResolver; public Builder withTargetPrincipal(String targetPrincipal) { this.targetPrincipal = targetPrincipal; @@ -203,6 +214,10 @@ public Builder withRefreshStrategy(RefreshStrategy refreshStrategy) { this.refreshStrategy = refreshStrategy; return this; } + public Builder withInstanceNameResolver(Function instanceNameResolver) { + this.instanceNameResolver = instanceNameResolver; + return this; + } /** Builds a new instance of {@code ConnectionConfig}. */ public ConnectorConfig build() { @@ -238,7 +253,8 @@ public ConnectorConfig build() { googleCredentialsPath, adminQuotaProject, universeDomain, - refreshStrategy); + refreshStrategy, + instanceNameResolver); } } } diff --git a/core/src/main/java/com/google/cloud/sql/InstanceNameResolver.java b/core/src/main/java/com/google/cloud/sql/InstanceNameResolver.java new file mode 100644 index 000000000..2b009a8f6 --- /dev/null +++ b/core/src/main/java/com/google/cloud/sql/InstanceNameResolver.java @@ -0,0 +1,5 @@ +package com.google.cloud.sql; + +public interface InstanceNameResolver { + String resolve(String name); +} diff --git a/core/src/main/java/com/google/cloud/sql/core/ConnectionConfig.java b/core/src/main/java/com/google/cloud/sql/core/ConnectionConfig.java index 592612c5b..1d913753d 100644 --- a/core/src/main/java/com/google/cloud/sql/core/ConnectionConfig.java +++ b/core/src/main/java/com/google/cloud/sql/core/ConnectionConfig.java @@ -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); @@ -113,6 +123,7 @@ public static ConnectionConfig fromConnectionProperties(Properties props) { ipTypes, authType, unixSocketPathSuffix, + domainName, new ConnectorConfig.Builder() .withTargetPrincipal(targetPrincipal) .withDelegates(delegates) @@ -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( @@ -178,6 +196,7 @@ private ConnectionConfig( List ipTypes, AuthType authType, String unixSocketPathSuffix, + String domainName, ConnectorConfig connectorConfig) { this.cloudSqlInstance = cloudSqlInstance; this.namedConnector = namedConnector; @@ -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. */ @@ -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; } @@ -228,6 +275,10 @@ public AuthType getAuthType() { return authType; } + public String getDomainName() { + return domainName; + } + /** The builder for the ConnectionConfig. */ public static class Builder { @@ -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; @@ -275,6 +327,11 @@ public Builder withIpTypes(List 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; @@ -290,6 +347,7 @@ public ConnectionConfig build() { ipTypes, authType, unixSocketPathSuffix, + domainName, connectorConfig); } } diff --git a/core/src/main/java/com/google/cloud/sql/core/Connector.java b/core/src/main/java/com/google/cloud/sql/core/Connector.java index 14379fcee..b872e8a82 100644 --- a/core/src/main/java/com/google/cloud/sql/core/Connector.java +++ b/core/src/main/java/com/google/cloud/sql/core/Connector.java @@ -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; @@ -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; diff --git a/core/src/test/java/com/google/cloud/sql/core/ConnectionConfigTest.java b/core/src/test/java/com/google/cloud/sql/core/ConnectionConfigTest.java index 0da8b0f62..26f5c5e92 100644 --- a/core/src/test/java/com/google/cloud/sql/core/ConnectionConfigTest.java +++ b/core/src/test/java/com/google/cloud/sql/core/ConnectionConfigTest.java @@ -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); @@ -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); @@ -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 @@ -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() @@ -115,6 +118,7 @@ public void testConfigFromBuilder() { .withUnixSocketPath(wantUnixSocket) .withUnixSocketPathSuffix(wantUnixSuffix) .withConnectorConfig(cc) + .withDomainName(wantDomainName) .build(); assertThat(c.getCloudSqlInstance()).isEqualTo(wantCsqlInstance); @@ -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