adding owner id to amis v2 (#1331)
* Adding owner-id/OwnerId to the AMI classes to allow the value to be specified to test filtering images based on owner. * Added default AMIs and filtering by owner-id * Fixed some tests * Fixed more random tests * Updated MANIFEST * .
This commit is contained in:
parent
123f369e6e
commit
bd8c1e4567
14 changed files with 758 additions and 110 deletions
|
|
@ -6,6 +6,7 @@ import ipaddress
|
|||
import json
|
||||
import re
|
||||
import six
|
||||
import warnings
|
||||
from pkg_resources import resource_filename
|
||||
|
||||
import boto.ec2
|
||||
|
|
@ -45,7 +46,6 @@ from .exceptions import (
|
|||
InvalidRouteTableIdError,
|
||||
InvalidRouteError,
|
||||
InvalidInstanceIdError,
|
||||
MalformedAMIIdError,
|
||||
InvalidAMIIdError,
|
||||
InvalidAMIAttributeItemValueError,
|
||||
InvalidSnapshotIdError,
|
||||
|
|
@ -113,8 +113,12 @@ from .utils import (
|
|||
tag_filter_matches,
|
||||
)
|
||||
|
||||
RES_FILE = resource_filename(__name__, 'resources/instance_types.json')
|
||||
INSTANCE_TYPES = json.load(open(RES_FILE, 'r'))
|
||||
INSTANCE_TYPES = json.load(
|
||||
open(resource_filename(__name__, 'resources/instance_types.json'), 'r')
|
||||
)
|
||||
AMIS = json.load(
|
||||
open(resource_filename(__name__, 'resources/amis.json'), 'r')
|
||||
)
|
||||
|
||||
|
||||
def utc_date_and_time():
|
||||
|
|
@ -384,6 +388,11 @@ class Instance(TaggedEC2Resource, BotoInstance):
|
|||
|
||||
amis = self.ec2_backend.describe_images(filters={'image-id': image_id})
|
||||
ami = amis[0] if amis else None
|
||||
if ami is None:
|
||||
warnings.warn('Could not find AMI with image-id:{0}, '
|
||||
'in the near future this will '
|
||||
'cause an error'.format(image_id),
|
||||
PendingDeprecationWarning)
|
||||
|
||||
self.platform = ami.platform if ami else None
|
||||
self.virtualization_type = ami.virtualization_type if ami else 'paravirtual'
|
||||
|
|
@ -1001,17 +1010,31 @@ class TagBackend(object):
|
|||
|
||||
class Ami(TaggedEC2Resource):
|
||||
def __init__(self, ec2_backend, ami_id, instance=None, source_ami=None,
|
||||
name=None, description=None):
|
||||
name=None, description=None, owner_id=None,
|
||||
|
||||
public=False, virtualization_type=None, architecture=None,
|
||||
state='available', creation_date=None, platform=None,
|
||||
image_type='machine', image_location=None, hypervisor=None,
|
||||
root_device_type=None, root_device_name=None, sriov='simple',
|
||||
region_name='us-east-1a'
|
||||
):
|
||||
self.ec2_backend = ec2_backend
|
||||
self.id = ami_id
|
||||
self.state = "available"
|
||||
self.state = state
|
||||
self.name = name
|
||||
self.image_type = image_type
|
||||
self.image_location = image_location
|
||||
self.owner_id = owner_id
|
||||
self.description = description
|
||||
self.virtualization_type = None
|
||||
self.architecture = None
|
||||
self.virtualization_type = virtualization_type
|
||||
self.architecture = architecture
|
||||
self.kernel_id = None
|
||||
self.platform = None
|
||||
self.creation_date = utc_date_and_time()
|
||||
self.platform = platform
|
||||
self.hypervisor = hypervisor
|
||||
self.root_device_name = root_device_name
|
||||
self.root_device_type = root_device_type
|
||||
self.sriov = sriov
|
||||
self.creation_date = utc_date_and_time() if creation_date is None else creation_date
|
||||
|
||||
if instance:
|
||||
self.instance = instance
|
||||
|
|
@ -1039,8 +1062,11 @@ class Ami(TaggedEC2Resource):
|
|||
self.launch_permission_groups = set()
|
||||
self.launch_permission_users = set()
|
||||
|
||||
if public:
|
||||
self.launch_permission_groups.add('all')
|
||||
|
||||
# AWS auto-creates these, we should reflect the same.
|
||||
volume = self.ec2_backend.create_volume(15, "us-east-1a")
|
||||
volume = self.ec2_backend.create_volume(15, region_name)
|
||||
self.ebs_snapshot = self.ec2_backend.create_snapshot(
|
||||
volume.id, "Auto-created snapshot for AMI %s" % self.id)
|
||||
|
||||
|
|
@ -1067,6 +1093,8 @@ class Ami(TaggedEC2Resource):
|
|||
return self.state
|
||||
elif filter_name == 'name':
|
||||
return self.name
|
||||
elif filter_name == 'owner-id':
|
||||
return self.owner_id
|
||||
else:
|
||||
return super(Ami, self).get_filter_value(
|
||||
filter_name, 'DescribeImages')
|
||||
|
|
@ -1075,14 +1103,22 @@ class Ami(TaggedEC2Resource):
|
|||
class AmiBackend(object):
|
||||
def __init__(self):
|
||||
self.amis = {}
|
||||
|
||||
self._load_amis()
|
||||
|
||||
super(AmiBackend, self).__init__()
|
||||
|
||||
def create_image(self, instance_id, name=None, description=None):
|
||||
def _load_amis(self):
|
||||
for ami in AMIS:
|
||||
ami_id = ami['ami_id']
|
||||
self.amis[ami_id] = Ami(self, **ami)
|
||||
|
||||
def create_image(self, instance_id, name=None, description=None, owner_id=None):
|
||||
# TODO: check that instance exists and pull info from it.
|
||||
ami_id = random_ami_id()
|
||||
instance = self.get_instance(instance_id)
|
||||
ami = Ami(self, ami_id, instance=instance, source_ami=None,
|
||||
name=name, description=description)
|
||||
name=name, description=description, owner_id=owner_id)
|
||||
self.amis[ami_id] = ami
|
||||
return ami
|
||||
|
||||
|
|
@ -1095,30 +1131,29 @@ class AmiBackend(object):
|
|||
self.amis[ami_id] = ami
|
||||
return ami
|
||||
|
||||
def describe_images(self, ami_ids=(), filters=None, exec_users=None):
|
||||
images = []
|
||||
def describe_images(self, ami_ids=(), filters=None, exec_users=None, owners=None):
|
||||
images = self.amis.values()
|
||||
|
||||
# Limit images by launch permissions
|
||||
if exec_users:
|
||||
for ami_id in self.amis:
|
||||
found = False
|
||||
tmp_images = []
|
||||
for ami in images:
|
||||
for user_id in exec_users:
|
||||
if user_id in self.amis[ami_id].launch_permission_users:
|
||||
found = True
|
||||
if found:
|
||||
images.append(self.amis[ami_id])
|
||||
if images == []:
|
||||
return images
|
||||
if user_id in ami.launch_permission_users:
|
||||
tmp_images.append(ami)
|
||||
images = tmp_images
|
||||
|
||||
# Limit by owner ids
|
||||
if owners:
|
||||
images = [ami for ami in images if ami.owner_id in owners]
|
||||
|
||||
if ami_ids:
|
||||
images = [ami for ami in images if ami.id in ami_ids]
|
||||
|
||||
# Generic filters
|
||||
if filters:
|
||||
images = images or self.amis.values()
|
||||
return generic_filter(filters, images)
|
||||
else:
|
||||
for ami_id in ami_ids:
|
||||
if ami_id in self.amis:
|
||||
images.append(self.amis[ami_id])
|
||||
elif not ami_id.startswith("ami-"):
|
||||
raise MalformedAMIIdError(ami_id)
|
||||
else:
|
||||
raise InvalidAMIIdError(ami_id)
|
||||
return images or self.amis.values()
|
||||
return images
|
||||
|
||||
def deregister_image(self, ami_id):
|
||||
if ami_id in self.amis:
|
||||
|
|
@ -3679,8 +3714,8 @@ class NatGatewayBackend(object):
|
|||
return self.nat_gateways.pop(nat_gateway_id)
|
||||
|
||||
|
||||
class EC2Backend(BaseBackend, InstanceBackend, TagBackend, AmiBackend,
|
||||
RegionsAndZonesBackend, SecurityGroupBackend, EBSBackend,
|
||||
class EC2Backend(BaseBackend, InstanceBackend, TagBackend, EBSBackend,
|
||||
RegionsAndZonesBackend, SecurityGroupBackend, AmiBackend,
|
||||
VPCBackend, SubnetBackend, SubnetRouteTableAssociationBackend,
|
||||
NetworkInterfaceBackend, VPNConnectionBackend,
|
||||
VPCPeeringConnectionBackend,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue