Skip to content

Commit

Permalink
Fix numbers to string conversion for lookup files
Browse files Browse the repository at this point in the history
  • Loading branch information
brushknight committed May 26, 2020
1 parent da18c49 commit b0e0528
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 29 deletions.
87 changes: 58 additions & 29 deletions internal/config/substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,68 @@ func SubLookupFileData(configs *[]load.Config, config load.Config) error {

// create a new config file per
for _, item := range jsonOut {
switch obj := item.(type) {
case map[string]interface{}:
tmpCfgStr := string(tmpCfgBytes)
variableReplaces := regexp.MustCompile(`\${lf:.*?}`).FindAllString(tmpCfgStr, -1)
replaceOccurred := false
for _, variableReplace := range variableReplaces {
variableKey := strings.TrimSuffix(strings.Split(variableReplace, "${lf:")[1], "}") // eg. "channel"
if obj[variableKey] != nil {
tmpCfgStr = strings.Replace(tmpCfgStr, variableReplace, fmt.Sprintf("%v", obj[variableKey]), -1)
replaceOccurred = true
}
tmpCfgStr := string(tmpCfgBytes)
newCfg, err := fillTemplateConfigWithValues(item, tmpCfgStr)

if err != nil {
load.Logrus.WithFields(logrus.Fields{
"file": config.LookupFile,
"name": config.Name,
"suggestion": "check for errors or run yaml lint against the below output",
}).WithError(err).Error("config: new lookup file unmarshal failed")
load.Logrus.Error(tmpCfgStr)
}

if newCfg != nil {
*configs = append(*configs, *newCfg)
}
}
return nil
}

func fillTemplateConfigWithValues(values interface{}, configTemplate string) (*load.Config, error) {
switch obj := values.(type) {
case map[string]interface{}:
variableReplaces := regexp.MustCompile(`\${lf:.*?}`).FindAllString(configTemplate, -1)
replaceOccurred := false
for _, variableReplace := range variableReplaces {
variableKey := strings.TrimSuffix(strings.Split(variableReplace, "${lf:")[1], "}") // eg. "channel"
if obj[variableKey] != nil {
value := obj[variableKey]
configTemplate = strings.Replace(
configTemplate,
variableReplace,
toString(value),
-1)
replaceOccurred = true
}
// if replace occurred convert string to config yaml and reload
if replaceOccurred {
newCfg, err := ReadYML(tmpCfgStr)
if err != nil {

load.Logrus.WithFields(logrus.Fields{
"file": config.LookupFile,
"name": config.Name,
"suggestion": "check for errors or run yaml lint against the below output",
}).WithError(err).Error("config: new lookup file unmarshal failed")
load.Logrus.Error(tmpCfgStr)

} else {
*configs = append(*configs, newCfg)
}
}
// if replace occurred convert string to values yaml and reload
if replaceOccurred {
newCfg, err := ReadYML(configTemplate)
if err != nil {
return nil, err
}
default:
load.Logrus.Debug("config: lookup file needs to contain an array of objects")
return &newCfg, nil
}
default:
load.Logrus.Debug("config: lookup file needs to contain an array of objects")
}
return nil
return nil, nil
}

func toString(value interface{}) string {
format := "%v"
switch value.(type) {
case int:
format = "%d"
case float64, float32:
format = "%f"
case string:
format = "%s"
}

return fmt.Sprintf(format, value)
}

// SubEnvVariables substitutes environment variables into config
Expand Down
45 changes: 45 additions & 0 deletions internal/config/substitutions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package config

import (
"github.com/newrelic/nri-flex/internal/load"
"github.com/stretchr/testify/assert"
"testing"
"time"
Expand Down Expand Up @@ -68,3 +69,47 @@ func TestSubTimestamps(t *testing.T) {

assert.Equal(t, expected, fileContent)
}

func Test_parseLookupFileAndUpdateConfig(t *testing.T) {

testConfig := `
custom_attributes:
replace_id_float: ${lf:id_float}
replace_id_int: ${lf:id_int}
replace_name: ${lf:name}
`

item := map[string]interface{}{
"id_float": 2456853.0,
"id_int": 2456854,
"name": "AMP_eov_ntet-np",
}

actual, err := fillTemplateConfigWithValues(item, testConfig)

assert.NoError(t, err)
assert.NotNil(t, actual)

expected := load.Config{
CustomAttributes: map[string]string{
"replace_id_float": "2456853.000000",
"replace_id_int": "2456854",
"replace_name": "AMP_eov_ntet-np",
},
}

assert.ObjectsAreEqual(expected.CustomAttributes, (*actual).CustomAttributes)
}

func Test_toString(t *testing.T) {
valueInt := 2456853
assert.Equal(t, "2456853", toString(valueInt))
valueFloat32 := float32(2456853)
assert.Equal(t, "2456853.000000", toString(valueFloat32))
valueFloat64 := float64(2456853)
assert.Equal(t, "2456853.000000", toString(valueFloat64))
valueString := "2456853"
assert.Equal(t, "2456853", toString(valueString))
valueMap := map[string]interface{}{"foo": "baz"}
assert.Equal(t, "map[foo:baz]", toString(valueMap))
}

0 comments on commit b0e0528

Please sign in to comment.