command: validate must set values for root variables
Since the intent of the validate command is to check config validity regardless of context (input variables, state, etc), we use unknown values of the requested type here, which will then allow us to complete type checking against the specified types of the variables without assuming any particular values.
This commit is contained in:
parent
f77e7a61b0
commit
eac8779870
|
@ -6,6 +6,8 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/hashicorp/terraform/tfdiags"
|
||||
)
|
||||
|
@ -119,8 +121,27 @@ func (c *ValidateCommand) validate(dir string) tfdiags.Diagnostics {
|
|||
return diags
|
||||
}
|
||||
|
||||
// "validate" is to check if the given module is valid regardless of
|
||||
// input values, current state, etc. Therefore we populate all of the
|
||||
// input values with unknown values of the expected type, allowing us
|
||||
// to perform a type check without assuming any particular values.
|
||||
varValues := make(terraform.InputValues)
|
||||
for name, variable := range cfg.Module.Variables {
|
||||
ty := variable.Type
|
||||
if ty == cty.NilType {
|
||||
// Can't predict the type at all, so we'll just mark it as
|
||||
// cty.DynamicVal (unknown value of cty.DynamicPseudoType).
|
||||
ty = cty.DynamicPseudoType
|
||||
}
|
||||
varValues[name] = &terraform.InputValue{
|
||||
Value: cty.UnknownVal(ty),
|
||||
SourceType: terraform.ValueFromCLIArg,
|
||||
}
|
||||
}
|
||||
|
||||
opts := c.contextOpts()
|
||||
opts.Config = cfg
|
||||
opts.Variables = varValues
|
||||
|
||||
tfCtx, ctxDiags := terraform.NewContext(opts)
|
||||
diags = diags.Append(ctxDiags)
|
||||
|
|
Loading…
Reference in New Issue