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

d/aws_nat_gateway: Fix filtering and reading of tags #6231

Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions aws/data_source_aws_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error
)...)
}

if tags, ok := d.GetOk("tags"); ok {
req.Filter = append(req.Filter, buildEC2TagFilterList(
tagsFromMap(tags.(map[string]interface{})),
)...)
}

req.Filter = append(req.Filter, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)
Expand Down Expand Up @@ -116,6 +122,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error
d.Set("state", ngw.State)
d.Set("subnet_id", ngw.SubnetId)
d.Set("vpc_id", ngw.VpcId)
d.Set("tags", tagsToMap(ngw.Tags))
Copy link
Contributor

Choose a reason for hiding this comment

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

When using d.Set() with aggregate types (TypeList, TypeSet, TypeMap), we should perform error checking to prevent issues where the code is not properly able to set the Terraform state. e.g.

if err := d.Set("tags", tagstoMap(ngw.Tags)); err != nil {
  return fmt.Errorf("error setting tags: %s", err)
}

Since the tagsToMap() function has well established usage across other resources I won't treat this as a blocker here though. 👍


for _, address := range ngw.NatGatewayAddresses {
if *address.AllocationId != "" {
Expand Down
16 changes: 13 additions & 3 deletions aws/data_source_aws_nat_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ func TestAccDataSourceAwsNatGateway(t *testing.T) {
resource.TestCheckResourceAttrPair(
"data.aws_nat_gateway.test_by_subnet_id", "subnet_id",
"aws_nat_gateway.test", "subnet_id"),
resource.TestCheckResourceAttrPair(
"data.aws_nat_gateway.test_by_tags", "tags.Name",
"aws_nat_gateway.test", "tags.Name"),
resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "state"),
resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "allocation_id"),
resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "network_interface_id"),
resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "public_ip"),
resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "private_ip"),
resource.TestCheckNoResourceAttr("data.aws_nat_gateway.test_by_id", "attached_vpc_id"),
resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "tags.OtherTag"),
),
},
},
Expand Down Expand Up @@ -73,13 +77,13 @@ resource "aws_internet_gateway" "test" {
}
}

# NGWs are not taggable, either
resource "aws_nat_gateway" "test" {
subnet_id = "${aws_subnet.test.id}"
allocation_id = "${aws_eip.test.id}"

tags {
Name = "terraform-testacc-nat-gw-data-source"
Name = "terraform-testacc-nat-gw-data-source-%d"
OtherTag = "some-value"
}

depends_on = ["aws_internet_gateway.test"]
Expand All @@ -93,5 +97,11 @@ data "aws_nat_gateway" "test_by_subnet_id" {
subnet_id = "${aws_nat_gateway.test.subnet_id}"
}

`, rInt, rInt, rInt)
data "aws_nat_gateway" "test_by_tags" {
tags {
Name = "${aws_nat_gateway.test.tags["Name"]}"
}
}

`, rInt, rInt, rInt, rInt)
}
2 changes: 2 additions & 0 deletions website/docs/d/nat_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Nat Gateway whose data will be exported as attributes.
* `subnet_id` - (Optional) The id of subnet that the Nat Gateway resides in.
* `vpc_id` - (Optional) The id of the VPC that the Nat Gateway resides in.
* `state` - (Optional) The state of the NAT gateway (pending | failed | available | deleting | deleted ).
* `tags` - (Optional) A mapping of tags, each pair of which must exactly match
a pair on the desired Nat Gateway.
* `filter` - (Optional) Custom filter block as described below.
More complex filters can be expressed using one or more `filter` sub-blocks,
which take the following arguments:
Expand Down