diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 365632abd..8f86d0a8d 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -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', diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 75f759c0f..a9af7c31f 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -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()