Skip to content

Commit

Permalink
instance_key should be of type interface{} (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasslash committed Jun 30, 2023
1 parent f8635fe commit a17c11b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type CheckDynamicAddress struct {
//
// InstanceKey will be empty if there was no foreach or count argument
// defined on the containing object.
InstanceKey string `json:"instance_key,omitempty"`
InstanceKey interface{} `json:"instance_key,omitempty"`
}

// CheckResultStatic is the container for a "checkable object".
Expand Down
6 changes: 6 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func testParse(t *testing.T, filename string, typ reflect.Type) {
continue
}

// Skip this directory as this is a unique test that only tests a subset
// of attributes for a plan. The fixture is not entirely valid, but that's okay.
if e.Name() == "has_checks" {
continue
}

t.Run(e.Name(), func(t *testing.T) {
expected, err := ioutil.ReadFile(filepath.Join(testFixtureDir, e.Name(), filename))
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tfjson
import (
"encoding/json"
"os"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -65,3 +66,36 @@ func TestPlan_015(t *testing.T) {
t.Fatalf("unexpected variables: %s", diff)
}
}

func TestPlan_withChecks(t *testing.T) {
f, err := os.Open("testdata/has_checks/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()

var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}

if err := plan.Validate(); err != nil {
t.Fatal(err)
}

if len(plan.Checks) == 0 {
t.Fatal("expected checks to not be empty")
}

for _, c := range plan.Checks {
for _, instance := range c.Instances {
k := reflect.TypeOf(instance.Address.InstanceKey).Kind()
switch k {
case reflect.Int, reflect.Float32, reflect.Float64, reflect.String:
t.Log("instance key is a valid type")
default:
t.Fatalf("unexpected type %s, expected string or int", k.String())
}
}
}
}
1 change: 1 addition & 0 deletions testdata/has_checks/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +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 comments on commit a17c11b

Please sign in to comment.