Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: v1 regression fixes #568

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
name: Run GoReleaser
uses: goreleaser/[email protected]
with:
version: v1.26.0
version: v1.26.2
args: release --rm-dist
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
Expand Down
91 changes: 55 additions & 36 deletions pkg/plan_modifier/pg_bouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,51 +39,70 @@ func (m CustomPgBouncerModifier) PlanModifyObject(ctx context.Context, req planm
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.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()
stateSettings := []attr.Value{}
if len(req.StateValue.Attributes()) != 0 {
stateSettings = req.StateValue.Attributes()["settings"].(basetypes.SetValue).Elements()
}
if !req.PlanValue.IsNull() {
reqPlanSettings := req.PlanValue.Attributes()["settings"].(basetypes.SetValue)
// have plan settings combine with state settings if planned settings len > 0
if len(reqPlanSettings.Elements()) > 0 {
reqPlanIsEnabled := req.PlanValue.Attributes()["is_enabled"].(basetypes.BoolValue)
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")
wai-wong-edb marked this conversation as resolved.
Show resolved Hide resolved
return
}
} else if reqPlanIsEnabled.ValueBool() {
respPlanSettings := resp.PlanValue.Attributes()["settings"].(basetypes.SetValue)
newPlanWithPrefilledPlannedSettings := respPlanSettings.Elements()
stateSettings := []attr.Value{}
if len(req.StateValue.Attributes()) != 0 {
stateSettings = req.StateValue.Attributes()["settings"].(basetypes.SetValue).Elements()
}

// combine state settings with plan settings
for _, sSetting := range stateSettings {
stateSettingName := sSetting.(basetypes.ObjectValue).Attributes()["name"]
for _, pSetting := range newPlanWithPrefilledPlannedSettings {
planSettingName := pSetting.(basetypes.ObjectValue).Attributes()["name"]
if stateSettingName.Equal(planSettingName) {
continue
}
// combine state settings with plan settings
for _, sSetting := range stateSettings {
stateSettingName := sSetting.(basetypes.ObjectValue).Attributes()["name"]
for _, pSetting := range newPlanWithPrefilledPlannedSettings {
planSettingName := pSetting.(basetypes.ObjectValue).Attributes()["name"]
if stateSettingName.Equal(planSettingName) {
continue
}

newPlanWithPrefilledPlannedSettings = append(newPlanWithPrefilledPlannedSettings, sSetting)
newPlanWithPrefilledPlannedSettings = append(newPlanWithPrefilledPlannedSettings, sSetting)
}
}

resp.PlanValue = basetypes.NewObjectValueMust(
req.StateValue.AttributeTypes(ctx),
map[string]attr.Value{
"is_enabled": basetypes.NewBoolValue(true),
"settings": basetypes.NewSetValueMust(req.StateValue.AttributeTypes(ctx)["settings"].(types.SetType).ElemType, newPlanWithPrefilledPlannedSettings),
},
)

return
}
}

// if is_enabled = true and settings = []
reqPlanIsEnabled := req.PlanValue.Attributes()["is_enabled"].(basetypes.BoolValue)
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
}

resp.PlanValue = basetypes.NewObjectValueMust(
req.StateValue.AttributeTypes(ctx),
map[string]attr.Value{
"is_enabled": basetypes.NewBoolValue(true),
"settings": basetypes.NewSetValueMust(req.StateValue.AttributeTypes(ctx)["settings"].(types.SetType).ElemType, newPlanWithPrefilledPlannedSettings),
},
)
if !reqPlanIsEnabled.ValueBool() && !reqPlanSettings.IsUnknown() && len(reqPlanSettings.Elements()) == 0 {
resp.Diagnostics.AddError("if pg_bouncer.is_enabled = false then pg_bouncer.settings cannot be []", "please remove pg_bouncer.settings or set pg_bouncer.settings = null")

return
}
// 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")

return
// if is_enabled = false and settings is null and state setting is null then use state value for unknown
if !reqPlanIsEnabled.ValueBool() &&
req.ConfigValue.Attributes()["settings"].(basetypes.SetValue).IsNull() &&
len(req.StateValue.Attributes()) != 0 &&
req.StateValue.Attributes()["settings"].(basetypes.SetValue).IsNull() {
resp.PlanValue = req.StateValue
return
}
}

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