Skip to content

Commit

Permalink
Merge pull request #32297 from hashicorp/b-tags-ForceNew
Browse files Browse the repository at this point in the history
provider: Prevent resource recreation if `tags` or `tags_all` change
  • Loading branch information
ewbankkit authored Jun 30, 2023
2 parents 1d064eb + 1fadbb1 commit 326b728
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .changelog/32297.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
provider: Prevent resource recreation if `tags` or `tags_all` are updated
```
2 changes: 1 addition & 1 deletion internal/service/ec2/ec2_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ResourceSpotInstanceRequest() *schema.Resource {
if v.Computed && !v.Optional {
continue
}
if k == names.AttrTags || k == "volume_tags" {
if k == names.AttrTags {
continue
}
v.ForceNew = true
Expand Down
10 changes: 4 additions & 6 deletions internal/service/ec2/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ func tagsFromTagDescriptions(tds []*ec2.TagDescription) []*ec2.Tag {
}

func tagsSchemaConflictsWith(conflictsWith []string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: conflictsWith,
}
v := tftags.TagsSchema()
v.ConflictsWith = conflictsWith

return v
}
10 changes: 6 additions & 4 deletions internal/service/ec2/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAccVPC_disappears(t *testing.T) {

func TestAccVPC_tags(t *testing.T) {
ctx := acctest.Context(t)
var vpc ec2.Vpc
var vpc1, vpc2, vpc3 ec2.Vpc
resourceName := "aws_vpc.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -103,7 +103,7 @@ func TestAccVPC_tags(t *testing.T) {
{
Config: testAccVPCConfig_tags1("key1", "value1"),
Check: resource.ComposeTestCheckFunc(
acctest.CheckVPCExists(ctx, resourceName, &vpc),
acctest.CheckVPCExists(ctx, resourceName, &vpc1),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
Expand All @@ -116,7 +116,8 @@ func TestAccVPC_tags(t *testing.T) {
{
Config: testAccVPCConfig_tags2("key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
acctest.CheckVPCExists(ctx, resourceName, &vpc),
acctest.CheckVPCExists(ctx, resourceName, &vpc2),
testAccCheckVPCIDsEqual(&vpc2, &vpc1),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
Expand All @@ -125,7 +126,8 @@ func TestAccVPC_tags(t *testing.T) {
{
Config: testAccVPCConfig_tags1("key2", "value2"),
Check: resource.ComposeTestCheckFunc(
acctest.CheckVPCExists(ctx, resourceName, &vpc),
acctest.CheckVPCExists(ctx, resourceName, &vpc3),
testAccCheckVPCIDsEqual(&vpc3, &vpc2),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
Expand Down
28 changes: 11 additions & 17 deletions internal/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
tagsSchema *schema.Schema = &schema.Schema{
// TagsSchema returns the schema to use for tags.
func TagsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
tagsSchemaComputed *schema.Schema = &schema.Schema{
}

func TagsSchemaComputed() *schema.Schema {
return &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
tagsSchemaForceNew *schema.Schema = &schema.Schema{
}

func TagsSchemaForceNew() *schema.Schema {
return &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
)

// TagsSchema returns the schema to use for tags.
func TagsSchema() *schema.Schema {
return tagsSchema
}

func TagsSchemaComputed() *schema.Schema {
return tagsSchemaComputed
}

func TagsSchemaForceNew() *schema.Schema {
return tagsSchemaForceNew
}

0 comments on commit 326b728

Please sign in to comment.