Ensure the accessIPv4 value gets into NodeMetadata.

This commit is contained in:
Everett Toews 2013-12-16 13:52:16 -06:00
parent ac1f05ffac
commit 5ba527ec33
3 changed files with 137 additions and 10 deletions

View File

@ -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<ServerInZone, NodeMe
builder.operatingSystem(findOperatingSystemForServerOrNull(serverInZone));
builder.hardware(findHardwareForServerOrNull(serverInZone));
builder.status(toPortableNodeStatus.get(from.getStatus()));
builder.publicAddresses(filter(
transform(filter(from.getAddresses().values(), Predicates.not(isPrivateAddress)),
AddressToStringTransformationFunction.INSTANCE), isInet4Address));
builder.privateAddresses(filter(
transform(filter(from.getAddresses().values(), isPrivateAddress), AddressToStringTransformationFunction.INSTANCE), isInet4Address));
Set<Address> 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());

View File

@ -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<Image> images = existingImage == null ? ImmutableSet.<Image> of() : ImmutableSet.of(existingImage);
Set<Hardware> hardwares = existingHardware == null ? ImmutableSet.<Hardware> 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
.<Set<? extends Image>> ofInstance(images), Suppliers
.<Set<? extends Hardware>> 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<Image> images = existingImage == null ? ImmutableSet.<Image> of() : ImmutableSet.of(existingImage);
Set<Hardware> hardwares = existingHardware == null ? ImmutableSet.<Hardware> 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
.<Set<? extends Image>> ofInstance(images), Suppliers
.<Set<? extends Hardware>> 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<Image> images = existingImage == null ? ImmutableSet.<Image> of() : ImmutableSet.of(existingImage);
Set<Hardware> hardwares = existingHardware == null ? ImmutableSet.<Hardware> 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
.<Set<? extends Image>> ofInstance(images), Suppliers
.<Set<? extends Hardware>> 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) {

View File

@ -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<Server> {
new ImmutableMap.Builder<String, String>().put("Server Label", "Web Head 1")
.put("Image Version", "2.1").build())
.addresses(ImmutableMultimap.<String, Address>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")),