mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
terraform: validation with unknown variables works [GH-432]
This commit is contained in:
parent
2e703afdad
commit
de4c922c9c
@ -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 {
|
||||
|
@ -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")
|
||||
|
5
terraform/test-fixtures/validate-variable-ref/main.tf
Normal file
5
terraform/test-fixtures/validate-variable-ref/main.tf
Normal file
@ -0,0 +1,5 @@
|
||||
variable "foo" {}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "${var.foo}"
|
||||
}
|
Loading…
Reference in New Issue
Block a user