From e0ea017a562cd448af78116f608c8d64e9e5739c Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Tue, 6 Aug 2013 18:17:06 -0700 Subject: [PATCH] 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. --- .../options/CloudStackTemplateOptions.java | 44 +++++++++++++++ .../CloudStackComputeServiceAdapter.java | 7 +++ .../options/DeployVirtualMachineOptions.java | 4 +- ...dStackComputeServiceAdapterExpectTest.java | 54 +++++++++++++++++++ .../CloudStackTemplateOptionsTest.java | 20 +++++++ 5 files changed, 126 insertions(+), 3 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java index 4b05a2116c..14e72d40fa 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java @@ -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 */ 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 ddc7f7a1b6..ecfe215616 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 @@ -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 inboundPorts = Ints.asList(templateOptions.getInboundPorts()); diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java index dcec8f6c51..c52d5f26b0 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java @@ -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; } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterExpectTest.java index 4eb89aa183..a79e29b6b4 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterExpectTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterExpectTest.java @@ -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 requestResponseMap = ImmutableMap. 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 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") diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptionsTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptionsTest.java index 7bb81f568d..747f0cca44 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptionsTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptionsTest.java @@ -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");