Merge pull request #165 from andreisavu/expose-static-ip

Expose the static ip settings as options
This commit is contained in:
Adrian Cole 2011-11-16 23:56:27 -08:00
commit 0e7b6a91e7
5 changed files with 106 additions and 18 deletions

View File

@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Maps;
import org.jclouds.cloudstack.options.DeployVirtualMachineOptions;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.options.TemplateOptions;
@ -56,6 +57,8 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
protected Set<Long> securityGroupIds = Sets.<Long> newLinkedHashSet();
protected Set<Long> networkIds = Sets.<Long> newLinkedHashSet();
protected Map<String, Long> ipsToNetworks = Maps.<String, Long>newLinkedHashMap();
protected String ipOnDefaultNetwork;
protected String keyPair;
@Override
@ -115,6 +118,30 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return networkIds;
}
/**
* @see DeployVirtualMachineOptions#ipOnDefaultNetwork
*/
public CloudStackTemplateOptions ipOnDefaultNetwork(String ipOnDefaultNetwork) {
this.ipOnDefaultNetwork = ipOnDefaultNetwork;
return this;
}
public String getIpOnDefaultNetwork() {
return ipOnDefaultNetwork;
}
/**
* @see DeployVirtualMachineOptions#ipOnDefaultNetwork(String)
*/
public CloudStackTemplateOptions ipsToNetworks(Map<String, Long> ipsToNetworks) {
this.ipsToNetworks.putAll(ipsToNetworks);
return this;
}
public Map<String, Long> getIpsToNetworks() {
return ipsToNetworks;
}
/**
* @see DeployVirtualMachineOptions#keyPair(String)
*/
@ -163,6 +190,22 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return options.networkIds(networkIds);
}
/**
* @see CloudStackTemplateOptions#ipOnDefaultNetwork
*/
public static CloudStackTemplateOptions ipOnDefaultNetwork(String ipAddress) {
CloudStackTemplateOptions options = new CloudStackTemplateOptions();
return options.ipOnDefaultNetwork(ipAddress);
}
/**
* @see CloudStackTemplateOptions#ipsToNetworks
*/
public static CloudStackTemplateOptions ipsToNetworks(Map<String, Long> ipToNetworkMap) {
CloudStackTemplateOptions options = new CloudStackTemplateOptions();
return options.ipsToNetworks(ipToNetworkMap);
}
/**
* @see CloudStackTemplateOptions#keyPair
*/

View File

@ -127,6 +127,14 @@ public class CloudStackComputeServiceAdapter implements
throw new IllegalArgumentException("please setup a network or security group for zone: " + zoneId);
}
if (templateOptions.getIpOnDefaultNetwork() != null) {
options.ipOnDefaultNetwork(templateOptions.getIpOnDefaultNetwork());
}
if (templateOptions.getIpsToNetworks().size() > 0) {
options.ipsToNetworks(templateOptions.getIpsToNetworks());
}
if (templateOptions.getKeyPair() != null) {
options.keyPair(templateOptions.getKeyPair());
if (templateOptions.getRunScript() != null) {

View File

@ -107,28 +107,28 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
}
/**
* @param ipAddress
* @param ipOnDefaultNetwork
* the requested ip address (2.2.12 only option)
*/
public DeployVirtualMachineOptions ipAddress(String ipAddress) {
this.queryParameters.replaceValues("ipaddress", ImmutableSet.of(ipAddress));
public DeployVirtualMachineOptions ipOnDefaultNetwork(String ipOnDefaultNetwork) {
this.queryParameters.replaceValues("ipaddress", ImmutableSet.of(ipOnDefaultNetwork));
return this;
}
/**
* @param ipToNetworkList
* @param ipsToNetworks
* mapping ip addresses to network ids (2.2.12 only option)
*/
public DeployVirtualMachineOptions ipToNetworkList(Map<String, Long> ipToNetworkList) {
public DeployVirtualMachineOptions ipsToNetworks(Map<String, Long> ipsToNetworks) {
int count = 0;
for(String ip : ipToNetworkList.keySet()) {
for(String ip : ipsToNetworks.keySet()) {
this.queryParameters.replaceValues(
String.format("ipnetworklist[%d].ip", count),
ImmutableSet.of(ip)
);
this.queryParameters.replaceValues(
String.format("ipnetworklist[%d].networkid", count),
ImmutableSet.of("" + ipToNetworkList.get(ip))
ImmutableSet.of("" + ipsToNetworks.get(ip))
);
count += 1;
}
@ -268,19 +268,19 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
}
/**
* @see DeployVirtualMachineOptions#ipAddress
* @see DeployVirtualMachineOptions#ipOnDefaultNetwork
*/
public static DeployVirtualMachineOptions ipAddress(String ipAddress) {
public static DeployVirtualMachineOptions ipOnDefaultNetwork(String ipOnDefaultNetwork) {
DeployVirtualMachineOptions options = new DeployVirtualMachineOptions();
return options.ipAddress(ipAddress);
return options.ipOnDefaultNetwork(ipOnDefaultNetwork);
}
/**
* @see DeployVirtualMachineOptions#ipToNetworkList
* @see DeployVirtualMachineOptions#ipsToNetworks
*/
public static DeployVirtualMachineOptions ipToNetworkList(Map<String, Long> ipToNetworkList) {
public static DeployVirtualMachineOptions ipsToNetworks(Map<String, Long> ipsToNetworks) {
DeployVirtualMachineOptions options = new DeployVirtualMachineOptions();
return options.ipToNetworkList(ipToNetworkList);
return options.ipsToNetworks(ipsToNetworks);
}
/**

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.cloudstack.compute.options;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.ipOnDefaultNetwork;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.ipsToNetworks;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.keyPair;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.networkId;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.networkIds;
@ -29,6 +31,9 @@ import org.jclouds.compute.options.TemplateOptions;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import org.testng.collections.Maps;
import java.util.Map;
/**
* Tests possible uses of {@code CloudStackTemplateOptions} and
@ -106,6 +111,39 @@ public class CloudStackTemplateOptionsTest {
assertEquals(options.as(CloudStackTemplateOptions.class).getNetworkIds(), ImmutableSet.of(3l));
}
@Test
public void testIpOnDefaultNetwork() {
TemplateOptions options = new CloudStackTemplateOptions().ipOnDefaultNetwork("10.0.0.1");
assertEquals(options.as(CloudStackTemplateOptions.class).getIpOnDefaultNetwork(), "10.0.0.1");
}
@Test
public void testIpOnDefaultNetworkStatic() {
TemplateOptions options = ipOnDefaultNetwork("10.0.0.1");
assertEquals(options.as(CloudStackTemplateOptions.class).getIpOnDefaultNetwork(), "10.0.0.1");
}
@Test
public void testIpsToNetwork() {
Map<String, Long> ipsToNetworks = Maps.newHashMap();
ipsToNetworks.put("10.0.0.1", 5L);
TemplateOptions options = new CloudStackTemplateOptions().ipsToNetworks(ipsToNetworks);
assertEquals(options.as(CloudStackTemplateOptions.class)
.getIpsToNetworks().get("10.0.0.1").longValue(), 5L);
}
@Test
public void testIpsToNetworkStatic() {
Map<String, Long> ipsToNetworks = Maps.newHashMap();
ipsToNetworks.put("10.0.0.1", 5L);
TemplateOptions options = ipsToNetworks(ipsToNetworks);
assertEquals(options.as(CloudStackTemplateOptions.class)
.getIpsToNetworks().get("10.0.0.1").longValue(), 5L);
}
@Test
public void testKeyPair() {
TemplateOptions options = keyPair("test");

View File

@ -19,7 +19,6 @@
package org.jclouds.cloudstack.features;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.or;
import static com.google.common.collect.Iterables.find;
@ -143,7 +142,7 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
return network.getId();
}
}));
options.ipToNetworkList(ipToNetwork);
options.ipsToNetworks(ipToNetwork);
return createVirtualMachineWithOptionsInZone(options, zoneId, templateId,
client, jobComplete, virtualMachineRunning);
@ -244,12 +243,12 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
String ipAddress = "192.168.0.4";
Map<String, Long> ipToNetwork = Maps.newHashMap();
ipToNetwork.put(ipAddress, network.getId());
Map<String, Long> ipsToNetworks = Maps.newHashMap();
ipsToNetworks.put(ipAddress, network.getId());
vm = createVirtualMachineInNetworkWithIp(
adminClient, templateId, ImmutableSet.of(requiredNetwork, network),
ipToNetwork, adminJobComplete, adminVirtualMachineRunning);
ipsToNetworks, adminJobComplete, adminVirtualMachineRunning);
logger.info("Created VM: " + vm);
boolean hasStaticIpNic = false;