Skip to content

Commit 6bc20aa

Browse files
authored
Add identity fields to Plan struct (#158)
1 parent b5939fa commit 6bc20aa

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

parse_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package tfjson
66
import (
77
"bytes"
88
"encoding/json"
9-
"io/ioutil"
109
"os"
1110
"path/filepath"
1211
"reflect"
@@ -21,7 +20,7 @@ const testGoldenStateFileName = "state.json"
2120
const testGoldenSchemasFileName = "schemas.json"
2221

2322
func testParse(t *testing.T, filename string, typ reflect.Type) {
24-
entries, err := ioutil.ReadDir(testFixtureDir)
23+
entries, err := os.ReadDir(testFixtureDir)
2524
if err != nil {
2625
t.Fatalf("err: %s", err)
2726
}
@@ -32,7 +31,7 @@ func testParse(t *testing.T, filename string, typ reflect.Type) {
3231
}
3332

3433
t.Run(e.Name(), func(t *testing.T) {
35-
expected, err := ioutil.ReadFile(filepath.Join(testFixtureDir, e.Name(), filename))
34+
expected, err := os.ReadFile(filepath.Join(testFixtureDir, e.Name(), filename))
3635
if err != nil {
3736
if os.IsNotExist(err) {
3837
t.Skip(err.Error())

plan.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,28 @@ type Change struct {
266266
// is either an integer pointing to a child of a set/list, or a string
267267
// pointing to the child of a map, object, or block.
268268
ReplacePaths []interface{} `json:"replace_paths,omitempty"`
269+
270+
// BeforeIdentity and AfterIdentity are representations of the resource
271+
// identity value both before and after the action.
272+
BeforeIdentity interface{} `json:"before_identity,omitempty"`
273+
AfterIdentity interface{} `json:"after_identity,omitempty"`
269274
}
270275

271276
// Importing is a nested object for the resource import metadata.
272277
type Importing struct {
273278
// The original ID of this resource used to target it as part of planned
274279
// import operation.
275280
ID string `json:"id,omitempty"`
281+
282+
// Unknown indicates the ID or identity was unknown at the time of
283+
// planning. This would have led to the overall change being deferred, as
284+
// such this should only be true when processing changes from the deferred
285+
// changes list.
286+
Unknown bool `json:"unknown,omitempty"`
287+
288+
// The identity can be used instead of the ID to target the resource as part
289+
// of the planned import operation.
290+
Identity interface{} `json:"identity,omitempty"`
276291
}
277292

278293
// PlanVariable is a top-level variable in the Terraform plan.

plan_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,34 @@ import (
1313
)
1414

1515
func TestPlanValidate(t *testing.T) {
16-
f, err := os.Open("testdata/basic/plan.json")
17-
if err != nil {
18-
t.Fatal(err)
16+
cases := map[string]struct {
17+
planPath string
18+
}{
19+
"basic plan": {
20+
planPath: "testdata/basic/plan.json",
21+
},
22+
"plan with identity": {
23+
planPath: "testdata/identity/plan.json",
24+
},
1925
}
20-
defer f.Close()
2126

22-
var plan *Plan
23-
if err := json.NewDecoder(f).Decode(&plan); err != nil {
24-
t.Fatal(err)
25-
}
27+
for tn, tc := range cases {
28+
t.Run(tn, func(t *testing.T) {
29+
f, err := os.Open(tc.planPath)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
defer f.Close()
2634

27-
if err := plan.Validate(); err != nil {
28-
t.Fatal(err)
35+
var plan *Plan
36+
if err := json.NewDecoder(f).Decode(&plan); err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
if err := plan.Validate(); err != nil {
41+
t.Fatal(err)
42+
}
43+
})
2944
}
3045
}
3146

testdata/identity/plan.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"format_version":"1.2","terraform_version":"1.13.0-dev","planned_values":{"root_module":{"resources":[{"address":"corner_user_identity.user","mode":"managed","type":"corner_user_identity","name":"user","provider_name":"registry.terraform.io/hashicorp/corner","schema_version":0,"values":{"age":999,"email":"[email protected]","id":"[email protected]","name":"test"},"sensitive_values":{},"identity_schema_version":1,"identity":{"email":"[email protected]"}}]}},"resource_changes":[{"address":"corner_user_identity.user","mode":"managed","type":"corner_user_identity","name":"user","provider_name":"registry.terraform.io/hashicorp/corner","change":{"actions":["update"],"before":{"age":null,"email":"[email protected]","id":"[email protected]","name":null},"after":{"age":999,"email":"[email protected]","id":"[email protected]","name":"test"},"after_unknown":{},"before_sensitive":{},"after_sensitive":{},"importing":{"identity":{"email":"[email protected]"}},"before_identity":{"email":"[email protected]"},"after_identity":{"email":"[email protected]"}}}],"prior_state":{"format_version":"1.0","terraform_version":"1.13.0","values":{"root_module":{"resources":[{"address":"corner_user_identity.user","mode":"managed","type":"corner_user_identity","name":"user","provider_name":"registry.terraform.io/hashicorp/corner","schema_version":0,"values":{"age":null,"email":"[email protected]","id":"[email protected]","name":null},"sensitive_values":{},"identity_schema_version":1,"identity":{"email":"[email protected]"}}]}}},"configuration":{"provider_config":{"corner":{"name":"corner","full_name":"registry.terraform.io/hashicorp/corner"}},"root_module":{"resources":[{"address":"corner_user_identity.user","mode":"managed","type":"corner_user_identity","name":"user","provider_config_key":"corner","expressions":{"age":{"constant_value":999},"email":{"constant_value":"[email protected]"},"name":{"constant_value":"test"}},"schema_version":0}]}},"timestamp":"2025-04-30T11:34:17Z"}

0 commit comments

Comments
 (0)