Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Fix definition names having to match param names (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
itowlson authored and jeremyrickard committed Aug 14, 2019
1 parent 568a0a8 commit 86561fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
25 changes: 17 additions & 8 deletions cmd/duffle/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func getBundleFilepath(bun, homePath string) (string, error) {
}

// overrides parses the --set data and returns values that should override other params.
func overrides(overrides []string, paramDefs definition.Definitions) (map[string]interface{}, error) {
func overrides(overrides []string, parameters map[string]bundle.Parameter, schemas definition.Definitions) (map[string]interface{}, error) {
res := map[string]interface{}{}
for _, p := range overrides {
pair := strings.SplitN(p, "=", 2)
Expand All @@ -199,19 +199,28 @@ func overrides(overrides []string, paramDefs definition.Definitions) (map[string
// a parameter in the file.
continue
}
def, ok := paramDefs[pair[0]]

parameterName := pair[0]
overrideValue := pair[1]

parameter, ok := parameters[parameterName]
if !ok {
return res, fmt.Errorf("parameter %s not defined in bundle", pair[0])
return res, fmt.Errorf("parameter %s not defined in bundle", parameterName)
}

if _, ok := res[pair[0]]; ok {
return res, fmt.Errorf("parameter %q specified multiple times", pair[0])
if _, ok := res[parameterName]; ok {
return res, fmt.Errorf("parameter %q specified multiple times", parameterName)
}

schema, ok := schemas[parameter.Definition]
if !ok {
return res, fmt.Errorf("definition %q of parameter %q is not present in bundle", parameter.Definition, parameterName)
}

var err error
res[pair[0]], err = def.ConvertValue(pair[1])
res[parameterName], err = schema.ConvertValue(overrideValue)
if err != nil {
return res, fmt.Errorf("cannot use %s as value of %s: %s", pair[1], pair[0], err)
return res, fmt.Errorf("cannot use %s as value of %s: %s", overrideValue, parameterName, err)
}
}
return res, nil
Expand Down Expand Up @@ -239,7 +248,7 @@ func calculateParamValues(bun *bundle.Bundle, valuesFile string, setParams, setF
}

}
overridden, err := overrides(setParams, bun.Definitions)
overridden, err := overrides(setParams, bun.Parameters, bun.Definitions)
if err != nil {
return vals, err
}
Expand Down
26 changes: 21 additions & 5 deletions cmd/duffle/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"testing"

"github.com/deislabs/cnab-go/bundle"

"github.com/deislabs/cnab-go/bundle/definition"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -62,17 +64,31 @@ func TestOverrides(t *testing.T) {
"second": {Type: "boolean"},
"third": {Type: "integer"},
}
params := map[string]bundle.Parameter{
"str": {Definition: "first"},
"second": {Definition: "second"},
"int": {Definition: "third"},
"bad": {Definition: "three hundred and thirty third"},
}

setVals := []string{"first=foo", "second=true", "third=2", "fourth"}
o, err := overrides(setVals, defs)
setVals := []string{"str=foo", "second=true", "int=2", "fourth"}
o, err := overrides(setVals, params, defs)
is.NoError(err)

is.Len(o, 3)
is.Equal(o["first"].(string), "foo")
is.Equal(o["str"].(string), "foo")
is.True(o["second"].(bool))
is.Equal(o["third"].(int), 2)
is.Equal(o["int"].(int), 2)

// We expect an error if we pass a param that was not defined:
_, err = overrides([]string{"undefined=foo"}, defs)
_, err = overrides([]string{"undefined=foo"}, params, defs)
is.Error(err)

// We expect an error if we pass a param that was not defined even if the name matches a definition:
_, err = overrides([]string{"first=foo"}, params, defs)
is.Error(err)

// We expect an error if we pass a param whose definition does not exist:
_, err = overrides([]string{"bad=worse"}, params, defs)
is.Error(err)
}

0 comments on commit 86561fe

Please sign in to comment.