-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
420 additions
and
344 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
// Package datautil provides utilities for working with data types. | ||
package datautil |
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,41 @@ | ||
package datautil | ||
|
||
import ( | ||
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" | ||
) | ||
|
||
// ConvertStringArr converts an array of interfaces to an array of strings. | ||
func ConvertStringArr(ifaceArr []interface{}) []string { | ||
return ConvertAndMapStringArr(ifaceArr, func(s string) string { return s }) | ||
} | ||
|
||
// ConvertAndMapStringArr converts an array of interfaces to an array of strings, | ||
// applying the supplied function to each element. | ||
func ConvertAndMapStringArr(ifaceArr []interface{}, f func(string) string) []string { | ||
var arr []string | ||
for _, v := range ifaceArr { | ||
if v == nil { | ||
continue | ||
} | ||
arr = append(arr, f(v.(string))) | ||
} | ||
return arr | ||
} | ||
|
||
// ConvertVariables converts an array of cfclient.Variables to a map of key/value pairs. | ||
func ConvertVariables(vars []cfclient.Variable) map[string]string { | ||
res := make(map[string]string, len(vars)) | ||
for _, v := range vars { | ||
res[v.Key] = v.Value | ||
} | ||
return res | ||
} | ||
|
||
// FlattenStringArr flattens an array of strings. | ||
func FlattenStringArr(sArr []string) []interface{} { | ||
iArr := []interface{}{} | ||
for _, s := range sArr { | ||
iArr = append(iArr, s) | ||
} | ||
return iArr | ||
} |
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,52 @@ | ||
package datautil | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/mikefarah/yq/v4/pkg/yqlib" | ||
"github.com/sclevine/yj/convert" | ||
"gopkg.in/op/go-logging.v1" | ||
) | ||
|
||
// Yq gets a value from a YAML string using yq | ||
func Yq(yamlString string, expression string) (string, error) { | ||
yqEncoder := yqlib.NewYamlEncoder(0, false, yqlib.NewDefaultYamlPreferences()) | ||
yqDecoder := yqlib.NewYamlDecoder(yqlib.NewDefaultYamlPreferences()) | ||
yqEvaluator := yqlib.NewStringEvaluator() | ||
|
||
// Disable yq logging | ||
yqLogBackend := logging.AddModuleLevel(logging.NewLogBackend(ioutil.Discard, "", 0)) | ||
yqlib.GetLogger().SetBackend(yqLogBackend) | ||
|
||
yamlString, err := yqEvaluator.Evaluate(yamlString, expression, yqEncoder, yqDecoder) | ||
yamlString = strings.TrimSpace(yamlString) | ||
|
||
if yamlString == "null" { // yq's Evaluate() returns "null" if the expression does not match anything | ||
return "", err | ||
} | ||
return yamlString, err | ||
} | ||
|
||
// YamlToJson converts a YAML string to JSON | ||
// | ||
// This function preserves the order of map keys (courtesy of yj package). | ||
// If this were to use yaml.Unmarshal() and json.Marshal() instead, the order of map keys would be lost. | ||
func YamlToJson(yamlString string) (string, error) { | ||
yamlConverter := convert.YAML{} | ||
jsonConverter := convert.JSON{} | ||
|
||
yamlDecoded, err := yamlConverter.Decode(strings.NewReader(yamlString)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
jsonBuffer := new(bytes.Buffer) | ||
err = jsonConverter.Encode(jsonBuffer, yamlDecoded) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return jsonBuffer.String(), nil | ||
} |
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,2 @@ | ||
// Package schemautil provides utilities for working with Terraform resource schemas. | ||
package schemautil |
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,58 @@ | ||
package schemautil | ||
|
||
import ( | ||
"log" | ||
"regexp" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
const ( | ||
NormalizedFieldNameRegex string = `[^a-z0-9_]+` | ||
) | ||
|
||
// NormalizeFieldName normalizes a field name to be lowercase and contain only alphanumeric characters and dashes. | ||
func NormalizeFieldName(fieldName string) (string, error) { | ||
reg, err := regexp.Compile(NormalizedFieldNameRegex) | ||
if err != nil { | ||
return "", err | ||
} | ||
return reg.ReplaceAllString(fieldName, ""), nil | ||
} | ||
|
||
// MustNormalizeFieldName is the same as NormalizeFieldName, but will log an error (legacy logging) instead of returning it. | ||
func MustNormalizeFieldName(fieldName string) string { | ||
normalizedFieldName, err := NormalizeFieldName(fieldName) | ||
if err != nil { | ||
log.Printf("[ERROR] Failed to normalize field name %q: %s", fieldName, err) | ||
} | ||
return normalizedFieldName | ||
} | ||
|
||
// NormalizeYAMLString normalizes a YAML string to a standardized order, format and indentation. | ||
func NormalizeYamlString(yamlString interface{}) (string, error) { | ||
var j map[string]interface{} | ||
|
||
if yamlString == nil || yamlString.(string) == "" { | ||
return "", nil | ||
} | ||
|
||
s := yamlString.(string) | ||
err := yaml.Unmarshal([]byte(s), &j) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
bytes, _ := yaml.Marshal(j) | ||
return string(bytes[:]), nil | ||
} | ||
|
||
|
||
// MustNormalizeYamlString is the same as NormalizeYamlString, but will log an error (legacy logging) instead of returning it. | ||
func MustNormalizeYamlString(yamlString interface{}) string { | ||
normalizedYamlString, err := NormalizeYamlString(yamlString) | ||
if err != nil { | ||
log.Printf("[ERROR] Failed to normalize YAML string %q: %s", yamlString, err) | ||
} | ||
return normalizedYamlString | ||
} |
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,34 @@ | ||
// Package schemautil provides utilities for working with Terraform resource schemas. | ||
// | ||
// Note that this package uses legacy logging because the provider context is not available | ||
package schemautil | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
normalizationFailedErrorFormat = "[ERROR] Unable to normalize data body: %s" | ||
) | ||
|
||
// SuppressEquivalentYamlDiffs returns SchemaDiffSuppressFunc that suppresses diffs between | ||
// equivalent YAML strings. | ||
func SuppressEquivalentYamlDiffs() schema.SchemaDiffSuppressFunc { | ||
return func(k, old, new string, d *schema.ResourceData) bool { | ||
normalizedOld, err := NormalizeYamlString(old) | ||
if err != nil { | ||
log.Printf(normalizationFailedErrorFormat, err) | ||
return false | ||
} | ||
|
||
normalizedNew, err := NormalizeYamlString(new) | ||
if err != nil { | ||
log.Printf(normalizationFailedErrorFormat, err) | ||
return false | ||
} | ||
|
||
return normalizedOld == normalizedNew | ||
} | ||
} |
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
Oops, something went wrong.