config: validate that resource references are valid

This commit is contained in:
Mitchell Hashimoto 2014-07-02 21:06:26 -07:00
parent 532cc33189
commit 78e056770c
3 changed files with 39 additions and 2 deletions

View File

@ -89,10 +89,13 @@ func (r *Resource) Id() string {
// Validate does some basic semantic checking of the configuration.
func (c *Config) Validate() error {
var errs []error
vars := c.allVariables()
// Check for references to user variables that do not actually
// exist and record those errors.
var errs []error
for source, v := range c.allVariables() {
for source, v := range vars {
uv, ok := v.(*UserVariable)
if !ok {
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 {
return &MultiError{Errors: errs}
}

View File

@ -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) {
c := testConfig(t, "validate-unknownvar")
if err := c.Validate(); err == nil {

View File

@ -0,0 +1,6 @@
resource "aws_instance" "web" {
}
resource "aws_instance" "db" {
ami = "${aws_instance.loadbalancer.foo}"
}