Adds OnPublicNetworkEnricher.addressSensor config If set, it will advertise the public address based on the IP/hostname advertised in this sensor, rather than using he PortForwardManager.
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/2a4e6eac Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/2a4e6eac Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/2a4e6eac Branch: refs/heads/master Commit: 2a4e6eac6a330f4b7d16f2fb5b1653c519298b47 Parents: ebbd377 Author: Aled Sage <aled.s...@gmail.com> Authored: Tue Oct 11 22:57:57 2016 +0100 Committer: Aled Sage <aled.s...@gmail.com> Committed: Tue Oct 11 22:57:57 2016 +0100 ---------------------------------------------------------------------- .../core/network/OnPublicNetworkEnricher.java | 23 +++++++++- .../network/OnPublicNetworkEnricherTest.java | 45 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2a4e6eac/core/src/main/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricher.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricher.java b/core/src/main/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricher.java index d37aa6d..878c1d5 100644 --- a/core/src/main/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricher.java +++ b/core/src/main/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricher.java @@ -21,11 +21,13 @@ package org.apache.brooklyn.core.network; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntityLocal; import org.apache.brooklyn.api.location.MachineLocation; +import org.apache.brooklyn.api.sensor.AttributeSensor; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.location.access.PortForwardManager; import org.apache.brooklyn.core.location.access.PortForwardManagerLocationResolver; import org.apache.brooklyn.util.guava.Maybe; +import org.apache.brooklyn.util.text.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,6 +36,7 @@ import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicates; import com.google.common.net.HostAndPort; +import com.google.common.reflect.TypeToken; /** * Can be added to an entity so that it advertises its mapped ports (according to the port-mappings @@ -90,6 +93,12 @@ public class OnPublicNetworkEnricher extends AbstractOnNetworkEnricher { "portForwardManager", "The PortForwardManager storing the port-mappings; if null, the global instance will be used"); + @SuppressWarnings("serial") + public static final ConfigKey<AttributeSensor<String>> ADDRESS_SENSOR = ConfigKeys.newConfigKey( + new TypeToken<AttributeSensor<String>>() {}, + "addressSensor", + "The sensor to use to retrieve the entity's public address; if null (default), then use the PortForwardManager instead"); + protected PortForwardManager.AssociationListener pfmListener; @Override @@ -137,8 +146,18 @@ public class OnPublicNetworkEnricher extends AbstractOnNetworkEnricher { @Override protected Optional<HostAndPort> getMappedEndpoint(Entity source, MachineLocation machine, int port) { - HostAndPort publicTarget = getPortForwardManager().lookup(machine, port); - return Optional.fromNullable(publicTarget); + AttributeSensor<String> sensor = config().get(ADDRESS_SENSOR); + if (sensor == null) { + HostAndPort publicTarget = getPortForwardManager().lookup(machine, port); + return Optional.fromNullable(publicTarget); + } else { + String address = source.sensors().get(sensor); + if (Strings.isNonBlank(address)) { + return Optional.of(HostAndPort.fromParts(address, port)); + } else { + return Optional.absent(); + } + } } protected PortForwardManager getPortForwardManager() { http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2a4e6eac/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java b/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java index f1c001b..3d5d3e0 100644 --- a/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java @@ -303,6 +303,51 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport { } @Test + public <T> void testTransformsToAddressInSensor() throws Exception { + AttributeSensor<String> stringUri = Sensors.newStringSensor("string.uri"); + + entity.sensors().set(Attributes.ADDRESS, "1.1.1.1"); + entity.sensors().set(stringUri, "http://127.0.0.1:1234/my/path"); + entity.addLocations(ImmutableList.of(machine)); + + entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) + .configure(OnPublicNetworkEnricher.ADDRESS_SENSOR, Attributes.ADDRESS)); + + assertAttributeEqualsEventually("string.uri.mapped.public", "http://1.1.1.1:1234/my/path"); + } + + @Test + public <T> void testTransformsToAddressInSensorIsNoopIfSensorNull() throws Exception { + AttributeSensor<String> stringUri = Sensors.newStringSensor("string.uri"); + + entity.sensors().set(Attributes.ADDRESS, null); + entity.sensors().set(stringUri, "http://127.0.0.1:1234/my/path"); + entity.addLocations(ImmutableList.of(machine)); + + entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) + .configure(OnPublicNetworkEnricher.ADDRESS_SENSOR, Attributes.ADDRESS)); + + assertAttributeEqualsContinually("string.uri.mapped.public", null, Duration.millis(250)); + } + + @Test(groups="Broken") + public <T> void testTransformsToAddressInSensorWithDefaultPorts() throws Exception { + AttributeSensor<String> stringUriWithHttpNoPort = Sensors.newStringSensor("string.uriWithHttpNoPort"); + AttributeSensor<String> stringUriWithHttpsNoPort = Sensors.newStringSensor("string.uriWithHttpsNoPort"); + + entity.sensors().set(Attributes.ADDRESS, "1.1.1.1"); + entity.sensors().set(stringUriWithHttpNoPort, "http://127.0.0.1/my/path"); + entity.sensors().set(stringUriWithHttpsNoPort, "https://127.0.0.1/my/path"); + entity.addLocations(ImmutableList.of(machine)); + + entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) + .configure(OnPublicNetworkEnricher.ADDRESS_SENSOR, Attributes.ADDRESS)); + + assertAttributeEqualsEventually("string.uriWithHttpNoPort.mapped.public", "http://1.1.1.1/my/path"); + assertAttributeEqualsEventually("string.uriWithHttspNoPort.mapped.public", "https://1.1.1.1/my/path"); + } + + @Test public <T> void testDoesNotDoRegexMatchingWhenSensorsSpecified() throws Exception { AttributeSensor<String> sensor = Sensors.newStringSensor("mysensor"); AttributeSensor<Integer> intPort = Sensors.newIntegerSensor("int.port");