Add filter "availabilityZone" to DescribeSubnets and add availability zone support too
This commit is contained in:
parent
82214d2bc2
commit
56cb06041c
@ -1695,11 +1695,12 @@ class VPCPeeringConnectionBackend(object):
|
|||||||
|
|
||||||
|
|
||||||
class Subnet(TaggedEC2Resource):
|
class Subnet(TaggedEC2Resource):
|
||||||
def __init__(self, ec2_backend, subnet_id, vpc_id, cidr_block):
|
def __init__(self, ec2_backend, subnet_id, vpc_id, cidr_block, availability_zone):
|
||||||
self.ec2_backend = ec2_backend
|
self.ec2_backend = ec2_backend
|
||||||
self.id = subnet_id
|
self.id = subnet_id
|
||||||
self.vpc_id = vpc_id
|
self.vpc_id = vpc_id
|
||||||
self.cidr_block = cidr_block
|
self.cidr_block = cidr_block
|
||||||
|
self._availability_zone = availability_zone
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||||
@ -1715,21 +1716,42 @@ class Subnet(TaggedEC2Resource):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def availability_zone(self):
|
def availability_zone(self):
|
||||||
# This could probably be smarter, but there doesn't appear to be a
|
if self._availability_zone is None:
|
||||||
# way to pull AZs for a region in boto
|
# This could probably be smarter, but there doesn't appear to be a
|
||||||
return self.ec2_backend.region_name + "a"
|
# way to pull AZs for a region in boto
|
||||||
|
return self.ec2_backend.region_name + "a"
|
||||||
|
else:
|
||||||
|
return self._availability_zone
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def physical_resource_id(self):
|
def physical_resource_id(self):
|
||||||
return self.id
|
return self.id
|
||||||
|
|
||||||
def get_filter_value(self, filter_name):
|
def get_filter_value(self, filter_name):
|
||||||
|
"""
|
||||||
|
API Version 2014-10-01 defines the following filters for DescribeSubnets:
|
||||||
|
|
||||||
|
* availabilityZone
|
||||||
|
* available-ip-address-count
|
||||||
|
* cidrBlock
|
||||||
|
* defaultForAz
|
||||||
|
* state
|
||||||
|
* subnet-id
|
||||||
|
* tag:key=value
|
||||||
|
* tag-key
|
||||||
|
* tag-value
|
||||||
|
* vpc-id
|
||||||
|
|
||||||
|
Taken from: http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSubnets.html
|
||||||
|
"""
|
||||||
if filter_name in ['cidr', 'cidrBlock', 'cidr-block']:
|
if filter_name in ['cidr', 'cidrBlock', 'cidr-block']:
|
||||||
return self.cidr_block
|
return self.cidr_block
|
||||||
elif filter_name == 'vpc-id':
|
elif filter_name == 'vpc-id':
|
||||||
return self.vpc_id
|
return self.vpc_id
|
||||||
elif filter_name == 'subnet-id':
|
elif filter_name == 'subnet-id':
|
||||||
return self.id
|
return self.id
|
||||||
|
elif filter_name == 'availabilityZone':
|
||||||
|
return self.availability_zone
|
||||||
|
|
||||||
filter_value = super(Subnet, self).get_filter_value(filter_name)
|
filter_value = super(Subnet, self).get_filter_value(filter_name)
|
||||||
|
|
||||||
@ -1756,9 +1778,9 @@ class SubnetBackend(object):
|
|||||||
raise InvalidSubnetIdError(subnet_id)
|
raise InvalidSubnetIdError(subnet_id)
|
||||||
return subnet
|
return subnet
|
||||||
|
|
||||||
def create_subnet(self, vpc_id, cidr_block):
|
def create_subnet(self, vpc_id, cidr_block, availability_zone=None):
|
||||||
subnet_id = random_subnet_id()
|
subnet_id = random_subnet_id()
|
||||||
subnet = Subnet(self, subnet_id, vpc_id, cidr_block)
|
subnet = Subnet(self, subnet_id, vpc_id, cidr_block, availability_zone)
|
||||||
self.get_vpc(vpc_id) # Validate VPC exists
|
self.get_vpc(vpc_id) # Validate VPC exists
|
||||||
|
|
||||||
# AWS associates a new subnet with the default Network ACL
|
# AWS associates a new subnet with the default Network ACL
|
||||||
|
@ -7,7 +7,15 @@ class Subnets(BaseResponse):
|
|||||||
def create_subnet(self):
|
def create_subnet(self):
|
||||||
vpc_id = self.querystring.get('VpcId')[0]
|
vpc_id = self.querystring.get('VpcId')[0]
|
||||||
cidr_block = self.querystring.get('CidrBlock')[0]
|
cidr_block = self.querystring.get('CidrBlock')[0]
|
||||||
subnet = self.ec2_backend.create_subnet(vpc_id, cidr_block)
|
if 'AvailabilityZone' in self.querystring:
|
||||||
|
availability_zone = self.querystring['AvailabilityZone'][0]
|
||||||
|
else:
|
||||||
|
availability_zone = None
|
||||||
|
subnet = self.ec2_backend.create_subnet(
|
||||||
|
vpc_id,
|
||||||
|
cidr_block,
|
||||||
|
availability_zone,
|
||||||
|
)
|
||||||
template = self.response_template(CREATE_SUBNET_RESPONSE)
|
template = self.response_template(CREATE_SUBNET_RESPONSE)
|
||||||
return template.render(subnet=subnet)
|
return template.render(subnet=subnet)
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import tests.backport_assert_raises
|
|||||||
from nose.tools import assert_raises
|
from nose.tools import assert_raises
|
||||||
|
|
||||||
import boto
|
import boto
|
||||||
|
import boto.vpc
|
||||||
from boto.exception import EC2ResponseError
|
from boto.exception import EC2ResponseError
|
||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
|
||||||
@ -62,12 +63,12 @@ def test_subnet_tagging():
|
|||||||
|
|
||||||
@mock_ec2
|
@mock_ec2
|
||||||
def test_get_subnets_filtering():
|
def test_get_subnets_filtering():
|
||||||
conn = boto.connect_vpc('the_key', 'the_secret')
|
conn = boto.vpc.connect_to_region('us-west-1')
|
||||||
vpcA = conn.create_vpc("10.0.0.0/16")
|
vpcA = conn.create_vpc("10.0.0.0/16")
|
||||||
subnetA = conn.create_subnet(vpcA.id, "10.0.0.0/24")
|
subnetA = conn.create_subnet(vpcA.id, "10.0.0.0/24", availability_zone='us-west-1a')
|
||||||
vpcB = conn.create_vpc("10.0.0.0/16")
|
vpcB = conn.create_vpc("10.0.0.0/16")
|
||||||
subnetB1 = conn.create_subnet(vpcB.id, "10.0.0.0/24")
|
subnetB1 = conn.create_subnet(vpcB.id, "10.0.0.0/24", availability_zone='us-west-1a')
|
||||||
subnetB2 = conn.create_subnet(vpcB.id, "10.0.1.0/24")
|
subnetB2 = conn.create_subnet(vpcB.id, "10.0.1.0/24", availability_zone='us-west-1b')
|
||||||
|
|
||||||
all_subnets = conn.get_all_subnets()
|
all_subnets = conn.get_all_subnets()
|
||||||
all_subnets.should.have.length_of(3)
|
all_subnets.should.have.length_of(3)
|
||||||
@ -100,5 +101,10 @@ def test_get_subnets_filtering():
|
|||||||
subnets_by_id.should.have.length_of(1)
|
subnets_by_id.should.have.length_of(1)
|
||||||
set([subnet.id for subnet in subnets_by_id]).should.equal(set([subnetA.id]))
|
set([subnet.id for subnet in subnets_by_id]).should.equal(set([subnetA.id]))
|
||||||
|
|
||||||
|
# Filter by availabilityZone
|
||||||
|
subnets_by_az = conn.get_all_subnets(filters={'availabilityZone': 'us-west-1a', 'vpc-id': vpcB.id})
|
||||||
|
subnets_by_az.should.have.length_of(1)
|
||||||
|
set([subnet.id for subnet in subnets_by_az]).should.equal(set([subnetB1.id]))
|
||||||
|
|
||||||
# Unsupported filter
|
# Unsupported filter
|
||||||
conn.get_all_subnets.when.called_with(filters={'not-implemented-filter': 'foobar'}).should.throw(NotImplementedError)
|
conn.get_all_subnets.when.called_with(filters={'not-implemented-filter': 'foobar'}).should.throw(NotImplementedError)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user