From 5ba527ec33bd587015c596ca845eefc121d494a2 Mon Sep 17 00:00:00 2001 From: Everett Toews Date: Mon, 16 Dec 2013 13:52:16 -0600 Subject: [PATCH] Ensure the accessIPv4 value gets into NodeMetadata. --- .../functions/ServerInZoneToNodeMetadata.java | 35 +++++- .../ServerInZoneToNodeMetadataTest.java | 104 +++++++++++++++++- .../nova/v2_0/parse/ParseServerTest.java | 8 +- 3 files changed, 137 insertions(+), 10 deletions(-) diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java index d1d5245044..10783d62bb 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java @@ -18,13 +18,18 @@ package org.jclouds.openstack.nova.v2_0.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Predicates.not; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.find; import static com.google.common.collect.Iterables.transform; +import static com.google.common.collect.Sets.newHashSet; import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue; import static org.jclouds.compute.util.ComputeServiceUtils.groupFromMapOrName; +import static org.jclouds.openstack.nova.v2_0.domain.Address.createV4; +import static org.jclouds.openstack.nova.v2_0.domain.Address.createV6; import java.net.Inet4Address; +import java.util.Collection; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; @@ -33,6 +38,7 @@ import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; +import com.google.common.collect.Sets; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.ComputeMetadata; import org.jclouds.compute.domain.Hardware; @@ -107,12 +113,29 @@ public class ServerInZoneToNodeMetadata implements Function addresses = newHashSet(from.getAddresses().values()); + if (from.getAccessIPv4() != null) { + addresses.add(createV4(from.getAccessIPv4())); + } + if (from.getAccessIPv6() != null) { + addresses.add(createV6(from.getAccessIPv6())); + } + + builder.publicAddresses( + filter( + transform( + filter(addresses, not(isPrivateAddress)), + AddressToStringTransformationFunction.INSTANCE), + isInet4Address)); + + builder.privateAddresses( + filter( + transform( + filter(addresses, isPrivateAddress), + AddressToStringTransformationFunction.INSTANCE), + isInet4Address)); + for (Link link: from.getLinks()) { if (link.getRelation().equals(Link.Relation.SELF)) { builder.uri(link.getHref()); diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java index 472f814acf..7ef9fa7047 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java @@ -94,7 +94,109 @@ public class ServerInZoneToNodeMetadataTest { existingImage.getOperatingSystem(), existingImage); } - // TODO: clean up this syntax + @Test + public void testNullAccessIPs() { + Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd") + .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(zone).build(); + Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54") + .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build()) + .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE) + .location(zone).build(); + + Set images = existingImage == null ? ImmutableSet. of() : ImmutableSet.of(existingImage); + Set hardwares = existingHardware == null ? ImmutableSet. of() : ImmutableSet + .of(existingHardware); + Server serverToConvert = new ParseServerTest().expected().toBuilder() + .accessIPv4(null) + .accessIPv6(null) + .build(); + + ServerInZone serverInZoneToConvert = new ServerInZone(serverToConvert, "az-1.region-a.geo-1"); + + ServerInZoneToNodeMetadata converter = new ServerInZoneToNodeMetadata( + NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers + .> ofInstance(images), Suppliers + .> ofInstance(hardwares), namingConvention); + + NodeMetadata convertedNodeMetadata = converter.apply(serverInZoneToConvert); + + assertNotNull(convertedNodeMetadata.getPrivateAddresses()); + assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("10.176.42.16")); + + assertNotNull(convertedNodeMetadata.getPublicAddresses()); + // note jclouds doesn't yet support ipv6 b/c not tested yet + assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("67.23.10.132", "67.23.10.131")); + } + + @Test + public void testDuplicateAccessIPs() { + Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd") + .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(zone).build(); + Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54") + .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build()) + .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE) + .location(zone).build(); + + Set images = existingImage == null ? ImmutableSet. of() : ImmutableSet.of(existingImage); + Set hardwares = existingHardware == null ? ImmutableSet. of() : ImmutableSet + .of(existingHardware); + Server serverToConvert = new ParseServerTest().expected().toBuilder() + .accessIPv4("67.23.10.132") + .accessIPv6("::babe:67.23.10.132") + .build(); + + ServerInZone serverInZoneToConvert = new ServerInZone(serverToConvert, "az-1.region-a.geo-1"); + + ServerInZoneToNodeMetadata converter = new ServerInZoneToNodeMetadata( + NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers + .> ofInstance(images), Suppliers + .> ofInstance(hardwares), namingConvention); + + NodeMetadata convertedNodeMetadata = converter.apply(serverInZoneToConvert); + + assertNotNull(convertedNodeMetadata.getPrivateAddresses()); + assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("10.176.42.16")); + + assertNotNull(convertedNodeMetadata.getPublicAddresses()); + // note jclouds doesn't yet support ipv6 b/c not tested yet + assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("67.23.10.132", "67.23.10.131")); + } + + @Test + public void testAlternateAccessIPs() { + Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd") + .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(zone).build(); + Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54") + .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build()) + .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE) + .location(zone).build(); + + Set images = existingImage == null ? ImmutableSet. of() : ImmutableSet.of(existingImage); + Set hardwares = existingHardware == null ? ImmutableSet. of() : ImmutableSet + .of(existingHardware); + Server serverToConvert = new ParseServerTest().expected().toBuilder() + .accessIPv4("76.32.1.231") + .accessIPv6("::babe:76.32.1.231") + .build(); + + ServerInZone serverInZoneToConvert = new ServerInZone(serverToConvert, "az-1.region-a.geo-1"); + + ServerInZoneToNodeMetadata converter = new ServerInZoneToNodeMetadata( + NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers + .> ofInstance(images), Suppliers + .> ofInstance(hardwares), namingConvention); + + NodeMetadata convertedNodeMetadata = converter.apply(serverInZoneToConvert); + + assertNotNull(convertedNodeMetadata.getPrivateAddresses()); + assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("10.176.42.16")); + + assertNotNull(convertedNodeMetadata.getPublicAddresses()); + // note jclouds doesn't yet support ipv6 b/c not tested yet + assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("67.23.10.132", "67.23.10.131", "76.32.1.231")); + } + + // TODO: clean up this syntax private void checkHardwareAndImageStatus(Hardware expectedHardware, Hardware existingHardware, String expectedImageId, OperatingSystem expectedOs, Image existingImage) { diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/parse/ParseServerTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/parse/ParseServerTest.java index db8e38747f..1b53135933 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/parse/ParseServerTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/parse/ParseServerTest.java @@ -39,6 +39,9 @@ import com.google.common.collect.ImmutableMultimap; import com.google.inject.Guice; import com.google.inject.Injector; +import static org.jclouds.openstack.nova.v2_0.domain.Address.createV4; +import static org.jclouds.openstack.nova.v2_0.domain.Address.createV6; + /** * @author Adrian Cole */ @@ -97,9 +100,8 @@ public class ParseServerTest extends BaseItemParserTest { new ImmutableMap.Builder().put("Server Label", "Web Head 1") .put("Image Version", "2.1").build()) .addresses(ImmutableMultimap.builder() - .putAll("public", Address.createV4("67.23.10.132"), Address.createV6("::babe:67.23.10.132"), - Address.createV4("67.23.10.131"), Address.createV6("::babe:4317:0A83")) - .putAll("private", Address.createV4("10.176.42.16"), Address.createV6("::babe:10.176.42.16")) + .putAll("public", createV4("67.23.10.132"), createV6("::babe:67.23.10.132"), createV4("67.23.10.131"), createV6("::babe:4317:0A83")) + .putAll("private", createV4("10.176.42.16"), createV6("::babe:10.176.42.16")) .build()) .links(Link.create( Relation.SELF, URI.create("http://servers.api.openstack.org/v1.1/1234/servers/52415800-8b69-11e0-9b19-734f6f006e54")),