Skip to content

Commit

Permalink
fix: private networking true allowed ip ranges bug fix (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
wai-wong-edb authored Jan 5, 2024
1 parent 1d0bcb9 commit 16cd83a
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions pkg/plan_modifier/allowed_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func CustomAllowedIps() planmodifier.Set {
Expand All @@ -27,16 +28,42 @@ func (m customAllowedIpsModifier) MarkdownDescription(_ context.Context) string

// PlanModifySet implements the plan modification logic.
func (m customAllowedIpsModifier) PlanModifySet(ctx context.Context, req planmodifier.SetRequest, resp *planmodifier.SetResponse) {
if len(resp.PlanValue.Elements()) == 0 {
// if plan value is [] the api will return 0.0.0.0/0
defaultAttrs := map[string]attr.Value{"cidr_block": basetypes.NewStringValue("0.0.0.0/0"), "description": basetypes.NewStringValue("")}
var planObject map[string]tftypes.Value

err := req.Plan.Raw.As(&planObject)
if err != nil {
resp.Diagnostics.AddError("Mapping plan object in allowed ip ranges plan modifier error", err.Error())
return
}

var privateNetworking bool
err = planObject["private_networking"].As(&privateNetworking)
if err != nil {
resp.Diagnostics.AddError("Mapping private networking object in allowed ip ranges plan modifier error", err.Error())
return
}

allowedIpRangesSetValueFunc := func(description string) basetypes.SetValue {
defaultAttrs := map[string]attr.Value{"cidr_block": basetypes.NewStringValue("0.0.0.0/0"), "description": basetypes.NewStringValue(description)}
defaultAttrTypes := map[string]attr.Type{"cidr_block": defaultAttrs["cidr_block"].Type(ctx), "description": defaultAttrs["description"].Type(ctx)}

defaultObjectValue := basetypes.NewObjectValueMust(defaultAttrTypes, defaultAttrs)
setOfObjects := []attr.Value{}
setOfObjects = append(setOfObjects, defaultObjectValue)
setValue := basetypes.NewSetValueMust(defaultObjectValue.Type(ctx), setOfObjects)
resp.PlanValue = setValue

return setValue
}

// if private networking set allowed IP ranges to cidr_block:"0.0.0.0/0" description:"To allow all access"
if privateNetworking {
resp.PlanValue = allowedIpRangesSetValueFunc("To allow all access")
return
}

// if allowed IP ranges plan value is [] set allowed IP ranges cidr_block:"0.0.0.0/0" description:""
if len(resp.PlanValue.Elements()) == 0 {
resp.PlanValue = allowedIpRangesSetValueFunc("")
return
}

Expand Down

0 comments on commit 16cd83a

Please sign in to comment.