diff --git a/pagerdutyplugin/resource_pagerduty_service.go b/pagerdutyplugin/resource_pagerduty_service.go index 64537de28..cea045ff8 100644 --- a/pagerdutyplugin/resource_pagerduty_service.go +++ b/pagerdutyplugin/resource_pagerduty_service.go @@ -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" @@ -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), diff --git a/util/planmodify/use_null_for_removed_with_state.go b/util/planmodify/use_null_for_removed_with_state.go new file mode 100644 index 000000000..764f563e1 --- /dev/null +++ b/util/planmodify/use_null_for_removed_with_state.go @@ -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 +}