Skip to content

Commit

Permalink
allow duplicate tags
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonaj committed Apr 20, 2023
1 parent 191b600 commit 4407d76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
9 changes: 7 additions & 2 deletions internal/service/ec2/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ func TestAccVPC_DefaultTagsProviderAndResource_overlappingTag(t *testing.T) {

func TestAccVPC_DefaultTagsProviderAndResource_duplicateTag(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) },
Expand All @@ -415,8 +417,11 @@ func TestAccVPC_DefaultTagsProviderAndResource_duplicateTag(t *testing.T) {
acctest.ConfigDefaultTags_Tags1("overlapkey", "overlapvalue"),
testAccVPCConfig_tags1("overlapkey", "overlapvalue"),
),
PlanOnly: true,
ExpectError: regexp.MustCompile(`"tags" are identical to those in the "default_tags" configuration block`),
Check: resource.ComposeTestCheckFunc(
acctest.CheckVPCExists(ctx, resourceName, &vpc),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"),
),
},
},
})
Expand Down
28 changes: 25 additions & 3 deletions internal/tags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,15 +754,28 @@ func (tags KeyValueTags) RemoveDuplicates(ctx context.Context, defaultConfig *De
configTags := make(map[string]string)
if config := d.GetRawPlan(); !config.IsNull() && config.IsKnown() {
c := config.GetAttr("tags")
if !c.IsNull() {
if !c.IsNull() && c.IsKnown() {
for k, v := range c.AsValueMap() {
configTags[k] = v.AsString()
}
}
}

var configIsNull bool
if config := d.GetRawConfig(); !config.IsNull() && config.IsKnown() {
c := config.GetAttr("tags")
configIsNull = c.IsNull()
if !c.IsNull() && c.IsKnown() {
for k, v := range c.AsValueMap() {
if _, ok := configTags[k]; !ok {
configTags[k] = v.AsString()
}
}
}
}

if state := d.GetRawState(); !state.IsNull() && state.IsKnown() {
c := state.GetAttr("tags")
if !c.IsNull() {
for k, v := range c.AsValueMap() {
if _, ok := configTags[k]; !ok {
Expand All @@ -774,11 +787,20 @@ func (tags KeyValueTags) RemoveDuplicates(ctx context.Context, defaultConfig *De

for k, v := range configTags {
if _, ok := result[k]; !ok {
result[k] = v
if defaultConfig != nil {
if val, ok := defaultConfig.Tags[k]; ok && val.ValueString() == v {
result[k] = v
}
}
}
}

return New(ctx, result)
out := tags
if !configIsNull {
out = New(ctx, result)
}

return out
}

// ToSnakeCase converts a string to snake case.
Expand Down
4 changes: 0 additions & 4 deletions internal/verify/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ func SetTagsDiff(ctx context.Context, diff *schema.ResourceDiff, meta interface{

resourceTags := tftags.New(ctx, diff.Get("tags").(map[string]interface{}))

//if defaultTagsConfig.TagsEqual(resourceTags) {
// return fmt.Errorf(`"tags" are identical to those in the "default_tags" configuration block of the provider: please de-duplicate and try again`)
//}

allTags := defaultTagsConfig.MergeTags(resourceTags).IgnoreConfig(ignoreTagsConfig)
// To ensure "tags_all" is correctly computed, we explicitly set the attribute diff
// when the merger of resource-level tags onto provider-level tags results in n > 0 tags,
Expand Down

0 comments on commit 4407d76

Please sign in to comment.