config: validate that resource references are valid
This commit is contained in:
parent
532cc33189
commit
78e056770c
|
@ -89,10 +89,13 @@ func (r *Resource) Id() string {
|
||||||
|
|
||||||
// Validate does some basic semantic checking of the configuration.
|
// Validate does some basic semantic checking of the configuration.
|
||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
|
var errs []error
|
||||||
|
|
||||||
|
vars := c.allVariables()
|
||||||
|
|
||||||
// Check for references to user variables that do not actually
|
// Check for references to user variables that do not actually
|
||||||
// exist and record those errors.
|
// exist and record those errors.
|
||||||
var errs []error
|
for source, v := range vars {
|
||||||
for source, v := range c.allVariables() {
|
|
||||||
uv, ok := v.(*UserVariable)
|
uv, ok := v.(*UserVariable)
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
|
@ -106,6 +109,27 @@ func (c *Config) Validate() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check that all references to resources are valid
|
||||||
|
resources := make(map[string]struct{})
|
||||||
|
for _, r := range c.Resources {
|
||||||
|
resources[r.Id()] = struct{}{}
|
||||||
|
}
|
||||||
|
for source, v := range vars {
|
||||||
|
rv, ok := v.(*ResourceVariable)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
id := fmt.Sprintf("%s.%s", rv.Type, rv.Name)
|
||||||
|
if _, ok := resources[id]; !ok {
|
||||||
|
errs = append(errs, fmt.Errorf(
|
||||||
|
"%s: unknown resource '%s' referenced in variable %s",
|
||||||
|
source,
|
||||||
|
id,
|
||||||
|
rv.FullKey()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
return &MultiError{Errors: errs}
|
return &MultiError{Errors: errs}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,13 @@ func TestConfigValidate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigValidate_unknownResourceVar(t *testing.T) {
|
||||||
|
c := testConfig(t, "validate-unknown-resource-var")
|
||||||
|
if err := c.Validate(); err == nil {
|
||||||
|
t.Fatal("should not be valid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigValidate_unknownVar(t *testing.T) {
|
func TestConfigValidate_unknownVar(t *testing.T) {
|
||||||
c := testConfig(t, "validate-unknownvar")
|
c := testConfig(t, "validate-unknownvar")
|
||||||
if err := c.Validate(); err == nil {
|
if err := c.Validate(); err == nil {
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
resource "aws_instance" "web" {
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "db" {
|
||||||
|
ami = "${aws_instance.loadbalancer.foo}"
|
||||||
|
}
|
Loading…
Reference in New Issue