Instead of setting up static NAT for all networks, we setup static NAT for default network only

This commit is contained in:
Graeme-Miller 2015-11-19 16:08:43 +00:00 committed by Andrea Turli
parent 72889bbf19
commit 36358bcca7
1 changed files with 26 additions and 18 deletions

View File

@ -42,6 +42,7 @@ import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder; import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import org.jclouds.cloudstack.CloudStackApi; 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.Capabilities;
import org.jclouds.cloudstack.domain.FirewallRule; import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.IPForwardingRule; import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.domain.NIC;
import org.jclouds.cloudstack.domain.Network; import org.jclouds.cloudstack.domain.Network;
import org.jclouds.cloudstack.domain.NetworkType; import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Project; import org.jclouds.cloudstack.domain.Project;
@ -273,24 +275,30 @@ public class CloudStackComputeServiceAdapter implements
vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId());
} }
if (templateOptions.shouldSetupStaticNat()) { if (templateOptions.shouldSetupStaticNat()) {
Capabilities capabilities = client.getConfigurationApi().listCapabilities(); Capabilities capabilities = client.getConfigurationApi().listCapabilities();
// TODO: possibly not all network ids, do we want to do this
for (String networkId : options.getNetworkIds()) { NIC nic = Iterables.find(vm.getNICs(), new Predicate<NIC>() {
logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId); @Override
PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm); public boolean apply(NIC input) {
logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId()); return (input == null) ? false : input.isDefault();
vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); }
List<Integer> ports = Ints.asList(templateOptions.getInboundPorts()); });
if (capabilities.getCloudStackVersion().startsWith("2")) { String networkId = nic.getNetworkId();
logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports);
Set<IPForwardingRule> rules = setupPortForwardingRulesForIP.apply(ip, ports); logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId);
logger.trace("<< setup %d IP forwarding rules on IPAddress(%s)", rules.size(), ip.getId()); PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm);
} else { logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId());
logger.debug(">> setting up firewall rules for IPAddress(%s) rules(%s)", ip.getId(), ports); vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId());
Set<FirewallRule> rules = setupFirewallRulesForIP.apply(ip, ports); List<Integer> ports = Ints.asList(templateOptions.getInboundPorts());
logger.trace("<< setup %d firewall rules on IPAddress(%s)", rules.size(), ip.getId()); if (capabilities.getCloudStackVersion().startsWith("2")) {
} logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports);
} Set<IPForwardingRule> 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<FirewallRule> rules = setupFirewallRulesForIP.apply(ip, ports);
logger.trace("<< setup %d firewall rules on IPAddress(%s)", rules.size(), ip.getId());
}
} }
} catch (RuntimeException re) { } catch (RuntimeException re) {
logger.error("-- exception after node has been created, trying to destroy the created virtualMachine(%s)", vm.getId()); logger.error("-- exception after node has been created, trying to destroy the created virtualMachine(%s)", vm.getId());