CLOUDSTACK-1129: [EC2 Query API] DescribeVolumes, add support for filter attachment.status
1. If volume is attached to a VM set attachment state based on the state of the VM it is attached to. Running, Starting, Stopped -> Attached Starting -> Attaching Destroyed, Error -> Detached 2. If volume is not attached to a VM set attachment state as 'detached' Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/c06a16c8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/c06a16c8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/c06a16c8 Branch: refs/heads/4.1 Commit: c06a16c88433041d7afdd5b49e0b932d4ccefa78 Parents: ee90ebf Author: Prachi Damle <[email protected]> Authored: Tue Feb 12 13:40:58 2013 -0800 Committer: Prachi Damle <[email protected]> Committed: Tue Feb 12 13:45:31 2013 -0800 ---------------------------------------------------------------------- .../cloud/bridge/service/EC2SoapServiceImpl.java | 32 ++------------- .../cloud/bridge/service/core/ec2/EC2Engine.java | 32 +++++++++++++- .../cloud/bridge/service/core/ec2/EC2Volume.java | 16 +++++++ .../service/core/ec2/EC2VolumeFilterSet.java | 5 ++- 4 files changed, 53 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c06a16c8/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java ---------------------------------------------------------------------- diff --git a/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java b/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java index 442b460..2fefb28 100644 --- a/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java +++ b/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java @@ -1290,7 +1290,7 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface { param5.setInstanceId(vol.getInstanceId().toString()); String devicePath = engine.cloudDeviceIdToDevicePath( vol.getHypervisor(), vol.getDeviceId()); param5.setDevice( devicePath ); - param5.setStatus( toVolumeAttachmentState( vol.getInstanceId(), vol.getVMState())); + param5.setStatus(vol.getAttachmentState()); if (vol.getAttached() == null) { param5.setAttachTime( cal ); } else { @@ -1546,25 +1546,7 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface { else if (cloudState.equalsIgnoreCase( "Expunging" )) return new String( "terminated"); else return new String( "running" ); } - - /** - * We assume a state for the volume based on what its associated VM is doing. - * - * @param vmId - * @param vmState - * @return - */ - public static String toVolumeAttachmentState(String instanceId, String vmState ) { - if (null == instanceId || null == vmState) return "detached"; - - if (vmState.equalsIgnoreCase( "Destroyed" )) return "detached"; - else if (vmState.equalsIgnoreCase( "Stopped" )) return "attached"; - else if (vmState.equalsIgnoreCase( "Running" )) return "attached"; - else if (vmState.equalsIgnoreCase( "Starting" )) return "attaching"; - else if (vmState.equalsIgnoreCase( "Stopping" )) return "attached"; - else if (vmState.equalsIgnoreCase( "Error" )) return "detached"; - else return "detached"; - } + public static StopInstancesResponse toStopInstancesResponse(EC2StopInstancesResponse engineResponse) { StopInstancesResponse response = new StopInstancesResponse(); @@ -1808,10 +1790,7 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface { param1.setVolumeId( engineResponse.getId().toString()); param1.setInstanceId( engineResponse.getInstanceId().toString()); param1.setDevice( engineResponse.getDevice()); - if ( null != engineResponse.getState()) - param1.setStatus( engineResponse.getState()); - else param1.setStatus( "" ); // ToDo - throw an Soap Fault - + param1.setStatus(engineResponse.getAttachmentState()); param1.setAttachTime( cal ); param1.setRequestId( UUID.randomUUID().toString()); @@ -1828,10 +1807,7 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface { param1.setVolumeId( engineResponse.getId().toString()); param1.setInstanceId( (null == engineResponse.getInstanceId() ? "" : engineResponse.getInstanceId().toString())); param1.setDevice( (null == engineResponse.getDevice() ? "" : engineResponse.getDevice())); - if ( null != engineResponse.getState()) - param1.setStatus( engineResponse.getState()); - else param1.setStatus( "" ); // ToDo - throw an Soap Fault - + param1.setStatus(engineResponse.getAttachmentState()); param1.setAttachTime( cal ); param1.setRequestId( UUID.randomUUID().toString()); http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c06a16c8/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java ---------------------------------------------------------------------- diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java index 1d278a2..277cdc8 100644 --- a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java +++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java @@ -1125,6 +1125,7 @@ public class EC2Engine extends ManagerBase { resp.setState(vol.getState()); resp.setType(vol.getVolumeType()); resp.setVMState(vol.getVirtualMachineState()); + resp.setAttachmentState(mapToAmazonVolumeAttachmentState(vol.getVirtualMachineState())); resp.setZoneName(vol.getZoneName()); return resp; } @@ -1211,6 +1212,7 @@ public class EC2Engine extends ManagerBase { resp.setState(vol.getState()); resp.setType(vol.getVolumeType()); resp.setVMState(vol.getVirtualMachineState()); + resp.setAttachmentState("detached"); resp.setZoneName(vol.getZoneName()); return resp; } @@ -1639,11 +1641,16 @@ public class EC2Engine extends ManagerBase { ec2Vol.setSize(vol.getSize()); ec2Vol.setType(vol.getVolumeType()); - if(vol.getVirtualMachineId() != null) + if(vol.getVirtualMachineId() != null) { ec2Vol.setInstanceId(vol.getVirtualMachineId()); + if (vol.getVirtualMachineState() != null) { + ec2Vol.setVMState(vol.getVirtualMachineState()); + ec2Vol.setAttachmentState(mapToAmazonVolumeAttachmentState(vol.getVirtualMachineState())); + } + } else { + ec2Vol.setAttachmentState("detached"); + } - if(vol.getVirtualMachineState() != null) - ec2Vol.setVMState(vol.getVirtualMachineState()); ec2Vol.setZoneName(vol.getZoneName()); List<CloudStackKeyValue> resourceTags = vol.getTags(); @@ -2405,6 +2412,25 @@ public class EC2Engine extends ManagerBase { } /** + * Map CloudStack VM state to Amazon volume attachment state + * + * @param CloudStack VM state + * @return Amazon Volume attachment state + */ + private String mapToAmazonVolumeAttachmentState (String vmState) { + if ( vmState.equalsIgnoreCase("Running") || vmState.equalsIgnoreCase("Stopping") || + vmState.equalsIgnoreCase("Stopped") ) { + return "attached"; + } + else if (vmState.equalsIgnoreCase("Starting")) { + return "attaching"; + } + else { // VM state is 'destroyed' or 'error' or other + return "detached"; + } + } + + /** * Map Amazon resourceType to CloudStack resourceType * * @param Amazon resourceType http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c06a16c8/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Volume.java ---------------------------------------------------------------------- diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Volume.java b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Volume.java index 23d6d70..79bc6e8 100644 --- a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Volume.java +++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Volume.java @@ -35,6 +35,7 @@ public class EC2Volume { private String hypervisor; private String created; private String attached; + private String attachmentState; private List<EC2TagKeyValue> tagsSet; public EC2Volume() { @@ -50,6 +51,7 @@ public class EC2Volume { hypervisor = null; created = null; attached = null; + attachmentState = null; tagsSet = new ArrayList<EC2TagKeyValue>(); } @@ -236,6 +238,20 @@ public class EC2Volume { this.attached = attached; } + /** + * @param state of the attached VM to set + */ + public void setAttachmentState(String attachedState) { + this.attachmentState = attachedState; + } + + /** + * @return state of the vm + */ + public String getAttachmentState() { + return attachmentState; + } + public void addResourceTag( EC2TagKeyValue param ) { tagsSet.add( param ); } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c06a16c8/awsapi/src/com/cloud/bridge/service/core/ec2/EC2VolumeFilterSet.java ---------------------------------------------------------------------- diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2VolumeFilterSet.java b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2VolumeFilterSet.java index 0594231..b8021f3 100644 --- a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2VolumeFilterSet.java +++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2VolumeFilterSet.java @@ -43,7 +43,7 @@ public class EC2VolumeFilterSet { filterTypes.put( "attachment.delete-on-termination", "null" ); filterTypes.put( "attachment.device", "string" ); filterTypes.put( "attachment.instance-id", "string" ); - filterTypes.put( "attachment.status", "null" ); + filterTypes.put( "attachment.status", "set:attached|attaching|detached|detaching" ); filterTypes.put( "availability-zone", "string" ); filterTypes.put( "create-time", "xsd:dateTime" ); filterTypes.put( "size", "integer" ); @@ -136,6 +136,9 @@ public class EC2VolumeFilterSet { return containsDevice(vol.getDeviceId(), valueSet ); else if (filterName.equalsIgnoreCase( "attachment.instance-id" )) return containsString(String.valueOf(vol.getInstanceId()), valueSet ); + else if ( filterName.equalsIgnoreCase( "attachment.status" ) ) { + return containsString(vol.getAttachmentState(), valueSet ); + } else if (filterName.equalsIgnoreCase("tag-key")) { EC2TagKeyValue[] tagSet = vol.getResourceTags();
