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

provider: update SetTagsDiff when setting the computed tags_all attribute #18958

Merged
merged 3 commits into from
Apr 20, 2021
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
3 changes: 3 additions & 0 deletions .changelog/18958.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
provider: Prevent `Provider produced inconsistent final plan` errors when resource `tags` are not known until apply
```
83 changes: 83 additions & 0 deletions aws/resource_aws_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,47 @@ func TestAccAWSSubnet_defaultAndIgnoreTags(t *testing.T) {
})
}

// TestAccAWSSubnet_updateTagsKnownAtApply ensures computed "tags_all"
// attributes are correctly determined when the provider-level default_tags block
// is left unused and resource tags are only known at apply time, thereby
// eliminating "Inconsistent final plan" errors
// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/18366
func TestAccAWSSubnet_updateTagsKnownAtApply(t *testing.T) {
var providers []*schema.Provider
var subnet ec2.Subnet
resourceName := "aws_subnet.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID),
ProviderFactories: testAccProviderFactoriesInternal(&providers),
CheckDestroy: testAccCheckSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccSubnetConfig_tagsComputedFromVpcDataSource1("key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(resourceName, &subnet),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"),
),
},
{
Config: testAccSubnetConfig_tagsComputedFromVpcDataSource2("key1", "value1", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(resourceName, &subnet),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSSubnet_ignoreTags(t *testing.T) {
var providers []*schema.Provider
var subnet ec2.Subnet
Expand Down Expand Up @@ -969,6 +1010,48 @@ resource "aws_subnet" "test" {
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

const testAccSubnetComputedTagsBaseConfig = `
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = local.tags
}

data "aws_vpc" "test" {
id = aws_vpc.test.id
}

resource "aws_subnet" "test" {
cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, 0)
vpc_id = aws_vpc.test.id
tags = data.aws_vpc.test.tags
}
`

func testAccSubnetConfig_tagsComputedFromVpcDataSource1(tagKey1, tagValue1 string) string {
return composeConfig(
testAccSubnetComputedTagsBaseConfig,
fmt.Sprintf(`
locals {
tags = {
%q = %q
}
}
`, tagKey1, tagValue1))
}

func testAccSubnetConfig_tagsComputedFromVpcDataSource2(tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return composeConfig(
testAccSubnetComputedTagsBaseConfig,
fmt.Sprintf(`
locals {
tags = {
%q = %q
%q = %q
}
}
`, tagKey1, tagValue1, tagKey2, tagValue2))
}

const testAccSubnetConfigPreIpv6 = `
resource "aws_vpc" "test" {
cidr_block = "10.10.0.0/16"
Expand Down
14 changes: 12 additions & 2 deletions aws/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,18 @@ func SetTagsDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{})

allTags := defaultTagsConfig.MergeTags(resourceTags).IgnoreConfig(ignoreTagsConfig)

if err := diff.SetNew("tags_all", allTags.Map()); err != nil {
return fmt.Errorf("error setting new tags_all diff: %w", err)
// To ensure "tags_all" is correctly computed with the value held in allTags,
// we explicitly set the attribute diff when values are known,
// otherwise mark the attribute only as "Computed"
// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/18366
if len(allTags) > 0 {
if err := diff.SetNew("tags_all", allTags.Map()); err != nil {
return fmt.Errorf("error setting new tags_all diff: %w", err)
}
} else {
if err := diff.SetNewComputed("tags_all"); err != nil {
return fmt.Errorf("error setting tags_all to computed: %w", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the messaging here in-case we'd need to differentiate between the 2 errors

}
}

return nil
Expand Down