Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags support to aws_nat_gateway resource #1625

Merged
merged 2 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion aws/resource_aws_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func resourceAwsNatGateway() *schema.Resource {
return &schema.Resource{
Create: resourceAwsNatGatewayCreate,
Read: resourceAwsNatGatewayRead,
Update: resourceAwsNatGatewayUpdate,
Delete: resourceAwsNatGatewayDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -52,6 +53,8 @@ func resourceAwsNatGateway() *schema.Resource {
Optional: true,
Computed: true,
},

"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -90,7 +93,7 @@ func resourceAwsNatGatewayCreate(d *schema.ResourceData, meta interface{}) error
}

// Update our attributes and return
return resourceAwsNatGatewayRead(d, meta)
return resourceAwsNatGatewayUpdate(d, meta)
}

func resourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -125,9 +128,28 @@ func resourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error {
d.Set("private_ip", address.PrivateIp)
d.Set("public_ip", address.PublicIp)

// Tags
d.Set("tags", tagsToMap(ng.Tags))

return nil
}

func resourceAwsNatGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

// Turn on partial mode
d.Partial(true)

if err := setTags(conn, d); err != nil {
return err
} else {
Copy link
Contributor

@Ninir Ninir Sep 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else is not needed since the if returns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

d.SetPartial("tags")
}

d.Partial(false)
return resourceAwsNatGatewayRead(d, meta)
}

func resourceAwsNatGatewayDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
deleteOpts := &ec2.DeleteNatGatewayInput{
Expand Down
166 changes: 166 additions & 0 deletions aws/resource_aws_nat_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ func TestAccAWSNatGateway_basic(t *testing.T) {
})
}

func TestAccAWSNatGateway_tags(t *testing.T) {
var natGateway ec2.NatGateway

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNatGatewayDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNatGatewayConfigTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckNatGatewayExists("aws_nat_gateway.gateway", &natGateway),
testAccCheckTags(&natGateway.Tags, "foo", "bar"),
),
},

resource.TestStep{
Config: testAccNatGatewayConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckNatGatewayExists("aws_nat_gateway.gateway", &natGateway),
testAccCheckTags(&natGateway.Tags, "foo", ""),
testAccCheckTags(&natGateway.Tags, "bar", "baz"),
),
},
},
})
}

func testAccCheckNatGatewayDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

Expand Down Expand Up @@ -161,3 +189,141 @@ resource "aws_route_table_association" "public" {
route_table_id = "${aws_route_table.public.id}"
}
`

const testAccNatGatewayConfigTags = `
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpick: can you fix the indentation through the tests here? perhaps using 2 spaces, so that it is consistent with other ones?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

tags {
Name = "testAccNatGatewayConfig"
}
}

resource "aws_subnet" "private" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = false
}

resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = true
}

resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.vpc.id}"
}

resource "aws_eip" "nat_gateway" {
vpc = true
}

// Actual SUT
resource "aws_nat_gateway" "gateway" {
allocation_id = "${aws_eip.nat_gateway.id}"
subnet_id = "${aws_subnet.public.id}"

tags {
foo = "bar"
}

depends_on = ["aws_internet_gateway.gw"]
}

resource "aws_route_table" "private" {
vpc_id = "${aws_vpc.vpc.id}"

route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.gateway.id}"
}
}

resource "aws_route_table_association" "private" {
subnet_id = "${aws_subnet.private.id}"
route_table_id = "${aws_route_table.private.id}"
}

resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.vpc.id}"

route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
}

resource "aws_route_table_association" "public" {
subnet_id = "${aws_subnet.public.id}"
route_table_id = "${aws_route_table.public.id}"
}
`

const testAccNatGatewayConfigTagsUpdate = `
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
tags {
Name = "testAccNatGatewayConfig"
}
}

resource "aws_subnet" "private" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = false
}

resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = true
}

resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.vpc.id}"
}

resource "aws_eip" "nat_gateway" {
vpc = true
}

// Actual SUT
resource "aws_nat_gateway" "gateway" {
allocation_id = "${aws_eip.nat_gateway.id}"
subnet_id = "${aws_subnet.public.id}"

tags {
bar = "baz"
}

depends_on = ["aws_internet_gateway.gw"]
}

resource "aws_route_table" "private" {
vpc_id = "${aws_vpc.vpc.id}"

route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.gateway.id}"
}
}

resource "aws_route_table_association" "private" {
subnet_id = "${aws_subnet.private.id}"
route_table_id = "${aws_route_table.private.id}"
}

resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.vpc.id}"

route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
}

resource "aws_route_table_association" "public" {
subnet_id = "${aws_subnet.public.id}"
route_table_id = "${aws_route_table.public.id}"
}
`
14 changes: 14 additions & 0 deletions website/docs/r/nat_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@ resource "aws_nat_gateway" "gw" {
}
```

Usage with tags:

```hcl
resource "aws_nat_gateway" "gw" {
allocation_id = "${aws_eip.nat.id}"
subnet_id = "${aws_subnet.public.id}"

tags {
Name = "gw NAT"
}
}
```

## Argument Reference

The following arguments are supported:

* `allocation_id` - (Required) The Allocation ID of the Elastic IP address for the gateway.
* `subnet_id` - (Required) The Subnet ID of the subnet in which to place the gateway.
* `tags` - (Optional) A mapping of tags to assign to the resource.

-> **Note:** It's recommended to denote that the NAT Gateway depends on the Internet Gateway for the VPC in which the NAT Gateway's subnet is located. For example:

Expand Down