Skip to content

Commit

Permalink
Pass Ambar API Errors to Terraform
Browse files Browse the repository at this point in the history
Handle taking Ambar API Exceptions and correctly outputting them to Terraform
for more useful error when doing Terraform template files.
  • Loading branch information
tjschutte committed Feb 12, 2024
1 parent 5913d95 commit 599de97
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
7 changes: 6 additions & 1 deletion internal/provider/data_destination_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -154,9 +155,13 @@ func (r *DataDestinationResource) Create(ctx context.Context, req resource.Creat
// Create the API call and execute it
createResourceResponse, httpResponse, err := r.client.AmbarAPI.CreateDataDestination(ctx).CreateDataDestinationRequest(createDataDestination).Execute()
if err != nil || createResourceResponse == nil || httpResponse == nil {
tflog.Debug(ctx, "StatusCode: "+httpResponse.Status)
httpBody, _ := io.ReadAll(httpResponse.Body)
errString := string(httpBody)
tflog.Debug(ctx, errString)
resp.Diagnostics.AddError(
"Error creating DataDestination",
"Could not create DataDestination, unexpected error: "+err.Error(),
"Could not create DataDestination: "+AmbarApiErrorToTerraformErrorString(errString),
)
return
}
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/data_source_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -160,9 +161,13 @@ func (r *dataSourceResource) Create(ctx context.Context, req resource.CreateRequ
// Create the API call and execute it
createResourceResponse, httpResponse, err := r.client.AmbarAPI.CreateDataSource(ctx).CreateDataSourceRequest(createDataSource).Execute()
if err != nil || createResourceResponse == nil || httpResponse == nil {
tflog.Debug(ctx, "StatusCode: "+httpResponse.Status)
httpBody, _ := io.ReadAll(httpResponse.Body)
errString := string(httpBody)
tflog.Debug(ctx, errString)
resp.Diagnostics.AddError(
"Error creating DataSource",
"Could not create DataSource, unexpected error: "+err.Error(),
"Could not create DataSource: "+AmbarApiErrorToTerraformErrorString(errString),
)
return
}
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/filter_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -146,9 +147,13 @@ func (r *FilterResource) Create(ctx context.Context, req resource.CreateRequest,
// Create the API call and execute it
createResourceResponse, httpResponse, err := r.client.AmbarAPI.CreateFilter(ctx).CreateFilterRequest(createFilter).Execute()
if err != nil || createResourceResponse == nil || httpResponse == nil {
tflog.Debug(ctx, "StatusCode: "+httpResponse.Status)
httpBody, _ := io.ReadAll(httpResponse.Body)
errString := string(httpBody)
tflog.Debug(ctx, errString)
resp.Diagnostics.AddError(
"Error creating Filter",
"Could not create Filter, unexpected error: "+err.Error(),
"Could not create Filter: "+AmbarApiErrorToTerraformErrorString(errString),
)
return
}
Expand Down
25 changes: 25 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package provider

import (
"regexp"
"strings"
)

var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")

// Small utility functions to help convert the usual camel case used in HTTP
// to the names we give fields in Terraform. This is just to remove some cognitive overhead
// for Terraform users.
func toSnakeCase(s string) string {
snake := matchAllCap.ReplaceAllString(s, "${1}_${2}")
return snake
}

// AmbarApiErrorToTerraformErrorString Extracts out just the error portion of the JSON body of the http response.
// We then use the other util functions to clean it up and correct the naming to be correct as fields would appear
// in Terraform template files.
func AmbarApiErrorToTerraformErrorString(apiString string) string {
slicedString := strings.SplitAfter(apiString, "\":")
errorContent := strings.Trim(slicedString[len(slicedString)-1], "\"{}")
return toSnakeCase(errorContent)
}

0 comments on commit 599de97

Please sign in to comment.