Skip to content

Commit

Permalink
Add network body response in terraform errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thogarty committed Nov 3, 2023
1 parent d161d42 commit 027c7ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
13 changes: 13 additions & 0 deletions equinix/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package equinix

import (
"fmt"
"io"
"net/http"
"sort"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/packethost/packngo"
Expand Down Expand Up @@ -51,6 +53,17 @@ func convertToFriendlyError(errors Errors, resp *http.Response) error {
return er
}

func networkErrorOutput(err error, resp *http.Response) diag.Diagnostics {
diags := diag.Diagnostics{}
responseBody, readErr := io.ReadAll(resp.Body)
resp.Body.Close()
if readErr != nil {
diags = append(diags, diag.Diagnostic{Severity: 2, Summary: fmt.Sprintf("error parsing network request body: %s", readErr)})
}
diags = append(diags, diag.Diagnostic{Severity: 2, Summary: fmt.Sprintf("%s; \n %s", err, responseBody)})
return diags
}

func isForbidden(err error) bool {
r, ok := err.(*packngo.ErrorResponse)
if ok && r.Response != nil {
Expand Down
27 changes: 10 additions & 17 deletions equinix/resource_fabric_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func resourceFabricConnectionCreate(ctx context.Context, d *schema.ResourceData,
Project: &project,
}

conn, _, err := client.ConnectionsApi.CreateConnection(ctx, createRequest)
conn, httpResponse, err := client.ConnectionsApi.CreateConnection(ctx, createRequest)
if err != nil {
return diag.FromErr(err)
return networkErrorOutput(err, httpResponse)
}
d.SetId(conn.Uuid)

Expand All @@ -124,9 +124,9 @@ func resourceFabricConnectionCreate(ctx context.Context, d *schema.ResourceData,
},
}

_, _, patchErr := client.ConnectionsApi.UpdateConnectionByUuid(ctx, patchChangeOperation, conn.Uuid)
_, patchHttpResponse, patchErr := client.ConnectionsApi.UpdateConnectionByUuid(ctx, patchChangeOperation, conn.Uuid)
if patchErr != nil {
return diag.FromErr(err)
return networkErrorOutput(err, patchHttpResponse)
}

if _, statusChangeErr := waitForConnectionProviderStatusChange(d.Id(), meta, ctx); err != nil {
Expand Down Expand Up @@ -156,13 +156,9 @@ func additionalInfoContainsAWSSecrets(info []interface{}) ([]interface{}, bool)
func resourceFabricConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*Config).fabricClient
ctx = context.WithValue(ctx, v4.ContextAccessToken, meta.(*Config).FabricAuthToken)
conn, _, err := client.ConnectionsApi.GetConnectionByUuid(ctx, d.Id(), nil)
conn, httpResponse, err := client.ConnectionsApi.GetConnectionByUuid(ctx, d.Id(), nil)
if err != nil {
log.Printf("[WARN] Connection %s not found , error %s", d.Id(), err)
if !strings.Contains(err.Error(), "500") {
d.SetId("")
}
return diag.FromErr(err)
return networkErrorOutput(err, httpResponse)
}
d.SetId(conn.Uuid)
return setFabricMap(d, conn)
Expand Down Expand Up @@ -218,9 +214,9 @@ func resourceFabricConnectionUpdate(ctx context.Context, d *schema.ResourceData,
updatedConn := dbConn

for _, update := range updateRequests {
_, _, err := client.ConnectionsApi.UpdateConnectionByUuid(ctx, update, d.Id())
_, httpResponse, err := client.ConnectionsApi.UpdateConnectionByUuid(ctx, update, d.Id())
if err != nil {
diags = append(diags, diag.Diagnostic{Severity: 2, Summary: fmt.Sprintf("connectionn property update request error: %v [update payload: %v] (other updates will be successful if the payload is not shown)", err, update)})
diags = append(diags, networkErrorOutput(fmt.Errorf("connectionn property update request error: %v [update payload: %v] (other updates will be successful if the payload is not shown)", err, update), httpResponse)...)
continue
}

Expand All @@ -236,9 +232,6 @@ func resourceFabricConnectionUpdate(ctx context.Context, d *schema.ResourceData,
conn, err := waitFunction(d.Id(), meta, ctx)

if err != nil {
if !strings.Contains(err.Error(), "500") {
d.SetId("")
}
diags = append(diags, diag.Diagnostic{Severity: 2, Summary: fmt.Sprintf("connection property update completion timeout error: %v [update payload: %v] (other updates will be successful if the payload is not shown)", err, update)})
} else {
updatedConn = conn
Expand Down Expand Up @@ -374,7 +367,7 @@ func resourceFabricConnectionDelete(ctx context.Context, d *schema.ResourceData,
diags := diag.Diagnostics{}
client := meta.(*Config).fabricClient
ctx = context.WithValue(ctx, v4.ContextAccessToken, meta.(*Config).FabricAuthToken)
_, _, err := client.ConnectionsApi.DeleteConnectionByUuid(ctx, d.Id())
_, httpResponse, err := client.ConnectionsApi.DeleteConnectionByUuid(ctx, d.Id())
if err != nil {
errors, ok := err.(v4.GenericSwaggerError).Model().([]v4.ModelError)
if ok {
Expand All @@ -383,7 +376,7 @@ func resourceFabricConnectionDelete(ctx context.Context, d *schema.ResourceData,
return diags
}
}
return diag.FromErr(fmt.Errorf("error response for the connection delete: %v", err))
return networkErrorOutput(fmt.Errorf("error response for the connection delete: %v", err), httpResponse)
}

err = waitUntilConnectionDeprovisioned(d.Id(), meta, ctx)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/antihax/optional v1.0.0
github.com/equinix-labs/fabric-go v0.7.0
github.com/equinix-labs/fabric-go v0.7.1
github.com/equinix-labs/metal-go v0.26.0
github.com/equinix/ecx-go/v2 v2.3.1
github.com/equinix/ne-go v1.11.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/equinix-labs/fabric-go v0.7.0 h1:AiiVPD4aE/aeiuCK7Fhsq4bvjmJ5RzmZ3boKnp0dl4g=
github.com/equinix-labs/fabric-go v0.7.0/go.mod h1:oqgGS3GOI8hHGPJKsAwDOEX0qRHl52sJGvwA/zMSd90=
github.com/equinix-labs/metal-go v0.25.1 h1:uL83lRKyAcOfab+9r2xujAuLD8lTsqv89+SPvVFkcBM=
github.com/equinix-labs/metal-go v0.25.1/go.mod h1:SmxCklxW+KjmBLVMdEXgtFO5gD5/b4N0VxcNgUYbOH4=
github.com/equinix-labs/fabric-go v0.7.1 h1:4yk0IKXMcc72rkRVbcYHokAEc1uUB06t6NXK+DtSsbs=
github.com/equinix-labs/fabric-go v0.7.1/go.mod h1:oqgGS3GOI8hHGPJKsAwDOEX0qRHl52sJGvwA/zMSd90=
github.com/equinix-labs/metal-go v0.26.0 h1:0rBTyjF8j58dg++kMFLRi9Jhs5gng5BFn5Y0bl5NPtM=
github.com/equinix-labs/metal-go v0.26.0/go.mod h1:SmxCklxW+KjmBLVMdEXgtFO5gD5/b4N0VxcNgUYbOH4=
github.com/equinix/ecx-go/v2 v2.3.1 h1:gFcAIeyaEUw7S8ebqApmT7E/S7pC7Ac3wgScp89fkPU=
Expand Down

0 comments on commit 027c7ae

Please sign in to comment.