Changed the way device name is passed

This commit is contained in:
Alex Yarmula 2010-02-20 15:55:03 -08:00
parent 55c8ce03ad
commit db31695940
7 changed files with 40 additions and 60 deletions

View File

@ -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,23 +29,24 @@ 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),
ebsBlockDevice.getVolumeId());
if(ebsBlockDevice.getDeviceName() != null) {
if(ebsBlockDeviceName != null) {
generatedRequest.addFormParam(format(deviceNamePattern, amazonOneBasedIndex),
ebsBlockDevice.getDeviceName());
ebsBlockDeviceName);
}
generatedRequest.addFormParam(format(deleteOnTerminationPattern, amazonOneBasedIndex),
String.valueOf(ebsBlockDevice.isDeleteOnTermination()));
amazonOneBasedIndex++;
}
}
}

View File

@ -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<RunningInstance.EbsBlockDevice> ebsBlockDevices = Lists.newArrayList();
private final Multimap<String, RunningInstance.EbsBlockDevice> 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<RunningInstance.EbsBlockDevice> ebsBlockDevices) {
this.ebsBlockDevices.addAll(checkNotNull(ebsBlockDevices,
public BlockDeviceMapping(Multimap<String, RunningInstance.EbsBlockDevice> 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<RunningInstance.EbsBlockDevice> getEbsBlockDevices() {
return ImmutableList.copyOf(ebsBlockDevices);
public Multimap<String, RunningInstance.EbsBlockDevice> getEbsBlockDevices() {
return ImmutableMultimap.copyOf(ebsBlockDevices);
}
}

View File

@ -41,30 +41,21 @@ public class RunningInstance implements Comparable<RunningInstance> {
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<RunningInstance> {
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<RunningInstance> {
return volumeId;
}
public String getDeviceName() {
return deviceName;
}
public Attachment.Status getAttachmentStatus() {
return attachmentStatus;
}

View File

@ -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;

View File

@ -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");

View File

@ -529,7 +529,7 @@ public class InstanceAsyncClientTest extends RestClientTest<InstanceAsyncClient>
BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
blockDeviceMapping.addEbsBlockDevice
(new RunningInstance.EbsBlockDevice("/dev/sda1", "vol-test1", true));
("/dev/sda1", new RunningInstance.EbsBlockDevice("vol-test1", true));
GeneratedHttpRequest<InstanceAsyncClient> httpMethod = processor.createRequest(method,
Region.DEFAULT, "1", blockDeviceMapping);
@ -538,11 +538,11 @@ public class InstanceAsyncClientTest extends RestClientTest<InstanceAsyncClient>
"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);

View File

@ -46,12 +46,10 @@ public class BlockDeviceMappingHandlerTest extends BaseHandlerTest {
DateService dateService = injector.getInstance(DateService.class);
Map<String, EbsBlockDevice> expected = ImmutableMap.<String, EbsBlockDevice> 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)
);