Repository: incubator-brooklyn Updated Branches: refs/heads/master 24b0f3e8a -> ddcebe8df
Fixes SetHostnameCustomizer for machines with no addresses If both getPrivateAddresses and getPublicAddresses return empty, then default to ânoneâ rather than failing. Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/abd47b3a Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/abd47b3a Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/abd47b3a Branch: refs/heads/master Commit: abd47b3a79d821f8f5fe54bb1ec6a94751a0e536 Parents: 3351c96 Author: Aled Sage <[email protected]> Authored: Thu Oct 22 11:59:48 2015 +0100 Committer: Aled Sage <[email protected]> Committed: Thu Oct 22 12:02:10 2015 +0100 ---------------------------------------------------------------------- .../entity/machine/SetHostnameCustomizer.java | 10 +++++++--- .../machine/SetHostnameCustomizerTest.java | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/abd47b3a/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java b/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java index 7fdb86a..56d2bcd 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java @@ -68,9 +68,13 @@ public class SetHostnameCustomizer extends BasicMachineLocationCustomizer { "hostname.templated", "The hostname template, to be resolved and then set on the machine (if non-null). " +"Assumed to be in free-marker format.", - "ip-" - + "${((location.privateAddresses[0]??)?string(" - + "location.privateAddresses[0]!'X', location.publicAddresses[0]))" + "ip-${(" + + "(location.privateAddresses[0]??)?string(" + + "location.privateAddresses[0]!'X', " + + "(location.publicAddresses[0]??)?string(" + + "location.publicAddresses[0]!'X', " + + "\"none\"))" + + ")" + "?replace(\".\",\"-\")}" + "-${location.id}"); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/abd47b3a/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java index 5ffdcdc..11981ce 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java @@ -20,6 +20,9 @@ package org.apache.brooklyn.entity.machine; import static org.testng.Assert.assertEquals; +import java.net.InetAddress; +import java.util.Set; + import org.apache.brooklyn.api.location.LocationSpec; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.location.ssh.SshMachineLocation; @@ -28,6 +31,7 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; public class SetHostnameCustomizerTest extends BrooklynAppUnitTestSupport { @@ -64,4 +68,20 @@ public class SetHostnameCustomizerTest extends BrooklynAppUnitTestSupport { assertEquals(customizer.generateHostname(machine), "ip-4-3-2-1-"+machine.getId()); } + + @Test + public void testGeneratedHostnameIfNoIps() throws Exception { + SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocationWithNoPublicIps.class) + .configure("address", "4.3.2.1")); + + assertEquals(customizer.generateHostname(machine), "ip-none-"+machine.getId()); + } + public static class SshMachineLocationWithNoPublicIps extends SshMachineLocation { + public SshMachineLocationWithNoPublicIps() { + } + @Override + public Set<String> getPublicAddresses() { + return ImmutableSet.<String>of(); + } + } }
