mirror of https://github.com/apache/jclouds.git
Collect all IP addresses before building the NodeMetadata object
This commit is contained in:
parent
19ff2eb89b
commit
d209388d64
|
@ -18,19 +18,19 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.compute.functions;
|
package org.jclouds.cloudstack.compute.functions;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import com.google.common.base.Function;
|
||||||
import static com.google.common.collect.Iterables.filter;
|
import com.google.common.base.Predicate;
|
||||||
import static com.google.common.collect.Iterables.transform;
|
import com.google.common.base.Supplier;
|
||||||
import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
|
import com.google.common.base.Throwables;
|
||||||
|
import com.google.common.cache.LoadingCache;
|
||||||
import java.util.Map;
|
import com.google.common.collect.ImmutableList;
|
||||||
import java.util.Set;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
import javax.annotation.Nullable;
|
import com.google.common.collect.Iterables;
|
||||||
import javax.inject.Inject;
|
import com.google.common.collect.Sets;
|
||||||
import javax.inject.Singleton;
|
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||||
|
|
||||||
import org.jclouds.cloudstack.domain.IPForwardingRule;
|
import org.jclouds.cloudstack.domain.IPForwardingRule;
|
||||||
|
import org.jclouds.cloudstack.domain.NIC;
|
||||||
import org.jclouds.cloudstack.domain.VirtualMachine;
|
import org.jclouds.cloudstack.domain.VirtualMachine;
|
||||||
import org.jclouds.collect.FindResourceInSet;
|
import org.jclouds.collect.FindResourceInSet;
|
||||||
import org.jclouds.collect.Memoized;
|
import org.jclouds.collect.Memoized;
|
||||||
|
@ -46,18 +46,21 @@ import org.jclouds.rest.ResourceNotFoundException;
|
||||||
import org.jclouds.util.InetAddresses2;
|
import org.jclouds.util.InetAddresses2;
|
||||||
import org.jclouds.util.Throwables2;
|
import org.jclouds.util.Throwables2;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import javax.annotation.Nullable;
|
||||||
import com.google.common.base.Predicate;
|
import javax.inject.Inject;
|
||||||
import com.google.common.base.Supplier;
|
import javax.inject.Singleton;
|
||||||
import com.google.common.base.Throwables;
|
import java.util.Map;
|
||||||
import com.google.common.cache.LoadingCache;
|
import java.util.Set;
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import static com.google.common.collect.Iterables.filter;
|
||||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
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
|
@Singleton
|
||||||
public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, NodeMetadata> {
|
public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, NodeMetadata> {
|
||||||
|
@ -120,18 +123,28 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
|
||||||
|
|
||||||
builder.state(vmStateToNodeState.get(from.getState()));
|
builder.state(vmStateToNodeState.get(from.getState()));
|
||||||
|
|
||||||
// TODO: check to see public or private
|
Set<String> publicAddresses = newHashSet(), privateAddresses = newHashSet();
|
||||||
if (from.getIPAddress() != null) {
|
if (from.getIPAddress() != null) {
|
||||||
boolean isPrivate = InetAddresses2.isPrivateIPAddress(from.getIPAddress());
|
boolean isPrivate = isPrivateIPAddress(from.getIPAddress());
|
||||||
Set<String> addresses = ImmutableSet.<String> of(from.getIPAddress());
|
if (isPrivate) {
|
||||||
if (isPrivate)
|
privateAddresses.add(from.getIPAddress());
|
||||||
builder.privateAddresses(addresses);
|
} else {
|
||||||
else
|
publicAddresses.add(from.getIPAddress());
|
||||||
builder.publicAddresses(addresses);
|
}
|
||||||
|
}
|
||||||
|
for (NIC nic : from.getNICs()) {
|
||||||
|
if (nic.getIPAddress() != null) {
|
||||||
|
if (isPrivateIPAddress(nic.getIPAddress())) {
|
||||||
|
privateAddresses.add(nic.getIPAddress());
|
||||||
|
} else {
|
||||||
|
publicAddresses.add(nic.getIPAddress());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
/* Also add to the list of public IPs any public IP address that has a
|
||||||
builder.publicAddresses(transform(
|
forwarding rule that links to this machine */
|
||||||
|
Iterables.addAll(publicAddresses, transform(
|
||||||
filter(getIPForwardingRulesByVirtualMachine.getUnchecked(from.getId()),
|
filter(getIPForwardingRulesByVirtualMachine.getUnchecked(from.getId()),
|
||||||
new Predicate<IPForwardingRule>() {
|
new Predicate<IPForwardingRule>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -150,7 +163,7 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return builder.build();
|
return builder.privateAddresses(privateAddresses).publicAddresses(publicAddresses).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|
|
@ -18,14 +18,18 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.compute.functions;
|
package org.jclouds.cloudstack.compute.functions;
|
||||||
|
|
||||||
import static org.testng.Assert.assertEquals;
|
import com.google.common.base.Supplier;
|
||||||
|
import com.google.common.base.Suppliers;
|
||||||
import java.net.UnknownHostException;
|
import com.google.common.cache.CacheBuilder;
|
||||||
import java.util.Set;
|
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.FindImageForVirtualMachine;
|
||||||
import org.jclouds.cloudstack.compute.functions.VirtualMachineToNodeMetadata.FindLocationForVirtualMachine;
|
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.IPForwardingRule;
|
||||||
|
import org.jclouds.cloudstack.domain.NIC;
|
||||||
|
import org.jclouds.cloudstack.domain.TrafficType;
|
||||||
import org.jclouds.cloudstack.domain.VirtualMachine;
|
import org.jclouds.cloudstack.domain.VirtualMachine;
|
||||||
import org.jclouds.cloudstack.parse.ListVirtualMachinesResponseTest;
|
import org.jclouds.cloudstack.parse.ListVirtualMachinesResponseTest;
|
||||||
import org.jclouds.compute.domain.Hardware;
|
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.NodeMetadata;
|
||||||
import org.jclouds.compute.domain.NodeMetadataBuilder;
|
import org.jclouds.compute.domain.NodeMetadataBuilder;
|
||||||
import org.jclouds.compute.domain.NodeState;
|
import org.jclouds.compute.domain.NodeState;
|
||||||
|
import org.jclouds.date.internal.SimpleDateFormatDateService;
|
||||||
import org.jclouds.domain.Location;
|
import org.jclouds.domain.Location;
|
||||||
import org.jclouds.rest.ResourceNotFoundException;
|
import org.jclouds.rest.ResourceNotFoundException;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
import java.net.UnknownHostException;
|
||||||
import com.google.common.base.Suppliers;
|
import java.util.Set;
|
||||||
import com.google.common.cache.CacheBuilder;
|
|
||||||
import com.google.common.cache.CacheLoader;
|
import static org.testng.Assert.assertEquals;
|
||||||
import com.google.common.collect.ImmutableSet;
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole, Andrei Savu
|
||||||
*/
|
*/
|
||||||
@Test(groups = "unit", testName = "VirtualMachineToNodeMetadataTest")
|
@Test(groups = "unit", testName = "VirtualMachineToNodeMetadataTest")
|
||||||
public class VirtualMachineToNodeMetadataTest {
|
public class VirtualMachineToNodeMetadataTest {
|
||||||
|
@ -55,10 +58,10 @@ public class VirtualMachineToNodeMetadataTest {
|
||||||
public void testApplyWhereVirtualMachineWithIPForwardingRule() throws UnknownHostException {
|
public void testApplyWhereVirtualMachineWithIPForwardingRule() throws UnknownHostException {
|
||||||
|
|
||||||
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
|
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
|
||||||
.<Location> of(ZoneToLocationTest.one, ZoneToLocationTest.two));
|
.<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
|
||||||
|
|
||||||
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
|
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
|
||||||
.<Image> of(TemplateToImageTest.one, TemplateToImageTest.two));
|
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
|
||||||
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
|
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
|
||||||
locationSupplier), new FindImageForVirtualMachine(
|
locationSupplier), new FindImageForVirtualMachine(
|
||||||
imageSupplier), CacheBuilder.newBuilder().<Long, Set<IPForwardingRule>> build(
|
imageSupplier), CacheBuilder.newBuilder().<Long, Set<IPForwardingRule>> build(
|
||||||
|
@ -87,14 +90,77 @@ 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
|
@Test
|
||||||
public void testApplyWhereVirtualMachineWithNoPassword() throws UnknownHostException {
|
public void testApplyWhereVirtualMachineWithNoPassword() throws UnknownHostException {
|
||||||
|
|
||||||
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
|
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
|
||||||
.<Location> of(ZoneToLocationTest.one, ZoneToLocationTest.two));
|
.<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
|
||||||
|
|
||||||
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
|
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
|
||||||
.<Image> of(TemplateToImageTest.one, TemplateToImageTest.two));
|
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
|
||||||
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
|
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
|
||||||
locationSupplier), new FindImageForVirtualMachine(
|
locationSupplier), new FindImageForVirtualMachine(
|
||||||
imageSupplier), CacheBuilder.newBuilder().<Long, Set<IPForwardingRule>> build(
|
imageSupplier), CacheBuilder.newBuilder().<Long, Set<IPForwardingRule>> build(
|
||||||
|
|
Loading…
Reference in New Issue