DescribeSubnets: Added support for 'filters' parameter.
This commit is contained in:
parent
348d1803ed
commit
d683c3b291
3 changed files with 68 additions and 3 deletions
|
|
@ -879,6 +879,19 @@ class Subnet(TaggedEC2Instance):
|
|||
def physical_resource_id(self):
|
||||
return self.id
|
||||
|
||||
def get_filter_value(self, filter_name):
|
||||
if filter_name in ['cidr', 'cidrBlock', 'cidr-block']:
|
||||
return self.cidr_block
|
||||
elif filter_name == 'vpc-id':
|
||||
return self.vpc_id
|
||||
elif filter_name == 'subnet-id':
|
||||
return self.id
|
||||
else:
|
||||
msg = "The filter '{0}' for DescribeSubnets has not been" \
|
||||
" implemented in Moto yet. Feel free to open an issue at" \
|
||||
" https://github.com/spulec/moto/issues"
|
||||
raise NotImplementedError(msg)
|
||||
|
||||
|
||||
class SubnetBackend(object):
|
||||
def __init__(self):
|
||||
|
|
@ -891,8 +904,14 @@ class SubnetBackend(object):
|
|||
self.subnets[subnet_id] = subnet
|
||||
return subnet
|
||||
|
||||
def get_all_subnets(self):
|
||||
return self.subnets.values()
|
||||
def get_all_subnets(self, filters=None):
|
||||
subnets = self.subnets.values()
|
||||
|
||||
if filters:
|
||||
for (_filter, _filter_value) in filters.iteritems():
|
||||
subnets = [ subnet for subnet in subnets if subnet.get_filter_value(_filter) in _filter_value ]
|
||||
|
||||
return subnets
|
||||
|
||||
def delete_subnet(self, subnet_id):
|
||||
deleted = self.subnets.pop(subnet_id, None)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from jinja2 import Template
|
|||
|
||||
from moto.core.responses import BaseResponse
|
||||
from moto.ec2.models import ec2_backend
|
||||
from moto.ec2.utils import filters_from_querystring
|
||||
|
||||
|
||||
class Subnets(BaseResponse):
|
||||
|
|
@ -19,7 +20,8 @@ class Subnets(BaseResponse):
|
|||
return template.render(subnet=subnet)
|
||||
|
||||
def describe_subnets(self):
|
||||
subnets = ec2_backend.get_all_subnets()
|
||||
filters = filters_from_querystring(self.querystring)
|
||||
subnets = ec2_backend.get_all_subnets(filters)
|
||||
template = Template(DESCRIBE_SUBNETS_RESPONSE)
|
||||
return template.render(subnets=subnets)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue