Fix:Added nat-gateway tags (#3560)

* Fix:Added nat-gateway tags

* Removed changes
This commit is contained in:
usmangani1 2021-01-07 23:29:46 +05:30 committed by GitHub
parent 9ecea2012a
commit bce682a867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 4 deletions

View File

@ -5773,7 +5773,7 @@ class CustomerGatewayBackend(object):
class NatGateway(CloudFormationModel):
def __init__(self, backend, subnet_id, allocation_id):
def __init__(self, backend, subnet_id, allocation_id, tags=[]):
# public properties
self.id = random_nat_gateway_id()
self.subnet_id = subnet_id
@ -5791,6 +5791,7 @@ class NatGateway(CloudFormationModel):
# associate allocation with ENI
self._backend.associate_address(eni=self._eni, allocation_id=self.allocation_id)
self.tags = tags
@property
def vpc_id(self):
@ -5867,8 +5868,8 @@ class NatGatewayBackend(object):
return nat_gateways
def create_nat_gateway(self, subnet_id, allocation_id):
nat_gateway = NatGateway(self, subnet_id, allocation_id)
def create_nat_gateway(self, subnet_id, allocation_id, tags=[]):
nat_gateway = NatGateway(self, subnet_id, allocation_id, tags)
self.nat_gateways[nat_gateway.id] = nat_gateway
return nat_gateway

View File

@ -7,8 +7,11 @@ class NatGateways(BaseResponse):
def create_nat_gateway(self):
subnet_id = self._get_param("SubnetId")
allocation_id = self._get_param("AllocationId")
tags = self._get_multi_param("TagSpecification")
if tags:
tags = tags[0].get("Tag")
nat_gateway = self.ec2_backend.create_nat_gateway(
subnet_id=subnet_id, allocation_id=allocation_id
subnet_id=subnet_id, allocation_id=allocation_id, tags=tags
)
template = self.response_template(CREATE_NAT_GATEWAY)
return template.render(nat_gateway=nat_gateway)
@ -44,6 +47,16 @@ DESCRIBE_NAT_GATEWAYS_RESPONSE = """<DescribeNatGatewaysResponse xmlns="http://e
<vpcId>{{ nat_gateway.vpc_id }}</vpcId>
<natGatewayId>{{ nat_gateway.id }}</natGatewayId>
<state>{{ nat_gateway.state }}</state>
{% if nat_gateway.tags %}
<tagSet>
{% for tag in nat_gateway.tags %}
<item>
<key>{{ tag['Key'] }}</key>
<value>{{ tag['Value'] }}</value>
</item>
{% endfor %}
</tagSet>
{% endif %}
</item>
{% endfor %}
</natGatewaySet>

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import boto3
import sure # noqa
from moto import mock_ec2
@ -31,6 +32,40 @@ def test_create_nat_gateway():
response["NatGateway"]["State"].should.equal("available")
@mock_ec2
def test_describe_nat_gateway_tags():
conn = boto3.client("ec2", "us-east-1")
vpc = conn.create_vpc(CidrBlock="10.0.0.0/16")
vpc_id = vpc["Vpc"]["VpcId"]
subnet = conn.create_subnet(
VpcId=vpc_id, CidrBlock="10.0.1.0/27", AvailabilityZone="us-east-1a"
)
allocation_id = conn.allocate_address(Domain="vpc")["AllocationId"]
subnet_id = subnet["Subnet"]["SubnetId"]
conn.create_nat_gateway(
SubnetId=subnet_id,
AllocationId=allocation_id,
TagSpecifications=[
{
"ResourceType": "nat-gateway",
"Tags": [
{"Key": "name", "Value": "some-nat-gateway"},
{"Key": "name", "Value": "some-nat-gateway-1"},
],
}
],
)
describe_response = conn.describe_nat_gateways()
assert describe_response["NatGateways"][0]["VpcId"] == vpc_id
assert describe_response["NatGateways"][0]["Tags"] == [
{"Key": "name", "Value": "some-nat-gateway"},
{"Key": "name", "Value": "some-nat-gateway-1"},
]
@mock_ec2
def test_delete_nat_gateway():
conn = boto3.client("ec2", "us-east-1")