From b9f49b2f60fb4c950fe7a42f6d500ebc75efbe6e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Jul 2014 09:27:28 -0700 Subject: [PATCH] terraform: semantic check that variables are set on the correct types --- terraform/semantics.go | 19 +++++++++++++++++++ terraform/semantics_test.go | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/terraform/semantics.go b/terraform/semantics.go index 21b8199e8..51c17b68a 100644 --- a/terraform/semantics.go +++ b/terraform/semantics.go @@ -11,6 +11,11 @@ import ( func smcUserVariables(c *config.Config, vs map[string]string) []error { var errs []error + cvs := make(map[string]*config.Variable) + for _, v := range c.Variables { + cvs[v.Name] = v + } + // Check that all required variables are present required := make(map[string]struct{}) for _, v := range c.Variables { @@ -28,6 +33,20 @@ func smcUserVariables(c *config.Config, vs map[string]string) []error { } } + // Check that types match up + for k, _ := range vs { + v, ok := cvs[k] + if !ok { + continue + } + + if v.Type() != config.VariableTypeString { + errs = append(errs, fmt.Errorf( + "%s: cannot assign string value to map type", + k)) + } + } + // TODO(mitchellh): variables that are unknown return errs diff --git a/terraform/semantics_test.go b/terraform/semantics_test.go index 018f448b1..bebf8f841 100644 --- a/terraform/semantics_test.go +++ b/terraform/semantics_test.go @@ -27,4 +27,14 @@ func TestSMCUserVariables(t *testing.T) { if len(errs) != 0 { t.Fatalf("err: %#v", errs) } + + // Mapping complete override + errs = smcUserVariables(c, map[string]string{ + "foo": "bar", + "map": "baz", + }) + if len(errs) == 0 { + t.Fatal("should have errors") + } + }