Skip to content

Commit

Permalink
refactor HTTP status code checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma committed Jul 25, 2024
1 parent 25b5ce7 commit c192685
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 23 deletions.
3 changes: 2 additions & 1 deletion equinix/resource_metal_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"path"
"reflect"
"regexp"
Expand Down Expand Up @@ -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))
}

Expand Down
21 changes: 8 additions & 13 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
}
6 changes: 4 additions & 2 deletions internal/resources/fabric/connection/sweeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/metal/gateway/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gateway
import (
"context"
"fmt"
"net/http"
"time"

"github.com/equinix/equinix-sdk-go/services/metalv1"
Expand Down Expand Up @@ -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(),
)
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/metal/project/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package project
import (
"context"
"fmt"
"net/http"
"reflect"

"github.com/equinix/equinix-sdk-go/services/metalv1"
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/metal/project_ssh_key/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/metal/ssh_key/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/metal/virtual_circuit/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"log"
"net/http"
"regexp"
"strconv"
"time"
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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("")
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/metal/virtual_circuit/sweeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"net/http"

"github.com/equinix/equinix-sdk-go/services/metalv1"

Expand Down Expand Up @@ -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))
}
}
Expand Down

0 comments on commit c192685

Please sign in to comment.