9 "github.com/hashicorp/hcl2/hcl"
10 "github.com/hashicorp/terraform/configs"
12 "github.com/hashicorp/terraform/addrs"
14 "github.com/hashicorp/terraform/config"
15 "github.com/hashicorp/terraform/config/module"
16 "github.com/zclconf/go-cty/cty"
17 "github.com/zclconf/go-cty/cty/convert"
20 // EvalTypeCheckVariable is an EvalNode which ensures that the variable
21 // values which are assigned as inputs to a module (including the root)
22 // match the types which are either declared for the variables explicitly
23 // or inferred from the default values.
25 // In order to achieve this three things are required:
26 // - a map of the proposed variable values
27 // - the configuration tree of the module in which the variable is
29 // - the path to the module (so we know which part of the tree to
30 // compare the values against).
31 type EvalTypeCheckVariable struct {
32 Variables map[string]interface{}
34 ModuleTree *module.Tree
37 func (n *EvalTypeCheckVariable) Eval(ctx EvalContext) (interface{}, error) {
38 currentTree := n.ModuleTree
39 for _, pathComponent := range n.ModulePath[1:] {
40 currentTree = currentTree.Children()[pathComponent]
42 targetConfig := currentTree.Config()
44 prototypes := make(map[string]config.VariableType)
45 for _, variable := range targetConfig.Variables {
46 prototypes[variable.Name] = variable.Type()
49 // Only display a module in an error message if we are not in the root module
50 modulePathDescription := fmt.Sprintf(" in module %s", strings.Join(n.ModulePath[1:], "."))
51 if len(n.ModulePath) == 1 {
52 modulePathDescription = ""
55 for name, declaredType := range prototypes {
56 proposedValue, ok := n.Variables[name]
58 // This means the default value should be used as no overriding value
59 // has been set. Therefore we should continue as no check is necessary.
63 if proposedValue == config.UnknownVariableValue {
68 case config.VariableTypeString:
69 switch proposedValue.(type) {
73 return nil, fmt.Errorf("variable %s%s should be type %s, got %s",
74 name, modulePathDescription, declaredType.Printable(), hclTypeName(proposedValue))
76 case config.VariableTypeMap:
77 switch proposedValue.(type) {
78 case map[string]interface{}:
81 return nil, fmt.Errorf("variable %s%s should be type %s, got %s",
82 name, modulePathDescription, declaredType.Printable(), hclTypeName(proposedValue))
84 case config.VariableTypeList:
85 switch proposedValue.(type) {
89 return nil, fmt.Errorf("variable %s%s should be type %s, got %s",
90 name, modulePathDescription, declaredType.Printable(), hclTypeName(proposedValue))
93 return nil, fmt.Errorf("variable %s%s should be type %s, got type string",
94 name, modulePathDescription, declaredType.Printable())
101 // EvalSetModuleCallArguments is an EvalNode implementation that sets values
102 // for arguments of a child module call, for later retrieval during
103 // expression evaluation.
104 type EvalSetModuleCallArguments struct {
105 Module addrs.ModuleCallInstance
106 Values map[string]cty.Value
110 func (n *EvalSetModuleCallArguments) Eval(ctx EvalContext) (interface{}, error) {
111 ctx.SetModuleCallArguments(n.Module, n.Values)
115 // EvalModuleCallArgument is an EvalNode implementation that produces the value
116 // for a particular variable as will be used by a child module instance.
118 // The result is written into the map given in Values, with its key
119 // set to the local name of the variable, disregarding the module instance
120 // address. Any existing values in that map are deleted first. This weird
121 // interface is a result of trying to be convenient for use with
122 // EvalContext.SetModuleCallArguments, which expects a map to merge in with
123 // any existing arguments.
124 type EvalModuleCallArgument struct {
125 Addr addrs.InputVariable
126 Config *configs.Variable
129 // If this flag is set, any diagnostics are discarded and this operation
130 // will always succeed, though may produce an unknown value in the
131 // event of an error.
132 IgnoreDiagnostics bool
134 Values map[string]cty.Value
137 func (n *EvalModuleCallArgument) Eval(ctx EvalContext) (interface{}, error) {
138 // Clear out the existing mapping
139 for k := range n.Values {
143 wantType := n.Config.Type
148 // Should never happen, but we'll bail out early here rather than
149 // crash in case it does. We set no value at all in this case,
150 // making a subsequent call to EvalContext.SetModuleCallArguments
152 log.Printf("[ERROR] attempt to evaluate %s with nil expression", n.Addr.String())
156 val, diags := ctx.EvaluateExpr(expr, cty.DynamicPseudoType, nil)
158 // We intentionally passed DynamicPseudoType to EvaluateExpr above because
159 // now we can do our own local type conversion and produce an error message
160 // with better context if it fails.
162 val, convErr = convert.Convert(val, wantType)
164 diags = diags.Append(&hcl.Diagnostic{
165 Severity: hcl.DiagError,
166 Summary: "Invalid value for module argument",
168 "The given value is not suitable for child module variable %q defined at %s: %s.",
169 name, n.Config.DeclRange.String(), convErr,
171 Subject: expr.Range().Ptr(),
173 // We'll return a placeholder unknown value to avoid producing
174 // redundant downstream errors.
175 val = cty.UnknownVal(wantType)
179 if n.IgnoreDiagnostics {
182 return nil, diags.ErrWithWarnings()
185 // hclTypeName returns the name of the type that would represent this value in
186 // a config file, or falls back to the Go type name if there's no corresponding
187 // HCL type. This is used for formatted output, not for comparing types.
188 func hclTypeName(i interface{}) string {
189 switch k := reflect.Indirect(reflect.ValueOf(i)).Kind(); k {
192 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
193 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
194 reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64:
196 case reflect.Array, reflect.Slice:
203 // fall back to the Go type if there's no match