diff --git a/terraform/context.go b/terraform/context.go index e79537128..ea4f4f39a 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -1069,6 +1069,9 @@ func (c *walkContext) validateWalkFn() depgraph.WalkFunc { l.Unlock() } + // Compute the variables in this resource + rn.Resource.Config.interpolate(c, rn.Resource) + log.Printf("[INFO] Validating resource: %s", rn.Resource.Id) ws, es := rn.Resource.Provider.ValidateResource( rn.Resource.Info.Type, rn.Resource.Config) @@ -1451,6 +1454,11 @@ func (c *walkContext) computeVars( } } case *config.ModuleVariable: + if c.Operation == walkValidate { + vs[n] = config.UnknownVariableValue + continue + } + value, err := c.computeModuleVariable(v) if err != nil { return err @@ -1476,6 +1484,11 @@ func (c *walkContext) computeVars( vs[n] = c.Context.module.Config().Dir } case *config.ResourceVariable: + if c.Operation == walkValidate { + vs[n] = config.UnknownVariableValue + continue + } + var attr string var err error if v.Multi && v.Index == -1 { @@ -1495,6 +1508,11 @@ func (c *walkContext) computeVars( continue } + if c.Operation == walkValidate { + vs[n] = config.UnknownVariableValue + continue + } + // Look up if we have any variables with this prefix because // those are map overrides. Include those. for k, val := range c.Variables { diff --git a/terraform/context_test.go b/terraform/context_test.go index dfe5cf0a9..4e526455c 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -499,6 +499,53 @@ func TestContextValidate_selfRefMultiAll(t *testing.T) { } } +func TestContextValidate_varRef(t *testing.T) { + m := testModule(t, "validate-variable-ref") + p := testProvider("aws") + c := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + computed := false + p.ValidateResourceFn = func(t string, c *ResourceConfig) ([]string, []error) { + computed = c.IsComputed("foo") + return nil, nil + } + + c.Validate() + if !computed { + t.Fatal("should be computed") + } +} + +func TestContextValidate_varRefFilled(t *testing.T) { + m := testModule(t, "validate-variable-ref") + p := testProvider("aws") + c := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Variables: map[string]string{ + "foo": "bar", + }, + }) + + var value interface{} + p.ValidateResourceFn = func(t string, c *ResourceConfig) ([]string, []error) { + value, _ = c.Get("foo") + return nil, nil + } + + c.Validate() + if value != "bar" { + t.Fatalf("bad: %#v", value) + } +} + func TestContextInput(t *testing.T) { input := new(MockUIInput) m := testModule(t, "input-vars") diff --git a/terraform/test-fixtures/validate-variable-ref/main.tf b/terraform/test-fixtures/validate-variable-ref/main.tf new file mode 100644 index 000000000..3bc9860b6 --- /dev/null +++ b/terraform/test-fixtures/validate-variable-ref/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +resource "aws_instance" "bar" { + foo = "${var.foo}" +}