from __future__ import unicode_literals
import random
from moto.core.responses import BaseResponse
from moto.core.utils import camelcase_to_underscores
from moto.ec2.utils import filters_from_querystring
class Subnets(BaseResponse):
def create_subnet(self):
vpc_id = self._get_param('VpcId')
cidr_block = self._get_param('CidrBlock')
availability_zone = self._get_param(
'AvailabilityZone', if_none=random.choice(
self.ec2_backend.describe_availability_zones()).name)
subnet = self.ec2_backend.create_subnet(
vpc_id,
cidr_block,
availability_zone,
context=self,
)
template = self.response_template(CREATE_SUBNET_RESPONSE)
return template.render(subnet=subnet)
def delete_subnet(self):
subnet_id = self._get_param('SubnetId')
subnet = self.ec2_backend.delete_subnet(subnet_id)
template = self.response_template(DELETE_SUBNET_RESPONSE)
return template.render(subnet=subnet)
def describe_subnets(self):
subnet_ids = self._get_multi_param('SubnetId')
filters = filters_from_querystring(self.querystring)
subnets = self.ec2_backend.get_all_subnets(subnet_ids, filters)
template = self.response_template(DESCRIBE_SUBNETS_RESPONSE)
return template.render(subnets=subnets)
def modify_subnet_attribute(self):
subnet_id = self._get_param('SubnetId')
for attribute in ('MapPublicIpOnLaunch', 'AssignIpv6AddressOnCreation'):
if self.querystring.get('%s.Value' % attribute):
attr_name = camelcase_to_underscores(attribute)
attr_value = self.querystring.get('%s.Value' % attribute)[0]
self.ec2_backend.modify_subnet_attribute(
subnet_id, attr_name, attr_value)
return MODIFY_SUBNET_ATTRIBUTE_RESPONSE
CREATE_SUBNET_RESPONSE = """
7a62c49f-347e-4fc4-9331-6e8eEXAMPLE
{{ subnet.id }}
pending
{{ subnet.vpc_id }}
{{ subnet.cidr_block }}
251
{{ subnet._availability_zone.name }}
{{ subnet._availability_zone.zone_id }}
{{ subnet.default_for_az }}
{{ subnet.map_public_ip_on_launch }}
{{ subnet.owner_id }}
{{ subnet.assign_ipv6_address_on_creation }}
{{ subnet.ipv6_cidr_block_associations }}
arn:aws:ec2:{{ subnet._availability_zone.name[0:-1] }}:{{ subnet.owner_id }}:subnet/{{ subnet.id }}
"""
DELETE_SUBNET_RESPONSE = """
7a62c49f-347e-4fc4-9331-6e8eEXAMPLE
true
"""
DESCRIBE_SUBNETS_RESPONSE = """
7a62c49f-347e-4fc4-9331-6e8eEXAMPLE
{% for subnet in subnets %}
-
{{ subnet.id }}
available
{{ subnet.vpc_id }}
{{ subnet.cidr_block }}
251
{{ subnet._availability_zone.name }}
{{ subnet._availability_zone.zone_id }}
{{ subnet.default_for_az }}
{{ subnet.map_public_ip_on_launch }}
{{ subnet.owner_id }}
{{ subnet.assign_ipv6_address_on_creation }}
{{ subnet.ipv6_cidr_block_associations }}
arn:aws:ec2:{{ subnet._availability_zone.name[0:-1] }}:{{ subnet.owner_id }}:subnet/{{ subnet.id }}
{% if subnet.get_tags() %}
{% for tag in subnet.get_tags() %}
-
{{ tag.resource_id }}
{{ tag.resource_type }}
{{ tag.key }}
{{ tag.value }}
{% endfor %}
{% endif %}
{% endfor %}
"""
MODIFY_SUBNET_ATTRIBUTE_RESPONSE = """
7a62c49f-347e-4fc4-9331-6e8eEXAMPLE
true
"""