config: if count > 1, variable references must have index

/cc @pearkes - Here is that validation
This commit is contained in:
Mitchell Hashimoto 2014-07-06 13:46:56 -07:00
parent dd8db83543
commit aa6a758f6b
3 changed files with 34 additions and 4 deletions

View File

@ -66,7 +66,9 @@ type ResourceVariable struct {
Type string // Resource type, i.e. "aws_instance"
Name string // Resource name
Field string // Resource field
Multi bool // True if multi-variable: aws_instance.foo.*.id
Multi bool // True if multi-variable: aws_instance.foo.*.id
Index int // Index for multi-variable: aws_instance.foo.1.id == 1
key string
}
@ -124,9 +126,9 @@ func (c *Config) Validate() error {
}
// Check that all references to resources are valid
resources := make(map[string]struct{})
resources := make(map[string]*Resource)
for _, r := range c.Resources {
resources[r.Id()] = struct{}{}
resources[r.Id()] = r
}
for source, vs := range vars {
for _, v := range vs {
@ -136,12 +138,26 @@ func (c *Config) Validate() error {
}
id := fmt.Sprintf("%s.%s", rv.Type, rv.Name)
if _, ok := resources[id]; !ok {
r, ok := resources[id]
if !ok {
errs = append(errs, fmt.Errorf(
"%s: unknown resource '%s' referenced in variable %s",
source,
id,
rv.FullKey()))
continue
}
// If it is a multi reference and resource has a single
// count, it is an error.
if r.Count > 1 && !rv.Multi {
errs = append(errs, fmt.Errorf(
"%s: variable '%s' must specify index for multi-count "+
"resource %s",
source,
rv.FullKey(),
id))
continue
}
}
}

View File

@ -15,6 +15,13 @@ func TestConfigValidate(t *testing.T) {
}
}
func TestConfigValidate_badMultiResource(t *testing.T) {
c := testConfig(t, "validate-bad-multi-resource")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_outputBadField(t *testing.T) {
c := testConfig(t, "validate-output-bad-field")
if err := c.Validate(); err == nil {

View File

@ -0,0 +1,7 @@
resource "aws_instance" "web" {
count = 5
}
output "ip" {
value = "${aws_instance.web.id}"
}