From fb488c192db6aed7e802963b2df0e524d53e2c89 Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Mon, 12 Mar 2012 18:11:22 +0000 Subject: [PATCH] added public address for vms and unit tested imachinetonodemetadata --- .../functions/IMachineToNodeMetadata.java | 54 +++++++++---------- .../virtualbox/functions/NodeCreator.java | 10 ++-- .../functions/IMachineToNodeMetadataTest.java | 53 +++++++++++------- 3 files changed, 65 insertions(+), 52 deletions(-) diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java index 16fa75c6a4..5b9f4086d8 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java @@ -19,6 +19,8 @@ package org.jclouds.virtualbox.functions; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; import static org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule.machineToNodeState; import java.util.HashSet; @@ -41,6 +43,7 @@ import org.jclouds.logging.Logger; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.INetworkAdapter; import org.virtualbox_4_1.MachineState; +import org.virtualbox_4_1.NetworkAttachmentType; import com.google.common.base.Function; import com.google.common.base.Splitter; @@ -66,22 +69,7 @@ public class IMachineToNodeMetadata implements Function locationBuilder.scope(LocationScope.HOST); nodeMetadataBuilder.location(locationBuilder.build()); - HardwareBuilder hardwareBuilder = new HardwareBuilder(); - hardwareBuilder.ram(vm.getMemorySize().intValue()); - - // TODO: Get more processor information - Set processors = new HashSet(); - for (int i = 0; i < vm.getCPUCount(); i++) { - Processor processor = new Processor(1, 0); - processors.add(processor); - } - hardwareBuilder.processors(processors); - - // TODO: How to get this? - hardwareBuilder.is64Bit(false); - nodeMetadataBuilder.hostname(vm.getName()); - MachineState vmState = vm.getState(); NodeState nodeState = machineToNodeState.get(vmState); @@ -91,23 +79,35 @@ public class IMachineToNodeMetadata implements Function logger.debug("Setting virtualbox node to: " + nodeState + " from machine state: " + vmState); - INetworkAdapter networkAdapter = vm.getNetworkAdapter(0l); - if (networkAdapter != null) { - nodeMetadataBuilder.privateAddresses(ImmutableSet.of(networkAdapter.getNatDriver().getHostIP())); - for (String nameProtocolnumberAddressInboudportGuestTargetport : networkAdapter.getNatDriver().getRedirects()){ - Iterable stuff = Splitter.on(',').split(nameProtocolnumberAddressInboudportGuestTargetport); - String protocolNumber = Iterables.get(stuff, 1); - String hostAddress= Iterables.get(stuff, 2); - String inboundPort= Iterables.get(stuff, 3); - String targetPort= Iterables.get(stuff, 5); - if ("1".equals(protocolNumber) && "22".equals(targetPort)) - nodeMetadataBuilder.privateAddresses(ImmutableSet.of(hostAddress)).loginPort(Integer.parseInt(inboundPort)); + // hardcoded set-up that works only for nat+host-only + + // nat adapter + INetworkAdapter natAdapter = vm.getNetworkAdapter(0l); + checkNotNull(natAdapter, "slot 0 networkadapter"); + checkState(natAdapter.getAttachmentType() == NetworkAttachmentType.NAT, + "expecting slot 0 to be a NAT attachment type (was: " + natAdapter.getAttachmentType() + ")"); + + int ipTermination = 0; + + nodeMetadataBuilder.publicAddresses(ImmutableSet.of(natAdapter.getNatDriver().getHostIP())); + for (String nameProtocolnumberAddressInboudportGuestTargetport : natAdapter.getNatDriver().getRedirects()) { + Iterable stuff = Splitter.on(',').split(nameProtocolnumberAddressInboudportGuestTargetport); + String protocolNumber = Iterables.get(stuff, 1); + String hostAddress = Iterables.get(stuff, 2); + String inboundPort = Iterables.get(stuff, 3); + String targetPort = Iterables.get(stuff, 5); + if ("1".equals(protocolNumber) && "22".equals(targetPort)) { + int inPort = Integer.parseInt(inboundPort); + ipTermination = inPort % NodeCreator.NODE_PORT_INIT; + nodeMetadataBuilder.publicAddresses(ImmutableSet.of(hostAddress)).loginPort(inPort); } } + nodeMetadataBuilder.privateAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + "")); + LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true); nodeMetadataBuilder.credentials(loginCredentials); - + return nodeMetadataBuilder.build(); } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java index 900118dc89..3270c5a894 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java @@ -68,8 +68,7 @@ public class NodeCreator implements Function manager; private final Function cloner; - private final AtomicInteger nodePorts; - private final AtomicInteger nodeIps; + private final AtomicInteger nodes; private MachineUtils machineUtils; private Function imachineToNodeMetadata; @@ -78,8 +77,7 @@ public class NodeCreator implements Function imachineToNodeMetadata) { this.manager = manager; this.cloner = cloner; - this.nodePorts = new AtomicInteger(NODE_PORT_INIT); - this.nodeIps = new AtomicInteger(1); + this.nodes = new AtomicInteger(0); this.machineUtils = machineUtils; this.imachineToNodeMetadata = imachineToNodeMetadata; } @@ -117,12 +115,12 @@ public class NodeCreator implements Function machineToNodeState = VirtualBoxComputeServiceContextModule.machineToNodeState; - Set images = ImmutableSet.of(); - Set hardwares = ImmutableSet.of(); + expect(vm.getName()).andReturn("mocked-vm").anyTimes(); + expect(vm.getState()).andReturn(MachineState.PoweredOff).once(); - VirtualBox virtualBox = new VirtualBox(); - IMachineToNodeMetadata parser = new IMachineToNodeMetadata(); - IMachineToHardware hwParser = new IMachineToHardware(Suppliers.ofInstance(manager)); + INetworkAdapter nat = createNiceMock(INetworkAdapter.class); + INATEngine natEng = createNiceMock(INATEngine.class); - // hwParser.apply() + expect(vm.getNetworkAdapter(eq(0l))).andReturn(nat).once(); + expect(nat.getAttachmentType()).andReturn(NetworkAttachmentType.NAT).once(); + expect(nat.getNatDriver()).andReturn(natEng).anyTimes(); + expect(natEng.getHostIP()).andReturn("127.0.0.1").once(); + expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,3001,,22")); + INetworkAdapter hostOnly = createNiceMock(INetworkAdapter.class); + + replay(vm, nat, natEng, hostOnly); + + NodeMetadata node = new IMachineToNodeMetadata().apply(vm); + + assertEquals("mocked-vm", node.getName()); + assertEquals(1, node.getPrivateAddresses().size()); + assertEquals((NodeCreator.VMS_NETWORK + 1), Iterables.get(node.getPrivateAddresses(), 0)); + assertEquals(1, node.getPublicAddresses().size()); + assertEquals("127.0.0.1", Iterables.get(node.getPublicAddresses(), 0)); + assertEquals(3001, node.getLoginPort()); } }