Enhanced support for ami filtering (#4175)
This commit is contained in:
parent
39185cafef
commit
d2c9e85ed3
@ -1412,6 +1412,7 @@ class Ami(TaggedEC2Resource):
|
|||||||
name=None,
|
name=None,
|
||||||
description=None,
|
description=None,
|
||||||
owner_id=OWNER_ID,
|
owner_id=OWNER_ID,
|
||||||
|
owner_alias=None,
|
||||||
public=False,
|
public=False,
|
||||||
virtualization_type=None,
|
virtualization_type=None,
|
||||||
architecture=None,
|
architecture=None,
|
||||||
@ -1433,6 +1434,7 @@ class Ami(TaggedEC2Resource):
|
|||||||
self.image_type = image_type
|
self.image_type = image_type
|
||||||
self.image_location = image_location
|
self.image_location = image_location
|
||||||
self.owner_id = owner_id
|
self.owner_id = owner_id
|
||||||
|
self.owner_alias = owner_alias
|
||||||
self.description = description
|
self.description = description
|
||||||
self.virtualization_type = virtualization_type
|
self.virtualization_type = virtualization_type
|
||||||
self.architecture = architecture
|
self.architecture = architecture
|
||||||
@ -1507,6 +1509,8 @@ class Ami(TaggedEC2Resource):
|
|||||||
return self.name
|
return self.name
|
||||||
elif filter_name == "owner-id":
|
elif filter_name == "owner-id":
|
||||||
return self.owner_id
|
return self.owner_id
|
||||||
|
elif filter_name == "owner-alias":
|
||||||
|
return self.owner_alias
|
||||||
else:
|
else:
|
||||||
return super(Ami, self).get_filter_value(filter_name, "DescribeImages")
|
return super(Ami, self).get_filter_value(filter_name, "DescribeImages")
|
||||||
|
|
||||||
@ -1524,6 +1528,9 @@ class AmiBackend(object):
|
|||||||
def _load_amis(self):
|
def _load_amis(self):
|
||||||
for ami in AMIS:
|
for ami in AMIS:
|
||||||
ami_id = ami["ami_id"]
|
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)
|
self.amis[ami_id] = Ami(self, **ami)
|
||||||
|
|
||||||
def create_image(
|
def create_image(
|
||||||
@ -1607,8 +1614,15 @@ class AmiBackend(object):
|
|||||||
# Limit by owner ids
|
# Limit by owner ids
|
||||||
if owners:
|
if owners:
|
||||||
# support filtering by Owners=['self']
|
# support filtering by Owners=['self']
|
||||||
owners = list(map(lambda o: OWNER_ID if o == "self" else o, owners,))
|
if "self" in owners:
|
||||||
images = [ami for ami in images if ami.owner_id 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
|
# Generic filters
|
||||||
if filters:
|
if filters:
|
||||||
|
@ -610,5 +610,22 @@
|
|||||||
"name": "amazon-eks-node-linux",
|
"name": "amazon-eks-node-linux",
|
||||||
"virtualization_type": "hvm",
|
"virtualization_type": "hvm",
|
||||||
"hypervisor": "xen"
|
"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"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -290,7 +290,7 @@ def test_ami_filters():
|
|||||||
|
|
||||||
amis_by_architecture = conn.get_all_images(filters={"architecture": "x86_64"})
|
amis_by_architecture = conn.get_all_images(filters={"architecture": "x86_64"})
|
||||||
set([ami.id for ami in amis_by_architecture]).should.contain(imageB.id)
|
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"})
|
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]))
|
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 = [ami.id for ami in amis_by_state]
|
||||||
ami_ids_by_state.should.contain(imageA.id)
|
ami_ids_by_state.should.contain(imageA.id)
|
||||||
ami_ids_by_state.should.contain(imageB.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})
|
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]))
|
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"})
|
amis_by_public = conn.get_all_images(filters={"is-public": "true"})
|
||||||
set([ami.id for ami in amis_by_public]).should.contain(imageB.id)
|
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"})
|
amis_by_nonpublic = conn.get_all_images(filters={"is-public": "false"})
|
||||||
set([ami.id for ami in amis_by_nonpublic]).should.contain(imageA.id)
|
set([ami.id for ami in amis_by_nonpublic]).should.contain(imageA.id)
|
||||||
|
Loading…
Reference in New Issue
Block a user