Enhanced support for ami filtering (#4175)

This commit is contained in:
Macwan Nevil 2021-08-15 14:39:50 +05:30 committed by GitHub
parent 39185cafef
commit d2c9e85ed3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 5 deletions

View File

@ -1412,6 +1412,7 @@ class Ami(TaggedEC2Resource):
name=None,
description=None,
owner_id=OWNER_ID,
owner_alias=None,
public=False,
virtualization_type=None,
architecture=None,
@ -1433,6 +1434,7 @@ class Ami(TaggedEC2Resource):
self.image_type = image_type
self.image_location = image_location
self.owner_id = owner_id
self.owner_alias = owner_alias
self.description = description
self.virtualization_type = virtualization_type
self.architecture = architecture
@ -1507,6 +1509,8 @@ class Ami(TaggedEC2Resource):
return self.name
elif filter_name == "owner-id":
return self.owner_id
elif filter_name == "owner-alias":
return self.owner_alias
else:
return super(Ami, self).get_filter_value(filter_name, "DescribeImages")
@ -1524,6 +1528,9 @@ class AmiBackend(object):
def _load_amis(self):
for ami in AMIS:
ami_id = ami["ami_id"]
# we are assuming the default loaded amis are owned by amazon
# owner_alias is required for terraform owner filters
ami["owner_alias"] = "amazon"
self.amis[ami_id] = Ami(self, **ami)
def create_image(
@ -1607,8 +1614,15 @@ class AmiBackend(object):
# Limit by owner ids
if owners:
# support filtering by Owners=['self']
owners = list(map(lambda o: OWNER_ID if o == "self" else o, owners,))
images = [ami for ami in images if ami.owner_id in owners]
if "self" in owners:
owners = list(
map(lambda o: OWNER_ID if o == "self" else o, owners,)
)
images = [
ami
for ami in images
if ami.owner_id in owners or ami.owner_alias in owners
]
# Generic filters
if filters:

View File

@ -610,5 +610,22 @@
"name": "amazon-eks-node-linux",
"virtualization_type": "hvm",
"hypervisor": "xen"
},
{
"ami_id": "ami-00a36856283d67c39",
"state": "available",
"public": true,
"owner_id": "137112412989",
"image_location": "amazon/amzn-ami-vpc-nat-2018.03.0.20210721.0-x86_64-ebs",
"sriov": "simple",
"root_device_type": "ebs",
"root_device_name": "/dev/xvda",
"description": "Amazon Linux AMI 2018.03.0.20210721.0 x86_64 VPC HVM ebs",
"image_type": "machine",
"platform": "Linux/UNIX",
"architecture": "x86_64",
"name": "amzn-ami-vpc-nat-2018.03.0.20210721.0-x86_64-ebs",
"virtualization_type": "hvm",
"hypervisor": "xen"
}
]

View File

@ -290,7 +290,7 @@ def test_ami_filters():
amis_by_architecture = conn.get_all_images(filters={"architecture": "x86_64"})
set([ami.id for ami in amis_by_architecture]).should.contain(imageB.id)
len(amis_by_architecture).should.equal(37)
len(amis_by_architecture).should.equal(38)
amis_by_kernel = conn.get_all_images(filters={"kernel-id": "k-abcd1234"})
set([ami.id for ami in amis_by_kernel]).should.equal(set([imageB.id]))
@ -312,14 +312,14 @@ def test_ami_filters():
ami_ids_by_state = [ami.id for ami in amis_by_state]
ami_ids_by_state.should.contain(imageA.id)
ami_ids_by_state.should.contain(imageB.id)
len(amis_by_state).should.equal(38)
len(amis_by_state).should.equal(39)
amis_by_name = conn.get_all_images(filters={"name": imageA.name})
set([ami.id for ami in amis_by_name]).should.equal(set([imageA.id]))
amis_by_public = conn.get_all_images(filters={"is-public": "true"})
set([ami.id for ami in amis_by_public]).should.contain(imageB.id)
len(amis_by_public).should.equal(37)
len(amis_by_public).should.equal(38)
amis_by_nonpublic = conn.get_all_images(filters={"is-public": "false"})
set([ami.id for ami in amis_by_nonpublic]).should.contain(imageA.id)