JCLOUDS-235. Add support for datadisks to CloudStackTemplateOptions

Also adds support for the new options to
CloudStackComputeServiceAdapter, unsurprisingly. Also got rid of the
in fact wrong mutual exclusivity of dataDiskSize and diskOfferingId in
DeployVirtualMachineOptions - that's a misleading bit from the
CloudStack API docs.
This commit is contained in:
Andrew Bayer 2013-08-06 18:17:06 -07:00
parent bf2138089d
commit e0ea017a56
5 changed files with 126 additions and 3 deletions

View File

@ -60,6 +60,8 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
protected String domainId;
protected boolean generateKeyPair = false;
protected boolean generateSecurityGroup = false;
protected String diskOfferingId;
protected int dataDiskSize;
@Override
public CloudStackTemplateOptions clone() {
@ -83,9 +85,35 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
eTo.account(this.account);
eTo.domainId(this.domainId);
eTo.setupStaticNat(setupStaticNat);
eTo.diskOfferingId(diskOfferingId);
eTo.dataDiskSize(dataDiskSize);
}
}
/**
* @see org.jclouds.cloudstack.options.DeployVirtualMachineOptions#diskOfferingId
*/
public CloudStackTemplateOptions diskOfferingId(String diskOfferingId) {
this.diskOfferingId = diskOfferingId;
return this;
}
public String getDiskOfferingId() {
return diskOfferingId;
}
/**
* @see DeployVirtualMachineOptions#dataDiskSize
*/
public CloudStackTemplateOptions dataDiskSize(int dataDiskSize) {
this.dataDiskSize = dataDiskSize;
return this;
}
public int getDataDiskSize() {
return dataDiskSize;
}
/**
* @see DeployVirtualMachineOptions#securityGroupId
*/
@ -230,6 +258,22 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
public static class Builder {
/**
* @see CloudStackTemplateOptions#diskOfferingId
*/
public static CloudStackTemplateOptions diskOfferingId(String diskOfferingId) {
CloudStackTemplateOptions options = new CloudStackTemplateOptions();
return options.diskOfferingId(diskOfferingId);
}
/**
* @see CloudStackTemplateOptions#dataDiskSize
*/
public static CloudStackTemplateOptions dataDiskSize(int dataDiskSize) {
CloudStackTemplateOptions options = new CloudStackTemplateOptions();
return options.dataDiskSize(dataDiskSize);
}
/**
* @see CloudStackTemplateOptions#securityGroupId
*/

View File

@ -196,6 +196,13 @@ public class CloudStackComputeServiceAdapter implements
options.keyPair(keyPair.getName());
}
if (templateOptions.getDiskOfferingId() != null) {
options.diskOfferingId(templateOptions.getDiskOfferingId());
if (templateOptions.getDataDiskSize() > 0) {
options.dataDiskSize(templateOptions.getDataDiskSize());
}
}
if (supportsSecurityGroups().apply(zone)) {
List<Integer> inboundPorts = Ints.asList(templateOptions.getInboundPorts());

View File

@ -199,11 +199,9 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
/**
* @param dataDiskSize
* the arbitrary size for the DATADISK volume. Mutually exclusive
* with diskOfferingId
* the arbitrary size for the DATADISK volume.
*/
public DeployVirtualMachineOptions dataDiskSize(long dataDiskSize) {
checkArgument(!queryParameters.containsKey("diskofferingid"), "Mutually exclusive with diskOfferingId");
this.queryParameters.replaceValues("size", ImmutableSet.of(dataDiskSize + ""));
return this;
}

View File

@ -274,6 +274,60 @@ public class CloudStackComputeServiceAdapterExpectTest extends BaseCloudStackCom
assertEquals(server.getCredentials().getPrivateKey(), privKey);
}
public void testCreateNodeWithGroupEncodedIntoNameWithKeyPairDefaultSecurityGroupAndDisk() throws IOException {
HttpRequest deployVM = HttpRequest.builder().method("GET")
.endpoint("http://localhost:8080/client/api")
.addQueryParam("response", "json")
.addQueryParam("command", "deployVirtualMachine")
.addQueryParam("zoneid", "2")
.addQueryParam("serviceofferingid", "1")
.addQueryParam("templateid", "241")
.addQueryParam("displayname", "test-e92")
.addQueryParam("name", "test-e92")
.addQueryParam("networkids", "204")
.addQueryParam("keypair", "mykeypair")
.addQueryParam("diskofferingid", "5678")
.addQueryParam("size", "10")
.addQueryParam("apiKey", "APIKEY")
.addQueryParam("signature", "FWWCEpsrbbjxiqoQve302rrfOjI%3D")
.addHeader("Accept", "application/json")
.build();
Map<HttpRequest, HttpResponse> requestResponseMap = ImmutableMap.<HttpRequest, HttpResponse> builder()
.put(listTemplates, listTemplatesResponse)
.put(listOsTypes, listOsTypesResponse)
.put(listOsCategories, listOsCategoriesResponse)
.put(listZones, listZonesResponse)
.put(listServiceOfferings, listServiceOfferingsResponse)
.put(listAccounts, listAccountsResponse)
.put(listNetworks, listNetworksWithSecurityGroupsResponse)
.put(getZoneWithSecurityGroups, getZoneWithSecurityGroupsResponse)
.put(deployVM, deployVMResponse)
.put(queryAsyncJobResult, queryAsyncJobResultResponse)
.build();
Injector forKeyPair = requestsSendResponses(requestResponseMap);
String privKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test"));
Template template = forKeyPair.getInstance(TemplateBuilder.class)
.osFamily(OsFamily.CENTOS)
.locationId("2")
.build();
template.getOptions().as(CloudStackTemplateOptions.class).keyPair("mykeypair")
.diskOfferingId("5678")
.dataDiskSize(10)
.setupStaticNat(false)
.overrideLoginPrivateKey(privKey);
CloudStackComputeServiceAdapter adapter = forKeyPair.getInstance(CloudStackComputeServiceAdapter.class);
NodeAndInitialCredentials<VirtualMachine> server = adapter.createNodeWithGroupEncodedIntoName("test", "test-e92",
template);
assertNotNull(server);
assertEquals(server.getCredentials().getPrivateKey(), privKey);
}
public void testCreateNodeWithGroupEncodedIntoNameWithKeyPairGenerateSecurityGroup() throws IOException {
HttpRequest deployVM = HttpRequest.builder().method("GET")
.endpoint("http://localhost:8080/client/api")

View File

@ -17,6 +17,8 @@
package org.jclouds.cloudstack.compute.options;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.account;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.dataDiskSize;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.diskOfferingId;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.domainId;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.generateKeyPair;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.generateSecurityGroup;
@ -220,6 +222,24 @@ public class CloudStackTemplateOptionsTest {
assertEquals(options.as(CloudStackTemplateOptions.class).getKeyPair(), "test");
}
@Test
public void testDiskOfferingId() {
TemplateOptions options = diskOfferingId("test");
assertEquals(options.as(CloudStackTemplateOptions.class).getDiskOfferingId(), "test");
}
@Test
public void testDataDiskSizeDefault() {
TemplateOptions options = new CloudStackTemplateOptions();
assertEquals(options.as(CloudStackTemplateOptions.class).getDataDiskSize(), 0);
}
@Test
public void testDataDiskSize() {
TemplateOptions options = dataDiskSize(10);
assertEquals(options.as(CloudStackTemplateOptions.class).getDataDiskSize(), 10);
}
@Test
public void testAccount() {
TemplateOptions options = account("test");