Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 975a96f

Browse files
committed
core: protect against count.index in modules
Modules should get a validation error just like outputs do. refs hashicorp#1528
1 parent 347690a commit 975a96f

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ func (c *Config) Validate() error {
290290
raw[k] = strVal
291291
}
292292

293+
// Check for invalid count variables
294+
for _, v := range m.RawConfig.Variables {
295+
if _, ok := v.(*CountVariable); ok {
296+
errs = append(errs, fmt.Errorf(
297+
"%s: count variables are only valid within resources", m.Name))
298+
}
299+
}
300+
293301
// Update the raw configuration to only contain the string values
294302
m.RawConfig, err = NewRawConfig(raw)
295303
if err != nil {

config/config_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,17 @@ func TestConfigValidate_countInt(t *testing.T) {
6363

6464
func TestConfigValidate_countBadContext(t *testing.T) {
6565
c := testConfig(t, "validate-count-bad-context")
66-
expected := "count variables are only valid within resources"
66+
6767
err := c.Validate()
68-
if !strings.Contains(err.Error(), expected) {
69-
t.Fatalf("expected: %q,\nto contain: %q", err, expected)
68+
69+
expected := []string{
70+
"no_count_in_output: count variables are only valid within resources",
71+
"no_count_in_module: count variables are only valid within resources",
72+
}
73+
for _, exp := range expected {
74+
if !strings.Contains(err.Error(), exp) {
75+
t.Fatalf("expected: %q,\nto contain: %q", err, exp)
76+
}
7077
}
7178
}
7279

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
resource "aws_instance" "foo" {
22
}
33

4-
output "notgood" {
4+
output "no_count_in_output" {
55
value = "${count.index}"
66
}
7+
8+
module "no_count_in_module" {
9+
source = "./child"
10+
somevar = "${count.index}"
11+
}

0 commit comments

Comments
 (0)