Skip to content

Commit

Permalink
WHIRR-163. Support environment variable interpolation in configuratio…
Browse files Browse the repository at this point in the history
…n properties.

git-svn-id: https://svn.apache.org/repos/asf/incubator/whirr/trunk@1050191 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
tomwhite committed Dec 16, 2010
1 parent 8bb2e0f commit 905a6b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -23,6 +23,9 @@ Trunk (unreleased changes)

WHIRR-150. Allow retrieval of instance roles. (tomwhite)

WHIRR-163. Support environment variable interpolation in configuration
properties. (tomwhite)

BUG FIXES

WHIRR-128. Fix DNS resolution for clients running within EC2.
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/org/apache/whirr/service/ClusterSpec.java
Expand Up @@ -43,7 +43,9 @@
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.text.StrLookup;
import org.jclouds.io.Payload;

/**
Expand All @@ -52,6 +54,18 @@
*/
public class ClusterSpec {

static {
// Environment variable interpolation (e.g. {env:MY_VAR}) is supported
// natively in Commons Configuration 1.7, but until it's released we
// do it ourselves here
ConfigurationInterpolator.registerGlobalLookup("env", new StrLookup() {
@Override
public String lookup(String key) {
return System.getenv(key);
}
});
}

public enum Property {
SERVICE_NAME(String.class, false),
INSTANCE_TEMPLATES(String.class, false),
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/org/apache/whirr/service/ClusterSpecTest.java
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
Expand All @@ -32,6 +33,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
Expand Down Expand Up @@ -87,6 +89,21 @@ public void testGetConfigurationForKeysWithPrefix()
assertThat(prefixKeys.get(0), is("a.b"));
assertThat(prefixKeys.get(1), is("a.c"));
}

@Test
public void testEnvVariableInterpolation() {
Map<String, String> envMap = System.getenv();
assertThat(envMap.isEmpty(), is(false));
String undefinedEnvVar = "UNDEFINED_ENV_VAR";
assertThat(envMap.containsKey(undefinedEnvVar), is(false));
Entry<String, String> firstEntry = Iterables.get(envMap.entrySet(), 0);
Configuration conf = new PropertiesConfiguration();
conf.setProperty("a", String.format("${env:%s}", firstEntry.getKey()));
conf.setProperty("b", String.format("${env:%s}", undefinedEnvVar));
assertThat(conf.getString("a"), is(firstEntry.getValue()));
assertThat(conf.getString("b"),
is(String.format("${env:%s}", undefinedEnvVar)));
}

@Test
public void testDefaultPublicKey()
Expand Down

0 comments on commit 905a6b5

Please sign in to comment.