Skip to content

Commit

Permalink
fix: pg bouncer settings = null will always show changes on update fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wai-wong-edb committed Jul 30, 2024
1 parent 8b1322c commit b848452
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions pkg/plan_modifier/pg_bouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,35 @@ func (m CustomPgBouncerModifier) MarkdownDescription(_ context.Context) string {

// PlanModifyObject implements the plan modification logic.
func (m CustomPgBouncerModifier) PlanModifyObject(ctx context.Context, req planmodifier.ObjectRequest, resp *planmodifier.ObjectResponse) {
reqPlanSettings := req.PlanValue.Attributes()["settings"].(basetypes.SetValue)
respPlanSettings := resp.PlanValue.Attributes()["settings"].(basetypes.SetValue)
reqStateSettings := req.StateValue.Attributes()["settings"].(basetypes.SetValue)
reqConfigSettings := req.ConfigValue.Attributes()["settings"].(basetypes.SetValue)
reqPlanIsEnabled := req.PlanValue.Attributes()["is_enabled"].(basetypes.BoolValue)

if req.PlanValue.IsUnknown() {
resp.PlanValue = basetypes.NewObjectValueMust(
req.StateValue.AttributeTypes(ctx),
map[string]attr.Value{
"is_enabled": basetypes.NewBoolValue(false),
"settings": basetypes.NewSetNull(req.StateValue.AttributeTypes(ctx)["settings"].(types.SetType).ElemType),
"settings": basetypes.NewSetNull(reqStateSettings.ElementType(ctx)),
},
)
return
}

// have plan settings combine with state settings if planned settings len > 0
if !req.PlanValue.IsNull() && len(req.PlanValue.Attributes()["settings"].(basetypes.SetValue).Elements()) > 0 {
if !req.PlanValue.Attributes()["is_enabled"].(basetypes.BoolValue).ValueBool() {
if !req.PlanValue.IsNull() && len(reqPlanSettings.Elements()) > 0 {
if !reqPlanIsEnabled.ValueBool() {
if !req.PlanValue.Attributes()["settings"].(basetypes.SetValue).IsNull() {
resp.Diagnostics.AddError("if pg_bouncer.is_enabled = false then pg_bouncer.settings should be removed", "please remove pg_bouncer.settings if pg_bouncer.is_enabled = false")
return
}
} else if req.PlanValue.Attributes()["is_enabled"].(basetypes.BoolValue).ValueBool() {
newPlanWithPrefilledPlannedSettings := resp.PlanValue.Attributes()["settings"].(basetypes.SetValue).Elements()
} else if reqPlanIsEnabled.ValueBool() {
newPlanWithPrefilledPlannedSettings := respPlanSettings.Elements()
stateSettings := []attr.Value{}
if len(req.StateValue.Attributes()) != 0 {
stateSettings = req.StateValue.Attributes()["settings"].(basetypes.SetValue).Elements()
stateSettings = reqStateSettings.Elements()
}

// combine state settings with plan settings
Expand All @@ -76,14 +82,18 @@ func (m CustomPgBouncerModifier) PlanModifyObject(ctx context.Context, req planm

return
}
} else if !req.PlanValue.IsNull() {
// if is_enabled = true and settings = []
} else if !req.PlanValue.IsNull() &&
req.PlanValue.Attributes()["is_enabled"].(basetypes.BoolValue).ValueBool() &&
!req.PlanValue.Attributes()["settings"].IsUnknown() &&
len(req.PlanValue.Attributes()["settings"].(basetypes.SetValue).Elements()) == 0 {
resp.Diagnostics.AddError("if pg_bouncer.is_enabled = true then pg_bouncer.settings cannot be []", "please remove pg_bouncer.settings or set pg_bouncer.settings")
if reqPlanIsEnabled.ValueBool() && !reqPlanSettings.IsUnknown() && len(reqPlanSettings.Elements()) == 0 {
resp.Diagnostics.AddError("if pg_bouncer.is_enabled = true then pg_bouncer.settings cannot be []", "please remove pg_bouncer.settings or set pg_bouncer.settings")
return
}

return
// if is_enabled = false and settings is null and state setting is null then use state value for unknown
if !reqPlanIsEnabled.ValueBool() && reqConfigSettings.IsNull() && reqStateSettings.IsNull() {
resp.PlanValue = req.StateValue
return
}
}

if !req.PlanValue.IsUnknown() {
Expand Down

0 comments on commit b848452

Please sign in to comment.