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

Commit 43231b5

Browse files
committed
Merge pull request hashicorp#1534 from hashicorp/b-crash-on-output-interpolate
core: don't crash when count.index is used in the wrong context
2 parents 0629475 + 975a96f commit 43231b5

5 files changed

Lines changed: 72 additions & 0 deletions

File tree

config/config.go

Lines changed: 15 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 {
@@ -472,6 +480,13 @@ func (c *Config) Validate() error {
472480
errs = append(errs, fmt.Errorf(
473481
"%s: output should only have 'value' field", o.Name))
474482
}
483+
484+
for _, v := range o.RawConfig.Variables {
485+
if _, ok := v.(*CountVariable); ok {
486+
errs = append(errs, fmt.Errorf(
487+
"%s: count variables are only valid within resources", o.Name))
488+
}
489+
}
475490
}
476491

477492
// Check that all variables are in the proper context

config/config_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"path/filepath"
55
"reflect"
6+
"strings"
67
"testing"
78
)
89

@@ -60,6 +61,22 @@ func TestConfigValidate_countInt(t *testing.T) {
6061
}
6162
}
6263

64+
func TestConfigValidate_countBadContext(t *testing.T) {
65+
c := testConfig(t, "validate-count-bad-context")
66+
67+
err := c.Validate()
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+
}
77+
}
78+
}
79+
6380
func TestConfigValidate_countCountVar(t *testing.T) {
6481
c := testConfig(t, "validate-count-count-var")
6582
if err := c.Validate(); err == nil {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "aws_instance" "foo" {
2+
}
3+
4+
output "no_count_in_output" {
5+
value = "${count.index}"
6+
}
7+
8+
module "no_count_in_module" {
9+
source = "./child"
10+
somevar = "${count.index}"
11+
}

terraform/interpolate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func (i *Interpolater) valueCountVar(
8585
result map[string]ast.Variable) error {
8686
switch v.Type {
8787
case config.CountValueIndex:
88+
if scope.Resource == nil {
89+
return fmt.Errorf("%s: count.index is only valid within resources", n)
90+
}
8891
result[n] = ast.Variable{
8992
Value: scope.Resource.CountIndex,
9093
Type: ast.TypeInt,

terraform/interpolate_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package terraform
22

33
import (
4+
"fmt"
45
"os"
56
"reflect"
67
"sync"
@@ -24,6 +25,31 @@ func TestInterpolater_countIndex(t *testing.T) {
2425
})
2526
}
2627

28+
func TestInterpolater_countIndexInWrongContext(t *testing.T) {
29+
i := &Interpolater{}
30+
31+
scope := &InterpolationScope{
32+
Path: rootModulePath,
33+
}
34+
35+
n := "count.index"
36+
37+
v, err := config.NewInterpolatedVariable(n)
38+
if err != nil {
39+
t.Fatalf("err: %s", err)
40+
}
41+
42+
expectedErr := fmt.Errorf("foo: count.index is only valid within resources")
43+
44+
_, err = i.Values(scope, map[string]config.InterpolatedVariable{
45+
"foo": v,
46+
})
47+
48+
if !reflect.DeepEqual(expectedErr, err) {
49+
t.Fatalf("expected: %#v, got %#v", expectedErr, err)
50+
}
51+
}
52+
2753
func TestInterpolater_moduleVariable(t *testing.T) {
2854
lock := new(sync.RWMutex)
2955
state := &State{

0 commit comments

Comments
 (0)