Merge pull request #176 from andreisavu/option-for-static-nat

Allow users to disable the creation of a static nat for a new VM
This commit is contained in:
Adrian Cole 2011-11-20 23:05:21 -08:00
commit 8dd04b5ce5
3 changed files with 47 additions and 4 deletions

View File

@ -60,6 +60,7 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
protected Map<String, Long> ipsToNetworks = Maps.<String, Long>newLinkedHashMap(); protected Map<String, Long> ipsToNetworks = Maps.<String, Long>newLinkedHashMap();
protected String ipOnDefaultNetwork; protected String ipOnDefaultNetwork;
protected String keyPair; protected String keyPair;
protected boolean setupStaticNat = true;
@Override @Override
public CloudStackTemplateOptions clone() { public CloudStackTemplateOptions clone() {
@ -118,6 +119,15 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return networkIds; return networkIds;
} }
public CloudStackTemplateOptions setupStaticNat(boolean setupStaticNat) {
this.setupStaticNat = setupStaticNat;
return this;
}
public boolean shouldSetupStaticNat() {
return this.setupStaticNat;
}
/** /**
* @see DeployVirtualMachineOptions#ipOnDefaultNetwork * @see DeployVirtualMachineOptions#ipOnDefaultNetwork
*/ */
@ -206,6 +216,14 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return options.ipsToNetworks(ipToNetworkMap); return options.ipsToNetworks(ipToNetworkMap);
} }
/**
* @see CloudStackTemplateOptions#setupStaticNat
*/
public static CloudStackTemplateOptions setupStaticNat(boolean setupStaticNat) {
CloudStackTemplateOptions options = new CloudStackTemplateOptions();
return options.setupStaticNat(setupStaticNat);
}
/** /**
* @see CloudStackTemplateOptions#keyPair * @see CloudStackTemplateOptions#keyPair
*/ */

View File

@ -168,10 +168,12 @@ public class CloudStackComputeServiceAdapter implements
} else { } else {
credentials = credentialStore.get("keypair#" + templateOptions.getKeyPair()); credentials = credentialStore.get("keypair#" + templateOptions.getKeyPair());
} }
// TODO: possibly not all network ids, do we want to do this if (templateOptions.shouldSetupStaticNat()) {
for (long networkId : options.getNetworkIds()) { // TODO: possibly not all network ids, do we want to do this
// TODO: log this for (long networkId : options.getNetworkIds()) {
PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm); // TODO: log this
PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm);
}
} }
return new NodeAndInitialCredentials<VirtualMachine>(vm, vm.getId() + "", credentials); return new NodeAndInitialCredentials<VirtualMachine>(vm, vm.getId() + "", credentials);
} }

View File

@ -25,7 +25,10 @@ import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.B
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.networkIds; import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.networkIds;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupId; import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupId;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupIds; import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupIds;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.setupStaticNat;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -143,6 +146,26 @@ public class CloudStackTemplateOptionsTest {
.getIpsToNetworks().get("10.0.0.1").longValue(), 5L); .getIpsToNetworks().get("10.0.0.1").longValue(), 5L);
} }
@Test
public void testSetupStaticNatDefaultsTrue() {
TemplateOptions options = new CloudStackTemplateOptions();
assertTrue(options.as(CloudStackTemplateOptions.class)
.shouldSetupStaticNat());
}
@Test
public void testSetupStaticNat() {
TemplateOptions options = new CloudStackTemplateOptions().setupStaticNat(false);
assertFalse(options.as(CloudStackTemplateOptions.class)
.shouldSetupStaticNat());
}
@Test
public void testSetupStaticNatStatic() {
TemplateOptions options = setupStaticNat(false);
assertFalse(options.as(CloudStackTemplateOptions.class)
.shouldSetupStaticNat());
}
@Test @Test
public void testKeyPair() { public void testKeyPair() {