diff --git a/equinix/resource_metal_device.go b/equinix/resource_metal_device.go index a83b30fdd..73dd5558d 100644 --- a/equinix/resource_metal_device.go +++ b/equinix/resource_metal_device.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "log" + "net/http" "path" "reflect" "regexp" @@ -792,7 +793,7 @@ func resourceMetalDeviceDelete(ctx context.Context, d *schema.ResourceData, meta start := time.Now() resp, err := client.DevicesApi.DeleteDevice(ctx, d.Id()).ForceDelete(fdv).Execute() - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(resp, err) != nil { return diag.FromErr(equinix_errors.FriendlyError(err)) } diff --git a/internal/errors/errors.go b/internal/errors/errors.go index 9a2f43cb5..59853c287 100644 --- a/internal/errors/errors.go +++ b/internal/errors/errors.go @@ -2,10 +2,12 @@ package errors import ( "fmt" - "github.com/equinix/equinix-sdk-go/services/fabricv4" "net/http" + "slices" "strings" + "github.com/equinix/equinix-sdk-go/services/fabricv4" + "github.com/equinix/rest-go" "github.com/packethost/packngo" ) @@ -191,21 +193,14 @@ func HasErrorCode(errors []fabricv4.Error, code string) bool { return false } -// ignoreHttpResponseErrors ignores http response errors when matched by one of the -// provided checks -func IgnoreHttpResponseErrors(ignore ...func(resp *http.Response, err error) bool) func(resp *http.Response, err error) error { +// IgnoreHttpResponseErrors ignores http response errors when the status +// code of the response matches one of the status codes in the ignore list +func IgnoreHttpResponseErrors(ignore ...int) func(resp *http.Response, err error) error { return func(resp *http.Response, err error) error { - mute := false - for _, ignored := range ignore { - if ignored(resp, err) { - mute = true - break - } - } - - if mute { + if resp != nil && slices.Contains(ignore, resp.StatusCode) { return nil } + return err } } diff --git a/internal/resources/fabric/connection/sweeper.go b/internal/resources/fabric/connection/sweeper.go index 90ea8aec0..a67049e89 100644 --- a/internal/resources/fabric/connection/sweeper.go +++ b/internal/resources/fabric/connection/sweeper.go @@ -4,11 +4,13 @@ import ( "context" "errors" "fmt" + "log" + "net/http" + "github.com/equinix/equinix-sdk-go/services/fabricv4" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" "github.com/equinix/terraform-provider-equinix/internal/sweep" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "log" ) func AddTestSweeper() { @@ -64,7 +66,7 @@ func testSweepConnections(region string) error { if sweep.IsSweepableFabricTestResource(connection.GetName()) { log.Printf("[DEBUG] Deleting Connection: %s", connection.GetName()) _, resp, err := fabric.ConnectionsApi.DeleteConnectionByUuid(ctx, connection.GetUuid()).Execute() - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(resp, err) != nil { errs = append(errs, fmt.Errorf("error deleting fabric connection: %s", err)) } } diff --git a/internal/resources/metal/gateway/resource.go b/internal/resources/metal/gateway/resource.go index 345febef7..fc830b202 100644 --- a/internal/resources/metal/gateway/resource.go +++ b/internal/resources/metal/gateway/resource.go @@ -3,6 +3,7 @@ package gateway import ( "context" "fmt" + "net/http" "time" "github.com/equinix/equinix-sdk-go/services/metalv1" @@ -191,7 +192,7 @@ func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp _, err = deleteWaiter.WaitForStateContext(ctx) } - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(nil, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(nil, err) != nil { resp.Diagnostics.AddError( fmt.Sprintf("Failed to delete Metal Gateway %s", id), err.Error(), ) diff --git a/internal/resources/metal/project/resource.go b/internal/resources/metal/project/resource.go index fd39d1e60..4d8e0fd87 100644 --- a/internal/resources/metal/project/resource.go +++ b/internal/resources/metal/project/resource.go @@ -3,6 +3,7 @@ package project import ( "context" "fmt" + "net/http" "reflect" "github.com/equinix/equinix-sdk-go/services/metalv1" @@ -292,7 +293,7 @@ func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp // API call to delete the project deleteResp, err := client.ProjectsApi.DeleteProject(ctx, id).Execute() - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(deleteResp, err) != nil { err = equinix_errors.FriendlyErrorForMetalGo(err, deleteResp) resp.Diagnostics.AddError( fmt.Sprintf("Failed to delete Project %s", id), diff --git a/internal/resources/metal/project_ssh_key/resource.go b/internal/resources/metal/project_ssh_key/resource.go index 26f863388..983702c30 100644 --- a/internal/resources/metal/project_ssh_key/resource.go +++ b/internal/resources/metal/project_ssh_key/resource.go @@ -3,6 +3,7 @@ package project_ssh_key import ( "context" "fmt" + "net/http" "github.com/equinix/equinix-sdk-go/services/metalv1" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" @@ -182,7 +183,7 @@ func (r *Resource) Delete( // Use API client to delete the resource deleteResp, err := client.SSHKeysApi.DeleteSSHKey(ctx, id).Execute() - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(deleteResp, err) != nil { err = equinix_errors.FriendlyError(err) resp.Diagnostics.AddError( fmt.Sprintf("Failed to delete Project SSHKey %s", id), diff --git a/internal/resources/metal/ssh_key/resource.go b/internal/resources/metal/ssh_key/resource.go index 4b76e4ec9..59957d132 100644 --- a/internal/resources/metal/ssh_key/resource.go +++ b/internal/resources/metal/ssh_key/resource.go @@ -3,6 +3,7 @@ package ssh_key import ( "context" "fmt" + "net/http" "github.com/equinix/equinix-sdk-go/services/metalv1" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" @@ -180,7 +181,7 @@ func (r *Resource) Delete( // Use API client to delete the resource deleteResp, err := client.SSHKeysApi.DeleteSSHKey(ctx, id).Execute() - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(deleteResp, err) != nil { err = equinix_errors.FriendlyError(err) resp.Diagnostics.AddError( fmt.Sprintf("Failed to delete SSHKey %s", id), diff --git a/internal/resources/metal/virtual_circuit/resource.go b/internal/resources/metal/virtual_circuit/resource.go index 0ab16e41e..f0b99272e 100644 --- a/internal/resources/metal/virtual_circuit/resource.go +++ b/internal/resources/metal/virtual_circuit/resource.go @@ -4,6 +4,7 @@ import ( "context" "errors" "log" + "net/http" "regexp" "strconv" "time" @@ -528,7 +529,7 @@ func resourceMetalVirtualCircuitDelete(ctx context.Context, d *schema.ResourceDa // in order to use existing checks for equinix_errors.IgnoreHttpResponseErrors err = equinix_errors.FriendlyErrorForMetalGo(err, resp) } - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(resp, err) != nil { return diag.FromErr(err) } } @@ -543,7 +544,7 @@ func resourceMetalVirtualCircuitDelete(ctx context.Context, d *schema.ResourceDa ) _, err = deleteWaiter.WaitForStateContext(ctx) - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(nil, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(nil, err) != nil { return diag.Errorf("Error deleting virtual circuit %s: %s", d.Id(), err) } d.SetId("") diff --git a/internal/resources/metal/virtual_circuit/sweeper.go b/internal/resources/metal/virtual_circuit/sweeper.go index 71fae8032..c0b6b6bd7 100644 --- a/internal/resources/metal/virtual_circuit/sweeper.go +++ b/internal/resources/metal/virtual_circuit/sweeper.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "net/http" "github.com/equinix/equinix-sdk-go/services/metalv1" @@ -54,7 +55,7 @@ func testSweepVirtualCircuits(region string) error { if sweep.IsSweepableTestResource(vcName) { log.Printf("[INFO][SWEEPER_LOG] Deleting VirtualCircuit: %s", vcName) _, resp, err := metal.InterconnectionsApi.DeleteVirtualCircuit(context.Background(), vcId).Execute() - if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { + if equinix_errors.IgnoreHttpResponseErrors(http.StatusForbidden, http.StatusNotFound)(resp, err) != nil { errs = append(errs, fmt.Errorf("error deleting VirtualCircuit: %s", err)) } }