fixed rt, eip, natgateway integration (#4176)

This commit is contained in:
Macwan Nevil 2021-08-15 14:41:04 +05:30 committed by GitHub
parent d2c9e85ed3
commit 7397b8c129
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 17 deletions

View File

@ -5297,7 +5297,7 @@ class SpotFleetBackend(object):
class ElasticAddress(TaggedEC2Resource, CloudFormationModel): class ElasticAddress(TaggedEC2Resource, CloudFormationModel):
def __init__(self, ec2_backend, domain, address=None): def __init__(self, ec2_backend, domain, address=None, tags=None):
self.ec2_backend = ec2_backend self.ec2_backend = ec2_backend
if address: if address:
self.public_ip = address self.public_ip = address
@ -5309,6 +5309,7 @@ class ElasticAddress(TaggedEC2Resource, CloudFormationModel):
self.instance = None self.instance = None
self.eni = None self.eni = None
self.association_id = None self.association_id = None
self.add_tags(tags or {})
@staticmethod @staticmethod
def cloudformation_name_type(): def cloudformation_name_type():
@ -5329,6 +5330,7 @@ class ElasticAddress(TaggedEC2Resource, CloudFormationModel):
instance_id = None instance_id = None
if properties: if properties:
domain = properties.get("Domain") domain = properties.get("Domain")
# TODO: support tags from cloudformation template
eip = ec2_backend.allocate_address(domain=domain if domain else "standard") eip = ec2_backend.allocate_address(domain=domain if domain else "standard")
instance_id = properties.get("InstanceId") instance_id = properties.get("InstanceId")
else: else:
@ -5380,13 +5382,13 @@ class ElasticAddressBackend(object):
self.addresses = [] self.addresses = []
super(ElasticAddressBackend, self).__init__() super(ElasticAddressBackend, self).__init__()
def allocate_address(self, domain, address=None): def allocate_address(self, domain, address=None, tags=None):
if domain not in ["standard", "vpc"]: if domain not in ["standard", "vpc"]:
raise InvalidDomainError(domain) raise InvalidDomainError(domain)
if address: if address:
address = ElasticAddress(self, domain=domain, address=address) address = ElasticAddress(self, domain=domain, address=address, tags=tags)
else: else:
address = ElasticAddress(self, domain=domain) address = ElasticAddress(self, domain=domain, tags=tags)
self.addresses.append(address) self.addresses.append(address)
return address return address
@ -6940,13 +6942,16 @@ class TransitGatewayRelationsBackend(object):
class NatGateway(CloudFormationModel): class NatGateway(CloudFormationModel):
def __init__(self, backend, subnet_id, allocation_id, tags=[]): def __init__(
self, backend, subnet_id, allocation_id, tags=[], connectivity_type="public"
):
# public properties # public properties
self.id = random_nat_gateway_id() self.id = random_nat_gateway_id()
self.subnet_id = subnet_id self.subnet_id = subnet_id
self.allocation_id = allocation_id self.allocation_id = allocation_id
self.state = "available" self.state = "available"
self.private_ip = random_private_ip() self.private_ip = random_private_ip()
self.connectivity_type = connectivity_type
# protected properties # protected properties
self._created_at = datetime.utcnow() self._created_at = datetime.utcnow()
@ -7039,8 +7044,12 @@ class NatGatewayBackend(object):
return nat_gateways return nat_gateways
def create_nat_gateway(self, subnet_id, allocation_id, tags=[]): def create_nat_gateway(
nat_gateway = NatGateway(self, subnet_id, allocation_id, tags) self, subnet_id, allocation_id, tags=[], connectivity_type="public"
):
nat_gateway = NatGateway(
self, subnet_id, allocation_id, tags, connectivity_type
)
self.nat_gateways[nat_gateway.id] = nat_gateway self.nat_gateways[nat_gateway.id] = nat_gateway
return nat_gateway return nat_gateway

View File

@ -1,19 +1,22 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from moto.core.responses import BaseResponse from moto.core.responses import BaseResponse
from moto.ec2.utils import filters_from_querystring from moto.ec2.utils import filters_from_querystring, add_tag_specification
class ElasticIPAddresses(BaseResponse): class ElasticIPAddresses(BaseResponse):
def allocate_address(self): def allocate_address(self):
domain = self._get_param("Domain", if_none="standard") domain = self._get_param("Domain", if_none="standard")
reallocate_address = self._get_param("Address", if_none=None) reallocate_address = self._get_param("Address", if_none=None)
tags = self._get_multi_param("TagSpecification")
tags = add_tag_specification(tags)
if self.is_not_dryrun("AllocateAddress"): if self.is_not_dryrun("AllocateAddress"):
if reallocate_address: if reallocate_address:
address = self.ec2_backend.allocate_address( address = self.ec2_backend.allocate_address(
domain, address=reallocate_address domain, address=reallocate_address, tags=tags
) )
else: else:
address = self.ec2_backend.allocate_address(domain) address = self.ec2_backend.allocate_address(domain, tags=tags)
template = self.response_template(ALLOCATE_ADDRESS_RESPONSE) template = self.response_template(ALLOCATE_ADDRESS_RESPONSE)
return template.render(address=address) return template.render(address=address)
@ -154,8 +157,6 @@ DESCRIBE_ADDRESS_RESPONSE = """<DescribeAddressesResponse xmlns="http://ec2.amaz
<tagSet> <tagSet>
{% for tag in address.get_tags() %} {% for tag in address.get_tags() %}
<item> <item>
<resourceId>{{ tag.resource_id }}</resourceId>
<resourceType>{{ tag.resource_type }}</resourceType>
<key>{{ tag.key }}</key> <key>{{ tag.key }}</key>
<value>{{ tag.value }}</value> <value>{{ tag.value }}</value>
</item> </item>

View File

@ -7,11 +7,15 @@ class NatGateways(BaseResponse):
def create_nat_gateway(self): def create_nat_gateway(self):
subnet_id = self._get_param("SubnetId") subnet_id = self._get_param("SubnetId")
allocation_id = self._get_param("AllocationId") allocation_id = self._get_param("AllocationId")
connectivity_type = self._get_param("ConnectivityType")
tags = self._get_multi_param("TagSpecification") tags = self._get_multi_param("TagSpecification")
if tags: if tags:
tags = tags[0].get("Tag") tags = tags[0].get("Tag")
nat_gateway = self.ec2_backend.create_nat_gateway( nat_gateway = self.ec2_backend.create_nat_gateway(
subnet_id=subnet_id, allocation_id=allocation_id, tags=tags subnet_id=subnet_id,
allocation_id=allocation_id,
tags=tags,
connectivity_type=connectivity_type,
) )
template = self.response_template(CREATE_NAT_GATEWAY) template = self.response_template(CREATE_NAT_GATEWAY)
return template.render(nat_gateway=nat_gateway) return template.render(nat_gateway=nat_gateway)
@ -46,6 +50,7 @@ DESCRIBE_NAT_GATEWAYS_RESPONSE = """<DescribeNatGatewaysResponse xmlns="http://e
<createTime>{{ nat_gateway.create_time }}</createTime> <createTime>{{ nat_gateway.create_time }}</createTime>
<vpcId>{{ nat_gateway.vpc_id }}</vpcId> <vpcId>{{ nat_gateway.vpc_id }}</vpcId>
<natGatewayId>{{ nat_gateway.id }}</natGatewayId> <natGatewayId>{{ nat_gateway.id }}</natGatewayId>
<connectivityType>{{ nat_gateway.connectivity_type }}</connectivityType>
<state>{{ nat_gateway.state }}</state> <state>{{ nat_gateway.state }}</state>
{% if nat_gateway.tags %} {% if nat_gateway.tags %}
<tagSet> <tagSet>
@ -75,6 +80,7 @@ CREATE_NAT_GATEWAY = """<CreateNatGatewayResponse xmlns="http://ec2.amazonaws.co
<createTime>{{ nat_gateway.create_time }}</createTime> <createTime>{{ nat_gateway.create_time }}</createTime>
<vpcId>{{ nat_gateway.vpc_id }}</vpcId> <vpcId>{{ nat_gateway.vpc_id }}</vpcId>
<natGatewayId>{{ nat_gateway.id }}</natGatewayId> <natGatewayId>{{ nat_gateway.id }}</natGatewayId>
<connectivityType>{{ nat_gateway.connectivity_type }}</connectivityType>
<state>{{ nat_gateway.state }}</state> <state>{{ nat_gateway.state }}</state>
</natGateway> </natGateway>
</CreateNatGatewayResponse> </CreateNatGatewayResponse>

View File

@ -81,6 +81,7 @@ TestAccAWSRouteTable_tags
TestAccAWSRouteTable_vgwRoutePropagation TestAccAWSRouteTable_vgwRoutePropagation
TestAccAWSRouteTable_RequireRouteTarget TestAccAWSRouteTable_RequireRouteTarget
TestAccAWSRouteTable_disappears_SubnetAssociation TestAccAWSRouteTable_disappears_SubnetAssociation
TestAccAWSRouteTable_IPv4_To_NatGateway
TestAccAWSRouteTable_disappears TestAccAWSRouteTable_disappears
TestAccAWSRouteTable_basic TestAccAWSRouteTable_basic
TestAccAwsEc2ManagedPrefixList TestAccAwsEc2ManagedPrefixList