Skip to content

Commit 49f99db

Browse files
authoredJun 19, 2023
terraform show -json: Add Errored field to output for plan (#33372)
* Add Errored field to JSON output * Fix test error message
1 parent 66e3c20 commit 49f99db

File tree

5 files changed

+118
-15
lines changed

5 files changed

+118
-15
lines changed
 

‎internal/command/jsonplan/plan.go

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type plan struct {
6363
RelevantAttributes []ResourceAttr `json:"relevant_attributes,omitempty"`
6464
Checks json.RawMessage `json:"checks,omitempty"`
6565
Timestamp string `json:"timestamp,omitempty"`
66+
Errored bool `json:"errored"`
6667
}
6768

6869
func newPlan() *plan {
@@ -221,6 +222,7 @@ func Marshal(
221222
output := newPlan()
222223
output.TerraformVersion = version.String()
223224
output.Timestamp = p.Timestamp.Format(time.RFC3339)
225+
output.Errored = p.Errored
224226

225227
err := output.marshalPlanVariables(p.VariableValues, config.Module.Variables)
226228
if err != nil {

‎internal/command/show_test.go

+61-14
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,43 @@ func TestShow_planWithForceReplaceChange(t *testing.T) {
421421
if !strings.Contains(got, want) {
422422
t.Fatalf("unexpected output\ngot: %s\nwant: %s", got, want)
423423
}
424+
}
425+
426+
func TestShow_planErrored(t *testing.T) {
427+
_, snap := testModuleWithSnapshot(t, "show")
428+
plan := testPlan(t)
429+
plan.Errored = true
430+
planFilePath := testPlanFile(
431+
t,
432+
snap,
433+
states.NewState(),
434+
plan,
435+
)
436+
437+
view, done := testView(t)
438+
c := &ShowCommand{
439+
Meta: Meta{
440+
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
441+
View: view,
442+
},
443+
}
424444

445+
args := []string{
446+
planFilePath,
447+
"-no-color",
448+
}
449+
code := c.Run(args)
450+
output := done(t)
451+
452+
if code != 0 {
453+
t.Fatalf("unexpected exit status %d; want 0\ngot: %s", code, output.Stderr())
454+
}
455+
456+
got := output.Stdout()
457+
want := `Planning failed. Terraform encountered an error while generating this plan.`
458+
if !strings.Contains(got, want) {
459+
t.Fatalf("unexpected output\ngot: %s\nwant: %s", got, want)
460+
}
425461
}
426462

427463
func TestShow_plan_json(t *testing.T) {
@@ -525,6 +561,20 @@ func TestShow_json_output(t *testing.T) {
525561
t.Fatalf("init failed\n%s", ui.ErrorWriter)
526562
}
527563

564+
// read expected output
565+
wantFile, err := os.Open("output.json")
566+
if err != nil {
567+
t.Fatalf("unexpected err: %s", err)
568+
}
569+
defer wantFile.Close()
570+
byteValue, err := ioutil.ReadAll(wantFile)
571+
if err != nil {
572+
t.Fatalf("unexpected err: %s", err)
573+
}
574+
575+
var want plan
576+
json.Unmarshal([]byte(byteValue), &want)
577+
528578
// plan
529579
planView, planDone := testView(t)
530580
pc := &PlanCommand{
@@ -542,8 +592,15 @@ func TestShow_json_output(t *testing.T) {
542592
code := pc.Run(args)
543593
planOutput := planDone(t)
544594

545-
if code != 0 {
546-
t.Fatalf("unexpected exit status %d; want 0\ngot: %s", code, planOutput.Stderr())
595+
var wantedCode int
596+
if want.Errored {
597+
wantedCode = 1
598+
} else {
599+
wantedCode = 0
600+
}
601+
602+
if code != wantedCode {
603+
t.Fatalf("unexpected exit status %d; want %d\ngot: %s", code, wantedCode, planOutput.Stderr())
547604
}
548605

549606
// show
@@ -569,22 +626,11 @@ func TestShow_json_output(t *testing.T) {
569626
}
570627

571628
// compare view output to wanted output
572-
var got, want plan
629+
var got plan
573630

574631
gotString := showOutput.Stdout()
575632
json.Unmarshal([]byte(gotString), &got)
576633

577-
wantFile, err := os.Open("output.json")
578-
if err != nil {
579-
t.Fatalf("unexpected err: %s", err)
580-
}
581-
defer wantFile.Close()
582-
byteValue, err := ioutil.ReadAll(wantFile)
583-
if err != nil {
584-
t.Fatalf("unexpected err: %s", err)
585-
}
586-
json.Unmarshal([]byte(byteValue), &want)
587-
588634
// Disregard format version to reduce needless test fixture churn
589635
want.FormatVersion = got.FormatVersion
590636

@@ -1150,6 +1196,7 @@ type plan struct {
11501196
OutputChanges map[string]interface{} `json:"output_changes,omitempty"`
11511197
PriorState priorState `json:"prior_state,omitempty"`
11521198
Config map[string]interface{} `json:"configuration,omitempty"`
1199+
Errored bool `json:"errored"`
11531200
}
11541201

11551202
type priorState struct {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
locals {
2+
ami = "bar"
3+
}
4+
5+
resource "test_instance" "test" {
6+
ami = local.ami
7+
8+
lifecycle {
9+
precondition {
10+
// failing condition
11+
condition = local.ami != "bar"
12+
error_message = "ami is bar"
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"format_version": "1.2",
3+
"planned_values": {
4+
"root_module": {}
5+
},
6+
"prior_state": {},
7+
"configuration": {
8+
"provider_config": {
9+
"test": {
10+
"full_name": "registry.terraform.io/hashicorp/test",
11+
"name": "test"
12+
}
13+
},
14+
"root_module": {
15+
"resources": [
16+
{
17+
"address": "test_instance.test",
18+
"expressions": {
19+
"ami": {
20+
"references": [
21+
"local.ami"
22+
]
23+
}
24+
},
25+
"mode": "managed",
26+
"name": "test",
27+
"provider_config_key": "test",
28+
"schema_version": 0,
29+
"type": "test_instance"
30+
}
31+
]
32+
}
33+
},
34+
"errored": true
35+
}

‎website/docs/internals/json-format.mdx

+5-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ For ease of consumption by callers, the plan representation includes a partial r
229229
// resources with postconditions, with as much information as Terraform can
230230
// recognize at plan time. Some objects will have status "unknown" to
231231
// indicate that their status will only be determined after applying the plan.
232-
"checks" <checks-representation>
232+
"checks" <checks-representation>,
233+
234+
// "errored" indicates whether planning failed. An errored plan cannot be applied,
235+
// but the actions planned before failure may help to understand the error.
236+
"errored": false
233237
}
234238
```
235239

0 commit comments

Comments
 (0)