Skip to content

Commit

Permalink
instance_key should be of type interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasslash committed Jun 27, 2023
1 parent f8635fe commit ab83c2a
Show file tree
Hide file tree
Showing 3 changed files with 136 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
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())
}
}
}
}
101 changes: 101 additions & 0 deletions testdata/has_checks/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"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.zone_ip_allow_list.module.ip_allow_list_firewall_rule",
"name": "this",
"to_display": "module.zone_ip_allow_list.module.ip_allow_list_firewall_rule.cloudflare_firewall_rule.this",
"type": "cloudflare_firewall_rule"
},
"status": "pass",
"instances": [
{
"address": {
"instance_key": 0,
"module": "module.zone_ip_allow_list.module.ip_allow_list_firewall_rule",
"to_display": "module.zone_ip_allow_list.module.ip_allow_list_firewall_rule.cloudflare_firewall_rule.this[0]"
},
"status": "pass"
}
]
},
{
"//": "EXPERIMENTAL: see docs for details",
"address": {
"kind": "resource",
"mode": "managed",
"module": "module.zone_ip_allow_list.module.ip_allow_list_firewall_rule",
"name": "this",
"to_display": "module.zone_ip_allow_list.module.ip_allow_list_firewall_rule.cloudflare_rate_limit.this",
"type": "cloudflare_rate_limit"
},
"status": "pass"
}
]
}

0 comments on commit ab83c2a

Please sign in to comment.