From 027c7aeb0d476095a162f3b04c3667adbc720c90 Mon Sep 17 00:00:00 2001 From: Tim Hogarty Date: Fri, 3 Nov 2023 16:44:23 -0700 Subject: [PATCH] Add network body response in terraform errors --- equinix/errors.go | 13 +++++++++++++ equinix/resource_fabric_connection.go | 27 ++++++++++----------------- go.mod | 2 +- go.sum | 6 ++---- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/equinix/errors.go b/equinix/errors.go index c3c546762..d94905367 100644 --- a/equinix/errors.go +++ b/equinix/errors.go @@ -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" @@ -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 { diff --git a/equinix/resource_fabric_connection.go b/equinix/resource_fabric_connection.go index 2764fe9a5..b134209aa 100755 --- a/equinix/resource_fabric_connection.go +++ b/equinix/resource_fabric_connection.go @@ -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) @@ -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 { @@ -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) @@ -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 } @@ -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 @@ -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 { @@ -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) diff --git a/go.mod b/go.mod index 8ac2dfb6e..aba0e7d7b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index da8230395..00a97ee0f 100644 --- a/go.sum +++ b/go.sum @@ -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=