Skip to content

Commit

Permalink
Add DATABASE_URL to the list of supported environment variable names …
Browse files Browse the repository at this point in the history
…for detecting Postgres databases in the Heroku cloud connector.
  • Loading branch information
scottfrederick committed Nov 20, 2015
1 parent a6d19e8 commit 3b0e497
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@

/**
* 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);
}
Expand All @@ -60,42 +59,43 @@ void setCloudEnvironment(EnvironmentAccessor environment) {

@Override
protected void registerServiceInfoCreator(ServiceInfoCreator<? extends ServiceInfo, UriBasedServiceData> serviceInfoCreator) {
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));
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));
}

/**
* Return object representation of the bound services
* <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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public PostgresqlServiceInfo createServiceInfo(String id, String uri) {

@Override
public String[] getEnvPrefixes() {
return new String[]{"HEROKU_POSTGRESQL_"};
return new String[]{"DATABASE_URL", "HEROKU_POSTGRESQL_"};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ public HerokuConnectorPostgresqlServiceTest() {
}

@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");
Expand Down

0 comments on commit 3b0e497

Please sign in to comment.