Skip to content

Commit a17c11b

Browse files
authored
instance_key should be of type interface{} (#94)
1 parent f8635fe commit a17c11b

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

checks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type CheckDynamicAddress struct {
9292
//
9393
// InstanceKey will be empty if there was no foreach or count argument
9494
// defined on the containing object.
95-
InstanceKey string `json:"instance_key,omitempty"`
95+
InstanceKey interface{} `json:"instance_key,omitempty"`
9696
}
9797

9898
// CheckResultStatic is the container for a "checkable object".

parse_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func testParse(t *testing.T, filename string, typ reflect.Type) {
3131
continue
3232
}
3333

34+
// Skip this directory as this is a unique test that only tests a subset
35+
// of attributes for a plan. The fixture is not entirely valid, but that's okay.
36+
if e.Name() == "has_checks" {
37+
continue
38+
}
39+
3440
t.Run(e.Name(), func(t *testing.T) {
3541
expected, err := ioutil.ReadFile(filepath.Join(testFixtureDir, e.Name(), filename))
3642
if err != nil {

plan_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tfjson
66
import (
77
"encoding/json"
88
"os"
9+
"reflect"
910
"testing"
1011

1112
"github.com/google/go-cmp/cmp"
@@ -65,3 +66,36 @@ func TestPlan_015(t *testing.T) {
6566
t.Fatalf("unexpected variables: %s", diff)
6667
}
6768
}
69+
70+
func TestPlan_withChecks(t *testing.T) {
71+
f, err := os.Open("testdata/has_checks/plan.json")
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
defer f.Close()
76+
77+
var plan *Plan
78+
if err := json.NewDecoder(f).Decode(&plan); err != nil {
79+
t.Fatal(err)
80+
}
81+
82+
if err := plan.Validate(); err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
if len(plan.Checks) == 0 {
87+
t.Fatal("expected checks to not be empty")
88+
}
89+
90+
for _, c := range plan.Checks {
91+
for _, instance := range c.Instances {
92+
k := reflect.TypeOf(instance.Address.InstanceKey).Kind()
93+
switch k {
94+
case reflect.Int, reflect.Float32, reflect.Float64, reflect.String:
95+
t.Log("instance key is a valid type")
96+
default:
97+
t.Fatalf("unexpected type %s, expected string or int", k.String())
98+
}
99+
}
100+
}
101+
}

testdata/has_checks/plan.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"format_version":"1.1","terraform_version":"1.5.0","planned_values":{"root_module":{"resources":[{"address":"null_resource.foo","mode":"managed","type":"null_resource","name":"foo","provider_name":"registry.terraform.io/hashicorp/null","schema_version":0,"values":{"triggers":null},"sensitive_values":{}}]}},"resource_changes":[{"address":"null_resource.foo","mode":"managed","type":"null_resource","name":"foo","provider_name":"registry.terraform.io/hashicorp/null","change":{"actions":["create"],"before":null,"after":{"triggers":null},"after_unknown":{"id":true},"before_sensitive":false,"after_sensitive":{}}}],"configuration":{"provider_config":{"null":{"name":"null","full_name":"registry.terraform.io/hashicorp/null"}},"root_module":{"resources":[{"address":"null_resource.foo","mode":"managed","type":"null_resource","name":"foo","provider_config_key":"null","schema_version":0}]}},"checks":[{"//":"EXPERIMENTAL: see docs for details","address":{"kind":"resource","mode":"managed","module":"module.foo.module.bar","name":"this","to_display":"module.foo.module.bar.foobar.this","type":"foobar"},"status":"pass","instances":[{"address":{"instance_key":0,"module":"module.foo.module.bar","to_display":"module.foo.module.bar.foobar.this[0]"},"status":"pass"}]},{"//":"EXPERIMENTAL: see docs for details","address":{"kind":"resource","mode":"managed","module":"module.foo.module.bar","name":"this","to_display":"module.foo.module.bar.foobar.this","type":"foobar"},"status":"pass"}]}

0 commit comments

Comments
 (0)