Skip to content

Commit

Permalink
Add planmodify util package
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed May 10, 2024
1 parent 98d0807 commit 5daccd7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pagerdutyplugin/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/PagerDuty/go-pagerduty"
"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/PagerDuty/terraform-provider-pagerduty/util/enumtypes"
"github.com/PagerDuty/terraform-provider-pagerduty/util/planmodify"
"github.com/PagerDuty/terraform-provider-pagerduty/util/tztypes"
"github.com/PagerDuty/terraform-provider-pagerduty/util/validate"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand Down Expand Up @@ -237,6 +238,7 @@ func (r *resourceService) Schema(ctx context.Context, req resource.SchemaRequest
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
listplanmodifier.UseStateForUnknown(),
planmodify.UseNullForRemovedWithState(),
},
Validators: []validator.List{
listvalidator.SizeBetween(1, 1),
Expand Down
37 changes: 37 additions & 0 deletions util/planmodify/use_null_for_removed_with_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package planmodify

import (
"context"

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

// UseNullForRemovedWithState sets plan to null if the list has an state, but
// the configuration is now null
func UseNullForRemovedWithState() planmodifier.List {
return useNullForRemovedWithStateModifier{}
}

type useNullForRemovedWithStateModifier struct{}

func (m useNullForRemovedWithStateModifier) Description(_ context.Context) string {
return "Removes the value if the list has an state, but the configuration changes to null"
}

func (m useNullForRemovedWithStateModifier) MarkdownDescription(_ context.Context) string {
return "Removes the value if the list has an state, but the configuration changes to null"
}

func (m useNullForRemovedWithStateModifier) PlanModifyList(_ context.Context, req planmodifier.ListRequest, resp *planmodifier.ListResponse) {
// Do nothing if there is no state value.
if req.StateValue.IsNull() {
return
}

// Do nothing if there is a known or an unknown configuration value.
if !req.ConfigValue.IsNull() {
return
}

resp.PlanValue = req.ConfigValue
}

0 comments on commit 5daccd7

Please sign in to comment.