Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Support native lists in Terraform configuration #6322

Merged
merged 3 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type VariableType byte
const (
VariableTypeUnknown VariableType = iota
VariableTypeString
VariableTypeList
VariableTypeMap
)

Expand All @@ -167,6 +168,8 @@ func (v VariableType) Printable() string {
return "string"
case VariableTypeMap:
return "map"
case VariableTypeList:
return "list"
default:
return "unknown"
}
Expand Down Expand Up @@ -348,16 +351,30 @@ func (c *Config) Validate() error {
m.Id()))
}

// Check that the configuration can all be strings
// Check that the configuration can all be strings, lists or maps
raw := make(map[string]interface{})
for k, v := range m.RawConfig.Raw {
var strVal string
if err := mapstructure.WeakDecode(v, &strVal); err != nil {
errs = append(errs, fmt.Errorf(
"%s: variable %s must be a string value",
m.Id(), k))
if err := mapstructure.WeakDecode(v, &strVal); err == nil {
raw[k] = strVal
continue
}

var mapVal map[string]interface{}
if err := mapstructure.WeakDecode(v, &mapVal); err == nil {
raw[k] = mapVal
continue
}

var sliceVal []interface{}
if err := mapstructure.WeakDecode(v, &sliceVal); err == nil {
raw[k] = sliceVal
continue
}
raw[k] = strVal

errs = append(errs, fmt.Errorf(
"%s: variable %s must be a string, list or map value",
m.Id(), k))
}

// Check for invalid count variables
Expand Down Expand Up @@ -705,7 +722,8 @@ func (c *Config) validateVarContextFn(

if rv.Multi && rv.Index == -1 {
*errs = append(*errs, fmt.Errorf(
"%s: multi-variable must be in a slice", source))
"%s: use of the splat ('*') operator must be wrapped in a list declaration",
source))
}
}
}
Expand Down Expand Up @@ -813,6 +831,7 @@ func (v *Variable) Merge(v2 *Variable) *Variable {
var typeStringMap = map[string]VariableType{
"string": VariableTypeString,
"map": VariableTypeMap,
"list": VariableTypeList,
}

// Type returns the type of variable this is.
Expand Down Expand Up @@ -872,9 +891,9 @@ func (v *Variable) inferTypeFromDefault() VariableType {
return VariableTypeString
}

var strVal string
if err := mapstructure.WeakDecode(v.Default, &strVal); err == nil {
v.Default = strVal
var s string
if err := mapstructure.WeakDecode(v.Default, &s); err == nil {
v.Default = s
return VariableTypeString
}

Expand All @@ -884,5 +903,11 @@ func (v *Variable) inferTypeFromDefault() VariableType {
return VariableTypeMap
}

var l []string
if err := mapstructure.WeakDecode(v.Default, &l); err == nil {
v.Default = l
return VariableTypeList
}

return VariableTypeUnknown
}
19 changes: 13 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,15 @@ func TestConfigValidate_moduleVarInt(t *testing.T) {

func TestConfigValidate_moduleVarMap(t *testing.T) {
c := testConfig(t, "validate-module-var-map")
if err := c.Validate(); err == nil {
t.Fatal("should be invalid")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}

func TestConfigValidate_moduleVarList(t *testing.T) {
c := testConfig(t, "validate-module-var-list")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}

Expand Down Expand Up @@ -367,10 +374,10 @@ func TestConfigValidate_varDefault(t *testing.T) {
}
}

func TestConfigValidate_varDefaultBadType(t *testing.T) {
c := testConfig(t, "validate-var-default-bad-type")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
func TestConfigValidate_varDefaultListType(t *testing.T) {
c := testConfig(t, "validate-var-default-list-type")
if err := c.Validate(); err != nil {
t.Fatal("should be valid: %s", err)
}
}

Expand Down
35 changes: 26 additions & 9 deletions config/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,35 @@ func DetectVariables(root ast.Node) ([]InterpolatedVariable, error) {
return n
}

vn, ok := n.(*ast.VariableAccess)
if !ok {
return n
}

v, err := NewInterpolatedVariable(vn.Name)
if err != nil {
resultErr = err
switch vn := n.(type) {
case *ast.VariableAccess:
v, err := NewInterpolatedVariable(vn.Name)
if err != nil {
resultErr = err
return n
}
result = append(result, v)
case *ast.Index:
if va, ok := vn.Target.(*ast.VariableAccess); ok {
v, err := NewInterpolatedVariable(va.Name)
if err != nil {
resultErr = err
return n
}
result = append(result, v)
}
if va, ok := vn.Key.(*ast.VariableAccess); ok {
v, err := NewInterpolatedVariable(va.Name)
if err != nil {
resultErr = err
return n
}
result = append(result, v)
}
default:
return n
}

result = append(result, v)
return n
}

Expand Down
Loading