Skip to content

Commit

Permalink
chore: add helper to catch API Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Feb 8, 2023
1 parent 727d672 commit 52667b6
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 31 deletions.
37 changes: 22 additions & 15 deletions internal/provider/edge_gateway_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,30 @@ func (d *edgeGatewayDataSource) Read(ctx context.Context, req datasource.ReadReq
return
}

gateway, _, err := d.client.EdgeGatewaysApi.ApiCustomersV20EdgesEdgeIdGet(d.client.auth, data.EdgeID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read gateway detail, got error: %s", err))
return
}

data = edgeGatewayDataSourceModel{
Tier0VrfID: types.StringValue(gateway.Tier0VrfId),
EdgeName: types.StringValue(gateway.EdgeName),
EdgeID: types.StringValue(gateway.EdgeId),
OwnerType: types.StringValue(gateway.OwnerType),
OwnerName: types.StringValue(gateway.OwnerName),
Description: types.StringValue(gateway.Description),
gateway, httpR, err := d.client.EdgeGatewaysApi.ApiCustomersV20EdgesEdgeIdGet(d.client.auth, data.EdgeID.ValueString())
if x := CheckApiError(err, httpR); x != nil {
resp.Diagnostics.Append(x.GetTerraformDiagnostic())
if resp.Diagnostics.HasError() {
return
}

// Is Not Found
data.ID = types.StringValue("")

} else {

data = edgeGatewayDataSourceModel{
Tier0VrfID: types.StringValue(gateway.Tier0VrfId),
EdgeName: types.StringValue(gateway.EdgeName),
EdgeID: types.StringValue(gateway.EdgeId),
OwnerType: types.StringValue(gateway.OwnerType),
OwnerName: types.StringValue(gateway.OwnerName),
Description: types.StringValue(gateway.Description),
}

data.ID = types.StringValue("frangipane")
}

data.ID = types.StringValue("frangipane")

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
Expand Down
40 changes: 24 additions & 16 deletions internal/provider/edge_gateways_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,33 @@ func (d *edgeGatewaysDataSource) Read(ctx context.Context, req datasource.ReadRe
return
}

gateways, _, err := d.client.EdgeGatewaysApi.ApiCustomersV20EdgesGet(d.client.auth)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read gateways detail, got error: %s", err))
return
}
gateways, httpR, err := d.client.EdgeGatewaysApi.ApiCustomersV20EdgesGet(d.client.auth)
if x := CheckApiError(err, httpR); x != nil {
resp.Diagnostics.Append(x.GetTerraformDiagnostic())
if resp.Diagnostics.HasError() {
return
}

for _, gw := range gateways {
d := gateway{
Tier0VrfID: types.StringValue(gw.Tier0VrfId),
EdgeName: types.StringValue(gw.EdgeName),
EdgeID: types.StringValue(gw.EdgeId),
OwnerType: types.StringValue(gw.OwnerType),
OwnerName: types.StringValue(gw.OwnerName),
Description: types.StringValue(gw.Description),
// Is Not Found
data.EdgeGateways = []gateway{}
data.ID = types.StringValue("")

} else {

for _, gw := range gateways {
d := gateway{
Tier0VrfID: types.StringValue(gw.Tier0VrfId),
EdgeName: types.StringValue(gw.EdgeName),
EdgeID: types.StringValue(gw.EdgeId),
OwnerType: types.StringValue(gw.OwnerType),
OwnerName: types.StringValue(gw.OwnerName),
Description: types.StringValue(gw.Description),
}
data.EdgeGateways = append(data.EdgeGateways, d)
}
data.EdgeGateways = append(data.EdgeGateways, d)
}

data.ID = types.StringValue("frangipane")
data.ID = types.StringValue("frangipane")
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
98 changes: 98 additions & 0 deletions internal/provider/helper_api_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package provider

import (
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/diag"
apiclient "github.com/orange-cloudavenue/cloudavenue-sdk-go"
)

type APIError struct {
LastError error
StatusCode int
apiclient.ApiError
}

func (e *APIError) Error() string {
if e.Code != "" && e.Message != "" {
return fmt.Sprintf("%s (HTTP Code:%s)", e.Message, e.Code)
}

if e.Message != "" {
return e.Message
}

return "couldn't find resource"
}

func (e *APIError) Unwrap() error {
return e.LastError
}

// GetSummary returns a summary of the error.
func (e *APIError) GetSummary() string {
return fmt.Sprintf("%s (HTTP Code:%d)", e.Reason, e.StatusCode)
}

// GetDetail returns a detailed description of the error.
func (e *APIError) GetDetail() string {
return e.Message
}

// GetTerraformDiagnostic returns a Terraform Diagnostic for the error.
func (e *APIError) GetTerraformDiagnostic() diag.Diagnostic {
var summary, detail string
if e.Reason != "" {
summary = e.GetSummary()
} else {
summary = fmt.Sprintf("HTTP response error: %d", e.StatusCode)
}
if e.Message != "" {
detail = e.GetDetail()
} else {
detail = e.LastError.Error()
}

if e.IsNotFound() {
return diag.NewWarningDiagnostic(summary, detail)
}
return diag.NewErrorDiagnostic(summary, detail)
}

// IsNotFound returns true if the error is a 404 Not Found error.
func (e *APIError) IsNotFound() bool {
return e.StatusCode == 404
}

// CheckApiError checks the HTTP response for errors and returns an APIError
// if the response code is >= 400.
// If the response code is < 400, nil is returned.
func CheckApiError(err error, httpR *http.Response) *APIError {

if err == nil {
return nil
}

if httpR != nil && httpR.StatusCode >= 400 {

if e, ok := err.(apiclient.GenericSwaggerError); ok {
x := &APIError{
LastError: err,
StatusCode: httpR.StatusCode,
}

if e.Model() != nil {
if m, ok := e.Model().(apiclient.ApiError); ok {
x.ApiError = m
}
}

return x
}
} else {
return nil
}

return nil
}

0 comments on commit 52667b6

Please sign in to comment.