mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
config: unknown keys give errors
This commit is contained in:
parent
ea4d975dac
commit
5329124cf9
@ -17,6 +17,10 @@ type Config struct {
|
|||||||
Resources []*Resource
|
Resources []*Resource
|
||||||
Variables map[string]*Variable
|
Variables map[string]*Variable
|
||||||
Outputs map[string]*Output
|
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.
|
// ProviderConfig is the configuration for a resource provider.
|
||||||
@ -114,6 +118,11 @@ func (r *Resource) Id() string {
|
|||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
var errs []error
|
var errs []error
|
||||||
|
|
||||||
|
for _, k := range c.unknownKeys {
|
||||||
|
errs = append(errs, fmt.Errorf(
|
||||||
|
"Unknown root level key: %s", k))
|
||||||
|
}
|
||||||
|
|
||||||
vars := c.allVariables()
|
vars := c.allVariables()
|
||||||
|
|
||||||
// Check for references to user variables that do not actually
|
// 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) {
|
func TestConfigValidate_unknownResourceVar(t *testing.T) {
|
||||||
c := testConfig(t, "validate-unknown-resource-var")
|
c := testConfig(t, "validate-unknown-resource-var")
|
||||||
if err := c.Validate(); err == nil {
|
if err := c.Validate(); err == nil {
|
||||||
|
@ -22,6 +22,13 @@ func (t *libuclConfigurable) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *libuclConfigurable) Config() (*Config, error) {
|
func (t *libuclConfigurable) Config() (*Config, error) {
|
||||||
|
validKeys := map[string]struct{}{
|
||||||
|
"output": struct{}{},
|
||||||
|
"provider": struct{}{},
|
||||||
|
"resource": struct{}{},
|
||||||
|
"variable": struct{}{},
|
||||||
|
}
|
||||||
|
|
||||||
type LibuclVariable struct {
|
type LibuclVariable struct {
|
||||||
Default string
|
Default string
|
||||||
Description 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
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
config/test-fixtures/validate-unknownthing/main.tf
Normal file
1
config/test-fixtures/validate-unknownthing/main.tf
Normal file
@ -0,0 +1 @@
|
|||||||
|
what "is this"
|
Loading…
Reference in New Issue
Block a user