config: unknown keys give errors
This commit is contained in:
parent
ea4d975dac
commit
5329124cf9
|
@ -17,6 +17,10 @@ type Config struct {
|
|||
Resources []*Resource
|
||||
Variables map[string]*Variable
|
||||
Outputs map[string]*Output
|
||||
|
||||
// The fields below can be filled in by loaders for validation
|
||||
// purposes.
|
||||
unknownKeys []string
|
||||
}
|
||||
|
||||
// ProviderConfig is the configuration for a resource provider.
|
||||
|
@ -114,6 +118,11 @@ func (r *Resource) Id() string {
|
|||
func (c *Config) Validate() error {
|
||||
var errs []error
|
||||
|
||||
for _, k := range c.unknownKeys {
|
||||
errs = append(errs, fmt.Errorf(
|
||||
"Unknown root level key: %s", k))
|
||||
}
|
||||
|
||||
vars := c.allVariables()
|
||||
|
||||
// Check for references to user variables that do not actually
|
||||
|
|
|
@ -29,6 +29,13 @@ func TestConfigValidate_outputBadField(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_unknownThing(t *testing.T) {
|
||||
c := testConfig(t, "validate-unknownthing")
|
||||
if err := c.Validate(); err == nil {
|
||||
t.Fatal("should not be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_unknownResourceVar(t *testing.T) {
|
||||
c := testConfig(t, "validate-unknown-resource-var")
|
||||
if err := c.Validate(); err == nil {
|
||||
|
|
|
@ -22,6 +22,13 @@ func (t *libuclConfigurable) Close() error {
|
|||
}
|
||||
|
||||
func (t *libuclConfigurable) Config() (*Config, error) {
|
||||
validKeys := map[string]struct{}{
|
||||
"output": struct{}{},
|
||||
"provider": struct{}{},
|
||||
"resource": struct{}{},
|
||||
"variable": struct{}{},
|
||||
}
|
||||
|
||||
type LibuclVariable struct {
|
||||
Default string
|
||||
Description string
|
||||
|
@ -88,6 +95,20 @@ func (t *libuclConfigurable) Config() (*Config, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Check for invalid keys
|
||||
iter := t.Object.Iterate(true)
|
||||
defer iter.Close()
|
||||
for o := iter.Next(); o != nil; o = iter.Next() {
|
||||
k := o.Key()
|
||||
o.Close()
|
||||
|
||||
if _, ok := validKeys[k]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
config.unknownKeys = append(config.unknownKeys, k)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
what "is this"
|
Loading…
Reference in New Issue