From db31695940b841e38a9d392e5f0a3473e6661d62 Mon Sep 17 00:00:00 2001 From: Alex Yarmula Date: Sat, 20 Feb 2010 15:55:03 -0800 Subject: [PATCH] Changed the way device name is passed --- ...BlockDeviceMappingToIndexedFormParams.java | 22 ++++++++-------- .../aws/ec2/domain/BlockDeviceMapping.java | 26 +++++++++---------- .../aws/ec2/domain/RunningInstance.java | 22 ++-------------- .../ec2/xml/BlockDeviceMappingHandler.java | 3 +-- .../aws/ec2/EBSBootEC2ClientLiveTest.java | 15 ++++++----- .../ec2/services/InstanceAsyncClientTest.java | 6 ++--- .../xml/BlockDeviceMappingHandlerTest.java | 6 ++--- 7 files changed, 40 insertions(+), 60 deletions(-) diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/binders/BindBlockDeviceMappingToIndexedFormParams.java b/aws/core/src/main/java/org/jclouds/aws/ec2/binders/BindBlockDeviceMappingToIndexedFormParams.java index 29eb7f8c9e..0d2c04d3f8 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/binders/BindBlockDeviceMappingToIndexedFormParams.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/binders/BindBlockDeviceMappingToIndexedFormParams.java @@ -2,7 +2,6 @@ package org.jclouds.aws.ec2.binders; import org.jclouds.aws.ec2.domain.BlockDeviceMapping; import org.jclouds.aws.ec2.domain.RunningInstance; -import org.jclouds.aws.ec2.domain.UserIdGroupPair; import org.jclouds.http.HttpRequest; import org.jclouds.rest.Binder; import org.jclouds.rest.internal.GeneratedHttpRequest; @@ -30,22 +29,23 @@ public class BindBlockDeviceMappingToIndexedFormParams implements Binder { GeneratedHttpRequest generatedRequest = (GeneratedHttpRequest) request; int amazonOneBasedIndex = 1; //according to docs, counters must start with 1 - for(RunningInstance.EbsBlockDevice ebsBlockDevice : blockDeviceMapping.getEbsBlockDevices()) { + for(String ebsBlockDeviceName : blockDeviceMapping.getEbsBlockDevices().keySet()) { + for(RunningInstance.EbsBlockDevice ebsBlockDevice : blockDeviceMapping.getEbsBlockDevices().get(ebsBlockDeviceName)) { - //not null by contract - generatedRequest.addFormParam(format(volumeIdPattern, amazonOneBasedIndex), + //not null by contract + generatedRequest.addFormParam(format(volumeIdPattern, amazonOneBasedIndex), ebsBlockDevice.getVolumeId()); - if(ebsBlockDevice.getDeviceName() != null) { - generatedRequest.addFormParam(format(deviceNamePattern, amazonOneBasedIndex), - ebsBlockDevice.getDeviceName()); - } - - generatedRequest.addFormParam(format(deleteOnTerminationPattern, amazonOneBasedIndex), + if(ebsBlockDeviceName != null) { + generatedRequest.addFormParam(format(deviceNamePattern, amazonOneBasedIndex), + ebsBlockDeviceName); + } + generatedRequest.addFormParam(format(deleteOnTerminationPattern, amazonOneBasedIndex), String.valueOf(ebsBlockDevice.isDeleteOnTermination())); - amazonOneBasedIndex++; + amazonOneBasedIndex++; + } } } diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/domain/BlockDeviceMapping.java b/aws/core/src/main/java/org/jclouds/aws/ec2/domain/BlockDeviceMapping.java index 7274d0a142..d2b1685c8d 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/domain/BlockDeviceMapping.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/domain/BlockDeviceMapping.java @@ -18,15 +18,11 @@ */ package org.jclouds.aws.ec2.domain; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; +import com.google.common.collect.*; import com.google.inject.internal.Nullable; import static com.google.common.base.Preconditions.checkNotNull; -import java.util.Date; -import java.util.List; - /** * Defines the mapping of volumes for * {@link org.jclouds.aws.ec2.services.InstanceClient#setBlockDeviceMappingForInstanceInRegion}. @@ -35,7 +31,8 @@ import java.util.List; */ public class BlockDeviceMapping { - private final List ebsBlockDevices = Lists.newArrayList(); + private final Multimap ebsBlockDevices = + LinkedHashMultimap.create(); public BlockDeviceMapping() { } @@ -45,26 +42,27 @@ public class BlockDeviceMapping { * * This method copies the values of the list. * @param ebsBlockDevices - * devices to be changed for the volume + * devices to be changed for the volume. This cannot be null. */ - public BlockDeviceMapping(List ebsBlockDevices) { - this.ebsBlockDevices.addAll(checkNotNull(ebsBlockDevices, + public BlockDeviceMapping(Multimap ebsBlockDevices) { + this.ebsBlockDevices.putAll(checkNotNull(ebsBlockDevices, /*or throw*/ "EbsBlockDevices can't be null")); } /** * Adds a {@link RunningInstance.EbsBlockDevice} to the mapping. + * @param deviceName name of the device to apply the mapping. Can be null. * @param ebsBlockDevice - * ebsBlockDevice to be added + * ebsBlockDevice to be added. This cannot be null. * @return the same instance for method chaining purposes */ - public BlockDeviceMapping addEbsBlockDevice(RunningInstance.EbsBlockDevice ebsBlockDevice) { - this.ebsBlockDevices.add(checkNotNull(ebsBlockDevice, + public BlockDeviceMapping addEbsBlockDevice(@Nullable String deviceName, RunningInstance.EbsBlockDevice ebsBlockDevice) { + this.ebsBlockDevices.put(deviceName, checkNotNull(ebsBlockDevice, /*or throw*/ "EbsBlockDevice can't be null")); return this; } - public List getEbsBlockDevices() { - return ImmutableList.copyOf(ebsBlockDevices); + public Multimap getEbsBlockDevices() { + return ImmutableMultimap.copyOf(ebsBlockDevices); } } diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/domain/RunningInstance.java b/aws/core/src/main/java/org/jclouds/aws/ec2/domain/RunningInstance.java index 15cbe8d2e2..d3145c5933 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/domain/RunningInstance.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/domain/RunningInstance.java @@ -41,30 +41,21 @@ public class RunningInstance implements Comparable { public static class EbsBlockDevice { private final String volumeId; - private final String deviceName; private final Attachment.Status attachmentStatus; private final Date attachTime; private final boolean deleteOnTermination; public EbsBlockDevice(String volumeId, Status attachmentStatus, Date attachTime, - boolean deleteOnTermination) { - this(volumeId, null, attachmentStatus, attachTime, deleteOnTermination); - } - - public EbsBlockDevice(String volumeId, String deviceName, - Status attachmentStatus, Date attachTime, boolean deleteOnTermination) { super(); this.volumeId = volumeId; this.attachmentStatus = attachmentStatus; this.attachTime = attachTime; this.deleteOnTermination = deleteOnTermination; - this.deviceName = deviceName; } - public EbsBlockDevice(String volumeId, String deviceName, - boolean deleteOnTermination) { - this(volumeId, deviceName, null, null, deleteOnTermination); + public EbsBlockDevice(String volumeId, boolean deleteOnTermination) { + this(volumeId, null, null, deleteOnTermination); } @Override @@ -92,11 +83,6 @@ public class RunningInstance implements Comparable { return false; } else if (!attachTime.equals(other.attachTime)) return false; - if (deviceName == null) { - if (other.deviceName != null) - return false; - } else if (!deviceName.equals(other.deviceName)) - return false; if (attachmentStatus == null) { if (other.attachmentStatus != null) return false; @@ -116,10 +102,6 @@ public class RunningInstance implements Comparable { return volumeId; } - public String getDeviceName() { - return deviceName; - } - public Attachment.Status getAttachmentStatus() { return attachmentStatus; } diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandler.java b/aws/core/src/main/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandler.java index 37bd4b639b..6305e979b7 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandler.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandler.java @@ -67,8 +67,7 @@ public class BlockDeviceMappingHandler extends } else if (qName.equals("attachTime")) { attachTime = dateService.iso8601DateParse(currentText.toString().trim()); } else if (qName.equals("item")) { - ebsBlockDevices.put(deviceName, new EbsBlockDevice(volumeId, deviceName, - attachmentStatus, attachTime, deleteOnTermination)); + ebsBlockDevices.put(deviceName, new EbsBlockDevice(volumeId, attachmentStatus, attachTime, deleteOnTermination)); this.volumeId = null; this.deviceName = null; this.deleteOnTermination = true; diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/EBSBootEC2ClientLiveTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/EBSBootEC2ClientLiveTest.java index 481c28ab09..00a73ffae1 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/EBSBootEC2ClientLiveTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/EBSBootEC2ClientLiveTest.java @@ -72,9 +72,9 @@ import com.google.inject.internal.ImmutableMap; * Adapted from the following sources: {@link http://gist.github.com/249915}, {@link http * ://www.capsunlock.net/2009/12/create-ebs-boot-ami.html} *

- * + * * Generally disabled, as it incurs higher fees. - * + * * @author Adrian Cole */ @Test(groups = "live", enabled = false, sequential = true, testName = "ec2.EBSBootEC2ClientLiveTest") @@ -448,9 +448,11 @@ public class EBSBootEC2ClientLiveTest { } private void setBlockDeviceMappingForInstanceInRegion() { + String volumeId = ebsInstance.getEbsBlockDevices().get("/dev/sda1").getVolumeId(); + BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping(); blockDeviceMapping.addEbsBlockDevice - (new RunningInstance.EbsBlockDevice(volume.getId(), "/dev/sda1", false)); + ("/dev/sda1", new RunningInstance.EbsBlockDevice(volumeId, false)); try { client.getInstanceServices().setBlockDeviceMappingForInstanceInRegion(Region.DEFAULT, ebsInstance.getId(), blockDeviceMapping); @@ -459,10 +461,11 @@ public class EBSBootEC2ClientLiveTest { .getInstanceServices().getBlockDeviceMappingForInstanceInRegion(Region.DEFAULT, ebsInstance.getId()); assertEquals(devices.size(), 1); + String deviceName = Iterables.getOnlyElement(devices.keySet()); RunningInstance.EbsBlockDevice device = Iterables.getOnlyElement(devices.values()); - assertEquals(device.getVolumeId(), volume.getId()); - assertEquals(device.getDeviceName(), "/dev/sda1"); + assertEquals(device.getVolumeId(), volumeId); + assertEquals(deviceName, "/dev/sda1"); assertEquals(device.isDeleteOnTermination(), false); System.out.println("OK: setBlockDeviceMappingForInstanceInRegion"); @@ -497,7 +500,7 @@ public class EBSBootEC2ClientLiveTest { /** * this tests "personality" as the file looked up was sent during instance creation - * + * * @throws UnknownHostException */ private void sshPing(RunningInstance newDetails) throws UnknownHostException { diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/services/InstanceAsyncClientTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/services/InstanceAsyncClientTest.java index 9bbcc39607..46378538d9 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/services/InstanceAsyncClientTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/services/InstanceAsyncClientTest.java @@ -529,7 +529,7 @@ public class InstanceAsyncClientTest extends RestClientTest BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping(); blockDeviceMapping.addEbsBlockDevice - (new RunningInstance.EbsBlockDevice("/dev/sda1", "vol-test1", true)); + ("/dev/sda1", new RunningInstance.EbsBlockDevice("vol-test1", true)); GeneratedHttpRequest httpMethod = processor.createRequest(method, Region.DEFAULT, "1", blockDeviceMapping); @@ -538,11 +538,11 @@ public class InstanceAsyncClientTest extends RestClientTest "Content-Length: 62\nContent-Type: application/x-www-form-urlencoded\nHost: ec2.amazonaws.com\n"); assertPayloadEquals( httpMethod, - "Version=2009-11-30&Action=ModifyInstanceAttribute&InstanceId=1&BlockDeviceMapping.1.Ebs.VolumeId=%2Fdev%2Fsda1&BlockDeviceMapping.1.DeviceName=vol-test1&BlockDeviceMapping.1.Ebs.DeleteOnTermination=true"); + "Version=2009-11-30&Action=ModifyInstanceAttribute&InstanceId=1&BlockDeviceMapping.1.Ebs.VolumeId=vol-test1&BlockDeviceMapping.1.DeviceName=%2Fdev%2Fsda1&BlockDeviceMapping.1.Ebs.DeleteOnTermination=true"); filter.filter(httpMethod);// ensure encoding worked properly assertPayloadEquals( httpMethod, - "Action=ModifyInstanceAttribute&BlockDeviceMapping.1.DeviceName=vol-test1&BlockDeviceMapping.1.Ebs.DeleteOnTermination=true&BlockDeviceMapping.1.Ebs.VolumeId=%2Fdev%2Fsda1&InstanceId=1&Signature=Htpx4bm6v0O%2FcYxrsGb74NbTzCJfPtKWeuB8l2TpL%2B0%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2009-11-08T15%3A54%3A08.897Z&Version=2009-11-30&AWSAccessKeyId=user"); + "Action=ModifyInstanceAttribute&BlockDeviceMapping.1.DeviceName=%2Fdev%2Fsda1&BlockDeviceMapping.1.Ebs.DeleteOnTermination=true&BlockDeviceMapping.1.Ebs.VolumeId=vol-test1&InstanceId=1&Signature=ME8%2FNeH5Zs3%2FY4otsKB0Q09mipVwoEkroUEChQ%2FMZao%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2009-11-08T15%3A54%3A08.897Z&Version=2009-11-30&AWSAccessKeyId=user"); assertResponseParserClassEquals(method, httpMethod, CloseContentAndReturn.class); assertSaxResponseParserClassEquals(method, null); diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandlerTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandlerTest.java index 07012b76c7..46c9035a0d 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandlerTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/xml/BlockDeviceMappingHandlerTest.java @@ -46,12 +46,10 @@ public class BlockDeviceMappingHandlerTest extends BaseHandlerTest { DateService dateService = injector.getInstance(DateService.class); Map expected = ImmutableMap. of("/dev/sda1", - new EbsBlockDevice("vol-d74b82be", "/dev/sda1", - Attachment.Status.ATTACHED, + new EbsBlockDevice("vol-d74b82be", Attachment.Status.ATTACHED, dateService.iso8601DateParse("2010-02-20T18:25:26.000Z"), true), "/dev/sdf", - new EbsBlockDevice("vol-another", "/dev/sdf", - Attachment.Status.DETACHED, + new EbsBlockDevice("vol-another", Attachment.Status.DETACHED, dateService.iso8601DateParse("2010-02-20T19:26:26.000Z"), false) );