Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
korenyoni committed Oct 16, 2023
1 parent 9d4e771 commit 4d00cf9
Show file tree
Hide file tree
Showing 28 changed files with 420 additions and 344 deletions.
5 changes: 3 additions & 2 deletions codefresh/data_idps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -132,7 +133,7 @@ func mapDataIdpToResource(idp cfclient.IDP, d *schema.ResourceData) error {

d.Set("access_token", idp.Access_token) // string `json:"access_token,omitempty"`

d.Set("accounts", flattenStringArr(idp.Accounts)) //
d.Set("accounts", datautil.FlattenStringArr(idp.Accounts)) //
//d.Set("apiHost", idp.ApiHost) // string `json:"apiHost,omitempty"`
//d.Set("apiPathPrefix", idp.ApiPathPrefix) // string `json:"apiPathPrefix,omitempty"`
//d.Set("apiURL", idp.ApiURL) // string `json:"apiURL,omitempty"`
Expand All @@ -152,7 +153,7 @@ func mapDataIdpToResource(idp cfclient.IDP, d *schema.ResourceData) error {
//d.Set("redirectUiUrl", idp.RedirectUiUrl) // string `json:"redirectUiUrl,omitempty"`
//d.Set("redirectUrl", idp.RedirectUrl) // string `json:"redirectUrl,omitempty"`
//d.Set("refreshTokenURL", idp.RefreshTokenURL) // string `json:"refreshTokenURL,omitempty"`
d.Set("scopes", flattenStringArr(idp.Scopes)) // []string `json:"scopes,omitempty"`
d.Set("scopes", datautil.FlattenStringArr(idp.Scopes)) // []string `json:"scopes,omitempty"`
d.Set("tenant", idp.Tenant) // string `json:"tenant,omitempty"`
//d.Set("tokenSecret", idp.TokenSecret) // string `json:"tokenSecret,omitempty"`
//d.Set("tokenURL", idp.TokenURL) // string `json:"tokenURL,omitempty"`
Expand Down
63 changes: 0 additions & 63 deletions codefresh/helper/validation/strings.go

This file was deleted.

16 changes: 0 additions & 16 deletions codefresh/helper/validation/validationopts/cron.go

This file was deleted.

16 changes: 0 additions & 16 deletions codefresh/helper/validation/validationopts/strings.go

This file was deleted.

2 changes: 2 additions & 0 deletions codefresh/internal/datautil/doc.go
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
41 changes: 41 additions & 0 deletions codefresh/internal/datautil/strings.go
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
}
52 changes: 52 additions & 0 deletions codefresh/internal/datautil/yaml.go
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
}
2 changes: 2 additions & 0 deletions codefresh/internal/schemautil/doc.go
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
58 changes: 58 additions & 0 deletions codefresh/internal/schemautil/normalize.go
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
}
34 changes: 34 additions & 0 deletions codefresh/internal/schemautil/supressdiff.go
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
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validationopts
package schemautil

import (
"github.com/dlclark/regexp2"
Expand All @@ -14,7 +14,7 @@ type ValidationOptions struct {
StringValidationOptions *StringValidationOptions
}

type OptionSetter func(*ValidationOptions)
type ValidationOptionSetter func(*ValidationOptions)

func NewValidationOptions() *ValidationOptions {
return &ValidationOptions{
Expand All @@ -30,25 +30,25 @@ func NewValidationOptions() *ValidationOptions {
}
}

func WithSeverity(severity diag.Severity) OptionSetter {
func WithSeverity(severity diag.Severity) ValidationOptionSetter {
return func(o *ValidationOptions) {
o.SetSeverity(severity)
}
}

func WithSummary(summary string) OptionSetter {
func WithSummary(summary string) ValidationOptionSetter {
return func(o *ValidationOptions) {
o.SetSummary(summary)
}
}

func WithDetailFormat(detailFormat string) OptionSetter {
func WithDetailFormat(detailFormat string) ValidationOptionSetter {
return func(o *ValidationOptions) {
o.SetDetailFormat(detailFormat)
}
}

func (o *ValidationOptions) Apply(setters []OptionSetter) *ValidationOptions {
func (o *ValidationOptions) Apply(setters []ValidationOptionSetter) *ValidationOptions {
for _, opt := range setters {
opt(o)
}
Expand Down
Loading

0 comments on commit 4d00cf9

Please sign in to comment.