mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
provider/template: validate vars are primitives
Closes #7160 by returning a proper error.
This commit is contained in:
parent
dc313de5b4
commit
b7e854fa8b
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/hil"
|
"github.com/hashicorp/hil"
|
||||||
"github.com/hashicorp/hil/ast"
|
"github.com/hashicorp/hil/ast"
|
||||||
@ -49,10 +50,11 @@ func dataSourceFile() *schema.Resource {
|
|||||||
ConflictsWith: []string{"template"},
|
ConflictsWith: []string{"template"},
|
||||||
},
|
},
|
||||||
"vars": &schema.Schema{
|
"vars": &schema.Schema{
|
||||||
Type: schema.TypeMap,
|
Type: schema.TypeMap,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Default: make(map[string]interface{}),
|
Default: make(map[string]interface{}),
|
||||||
Description: "variables to substitute",
|
Description: "variables to substitute",
|
||||||
|
ValidateFunc: validateVarsAttribute,
|
||||||
},
|
},
|
||||||
"rendered": &schema.Schema{
|
"rendered": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
@ -156,3 +158,22 @@ func validateTemplateAttribute(v interface{}, key string) (ws []string, es []err
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateVarsAttribute(v interface{}, key string) (ws []string, es []error) {
|
||||||
|
// vars can only be primitives right now
|
||||||
|
var badVars []string
|
||||||
|
for k, v := range v.(map[string]interface{}) {
|
||||||
|
switch v.(type) {
|
||||||
|
case []interface{}:
|
||||||
|
badVars = append(badVars, fmt.Sprintf("%s (list)", k))
|
||||||
|
case map[string]interface{}:
|
||||||
|
badVars = append(badVars, fmt.Sprintf("%s (map)", k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(badVars) > 0 {
|
||||||
|
es = append(es, fmt.Errorf(
|
||||||
|
"%s: cannot contain non-primitives; bad keys: %s",
|
||||||
|
key, strings.Join(badVars, ", ")))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@ -71,6 +71,49 @@ func TestValidateTemplateAttribute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateVarsAttribute(t *testing.T) {
|
||||||
|
cases := map[string]struct {
|
||||||
|
Vars map[string]interface{}
|
||||||
|
ExpectErr string
|
||||||
|
}{
|
||||||
|
"lists are invalid": {
|
||||||
|
map[string]interface{}{
|
||||||
|
"list": []interface{}{},
|
||||||
|
},
|
||||||
|
`vars: cannot contain non-primitives`,
|
||||||
|
},
|
||||||
|
"maps are invalid": {
|
||||||
|
map[string]interface{}{
|
||||||
|
"map": map[string]interface{}{},
|
||||||
|
},
|
||||||
|
`vars: cannot contain non-primitives`,
|
||||||
|
},
|
||||||
|
"strings, integers, floats, and bools are AOK": {
|
||||||
|
map[string]interface{}{
|
||||||
|
"string": "foo",
|
||||||
|
"int": 1,
|
||||||
|
"bool": true,
|
||||||
|
"float": float64(1.0),
|
||||||
|
},
|
||||||
|
``,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for tn, tc := range cases {
|
||||||
|
_, es := validateVarsAttribute(tc.Vars, "vars")
|
||||||
|
if len(es) > 0 {
|
||||||
|
if tc.ExpectErr == "" {
|
||||||
|
t.Fatalf("%s: expected no err, got: %#v", tn, es)
|
||||||
|
}
|
||||||
|
if !strings.Contains(es[0].Error(), tc.ExpectErr) {
|
||||||
|
t.Fatalf("%s: expected\n%s\nto contain\n%s", tn, es[0], tc.ExpectErr)
|
||||||
|
}
|
||||||
|
} else if tc.ExpectErr != "" {
|
||||||
|
t.Fatalf("%s: expected err containing %q, got none!", tn, tc.ExpectErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This test covers a panic due to config.Func formerly being a
|
// This test covers a panic due to config.Func formerly being a
|
||||||
// shared map, causing multiple template_file resources to try and
|
// shared map, causing multiple template_file resources to try and
|
||||||
// accessing it parallel during their lang.Eval() runs.
|
// accessing it parallel during their lang.Eval() runs.
|
||||||
|
@ -31,7 +31,9 @@ The following arguments are supported:
|
|||||||
from a file on disk using the [`file()` interpolation
|
from a file on disk using the [`file()` interpolation
|
||||||
function](/docs/configuration/interpolation.html#file_path_).
|
function](/docs/configuration/interpolation.html#file_path_).
|
||||||
|
|
||||||
* `vars` - (Optional) Variables for interpolation within the template.
|
* `vars` - (Optional) Variables for interpolation within the template. Note
|
||||||
|
that variables must all be primitives. Direct references to lists or maps
|
||||||
|
will cause a validation error.
|
||||||
|
|
||||||
The following arguments are maintained for backwards compatibility and may be
|
The following arguments are maintained for backwards compatibility and may be
|
||||||
removed in a future version:
|
removed in a future version:
|
||||||
|
Loading…
Reference in New Issue
Block a user