Add list support to filters (#1083)

* initial pass with TODOs

* add list support to get_object_value

* fix group-id filters

* add tests for sg name and id filters
This commit is contained in:
Daniel Lutsch 2017-08-25 23:43:29 -07:00 committed by Jack Danger
parent 8e57dc92f4
commit 1f6b1b8c4a
2 changed files with 43 additions and 2 deletions

View File

@ -335,6 +335,11 @@ def get_object_value(obj, attr):
val = getattr(val, key)
elif isinstance(val, dict):
val = val[key]
elif isinstance(val, list):
for item in val:
item_val = get_object_value(item, key)
if item_val:
return item_val
else:
return None
return val
@ -385,8 +390,9 @@ filter_dict_attribute_mapping = {
'state-reason-code': '_state_reason.code',
'source-dest-check': 'source_dest_check',
'vpc-id': 'vpc_id',
'group-id': 'security_groups',
'instance.group-id': 'security_groups',
'group-id': 'security_groups.id',
'instance.group-id': 'security_groups.id',
'instance.group-name': 'security_groups.name',
'instance-type': 'instance_type',
'private-ip-address': 'private_ip',
'ip-address': 'public_ip',

View File

@ -437,6 +437,41 @@ def test_get_instances_filtering_by_ni_private_dns():
])['Reservations']
reservations[0]['Instances'].should.have.length_of(1)
@mock_ec2
def test_get_instances_filtering_by_instance_group_name():
image_id = 'ami-1234abcd'
client = boto3.client('ec2', region_name='us-east-1')
client.create_security_group(
Description='test',
GroupName='test_sg'
)
client.run_instances(ImageId=image_id,
MinCount=1,
MaxCount=1,
SecurityGroups=['test_sg'])
reservations = client.describe_instances(Filters=[
{'Name': 'instance.group-name', 'Values': ['test_sg']}
])['Reservations']
reservations[0]['Instances'].should.have.length_of(1)
@mock_ec2
def test_get_instances_filtering_by_instance_group_id():
image_id = 'ami-1234abcd'
client = boto3.client('ec2', region_name='us-east-1')
create_sg = client.create_security_group(
Description='test',
GroupName='test_sg'
)
group_id = create_sg['GroupId']
client.run_instances(ImageId=image_id,
MinCount=1,
MaxCount=1,
SecurityGroups=['test_sg'])
reservations = client.describe_instances(Filters=[
{'Name': 'instance.group-id', 'Values': [group_id]}
])['Reservations']
reservations[0]['Instances'].should.have.length_of(1)
@mock_ec2_deprecated
def test_get_instances_filtering_by_tag():
conn = boto.connect_ec2()