config: validate that only proper variables can be in the count

This commit is contained in:
Mitchell Hashimoto 2014-10-02 16:30:46 -07:00
parent 53d05cb81f
commit 5090678168
5 changed files with 58 additions and 0 deletions

View File

@ -255,6 +255,26 @@ func (c *Config) Validate() error {
// Validate resources
for n, r := range resources {
// Verify count variables
for _, v := range r.RawCount.Variables {
switch v.(type) {
case *ModuleVariable:
errs = append(errs, fmt.Errorf(
"%s: resource count can't reference module variable: %s",
n,
v.FullKey()))
case *ResourceVariable:
errs = append(errs, fmt.Errorf(
"%s: resource count can't reference resource variable: %s",
n,
v.FullKey()))
case *UserVariable:
// Good
default:
panic("Unknown type in count var: " + n)
}
}
for _, d := range r.DependsOn {
if _, ok := resources[d]; !ok {
errs = append(errs, fmt.Errorf(

View File

@ -53,6 +53,27 @@ func TestConfigValidate_badDependsOn(t *testing.T) {
}
}
func TestConfigValidate_countModuleVar(t *testing.T) {
c := testConfig(t, "validate-count-module-var")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_countResourceVar(t *testing.T) {
c := testConfig(t, "validate-count-resource-var")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_countUserVar(t *testing.T) {
c := testConfig(t, "validate-count-user-var")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestConfigValidate_dupModule(t *testing.T) {
c := testConfig(t, "validate-dup-module")
if err := c.Validate(); err == nil {

View File

@ -0,0 +1,7 @@
module "foo" {
source = "./bar"
}
resource "aws_instance" "web" {
count = "${module.foo.bar}"
}

View File

@ -0,0 +1,5 @@
resource "aws_instance" "foo" {}
resource "aws_instance" "web" {
count = "${aws_instance.foo.bar}"
}

View File

@ -0,0 +1,5 @@
variable "foo" {}
resource "aws_instance" "web" {
count = "${var.foo}"
}