Skip to content

Commit

Permalink
refactor: move error functions out of package.go (#502)
Browse files Browse the repository at this point in the history
This moves some private error-related functions out of `provider.go`
into a separate package so that resources and data sources aren't
unnecessarily tied to the provider code file.
  • Loading branch information
ctreatma authored Jan 8, 2024
1 parent 4a5a871 commit f1c4836
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 80 deletions.
30 changes: 0 additions & 30 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package equinix
import (
"context"
"fmt"
"net/http"
"reflect"
"strings"
"time"

"github.com/equinix/terraform-provider-equinix/internal/resources/metal/metal_project_ssh_key"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/metal_ssh_key"

v4 "github.com/equinix-labs/fabric-go/fabric/v4"
"github.com/equinix/ecx-go/v2"
"github.com/equinix/rest-go"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -254,24 +251,6 @@ func expandInterfaceMapToStringMap(mapIn map[string]interface{}) map[string]stri
return mapOut
}

func hasApplicationErrorCode(errors []rest.ApplicationError, code string) bool {
for _, err := range errors {
if err.Code == code {
return true
}
}
return false
}

func hasModelErrorCode(errors []v4.ModelError, code string) bool {
for _, err := range errors {
if err.ErrorCode == code {
return true
}
}
return false
}

func stringsFound(source []string, target []string) bool {
for i := range source {
if !isStringInSlice(source[i], target) {
Expand Down Expand Up @@ -398,15 +377,6 @@ func slicesMatchCaseInsensitive(s1, s2 []string) bool {
return true
}

func isRestNotFoundError(err error) bool {
if restErr, ok := err.(rest.Error); ok {
if restErr.HTTPCode == http.StatusNotFound {
return true
}
}
return false
}

func schemaSetToMap(set *schema.Set) map[int]interface{} {
transformed := make(map[int]interface{})
if set != nil {
Expand Down
41 changes: 0 additions & 41 deletions equinix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package equinix

import (
"fmt"
"net/http"
"os"
"reflect"
"regexp"
Expand All @@ -13,8 +12,6 @@ import (
"github.com/equinix/terraform-provider-equinix/internal/hashcode"

"github.com/equinix/ecx-go/v2"
"github.com/equinix/rest-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -149,23 +146,6 @@ func TestProvider(t *testing.T) {
}
}

func TestProvider_hasApplicationErrorCode(t *testing.T) {
// given
code := "ERR-505"
errors := []rest.ApplicationError{
{
Code: "ERR-505",
},
{
Code: acctest.RandString(10),
},
}
// when
result := hasApplicationErrorCode(errors, code)
// then
assert.True(t, result, "Error list contains error with given code")
}

func TestProvider_stringsFound(t *testing.T) {
// given
needles := []string{"key1", "key5"}
Expand Down Expand Up @@ -351,27 +331,6 @@ func TestProvider_slicesMatch(t *testing.T) {
}
}

func TestProvider_isRestNotFoundError(t *testing.T) {
// given
input := []error{
rest.Error{HTTPCode: http.StatusNotFound, Message: "Not Found"},
rest.Error{HTTPCode: http.StatusInternalServerError, Message: "Internal Server Error"},
fmt.Errorf("some bogus error"),
}
expected := []bool{
true,
false,
false,
}
// when
result := make([]bool, len(input))
for i := range input {
result[i] = isRestNotFoundError(input[i])
}
// then
assert.Equal(t, expected, result, "Result matches expected output")
}

func TestProvider_schemaSetToMap(t *testing.T) {
// given
type item struct {
Expand Down
5 changes: 3 additions & 2 deletions equinix/resource_ecx_l2_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ecx-go/v2"
Expand Down Expand Up @@ -778,7 +779,7 @@ func resourceECXL2ConnectionDelete(ctx context.Context, d *schema.ResourceData,
restErr, ok := err.(rest.Error)
if ok {
// IC-LAYER2-4021 = Connection already deleted
if hasApplicationErrorCode(restErr.ApplicationErrors, "IC-LAYER2-4021") {
if equinix_errors.HasApplicationErrorCode(restErr.ApplicationErrors, "IC-LAYER2-4021") {
return diags
}
}
Expand All @@ -792,7 +793,7 @@ func resourceECXL2ConnectionDelete(ctx context.Context, d *schema.ResourceData,
restErr, ok := err.(rest.Error)
if ok {
// IC-LAYER2-4021 = Connection already deleted
if hasApplicationErrorCode(restErr.ApplicationErrors, "IC-LAYER2-4021") {
if equinix_errors.HasApplicationErrorCode(restErr.ApplicationErrors, "IC-LAYER2-4021") {
return diags
}
}
Expand Down
3 changes: 2 additions & 1 deletion equinix/resource_ecx_l2_serviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ecx-go/v2"
Expand Down Expand Up @@ -392,7 +393,7 @@ func resourceECXL2ServiceProfileDelete(ctx context.Context, d *schema.ResourceDa
restErr, ok := err.(rest.Error)
if ok {
// IC-PROFILE-004 = profile does not exist
if hasApplicationErrorCode(restErr.ApplicationErrors, "IC-PROFILE-004") {
if equinix_errors.HasApplicationErrorCode(restErr.ApplicationErrors, "IC-PROFILE-004") {
return diags
}
}
Expand Down
2 changes: 1 addition & 1 deletion equinix/resource_fabric_cloud_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func resourceCloudRouterDelete(ctx context.Context, d *schema.ResourceData, meta
errors, ok := err.(v4.GenericSwaggerError).Model().([]v4.ModelError)
if ok {
// EQ-3040055 = There is an existing update in REQUESTED state
if hasModelErrorCode(errors, "EQ-3040055") {
if equinix_errors.HasModelErrorCode(errors, "EQ-3040055") {
return diags
}
}
Expand Down
2 changes: 1 addition & 1 deletion equinix/resource_fabric_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func resourceFabricConnectionDelete(ctx context.Context, d *schema.ResourceData,
errors, ok := err.(v4.GenericSwaggerError).Model().([]v4.ModelError)
if ok {
// EQ-3142509 = Connection already deleted
if hasModelErrorCode(errors, "EQ-3142509") {
if equinix_errors.HasModelErrorCode(errors, "EQ-3142509") {
return diags
}
}
Expand Down
2 changes: 1 addition & 1 deletion equinix/resource_fabric_routing_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func resourceFabricRoutingProtocolDelete(ctx context.Context, d *schema.Resource
errors, ok := err.(v4.GenericSwaggerError).Model().([]v4.ModelError)
if ok {
// EQ-3142509 = Connection already deleted
if hasModelErrorCode(errors, "EQ-3142509") {
if equinix_errors.HasModelErrorCode(errors, "EQ-3142509") {
return diags
}
}
Expand Down
7 changes: 4 additions & 3 deletions equinix/resource_network_device_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
"github.com/equinix/terraform-provider-equinix/internal/hashcode"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

Expand Down Expand Up @@ -251,7 +252,7 @@ func resourceNetworkDeviceLinkRead(ctx context.Context, d *schema.ResourceData,
var diags diag.Diagnostics
link, err := client.GetDeviceLinkGroup(d.Id())
if err != nil {
if isRestNotFoundError(err) {
if equinix_errors.IsRestNotFoundError(err) {
d.SetId("")
return nil
}
Expand Down Expand Up @@ -312,7 +313,7 @@ func resourceNetworkDeviceLinkDelete(ctx context.Context, d *schema.ResourceData
m.(*config.Config).AddModuleToNEUserAgent(&client, d)
var diags diag.Diagnostics
if err := client.DeleteDeviceLinkGroup(d.Id()); err != nil {
if isRestNotFoundError(err) {
if equinix_errors.IsRestNotFoundError(err) {
return nil
}
return diag.FromErr(err)
Expand Down Expand Up @@ -470,7 +471,7 @@ func createDeviceLinkStatusDeleteWaitConfiguration(fetchFunc getDeviceLinkGroup,
Refresh: func() (interface{}, string, error) {
resp, err := fetchFunc(id)
if err != nil {
if isRestNotFoundError(err) {
if equinix_errors.IsRestNotFoundError(err) {
return resp, ne.DeviceLinkGroupStatusDeprovisioned, nil
}
return nil, "", err
Expand Down
28 changes: 28 additions & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

fabric "github.com/equinix-labs/fabric-go/fabric/v4"
"github.com/equinix/rest-go"
"github.com/packethost/packngo"
)

Expand Down Expand Up @@ -192,3 +193,30 @@ func IgnoreResponseErrors(ignore ...func(resp *http.Response, err error) bool) f
return err
}
}

func IsRestNotFoundError(err error) bool {
if restErr, ok := err.(rest.Error); ok {
if restErr.HTTPCode == http.StatusNotFound {
return true
}
}
return false
}

func HasApplicationErrorCode(errors []rest.ApplicationError, code string) bool {
for _, err := range errors {
if err.Code == code {
return true
}
}
return false
}

func HasModelErrorCode(errors []fabric.ModelError, code string) bool {
for _, err := range errors {
if err.ErrorCode == code {
return true
}
}
return false
}
49 changes: 49 additions & 0 deletions internal/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package errors

import (
"fmt"
"net/http"
"testing"

"github.com/equinix/rest-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/stretchr/testify/assert"
)

func TestProvider_HasApplicationErrorCode(t *testing.T) {
// given
code := "ERR-505"
errors := []rest.ApplicationError{
{
Code: "ERR-505",
},
{
Code: acctest.RandString(10),
},
}
// when
result := HasApplicationErrorCode(errors, code)
// then
assert.True(t, result, "Error list contains error with given code")
}

func TestProvider_IsRestNotFoundError(t *testing.T) {
// given
input := []error{
rest.Error{HTTPCode: http.StatusNotFound, Message: "Not Found"},
rest.Error{HTTPCode: http.StatusInternalServerError, Message: "Internal Server Error"},
fmt.Errorf("some bogus error"),
}
expected := []bool{
true,
false,
false,
}
// when
result := make([]bool, len(input))
for i := range input {
result[i] = IsRestNotFoundError(input[i])
}
// then
assert.Equal(t, expected, result, "Result matches expected output")
}

0 comments on commit f1c4836

Please sign in to comment.