moto/moto/ec2/responses/subnets.py

84 lines
2.9 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2013-02-21 23:13:01 -05:00
from jinja2 import Template
from moto.core.responses import BaseResponse
from moto.ec2.utils import filters_from_querystring
2013-02-21 23:13:01 -05:00
class Subnets(BaseResponse):
2013-02-21 23:13:01 -05:00
def create_subnet(self):
2013-03-05 22:53:53 -05:00
vpc_id = self.querystring.get('VpcId')[0]
cidr_block = self.querystring.get('CidrBlock')[0]
subnet = self.ec2_backend.create_subnet(vpc_id, cidr_block)
2013-03-05 22:53:53 -05:00
template = Template(CREATE_SUBNET_RESPONSE)
return template.render(subnet=subnet)
2013-02-21 23:13:01 -05:00
def delete_subnet(self):
2013-03-05 22:53:53 -05:00
subnet_id = self.querystring.get('SubnetId')[0]
subnet = self.ec2_backend.delete_subnet(subnet_id)
template = Template(DELETE_SUBNET_RESPONSE)
return template.render(subnet=subnet)
2013-02-21 23:13:01 -05:00
def describe_subnets(self):
filters = filters_from_querystring(self.querystring)
subnets = self.ec2_backend.get_all_subnets(filters)
2013-03-05 22:53:53 -05:00
template = Template(DESCRIBE_SUBNETS_RESPONSE)
return template.render(subnets=subnets)
CREATE_SUBNET_RESPONSE = """
<CreateSubnetResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<subnet>
<subnetId>{{ subnet.id }}</subnetId>
<state>pending</state>
<vpcId>{{ subnet.vpc_id }}</vpcId>
2013-03-05 22:53:53 -05:00
<cidrBlock>{{ subnet.cidr_block }}</cidrBlock>
<availableIpAddressCount>251</availableIpAddressCount>
<availabilityZone>us-east-1a</availabilityZone>
<tagSet>
{% for tag in subnet.get_tags() %}
<item>
<resourceId>{{ tag.resource_id }}</resourceId>
<resourceType>{{ tag.resource_type }}</resourceType>
<key>{{ tag.key }}</key>
<value>{{ tag.value }}</value>
</item>
{% endfor %}
</tagSet>
2013-03-05 22:53:53 -05:00
</subnet>
</CreateSubnetResponse>"""
DELETE_SUBNET_RESPONSE = """
<DeleteSubnetResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<return>true</return>
</DeleteSubnetResponse>"""
DESCRIBE_SUBNETS_RESPONSE = """
<DescribeSubnetsResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<subnetSet>
{% for subnet in subnets %}
<item>
<subnetId>{{ subnet.id }}</subnetId>
<state>available</state>
<vpcId>{{ subnet.vpc_id }}</vpcId>
2013-03-05 22:53:53 -05:00
<cidrBlock>{{ subnet.cidr_block }}</cidrBlock>
<availableIpAddressCount>251</availableIpAddressCount>
<availabilityZone>us-east-1a</availabilityZone>
<tagSet>
{% for tag in subnet.get_tags() %}
<item>
<resourceId>{{ tag.resource_id }}</resourceId>
<resourceType>{{ tag.resource_type }}</resourceType>
<key>{{ tag.key }}</key>
<value>{{ tag.value }}</value>
</item>
{% endfor %}
</tagSet>
2013-03-05 22:53:53 -05:00
</item>
{% endfor %}
</subnetSet>
</DescribeSubnetsResponse>"""