terraform: validation with unknown variables works [GH-432]

This commit is contained in:
Mitchell Hashimoto 2014-10-16 15:54:56 -07:00
parent 2e703afdad
commit de4c922c9c
3 changed files with 70 additions and 0 deletions

View File

@ -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 {

View File

@ -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")

View File

@ -0,0 +1,5 @@
variable "foo" {}
resource "aws_instance" "bar" {
foo = "${var.foo}"
}