Skip to content

Commit

Permalink
fix: added unsetting of ip_lists
Browse files Browse the repository at this point in the history
  • Loading branch information
AS-auxmoney committed May 7, 2024
1 parent 3fa3d93 commit 87bb0f2
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions pkg/resources/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,18 @@ func UpdateContextNetworkPolicy(ctx context.Context, d *schema.ResourceData, met
}
}

// TODO: empty ip list (that is unsetting) does not work, as WithUnset is missing.
// Removing the validation in network_policies_validations_gen.go does not solve the problem, as the SDK cannot
// handle empty lists
if d.HasChange("allowed_ip_list") {
baseReq := sdk.NewAlterNetworkPolicyRequest(sdk.NewAccountObjectIdentifier(name))
ipRequests := parseIPList(d.Get("allowed_ip_list"))

setReq := sdk.NewNetworkPolicySetRequest().WithAllowedIpList(sdk.NewAllowedIPListRequest().WithAllowedIPList(ipRequests))
err := client.NetworkPolicies.Alter(ctx, baseReq.WithSet(setReq))
var err error
if len(ipRequests) == 0 {
unsetReq := sdk.NewNetworkPolicyUnsetRequest().WithAllowedIpList(sdk.Bool(true))
err = client.NetworkPolicies.Alter(ctx, baseReq.WithUnset(unsetReq))
} else {
setReq := sdk.NewNetworkPolicySetRequest().WithAllowedIpList(sdk.NewAllowedIPListRequest().WithAllowedIPList(ipRequests))
err = client.NetworkPolicies.Alter(ctx, baseReq.WithSet(setReq))
}

if err != nil {
return diag.Diagnostics{
Expand All @@ -281,15 +284,18 @@ func UpdateContextNetworkPolicy(ctx context.Context, d *schema.ResourceData, met
}
}

// TODO: empty ip list (that is unsetting) does not work, as WithUnset is missing.
// Removing the validation in network_policies_validations_gen.go does not solve the problem, as the SDK cannot
// handle empty lists
if d.HasChange("blocked_ip_list") {
baseReq := sdk.NewAlterNetworkPolicyRequest(sdk.NewAccountObjectIdentifier(name))
ipRequests := parseIPList(d.Get("blocked_ip_list"))

setReq := sdk.NewNetworkPolicySetRequest().WithBlockedIpList(sdk.NewBlockedIPListRequest().WithBlockedIPList(ipRequests))
err := client.NetworkPolicies.Alter(ctx, baseReq.WithSet(setReq))
var err error
if len(ipRequests) == 0 {
unsetReq := sdk.NewNetworkPolicyUnsetRequest().WithBlockedIpList(sdk.Bool(true))
err = client.NetworkPolicies.Alter(ctx, baseReq.WithUnset(unsetReq))
} else {
setReq := sdk.NewNetworkPolicySetRequest().WithBlockedIpList(sdk.NewBlockedIPListRequest().WithBlockedIPList(ipRequests))
err = client.NetworkPolicies.Alter(ctx, baseReq.WithSet(setReq))
}

if err != nil {
return diag.Diagnostics{
Expand Down Expand Up @@ -321,7 +327,6 @@ func DeleteContextNetworkPolicy(ctx context.Context, d *schema.ResourceData, met

err := client.NetworkPolicies.Drop(ctx, sdk.NewDropNetworkPolicyRequest(sdk.NewAccountObjectIdentifier(name)))
if err != nil {

return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Expand Down

0 comments on commit 87bb0f2

Please sign in to comment.