Add filters for describeVolumes and describeSnapshots.

This commit is contained in:
Denver Janke 2016-01-11 15:44:29 +10:00
parent 6a13e0e226
commit 82b0b12e45
3 changed files with 162 additions and 6 deletions

View File

@ -1430,6 +1430,36 @@ class Volume(TaggedEC2Resource):
else:
return 'available'
def get_filter_value(self, filter_name):
if filter_name.startswith('attachment') and not self.attachment:
return None
if filter_name == 'attachment.attach-time':
return self.attachment.attach_time
if filter_name == 'attachment.device':
return self.attachment.device
if filter_name == 'attachment.instance-id':
return self.attachment.instance.id
if filter_name == 'create-time':
return self.create_time
if filter_name == 'size':
return self.size
if filter_name == 'snapshot-id':
return self.snapshot_id
if filter_name == 'status':
return self.status
filter_value = super(Volume, self).get_filter_value(filter_name)
if filter_value is None:
self.ec2_backend.raise_not_implemented_error("The filter '{0}' for DescribeVolumes".format(filter_name))
return filter_value
class Snapshot(TaggedEC2Resource):
def __init__(self, ec2_backend, snapshot_id, volume, description):
@ -1440,6 +1470,30 @@ class Snapshot(TaggedEC2Resource):
self.create_volume_permission_groups = set()
self.ec2_backend = ec2_backend
def get_filter_value(self, filter_name):
if filter_name == 'description':
return self.description
if filter_name == 'snapshot-id':
return self.id
if filter_name == 'start-time':
return self.start_time
if filter_name == 'volume-id':
return self.volume.id
if filter_name == 'volume-size':
return self.volume.size
filter_value = super(Snapshot, self).get_filter_value(filter_name)
if filter_value is None:
self.ec2_backend.raise_not_implemented_error("The filter '{0}' for DescribeSnapshots".format(filter_name))
return filter_value
class EBSBackend(object):
def __init__(self):
@ -1459,7 +1513,10 @@ class EBSBackend(object):
self.volumes[volume_id] = volume
return volume
def describe_volumes(self):
def describe_volumes(self, filters=None):
if filters:
volumes = self.volumes.values()
return generic_filter(filters, volumes)
return self.volumes.values()
def get_volume(self, volume_id):
@ -1505,7 +1562,10 @@ class EBSBackend(object):
self.snapshots[snapshot_id] = snapshot
return snapshot
def describe_snapshots(self):
def describe_snapshots(self, filters=None):
if filters:
snapshots = self.snapshots.values()
return generic_filter(filters, snapshots)
return self.snapshots.values()
def get_snapshot(self, snapshot_id):

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals
from moto.core.responses import BaseResponse
from moto.ec2.utils import filters_from_querystring
class ElasticBlockStore(BaseResponse):
@ -43,22 +44,22 @@ class ElasticBlockStore(BaseResponse):
return DELETE_VOLUME_RESPONSE
def describe_snapshots(self):
filters = filters_from_querystring(self.querystring)
# querystring for multiple snapshotids results in SnapshotId.1, SnapshotId.2 etc
snapshot_ids = ','.join([','.join(s[1]) for s in self.querystring.items() if 'SnapshotId' in s[0]])
snapshots = self.ec2_backend.describe_snapshots()
snapshots = self.ec2_backend.describe_snapshots(filters=filters)
# Describe snapshots to handle filter on snapshot_ids
snapshots = [s for s in snapshots if s.id in snapshot_ids] if snapshot_ids else snapshots
# snapshots = self.ec2_backend.describe_snapshots()
template = self.response_template(DESCRIBE_SNAPSHOTS_RESPONSE)
return template.render(snapshots=snapshots)
def describe_volumes(self):
filters = filters_from_querystring(self.querystring)
# querystring for multiple volumeids results in VolumeId.1, VolumeId.2 etc
volume_ids = ','.join([','.join(v[1]) for v in self.querystring.items() if 'VolumeId' in v[0]])
volumes = self.ec2_backend.describe_volumes()
volumes = self.ec2_backend.describe_volumes(filters=filters)
# Describe volumes to handle filter on volume_ids
volumes = [v for v in volumes if v.id in volume_ids] if volume_ids else volumes
# volumes = self.ec2_backend.describe_volumes()
template = self.response_template(DESCRIBE_VOLUMES_RESPONSE)
return template.render(volumes=volumes)

View File

@ -48,6 +48,63 @@ def test_filter_volume_by_id():
vol2.should.have.length_of(2)
@mock_ec2
def test_volume_filters():
conn = boto.connect_ec2('the_key', 'the_secret')
reservation = conn.run_instances('ami-1234abcd')
instance = reservation.instances[0]
instance.update()
volume1 = conn.create_volume(80, "us-east-1a")
volume2 = conn.create_volume(36, "us-east-1b")
volume3 = conn.create_volume(20, "us-east-1c")
snapshot = volume3.create_snapshot(description='testsnap')
volume4 = conn.create_volume(25, "us-east-1a", snapshot=snapshot)
conn.create_tags([volume1.id], {'testkey1': 'testvalue1'})
conn.create_tags([volume2.id], {'testkey2': 'testvalue2'})
volume1.update()
volume2.update()
volume3.update()
volume4.update()
block_mapping = instance.block_device_mapping['/dev/sda1']
volumes_by_attach_time = conn.get_all_volumes(filters={'attachment.attach-time': block_mapping.attach_time})
set([vol.id for vol in volumes_by_attach_time]).should.equal(set([block_mapping.volume_id]))
volumes_by_attach_device = conn.get_all_volumes(filters={'attachment.device': '/dev/sda1'})
set([vol.id for vol in volumes_by_attach_device]).should.equal(set([block_mapping.volume_id]))
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_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]))
volumes_by_size = conn.get_all_volumes(filters={'size': volume2.size})
set([vol.id for vol in volumes_by_size]).should.equal(set([volume2.id]))
volumes_by_snapshot_id = conn.get_all_volumes(filters={'snapshot-id': snapshot.id})
set([vol.id for vol in volumes_by_snapshot_id]).should.equal(set([volume4.id]))
volumes_by_status = conn.get_all_volumes(filters={'status': 'in-use'})
set([vol.id for vol in volumes_by_status]).should.equal(set([block_mapping.volume_id]))
volumes_by_tag_key = conn.get_all_volumes(filters={'tag-key': 'testkey1'})
set([vol.id for vol in volumes_by_tag_key]).should.equal(set([volume1.id]))
volumes_by_tag_value = conn.get_all_volumes(filters={'tag-value': 'testvalue1'})
set([vol.id for vol in volumes_by_tag_value]).should.equal(set([volume1.id]))
volumes_by_tag = conn.get_all_volumes(filters={'tag:testkey1': 'testvalue1'})
set([vol.id for vol in volumes_by_tag]).should.equal(set([volume1.id]))
@mock_ec2
def test_volume_attach_and_detach():
conn = boto.connect_ec2('the_key', 'the_secret')
@ -139,6 +196,44 @@ def test_filter_snapshot_by_id():
s.region.name.should.equal(conn.region.name)
@mock_ec2
def test_snapshot_filters():
conn = boto.connect_ec2('the_key', 'the_secret')
volume1 = conn.create_volume(20, "us-east-1a")
volume2 = conn.create_volume(25, "us-east-1a")
snapshot1 = volume1.create_snapshot(description='testsnapshot1')
snapshot2 = volume1.create_snapshot(description='testsnapshot2')
snapshot3 = volume2.create_snapshot(description='testsnapshot3')
conn.create_tags([snapshot1.id], {'testkey1': 'testvalue1'})
conn.create_tags([snapshot2.id], {'testkey2': 'testvalue2'})
snapshots_by_description = conn.get_all_snapshots(filters={'description': 'testsnapshot1'})
set([snap.id for snap in snapshots_by_description]).should.equal(set([snapshot1.id]))
snapshots_by_id = conn.get_all_snapshots(filters={'snapshot-id': snapshot1.id})
set([snap.id for snap in snapshots_by_id]).should.equal(set([snapshot1.id]))
snapshots_by_start_time = conn.get_all_snapshots(filters={'start-time': snapshot1.start_time})
set([snap.start_time for snap in snapshots_by_start_time]).should.equal(set([snapshot1.start_time]))
snapshots_by_volume_id = conn.get_all_snapshots(filters={'volume-id': volume1.id})
set([snap.id for snap in snapshots_by_volume_id]).should.equal(set([snapshot1.id, snapshot2.id]))
snapshots_by_volume_size = conn.get_all_snapshots(filters={'volume-size': volume1.size})
set([snap.id for snap in snapshots_by_volume_size]).should.equal(set([snapshot1.id, snapshot2.id]))
snapshots_by_tag_key = conn.get_all_snapshots(filters={'tag-key': 'testkey1'})
set([snap.id for snap in snapshots_by_tag_key]).should.equal(set([snapshot1.id]))
snapshots_by_tag_value = conn.get_all_snapshots(filters={'tag-value': 'testvalue1'})
set([snap.id for snap in snapshots_by_tag_value]).should.equal(set([snapshot1.id]))
snapshots_by_tag = conn.get_all_snapshots(filters={'tag:testkey1': 'testvalue1'})
set([snap.id for snap in snapshots_by_tag]).should.equal(set([snapshot1.id]))
@mock_ec2
def test_snapshot_attribute():
conn = boto.connect_ec2('the_key', 'the_secret')