-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle taking Ambar API Exceptions and correctly outputting them to Terraform for more useful error when doing Terraform template files.
- Loading branch information
Showing
4 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |