From b4176050e2b53eb2b8b8e3991baaec6f8362c88b Mon Sep 17 00:00:00 2001 From: Andrew Gross Date: Fri, 22 Jul 2016 14:23:42 -0400 Subject: [PATCH] Add volume attachment status --- moto/ec2/models.py | 8 ++++++-- tests/test_ec2/test_elastic_block_store.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index e7cce341f..99fd2a06d 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1513,11 +1513,12 @@ class SecurityGroupIngress(object): class VolumeAttachment(object): - def __init__(self, volume, instance, device): + def __init__(self, volume, instance, device, status): self.volume = volume self.attach_time = utc_date_and_time() self.instance = instance self.device = device + self.status = status @classmethod def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name): @@ -1578,6 +1579,8 @@ class Volume(TaggedEC2Resource): return self.attachment.device if filter_name == 'attachment.instance-id': return self.attachment.instance.id + if filter_name == 'attachment.status': + return self.attachment.status if filter_name == 'create-time': return self.create_time @@ -1688,7 +1691,7 @@ class EBSBackend(object): if not volume or not instance: return False - volume.attachment = VolumeAttachment(volume, instance, device_path) + volume.attachment = VolumeAttachment(volume, instance, device_path, 'attached') # Modify instance to capture mount of block device. bdt = BlockDeviceType(volume_id=volume_id, status=volume.status, size=volume.size, attach_time=utc_date_and_time()) @@ -1702,6 +1705,7 @@ class EBSBackend(object): old_attachment = volume.attachment if not old_attachment: raise InvalidVolumeAttachmentError(volume_id, instance_id) + old_attachment.status = 'detached' volume.attachment = None return old_attachment diff --git a/tests/test_ec2/test_elastic_block_store.py b/tests/test_ec2/test_elastic_block_store.py index 5ad6a5399..f06328422 100644 --- a/tests/test_ec2/test_elastic_block_store.py +++ b/tests/test_ec2/test_elastic_block_store.py @@ -93,6 +93,9 @@ def test_volume_filters(): volumes_by_attach_instance_id = conn.get_all_volumes(filters={'attachment.instance-id': instance.id}) set([vol.id for vol in volumes_by_attach_instance_id]).should.equal(set([block_mapping.volume_id])) + volumes_by_attach_status = conn.get_all_volumes(filters={'attachment.status': 'attached'}) + set([vol.id for vol in volumes_by_attach_status]).should.equal(set([block_mapping.volume_id])) + volumes_by_create_time = conn.get_all_volumes(filters={'create-time': volume4.create_time}) set([vol.create_time for vol in volumes_by_create_time]).should.equal(set([volume4.create_time])) @@ -142,6 +145,7 @@ def test_volume_attach_and_detach(): volume.update() volume.volume_state().should.equal('in-use') + volume.attachment_state().should.equal('attached') volume.attach_data.instance_id.should.equal(instance.id)