Skip to content

Commit

Permalink
chore: add handling for value_type key changes in tag definition reso…
Browse files Browse the repository at this point in the history
…urce
  • Loading branch information
malhussan committed Nov 8, 2024
1 parent f8852e1 commit f21bf3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tagdefinitionmodifier

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
)

func ReplaceIfValueTypeKeyChanges(ctx context.Context, req planmodifier.ObjectRequest, resp *objectplanmodifier.RequiresReplaceIfFuncResponse) {
planValue := req.PlanValue
stateValue := req.StateValue

for k, v := range planValue.Attributes() {
if v.IsNull() && !stateValue.Attributes()[k].IsNull() {
resp.RequiresReplace = true
return
}
}
}
10 changes: 7 additions & 3 deletions internal/provider/tag_definition_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"

"github.com/meshcloud/terraform-provider-meshstack/client"
"github.com/meshcloud/terraform-provider-meshstack/internal/modifiers/tagdefinitionmodifier"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -177,9 +178,12 @@ func (r *tagDefinitionResource) Schema(_ context.Context, _ resource.SchemaReque
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"value_type": schema.SingleNestedAttribute{
// TODO: this should not require a replace if fields of a value type are changed (e.g. if string.default_value is changed)
PlanModifiers: []planmodifier.Object{objectplanmodifier.RequiresReplace()},
Required: true,
PlanModifiers: []planmodifier.Object{objectplanmodifier.RequiresReplaceIf(
tagdefinitionmodifier.ReplaceIfValueTypeKeyChanges,
"resource will be replaced if value_type key changes, but not when key values change",
"resource will be replaced if value_type key changes, but not when key values change",
)},
Required: true,
Attributes: map[string]schema.Attribute{
"string": schema.SingleNestedAttribute{
Optional: true,
Expand Down

0 comments on commit f21bf3b

Please sign in to comment.