Skip to content

Commit 58bfb10

Browse files
Fix issue 127 Support Google Cloud Postgres VCAP_SERVICES (#210)
* Changed mvnw to gradlew * Changed to build to run tests * Included additional tag for postgres * detect "postgres" in the tags * detect credentials.uri, in addition to credentials.jdbcUrl * detect credentials.Username and credentials.Password --------- Co-authored-by: pvtl-pre <>
1 parent ce426ee commit 58bfb10

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ io.pivotal.cfenv.spring.boot.CfEnvProcessor=com.example.MyCoolServiceCfEnvProces
324324
Clone the repo and type
325325

326326
----
327-
$ ./mvnw clean install
327+
$ ./gradlew clean build
328328
----
329329

330330
which will run the tests as well.

java-cfenv-jdbc/src/main/java/io/pivotal/cfenv/jdbc/PostgresqlJdbcUrlCreator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ public class PostgresqlJdbcUrlCreator extends AbstractJdbcUrlCreator {
3030

3131
public static final String POSTGRESQL_TAG = "postgresql";
3232

33+
public static final String POSTGRES_JDBC_TAG = "postgres";
3334
public static final String POSTGRESQL_LABEL = "postgresql";
3435

3536
@Override
3637
public boolean isDatabaseService(CfService cfService) {
3738
if (jdbcUrlMatchesScheme(cfService, POSTGRESQL_SCHEME, POSTGRES_JDBC_SCHEME)
38-
|| cfService.existsByTagIgnoreCase(POSTGRESQL_TAG)
39+
|| cfService.existsByTagIgnoreCase(POSTGRESQL_TAG, POSTGRES_JDBC_TAG)
3940
|| cfService.existsByLabelStartsWith(POSTGRESQL_LABEL)
4041
|| cfService.existsByUriSchemeStartsWith(POSTGRESQL_SCHEME, POSTGRES_JDBC_SCHEME)
4142
|| cfService.existsByCredentialsContainsUriField(POSTGRESQL_SCHEME, POSTGRES_JDBC_SCHEME)) {

java-cfenv-jdbc/src/test/java/io/pivotal/cfenv/jdbc/PostgresqlJdbcTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ public void postgresqlServiceCreationAlternate() {
7878
"No unique database service matching by name [postgresql.*] was found. Matching service names are [postgresql-1, postgresql-2]");
7979
}
8080

81+
@Test
82+
public void postgresqlServiceCreationGoogleCloud() {
83+
String name1 = "database-1";
84+
String name2 = "database-2";
85+
86+
mockVcapServices(getServicesPayload(
87+
getPostgresqlServicePayloadGoogleCloudSql("postgresql-1", hostname, port, username, password, name1),
88+
getPostgresqlServicePayloadGoogleCloudSql("postgresql-2", hostname, port, username, password, name2)));
89+
90+
CfJdbcEnv cfJdbcEnv = new CfJdbcEnv();
91+
CfJdbcService cfJdbcService = cfJdbcEnv.findJdbcServiceByName("postgresql-1");
92+
assertThat(cfJdbcService.getUsername()).isEqualTo(username);
93+
assertThat(cfJdbcService.getPassword()).isEqualTo(password);
94+
assertThat(cfJdbcService.getDriverClassName())
95+
.isEqualTo("org.postgresql.Driver");
96+
97+
assertThat(cfJdbcService.getJdbcUrl()).isEqualTo(
98+
"jdbc:postgresql://10.20.30.40/database-1?user=myuser&password=mypass&sslmode=require&sslcert=REDACTED&sslkey=REDACTED&sslrootcert=REDACTED"
99+
);
100+
101+
assertThatThrownBy(() -> {
102+
cfJdbcEnv.findJdbcServiceByName("postgresql.*");
103+
}).isInstanceOf(IllegalArgumentException.class).hasMessage(
104+
"No unique database service matching by name [postgresql.*] was found. Matching service names are [postgresql-1, postgresql-2]");
105+
}
106+
81107
@Test
82108
public void postgresqlWithSpecialCharsServiceCreation() {
83109
String userWithSpecialChars = "u%u:u+";
@@ -191,6 +217,13 @@ private String getPostgresqlServicePayloadAlternate(String serviceName,
191217
hostname, port, user, password, name);
192218
}
193219

220+
private String getPostgresqlServicePayloadGoogleCloudSql(String serviceName,
221+
String hostname, int port,
222+
String user, String password, String name) {
223+
return getTemplatedPayload("test-postgresql-info-google-cloudsql.json", serviceName,
224+
hostname, port, user, password, name);
225+
}
226+
194227
private String getPostgresqlServicePayloadNoLabelNoTags(String serviceName,
195228
String hostname, int port,
196229
String user, String password, String name) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "$serviceName",
3+
"credentials": {
4+
"CaCert": "REDACTED",
5+
"ClientCert": "REDACTED",
6+
"ClientKey": "REDACTED",
7+
"UriPrefix": "jdbc:",
8+
"Username": "$user",
9+
"Password": "$password",
10+
"instance_name": "INSTANCE_NAME",
11+
"uri": "jdbc:postgres://$user:$password@$hostname/$name?sslmode=require&sslcert=REDACTED&sslkey=REDACTED&sslrootcert=REDACTED"
12+
},
13+
"label": "google-cloudsql-postgres",
14+
"tags": [
15+
"gcp",
16+
"cloudsql",
17+
"postgres"
18+
]
19+
}

java-cfenv/src/main/java/io/pivotal/cfenv/core/CfCredentials.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class CfCredentials {
3232

3333
private UriInfo uriInfo;
3434

35-
private Map<String, String> derivedCredentials = new HashMap<>();
35+
private final Map<String, String> derivedCredentials = new HashMap<>();
3636

3737
public CfCredentials(Map<String, Object> credentialsData) {
3838
this.credentialsData = credentialsData;
@@ -98,7 +98,7 @@ public String getName() {
9898
* @return value of the username, null if not found.
9999
*/
100100
public String getUsername() {
101-
String username = getString("username", "user");
101+
String username = getString("username", "user", "Username");
102102
if (username != null) {
103103
return username;
104104
}
@@ -112,7 +112,7 @@ public String getUsername() {
112112
* @return value of the password, null if not found.
113113
*/
114114
public String getPassword() {
115-
String password = getString("password");
115+
String password = getString("password", "Password");
116116
if (password != null) {
117117
return password;
118118
}
@@ -162,9 +162,11 @@ public UriInfo getUriInfo(String uriScheme) {
162162
String username = getUsername();
163163
String password = getPassword();
164164
String databaseName = getName();
165-
uriInfo = new UriInfo(uriScheme, hostname, Integer.valueOf(port), username,
165+
uriInfo = new UriInfo(uriScheme, hostname, Integer.parseInt(port), username,
166166
password, databaseName);
167167
} else {
168+
// in the case the CF instance returned a URI starting with jdbc://, we need to remove it
169+
uri = uri.startsWith("jdbc:") ? uri.split("jdbc:")[1] : uri;
168170
uriInfo = new UriInfo(uri);
169171
}
170172
return uriInfo;

0 commit comments

Comments
 (0)