Fix : EC2 - Added ownerId filter for describe instances (#3149)

* Fix : EC2 - Added ownerId filter for describe instances

* linting
This commit is contained in:
usmangani1 2020-07-19 15:14:58 +05:30 committed by GitHub
parent 6fb7867767
commit 552b1294df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -11,6 +11,7 @@ from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives.asymmetric import rsa
from moto.iam.models import ACCOUNT_ID
EC2_RESOURCE_TO_PREFIX = { EC2_RESOURCE_TO_PREFIX = {
"customer-gateway": "cgw", "customer-gateway": "cgw",
@ -291,7 +292,9 @@ def get_object_value(obj, attr):
keys = attr.split(".") keys = attr.split(".")
val = obj val = obj
for key in keys: for key in keys:
if hasattr(val, key): if key == "owner_id":
return ACCOUNT_ID
elif hasattr(val, key):
val = getattr(val, key) val = getattr(val, key)
elif isinstance(val, dict): elif isinstance(val, dict):
val = val[key] val = val[key]
@ -364,6 +367,7 @@ filter_dict_attribute_mapping = {
"image-id": "image_id", "image-id": "image_id",
"network-interface.private-dns-name": "private_dns", "network-interface.private-dns-name": "private_dns",
"private-dns-name": "private_dns", "private-dns-name": "private_dns",
"owner-id": "owner_id",
} }

View File

@ -536,6 +536,20 @@ def test_get_instances_filtering_by_image_id():
reservations[0]["Instances"].should.have.length_of(1) 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 @mock_ec2
def test_get_instances_filtering_by_private_dns(): def test_get_instances_filtering_by_private_dns():
image_id = "ami-1234abcd" image_id = "ami-1234abcd"