Skip to content

Commit 42add97

Browse files
committed
Added Retry for Security Policy and Network Lists
1 parent 5255c3b commit 42add97

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

internal/service/fw/api_network_list_resource.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,24 @@ func (r *NetworkListResource) Delete(ctx context.Context, req resource.DeleteReq
169169
return
170170
}
171171

172-
httpRes, err := r.client.FWAPI.
173-
NetworkListsAPI.
174-
DeleteSingleNetworkLists(ctx, data.Id.ValueInt32()).
175-
Execute()
176-
if err != nil {
177-
if httpRes != nil && httpRes.StatusCode == http.StatusNotFound {
178-
return
172+
err := retry.RetryContext(ctx, NetworkListOperationTimeout, func() *retry.RetryError {
173+
httpRes, err := r.client.FWAPI.
174+
NetworkListsAPI.
175+
DeleteSingleNetworkLists(ctx, data.Id.ValueInt32()).
176+
Execute()
177+
if err != nil {
178+
if httpRes != nil && httpRes.StatusCode == http.StatusNotFound {
179+
return nil
180+
}
181+
if strings.Contains(err.Error(), "Internal Server Error") || strings.Contains(err.Error(), "Operation timed out") {
182+
return retry.RetryableError(err)
183+
}
184+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete NetworkLists, got error: %s", err))
185+
return retry.NonRetryableError(err)
179186
}
180-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete NetworkLists, got error: %s", err))
187+
return nil
188+
})
189+
if err != nil {
181190
return
182191
}
183192
}

internal/service/fw/api_security_policy_resource.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/path"
1111
"github.com/hashicorp/terraform-plugin-framework/resource"
1212
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
13+
"github.com/hashicorp/terraform-plugin-log/tflog"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1415

1516
bloxoneclient "github.com/infobloxopen/bloxone-go-client/client"
@@ -74,19 +75,29 @@ func (r *SecurityPolicyResource) Create(ctx context.Context, req resource.Create
7475
return
7576
}
7677

77-
apiRes, _, err := r.client.FWAPI.
78-
SecurityPoliciesAPI.
79-
CreateSecurityPolicy(ctx).
80-
Body(*data.Expand(ctx, &resp.Diagnostics)).
81-
Execute()
78+
err := retry.RetryContext(ctx, NetworkListOperationTimeout, func() *retry.RetryError {
79+
apiRes, _, err := r.client.FWAPI.
80+
SecurityPoliciesAPI.
81+
CreateSecurityPolicy(ctx).
82+
Body(*data.Expand(ctx, &resp.Diagnostics)).
83+
Execute()
84+
if err != nil {
85+
if strings.Contains(err.Error(), "Internal Server Error") {
86+
tflog.Debug(ctx, "Waiting for related objects to be present, will retry", map[string]interface{}{"error": err.Error()})
87+
return retry.RetryableError(err)
88+
}
89+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create SecurityPolicies, got error: %s", err))
90+
return retry.NonRetryableError(err)
91+
}
92+
res := apiRes.GetResults()
93+
data.Flatten(ctx, &res, &resp.Diagnostics)
94+
95+
return nil
96+
})
8297
if err != nil {
83-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create SecurityPolicies, got error: %s", err))
8498
return
8599
}
86100

87-
res := apiRes.GetResults()
88-
data.Flatten(ctx, &res, &resp.Diagnostics)
89-
90101
// Save data into Terraform state
91102
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
92103
}

0 commit comments

Comments
 (0)