From 36358bcca7b61ac4b3becf5d5aa6088466c2cfc5 Mon Sep 17 00:00:00 2001 From: Graeme-Miller Date: Thu, 19 Nov 2015 16:08:43 +0000 Subject: [PATCH] Instead of setting up static NAT for all networks, we setup static NAT for default network only --- .../CloudStackComputeServiceAdapter.java | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java index 3806a83989..005902b896 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java @@ -42,6 +42,7 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; +import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.common.primitives.Ints; import org.jclouds.cloudstack.CloudStackApi; @@ -50,6 +51,7 @@ import org.jclouds.cloudstack.domain.AsyncCreateResponse; import org.jclouds.cloudstack.domain.Capabilities; import org.jclouds.cloudstack.domain.FirewallRule; import org.jclouds.cloudstack.domain.IPForwardingRule; +import org.jclouds.cloudstack.domain.NIC; import org.jclouds.cloudstack.domain.Network; import org.jclouds.cloudstack.domain.NetworkType; import org.jclouds.cloudstack.domain.Project; @@ -273,24 +275,30 @@ public class CloudStackComputeServiceAdapter implements vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); } if (templateOptions.shouldSetupStaticNat()) { - Capabilities capabilities = client.getConfigurationApi().listCapabilities(); - // TODO: possibly not all network ids, do we want to do this - for (String networkId : options.getNetworkIds()) { - logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId); - PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm); - logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId()); - vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); - List ports = Ints.asList(templateOptions.getInboundPorts()); - if (capabilities.getCloudStackVersion().startsWith("2")) { - logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports); - Set rules = setupPortForwardingRulesForIP.apply(ip, ports); - logger.trace("<< setup %d IP forwarding rules on IPAddress(%s)", rules.size(), ip.getId()); - } else { - logger.debug(">> setting up firewall rules for IPAddress(%s) rules(%s)", ip.getId(), ports); - Set rules = setupFirewallRulesForIP.apply(ip, ports); - logger.trace("<< setup %d firewall rules on IPAddress(%s)", rules.size(), ip.getId()); - } - } + Capabilities capabilities = client.getConfigurationApi().listCapabilities(); + + NIC nic = Iterables.find(vm.getNICs(), new Predicate() { + @Override + public boolean apply(NIC input) { + return (input == null) ? false : input.isDefault(); + } + }); + String networkId = nic.getNetworkId(); + + logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId); + PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm); + logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId()); + vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); + List ports = Ints.asList(templateOptions.getInboundPorts()); + if (capabilities.getCloudStackVersion().startsWith("2")) { + logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports); + Set rules = setupPortForwardingRulesForIP.apply(ip, ports); + logger.trace("<< setup %d IP forwarding rules on IPAddress(%s)", rules.size(), ip.getId()); + } else { + logger.debug(">> setting up firewall rules for IPAddress(%s) rules(%s)", ip.getId(), ports); + Set rules = setupFirewallRulesForIP.apply(ip, ports); + logger.trace("<< setup %d firewall rules on IPAddress(%s)", rules.size(), ip.getId()); + } } } catch (RuntimeException re) { logger.error("-- exception after node has been created, trying to destroy the created virtualMachine(%s)", vm.getId());