Skip to content

Commit

Permalink
test: computed tag values
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonaj committed Apr 21, 2023
1 parent 44eb254 commit 8605329
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal/provider/intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ func (r tagsInterceptor) run(ctx context.Context, d schemaResourceData, meta any
// Remove any provider configured ignore_tags and system tags from those returned from the service API.
tags := tagsInContext.TagsOut.UnwrapOrDefault().IgnoreSystem(inContext.ServicePackageName).IgnoreConfig(tagsInContext.IgnoreConfig)

// The resource's configured tags do not include any provider configured default_tags.
if err := d.Set(names.AttrTags, tags.RemoveDefaultConfig(tagsInContext.DefaultConfig).ResolveDuplicates(ctx, tagsInContext.DefaultConfig, d).Map()); err != nil {
// The resource's configured tags can now include duplicate tags that have been configured on the provider.
if err := d.Set(names.AttrTags, tags.ResolveDuplicates(ctx, tagsInContext.DefaultConfig, d).Map()); err != nil {
return ctx, sdkdiag.AppendErrorf(diags, "setting %s: %s", names.AttrTags, err)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/provider/tags_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ func tagsReadFunc(ctx context.Context, d schemaResourceData, sp conns.ServicePac
// Remove any provider configured ignore_tags and system tags from those returned from the service API.
toAdd := tagsInContext.TagsOut.UnwrapOrDefault().IgnoreSystem(inContext.ServicePackageName).IgnoreConfig(tagsInContext.IgnoreConfig)

// The resource's configured tags do not include any provider configured default_tags.
if err := d.Set(names.AttrTags, toAdd.RemoveDefaultConfig(tagsInContext.DefaultConfig).ResolveDuplicates(ctx, tagsInContext.DefaultConfig, d).Map()); err != nil {
// The resource's configured tags can now include duplicate tags that have been configured on the provider.
if err := d.Set(names.AttrTags, toAdd.ResolveDuplicates(ctx, tagsInContext.DefaultConfig, d).Map()); err != nil {
return ctx, sdkdiag.AppendErrorf(diags, "setting %s: %s", names.AttrTags, err)
}

Expand Down
37 changes: 37 additions & 0 deletions internal/service/ec2/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ func TestAccVPC_tags(t *testing.T) {
})
}

func TestAccVPC_tags_computed(t *testing.T) {
ctx := acctest.Context(t)
var vpc ec2.Vpc
resourceName := "aws_vpc.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckVPCDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccVPCConfig_tags_computed,
Check: resource.ComposeTestCheckFunc(
acctest.CheckVPCExists(ctx, resourceName, &vpc),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttrSet(resourceName, "tags.eip"),
),
},
},
})
}

func TestAccVPC_DefaultTags_providerOnlyTestAccVPC_DefaultTags_providerOnly(t *testing.T) {
ctx := acctest.Context(t)
var vpc ec2.Vpc
Expand Down Expand Up @@ -1044,6 +1067,20 @@ resource "aws_vpc" "test" {
`, tagKey1, tagValue1, tagKey2, tagValue2)
}

const testAccVPCConfig_tags_computed = `
resource "aws_eip" "test" {
vpc = true
}
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
eip = aws_eip.test.public_ip
}
}
`

func testAccVPCConfig_ignoreChangesDynamicTagsMergedLocals(localTagKey1, localTagValue1 string) string {
return fmt.Sprintf(`
locals {
Expand Down
7 changes: 5 additions & 2 deletions internal/tags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,11 @@ type schemaResourceData interface {
}

func (tags KeyValueTags) ResolveDuplicates(ctx context.Context, defaultConfig *DefaultConfig, d schemaResourceData) KeyValueTags {
// remove default config.
t := tags.RemoveDefaultConfig(defaultConfig)

result := make(map[string]string)
for k, v := range tags {
for k, v := range t {
result[k] = v.ValueString()
}

Expand All @@ -767,7 +770,7 @@ func (tags KeyValueTags) ResolveDuplicates(ctx context.Context, defaultConfig *D
// if the config is null just return the incoming tags
// no duplicates to calculate
if c.IsNull() {
return tags
return t
}

if !c.IsNull() && c.IsKnown() {
Expand Down

0 comments on commit 8605329

Please sign in to comment.