From 552b1294df3e26aad23eb4e6497d7cda55d7928e Mon Sep 17 00:00:00 2001 From: usmangani1 Date: Sun, 19 Jul 2020 15:14:58 +0530 Subject: [PATCH] Fix : EC2 - Added ownerId filter for describe instances (#3149) * Fix : EC2 - Added ownerId filter for describe instances * linting --- moto/ec2/utils.py | 6 +++++- tests/test_ec2/test_instances.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index b8c19b580..bc124bddf 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -11,6 +11,7 @@ from cryptography.hazmat.primitives import serialization from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import rsa +from moto.iam.models import ACCOUNT_ID EC2_RESOURCE_TO_PREFIX = { "customer-gateway": "cgw", @@ -291,7 +292,9 @@ def get_object_value(obj, attr): keys = attr.split(".") val = obj for key in keys: - if hasattr(val, key): + if key == "owner_id": + return ACCOUNT_ID + elif hasattr(val, key): val = getattr(val, key) elif isinstance(val, dict): val = val[key] @@ -364,6 +367,7 @@ filter_dict_attribute_mapping = { "image-id": "image_id", "network-interface.private-dns-name": "private_dns", "private-dns-name": "private_dns", + "owner-id": "owner_id", } diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index c775ab0ab..1310b3a1d 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -536,6 +536,20 @@ def test_get_instances_filtering_by_image_id(): reservations[0]["Instances"].should.have.length_of(1) +@mock_ec2 +def test_get_instances_filtering_by_account_id(): + image_id = "ami-1234abcd" + client = boto3.client("ec2", region_name="us-east-1") + conn = boto3.resource("ec2", "us-east-1") + conn.create_instances(ImageId=image_id, MinCount=1, MaxCount=1) + + reservations = client.describe_instances( + Filters=[{"Name": "owner-id", "Values": ["123456789012"]}] + )["Reservations"] + + reservations[0]["Instances"].should.have.length_of(1) + + @mock_ec2 def test_get_instances_filtering_by_private_dns(): image_id = "ami-1234abcd"