Add DATABASE_URL to the list of supported environment variable names for detecting Postgres databases in the Heroku cloud connector.

This commit is contained in:
Scott Frederick
2015-11-20 17:43:11 -06:00
parent a6d19e86d1
commit 3b0e497d1d
3 changed files with 32 additions and 23 deletions

View File

@@ -18,22 +18,21 @@ import org.springframework.cloud.util.EnvironmentAccessor;
/**
* Implementation of CloudConnector for Heroku
*
* <p/>
* Currently support Postgres (default provided), Mysql (Cleardb), MongoDb (MongoLab, MongoHQ, MongoSoup),
* Redis (RedisToGo, RedisCloud, OpenRedis, RedisGreen), and AMQP (CloudAmqp).
*
* @author Ramnivas Laddad
*
*/
public class HerokuConnector extends AbstractCloudConnector<UriBasedServiceData> {
private EnvironmentAccessor environment = new EnvironmentAccessor();
private ApplicationInstanceInfoCreator applicationInstanceInfoCreator
= new ApplicationInstanceInfoCreator(environment);
= new ApplicationInstanceInfoCreator(environment);
private List<String> serviceEnvPrefixes;
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
public HerokuConnector() {
super((Class) HerokuServiceInfoCreator.class);
}
@@ -60,15 +59,15 @@ public class HerokuConnector extends AbstractCloudConnector<UriBasedServiceData>
@Override
protected void registerServiceInfoCreator(ServiceInfoCreator<? extends ServiceInfo, UriBasedServiceData> serviceInfoCreator) {
super.registerServiceInfoCreator(serviceInfoCreator);
HerokuServiceInfoCreator<?> herokuServiceInfoCreator = (HerokuServiceInfoCreator<?>)serviceInfoCreator;
String[] envPrefixes = herokuServiceInfoCreator.getEnvPrefixes();
super.registerServiceInfoCreator(serviceInfoCreator);
HerokuServiceInfoCreator<?> herokuServiceInfoCreator = (HerokuServiceInfoCreator<?>) serviceInfoCreator;
String[] envPrefixes = herokuServiceInfoCreator.getEnvPrefixes();
// need to do this since this method gets called during construction and we cannot initialize serviceEnvPrefixes before this
if (serviceEnvPrefixes == null) {
serviceEnvPrefixes = new ArrayList<String>();
}
serviceEnvPrefixes.addAll(Arrays.asList(envPrefixes));
// need to do this since this method gets called during construction and we cannot initialize serviceEnvPrefixes before this
if (serviceEnvPrefixes == null) {
serviceEnvPrefixes = new ArrayList<String>();
}
serviceEnvPrefixes.addAll(Arrays.asList(envPrefixes));
}
/**
@@ -76,26 +75,27 @@ public class HerokuConnector extends AbstractCloudConnector<UriBasedServiceData>
* <p>
* Returns map whose key is the env key and value is the associated url
* </p>
*
* @return information about services bound to the app
*/
protected List<UriBasedServiceData> getServicesData() {
List<UriBasedServiceData> serviceData = new ArrayList<UriBasedServiceData>();
Map<String,String> env = environment.getEnv();
Map<String, String> env = environment.getEnv();
for (Map.Entry<String, String> envEntry : env.entrySet()) {
for (String envPrefix : serviceEnvPrefixes) {
if (envEntry.getKey().startsWith(envPrefix)) {
serviceData.add(new UriBasedServiceData(envEntry.getKey(), envEntry.getValue()));
}
}
for (String envPrefix : serviceEnvPrefixes) {
if (envEntry.getKey().startsWith(envPrefix)) {
serviceData.add(new UriBasedServiceData(envEntry.getKey(), envEntry.getValue()));
}
}
}
return serviceData;
}
@Override
protected FallbackServiceInfoCreator<BaseServiceInfo,UriBasedServiceData> getFallbackServiceInfoCreator() {
protected FallbackServiceInfoCreator<BaseServiceInfo, UriBasedServiceData> getFallbackServiceInfoCreator() {
return new FallbackBaseServiceInfoCreator();
}
}

View File

@@ -20,6 +20,6 @@ public class PostgresqlServiceInfoCreator extends RelationalServiceInfoCreator<P
@Override
public String[] getEnvPrefixes() {
return new String[]{"HEROKU_POSTGRESQL_"};
return new String[]{"DATABASE_URL", "HEROKU_POSTGRESQL_"};
}
}

View File

@@ -21,14 +21,23 @@ public class HerokuConnectorPostgresqlServiceTest extends AbstractHerokuConnecto
}
@Test
public void postgresqlServiceCreation() {
public void postgresqlServiceCreationPrimary() {
assertPostgresServiceCreated("DATABASE_URL", "DATABASE");
}
@Test
public void postgresqlServiceCreationSecondary() {
assertPostgresServiceCreated("HEROKU_POSTGRESQL_YELLOW_URL", "HEROKU_POSTGRESQL_YELLOW");
}
private void assertPostgresServiceCreated(String envVarName, String serviceInstanceName) {
Map<String, String> env = new HashMap<String, String>();
String postgresUrl = getRelationalServiceUrl("db");
env.put("HEROKU_POSTGRESQL_YELLOW_URL", postgresUrl);
env.put(envVarName, postgresUrl);
when(mockEnvironment.getEnv()).thenReturn(env);
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "HEROKU_POSTGRESQL_YELLOW");
ServiceInfo serviceInfo = getServiceInfo(serviceInfos, serviceInstanceName);
assertNotNull(serviceInfo);
assertTrue(serviceInfo instanceof PostgresqlServiceInfo);
assertReleationServiceInfo((PostgresqlServiceInfo) serviceInfo, "db");