Collect all IP addresses before building the NodeMetadata object

This commit is contained in:
Andrei Savu 2012-01-31 17:59:26 +02:00
parent 19ff2eb89b
commit d209388d64
2 changed files with 151 additions and 72 deletions

View File

@ -18,19 +18,19 @@
*/
package org.jclouds.cloudstack.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.UncheckedExecutionException;
import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.domain.NIC;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized;
@ -46,18 +46,21 @@ import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.util.InetAddresses2;
import org.jclouds.util.Throwables2;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.UncheckedExecutionException;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Sets.newHashSet;
import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
import static org.jclouds.util.InetAddresses2.isPrivateIPAddress;
/**
* @author Adrian Cole
* @author Adrian Cole, Andrei Savu
*/
@Singleton
public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, NodeMetadata> {
@ -120,18 +123,28 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
builder.state(vmStateToNodeState.get(from.getState()));
// TODO: check to see public or private
Set<String> publicAddresses = newHashSet(), privateAddresses = newHashSet();
if (from.getIPAddress() != null) {
boolean isPrivate = InetAddresses2.isPrivateIPAddress(from.getIPAddress());
Set<String> addresses = ImmutableSet.<String> of(from.getIPAddress());
if (isPrivate)
builder.privateAddresses(addresses);
else
builder.publicAddresses(addresses);
boolean isPrivate = isPrivateIPAddress(from.getIPAddress());
if (isPrivate) {
privateAddresses.add(from.getIPAddress());
} else {
publicAddresses.add(from.getIPAddress());
}
}
for (NIC nic : from.getNICs()) {
if (nic.getIPAddress() != null) {
if (isPrivateIPAddress(nic.getIPAddress())) {
privateAddresses.add(nic.getIPAddress());
} else {
publicAddresses.add(nic.getIPAddress());
}
}
}
try {
builder.publicAddresses(transform(
/* Also add to the list of public IPs any public IP address that has a
forwarding rule that links to this machine */
Iterables.addAll(publicAddresses, transform(
filter(getIPForwardingRulesByVirtualMachine.getUnchecked(from.getId()),
new Predicate<IPForwardingRule>() {
@Override
@ -150,7 +163,7 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
throw e;
}
}
return builder.build();
return builder.privateAddresses(privateAddresses).publicAddresses(publicAddresses).build();
}
@Singleton

View File

@ -18,14 +18,18 @@
*/
package org.jclouds.cloudstack.compute.functions;
import static org.testng.Assert.assertEquals;
import java.net.UnknownHostException;
import java.util.Set;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.jclouds.cloudstack.compute.functions.VirtualMachineToNodeMetadata.FindImageForVirtualMachine;
import org.jclouds.cloudstack.compute.functions.VirtualMachineToNodeMetadata.FindLocationForVirtualMachine;
import org.jclouds.cloudstack.domain.GuestIPType;
import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.domain.NIC;
import org.jclouds.cloudstack.domain.TrafficType;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.parse.ListVirtualMachinesResponseTest;
import org.jclouds.compute.domain.Hardware;
@ -34,19 +38,18 @@ import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.domain.Location;
import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.net.UnknownHostException;
import java.util.Set;
import static org.testng.Assert.assertEquals;
/**
* @author Adrian Cole
* @author Adrian Cole, Andrei Savu
*/
@Test(groups = "unit", testName = "VirtualMachineToNodeMetadataTest")
public class VirtualMachineToNodeMetadataTest {
@ -87,6 +90,69 @@ public class VirtualMachineToNodeMetadataTest {
}
@Test
public void testApplyWhereVirtualMachineHasNoIpForwardingRuleAndAPublicIP() throws UnknownHostException {
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
locationSupplier), new FindImageForVirtualMachine(
imageSupplier), CacheBuilder.newBuilder().<Long, Set<IPForwardingRule>> build(
new CacheLoader<Long, Set<IPForwardingRule>>() {
@Override
public Set<IPForwardingRule> load(Long arg0) throws Exception {
return ImmutableSet.of();
}
}));
VirtualMachine guest =VirtualMachine.builder()
.id(54)
.name("i-3-54-VM")
.displayName("i-3-54-VM")
.account("adrian")
.domainId(1)
.domain("ROOT")
.created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-02-16T14:28:37-0800"))
.state(VirtualMachine.State.STARTING)
.isHAEnabled(false)
.zoneId(1)
.zoneName("San Jose 1")
.templateId(2)
.templateName("CentOS 5.3(64-bit) no GUI (XenServer)")
.templateDisplayText("CentOS 5.3(64-bit) no GUI (XenServer)")
.passwordEnabled(false)
.serviceOfferingId(1)
.serviceOfferingName("Small Instance")
.cpuCount(1)
.cpuSpeed(500)
.memory(512)
.guestOSId(11)
.rootDeviceId(0)
.rootDeviceType("NetworkFilesystem")
.jobId(63l)
.jobStatus(0)
.nics(ImmutableSet.of(NIC.builder().id(72).networkId(204).netmask("255.255.255.0").gateway("1.1.1.1")
.IPAddress("1.1.1.5").trafficType(TrafficType.GUEST).guestIPType(GuestIPType.VIRTUAL)
.isDefault(true).build())).hypervisor("XenServer").build();
NodeMetadata node = parser.apply(guest);
assertEquals(
node.toString(),
new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3")
.location(ZoneToLocationTest.one).state(NodeState.PENDING).hostname("i-3-54-VM")
.privateAddresses(ImmutableSet.<String>of())
.publicAddresses(ImmutableSet.<String>of("1.1.1.5"))
.hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer"))
.imageId(TemplateToImageTest.one.getId())
.operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
}
@Test
public void testApplyWhereVirtualMachineWithNoPassword() throws UnknownHostException {