From 082d5404a9b40f1d80004f0b52896c0256a5cbda Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Wed, 23 Dec 2009 06:13:59 +0000 Subject: [PATCH] Issue 112: added delete disk command git-svn-id: http://jclouds.googlecode.com/svn/trunk@2506 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../vcloud/domain/ResourceAllocation.java | 10 +- .../BindVAppConfigurationToXmlPayload.java | 5 +- .../terremark/domain/TerremarkVApp.java | 4 - .../terremark/domain/VAppConfiguration.java | 31 ++ .../domain/internal/TerremarkVAppImpl.java | 33 +- .../terremark/xml/TerremarkVAppHandler.java | 11 +- .../TerremarkVCloudClientLiveTest.java | 16 +- ...BindVAppConfigurationToXmlPayloadTest.java | 55 +++- .../xml/TerremarkVAppHandlerTest.java | 81 +++-- .../resources/terremark/get_vapp2disks.xml | 297 ++++++++++++++++++ 10 files changed, 469 insertions(+), 74 deletions(-) create mode 100644 vcloud/terremark/src/test/resources/terremark/get_vapp2disks.xml diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/ResourceAllocation.java b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/ResourceAllocation.java index 9ba7470d0a..b9cf69aaf4 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/ResourceAllocation.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/ResourceAllocation.java @@ -45,8 +45,8 @@ public class ResourceAllocation implements Comparable { private final String virtualQuantityUnits; public ResourceAllocation(int id, String name, String description, ResourceType type, - String subType, String hostResource, Integer address, Integer addressOnParent, Integer parent, - Boolean connected, long virtualQuantity, String virtualQuantityUnits) { + String subType, String hostResource, Integer address, Integer addressOnParent, + Integer parent, Boolean connected, long virtualQuantity, String virtualQuantityUnits) { this.id = id; this.name = checkNotNull(name, "name"); this.description = description; @@ -73,6 +73,12 @@ public class ResourceAllocation implements Comparable { return BEFORE; if (this.id > that.id) return AFTER; + if (this.addressOnParent != null && that.addressOnParent != null) { + if (this.addressOnParent < that.addressOnParent) + return BEFORE; + if (this.addressOnParent > that.addressOnParent) + return AFTER; + } return 1; } diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayload.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayload.java index d868de4157..eaba1c0154 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayload.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayload.java @@ -61,7 +61,7 @@ import com.jamesmurty.utils.XMLBuilder; public class BindVAppConfigurationToXmlPayload implements MapBinder { private static final String RESOURCE_ALLOCATION_NS = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"; - + protected final String ns; protected final String schema; private final BindToStringPayload stringBinder; @@ -141,7 +141,8 @@ public class BindVAppConfigurationToXmlPayload implements MapBinder { VAppConfiguration configuration) { for (ResourceAllocation disk : vApp.getResourceAllocationByType() .get(ResourceType.DISK_DRIVE)) { - addDiskWithQuantity(sectionBuilder, disk); + if (!configuration.getDisksToDelete().contains(disk.getAddressOnParent())) + addDiskWithQuantity(sectionBuilder, disk); } for (Long quantity : configuration.getDisks()) { ResourceAllocation disk = new ResourceAllocation(9, "n/a", null, ResourceType.DISK_DRIVE, diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/TerremarkVApp.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/TerremarkVApp.java index 68a0236a15..927aeada18 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/TerremarkVApp.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/TerremarkVApp.java @@ -37,8 +37,4 @@ public interface TerremarkVApp extends VApp { NamedResource getVDC(); - NamedResource getComputeOptions(); - - NamedResource getCustomizationOptions(); - } \ No newline at end of file diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/VAppConfiguration.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/VAppConfiguration.java index 16dcfe84ca..26facde885 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/VAppConfiguration.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/VAppConfiguration.java @@ -39,6 +39,7 @@ public class VAppConfiguration { private Integer processorCount = null; private Long memory = null; private List disks = Lists.newArrayList(); + private List disksToDelete = Lists.newArrayList(); /** * The vApp name has the following requirements: Name can use uppercase and/or lowercase letters. @@ -97,6 +98,24 @@ public class VAppConfiguration { return this; } + /** + * To remove a disk, you specify its addressOnParent. + * + * Ex. + * + *
+    * SortedSet<ResourceAllocation> disks = Sets.newTreeSet(vApp.getResourceAllocationByType().get(
+    *          ResourceType.DISK_DRIVE));
+    * ResourceAllocation lastDisk = disks.last();
+    * VAppConfiguration config = deleteDiskWithAddressOnParent(lastDisk.getAddressOnParent());
+    * 
+ */ + public VAppConfiguration deleteDiskWithAddressOnParent(int addressOnParent) { + checkArgument(addressOnParent > 0, "you cannot delete the system disk"); + disksToDelete.add(addressOnParent); + return this; + } + public static class Builder { /** @@ -131,6 +150,14 @@ public class VAppConfiguration { return options.addDisk(kilobytes); } + /** + * @see VAppConfiguration#deleteDiskWithAddressOnParent(int) + */ + public static VAppConfiguration deleteDiskWithAddressOnParent(int addressOnParent) { + VAppConfiguration options = new VAppConfiguration(); + return options.deleteDiskWithAddressOnParent(addressOnParent); + } + } public Integer getProcessorCount() { @@ -148,4 +175,8 @@ public class VAppConfiguration { public String getName() { return name; } + + public List getDisksToDelete() { + return disksToDelete; + } } diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/internal/TerremarkVAppImpl.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/internal/TerremarkVAppImpl.java index 49122e8214..0d0976e712 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/internal/TerremarkVAppImpl.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/domain/internal/TerremarkVAppImpl.java @@ -45,43 +45,27 @@ import com.google.common.collect.ListMultimap; public class TerremarkVAppImpl extends VAppImpl implements TerremarkVApp { private final NamedResource vDC; - private final NamedResource computeOptions; - private final NamedResource customizationOptions; /** The serialVersionUID */ private static final long serialVersionUID = 8464716396538298809L; public TerremarkVAppImpl(String id, String name, String type, URI location, VAppStatus status, - long size, NamedResource vDC, NamedResource computeOptions, NamedResource customizationOptions, - ListMultimap networkToAddresses, + long size, NamedResource vDC, ListMultimap networkToAddresses, String operatingSystemDescription, TerremarkVirtualSystem system, SortedSet resourceAllocations) { - super(id, name, location, status, size, networkToAddresses, operatingSystemDescription, system, - resourceAllocations); + super(id, name, location, status, size, networkToAddresses, operatingSystemDescription, + system, resourceAllocations); this.vDC = vDC; - this.computeOptions = computeOptions; - this.customizationOptions = customizationOptions; } public NamedResource getVDC() { return vDC; } - public NamedResource getComputeOptions() { - return computeOptions; - } - - public NamedResource getCustomizationOptions() { - return customizationOptions; - } - @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); - result = prime * result + ((computeOptions == null) ? 0 : computeOptions.hashCode()); - result = prime * result - + ((customizationOptions == null) ? 0 : customizationOptions.hashCode()); result = prime * result + ((vDC == null) ? 0 : vDC.hashCode()); return result; } @@ -95,16 +79,6 @@ public class TerremarkVAppImpl extends VAppImpl implements TerremarkVApp { if (getClass() != obj.getClass()) return false; TerremarkVAppImpl other = (TerremarkVAppImpl) obj; - if (computeOptions == null) { - if (other.computeOptions != null) - return false; - } else if (!computeOptions.equals(other.computeOptions)) - return false; - if (customizationOptions == null) { - if (other.customizationOptions != null) - return false; - } else if (!customizationOptions.equals(other.customizationOptions)) - return false; if (vDC == null) { if (other.vDC != null) return false; @@ -113,4 +87,5 @@ public class TerremarkVAppImpl extends VAppImpl implements TerremarkVApp { return true; } + } \ No newline at end of file diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandler.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandler.java index c84c8fa456..37fc3e5412 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandler.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandler.java @@ -71,8 +71,6 @@ public class TerremarkVAppHandler extends ParseSax.HandlerWithResult networkToAddresses = ArrayListMultimap.create(); private StringBuilder currentText = new StringBuilder(); private String operatingSystemDescription; @@ -81,8 +79,8 @@ public class TerremarkVAppHandler extends ParseSax.HandlerWithResult disks = Lists.newArrayList(vApp.getResourceAllocationByType() + .get(ResourceType.DISK_DRIVE)); + + // delete the second disk + task = tmClient.configureVApp(vApp, deleteDiskWithAddressOnParent(disks.get(1).getAddressOnParent())); + + assert successTester.apply(task.getId()); + assert successTester.apply(tmClient.powerOnVApp(vApp.getId()).getId()); } @@ -439,7 +453,7 @@ public class TerremarkVCloudClientLiveTest extends VCloudClientLiveTest { .getInstance(SocketOpen.class), 130, 10, TimeUnit.SECONDS);// make it longer then // default internet // service timeout - successTester = new RetryablePredicate(injector.getInstance(TaskSuccess.class), 300, + successTester = new RetryablePredicate(injector.getInstance(TaskSuccess.class), 450, 10, TimeUnit.SECONDS); } diff --git a/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayloadTest.java b/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayloadTest.java index 53ce58dfd3..f203a3beb3 100644 --- a/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayloadTest.java +++ b/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/binders/BindVAppConfigurationToXmlPayloadTest.java @@ -77,16 +77,17 @@ public class BindVAppConfigurationToXmlPayloadTest { TerremarkVAppImpl vApp = new TerremarkVAppImpl("4213", "MyAppServer6", "application/vnd.vmware.vcloud.vApp+xml", URI .create("https://services.vcloudexpress/terremark.com/api/v0.8/vapp/4213"), - VAppStatus.OFF, 4194304, null, null, null, ImmutableListMultimap - . of(), null, null, ImmutableSortedSet.of( - new ResourceAllocation(1, "n/a", null, ResourceType.PROCESSOR, null, null, - null, null, null, null, 2, null), new ResourceAllocation(2, "n/a", - null, ResourceType.MEMORY, null, null, null, null, null, null, - 1024, null), new ResourceAllocation(9, "n/a", null, - ResourceType.DISK_DRIVE, null, "1048576", null, 0, null, null, - 209152, null))); + VAppStatus.OFF, 4194304, null, ImmutableListMultimap. of(), + null, null, ImmutableSortedSet.of(new ResourceAllocation(1, "n/a", null, + ResourceType.PROCESSOR, null, null, null, null, null, null, 2, null), + new ResourceAllocation(2, "n/a", null, ResourceType.MEMORY, null, null, + null, null, null, null, 1024, null), new ResourceAllocation(9, + "n/a", null, ResourceType.DISK_DRIVE, null, "1048576", null, 0, + null, null, 209152, null))); - String expected = Utils.toStringAndClose(getClass().getResourceAsStream("/terremark/configureVApp.xml")).replace("eduardo", "roberto"); + String expected = Utils.toStringAndClose( + getClass().getResourceAsStream("/terremark/configureVApp.xml")).replace("eduardo", + "roberto"); Multimap headers = Multimaps.synchronizedMultimap(HashMultimap . create()); VAppConfiguration config = new VAppConfiguration().changeNameTo("roberto"); @@ -106,4 +107,40 @@ public class BindVAppConfigurationToXmlPayloadTest { verify(request); } + public void testRemoveDisk() throws IOException { + TerremarkVAppImpl vApp = new TerremarkVAppImpl("4213", "MyAppServer6", + "application/vnd.vmware.vcloud.vApp+xml", URI + .create("https://services.vcloudexpress/terremark.com/api/v0.8/vapp/4213"), + VAppStatus.OFF, 4194304, null, ImmutableListMultimap. of(), + null, null, ImmutableSortedSet.of(new ResourceAllocation(1, "n/a", null, + ResourceType.PROCESSOR, null, null, null, null, null, null, 2, null), + new ResourceAllocation(2, "n/a", null, ResourceType.MEMORY, null, null, + null, null, null, null, 1024, null), new ResourceAllocation(9, + "n/a", null, ResourceType.DISK_DRIVE, null, "1048576", null, 0, + null, null, 209152, null),new ResourceAllocation(9, + "n/a", null, ResourceType.DISK_DRIVE, null, "1048576", null, 1, + null, null, 209152, null))); + + String expected = Utils.toStringAndClose( + getClass().getResourceAsStream("/terremark/configureVApp.xml")).replace("eduardo", + "MyAppServer6"); + Multimap headers = Multimaps.synchronizedMultimap(HashMultimap + . create()); + VAppConfiguration config = new VAppConfiguration().deleteDiskWithAddressOnParent(1); + GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); + expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); + expect(request.getArgs()).andReturn(new Object[] { vApp, config }).atLeastOnce(); + expect(request.getFirstHeaderOrNull("Content-Type")).andReturn(null).atLeastOnce(); + expect(request.getHeaders()).andReturn(headers).atLeastOnce(); + request.setPayload(expected); + replay(request); + + BindVAppConfigurationToXmlPayload binder = injector + .getInstance(BindVAppConfigurationToXmlPayload.class); + + Map map = Maps.newHashMap(); + binder.bindToRequest(request, map); + verify(request); + } + } diff --git a/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandlerTest.java b/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandlerTest.java index 57b3f29c0c..296b31b847 100644 --- a/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandlerTest.java +++ b/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/xml/TerremarkVAppHandlerTest.java @@ -29,8 +29,7 @@ import java.io.InputStream; import java.net.InetAddress; import java.net.URI; import java.net.UnknownHostException; - -import javax.ws.rs.core.MediaType; +import java.util.List; import org.jclouds.http.functions.BaseHandlerTest; import org.jclouds.http.functions.ParseSax; @@ -48,6 +47,7 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.Lists; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Provides; @@ -118,22 +118,7 @@ public class TerremarkVAppHandlerTest extends BaseHandlerTest { .create("https://services.vcloudexpress.terremark.com/api/v0.8/vapp/13850")); assertEquals(result.getVDC(), new NamedResourceImpl("32", null, VCloudMediaType.VDC_XML, URI .create("https://services.vcloudexpress.terremark.com/api/v0.8/vdc/32"))); - assertEquals( - result.getComputeOptions(), - new NamedResourceImpl( - "compute", - "Compute Options", - MediaType.APPLICATION_XML, - URI - .create("https://services.vcloudexpress.terremark.com/api/v0.8/vapp/13850/options/compute"))); - assertEquals( - result.getCustomizationOptions(), - new NamedResourceImpl( - "customization", - "Customization Options", - MediaType.APPLICATION_XML, - URI - .create("https://services.vcloudexpress.terremark.com/api/v0.8/vapp/13850/options/customization"))); + assertEquals(result.getSystem(), new TerremarkVirtualSystem(null, null, null, null, null, null, null, null, null, null, null, "Virtual Hardware Family", 0, null, null, null, null, null, "adriantest1", "vmx-07")); @@ -169,4 +154,64 @@ public class TerremarkVAppHandlerTest extends BaseHandlerTest { result.getResourceAllocationByType().get(ResourceType.DISK_DRIVE)) .getVirtualQuantity()); } + + public void testGetVApp2disks() throws UnknownHostException { + InputStream is = getClass().getResourceAsStream("/terremark/get_vapp2disks.xml"); + + TerremarkVApp vApp = (TerremarkVApp) factory.create( + injector.getInstance(TerremarkVAppHandler.class)).parse(is); + assertEquals(vApp.getId(), 15639 + ""); + + assertEquals(vApp.getName(), "eduardo"); + assertEquals(vApp.getStatus(), VAppStatus.OFF); + + assertEquals(vApp.getSize().longValue(), 30408704); + assertEquals(vApp.getOperatingSystemDescription(), "Ubuntu Linux (32-bit)"); + + assertEquals(vApp.getLocation(), URI + .create("https://services.vcloudexpress.terremark.com/api/v0.8/vapp/15639")); + assertEquals(vApp.getVDC(), new NamedResourceImpl("32", null, VCloudMediaType.VDC_XML, URI + .create("https://services.vcloudexpress.terremark.com/api/v0.8/vdc/32"))); + + assertEquals(vApp.getSystem(), new TerremarkVirtualSystem(null, null, null, null, null, + null, null, null, null, null, null, "Virtual Hardware Family", 0, null, null, null, + null, null, "eduardo", "vmx-07")); + assertEquals(vApp.getNetworkToAddresses().get("Internal"), ImmutableList.of(InetAddress + .getByName("10.114.34.131"))); + + ResourceAllocation cpu = new ResourceAllocation(1, "2 virtual CPU(s)", + "Number of Virtual CPUs", ResourceType.PROCESSOR, null, null, null, null, null, + null, 2, "hertz * 10^6"); + + ResourceAllocation controller = new ResourceAllocation(3, "SCSI Controller 0", + "SCSI Controller", ResourceType.SCSI_CONTROLLER, "lsilogic", null, 0, null, null, + null, 1, null); + ResourceAllocation memory = new ResourceAllocation(2, "1024MB of memory", "Memory Size", + ResourceType.MEMORY, null, null, null, null, null, null, 1024, "byte * 2^20"); + ResourceAllocation disk = new ResourceAllocation(9, "Hard Disk 1", null, + ResourceType.DISK_DRIVE, null, "4194304", null, 0, 3, null, 4194304, null); + ResourceAllocation disk2 = new ResourceAllocation(9, "Hard Disk 2", null, + ResourceType.DISK_DRIVE, null, "26214400", null, 1, 3, null, 26214400, null); + + assertEquals(vApp.getResourceAllocations(), ImmutableSortedSet.of(cpu, controller, memory, + disk, disk2)); + assertEquals(Iterables.getOnlyElement( + vApp.getResourceAllocationByType().get(ResourceType.PROCESSOR)) + .getVirtualQuantity(), 2); + assertEquals(Iterables.getOnlyElement( + vApp.getResourceAllocationByType().get(ResourceType.SCSI_CONTROLLER)) + .getVirtualQuantity(), 1); + assertEquals(Iterables.getOnlyElement( + vApp.getResourceAllocationByType().get(ResourceType.MEMORY)).getVirtualQuantity(), + 1024); + + // extract the disks on the vApp sorted by addressOnParent + List disks = Lists.newArrayList(vApp.getResourceAllocationByType() + .get(ResourceType.DISK_DRIVE)); + + assertEquals(disks.get(0).getVirtualQuantity(), 4194304); + assertEquals(disks.get(1).getVirtualQuantity(), 26214400); + + } + } diff --git a/vcloud/terremark/src/test/resources/terremark/get_vapp2disks.xml b/vcloud/terremark/src/test/resources/terremark/get_vapp2disks.xml new file mode 100644 index 0000000000..39c3e7f5a2 --- /dev/null +++ b/vcloud/terremark/src/test/resources/terremark/get_vapp2disks.xml @@ -0,0 +1,297 @@ + + + + +
+ + 10.114.34.131 + +
+ + The kind of installed guest operating system + Ubuntu Linux (32-bit) + +
+ Virtual Hardware + + + + + + + + + + + + + Virtual Hardware Family + 0 + + + + + + eduardo + vmx-07 + + +
+ + hertz * 10^6 + + + + + Number of Virtual CPUs + 2 virtual CPU(s) + 1 + + + + + + + + 3 + 2 + count + + + +
+ + byte * 2^20 + + + + + Memory Size + 1024MB of memory + 2 + + + + + + + + 4 + 1024 + byte * 2^20 + + + +
0
+ + + + + + + SCSI Controller + SCSI Controller 0 + 3 + + + + + + + lsilogic + 6 + + + +
+ +
+ 0 + + + + + + + Hard Disk 1 + 4194304 + 9 + + + + 3 + + + + 17 + 4194304 + + + + +
+ 1 + + + + + + + Hard Disk 2 + 26214400 + 9 + + + + 3 + + + + 17 + 26214400 + + + +
+
\ No newline at end of file