diff --git a/internal/command/jsonplan/plan.go b/internal/command/jsonplan/plan.go index 0a4f9bcd6e..b8ff13676b 100644 --- a/internal/command/jsonplan/plan.go +++ b/internal/command/jsonplan/plan.go @@ -442,7 +442,8 @@ func (p *plan) marshalOutputChanges(changes *plans.Changes) error { changeV.After, _ = changeV.After.UnmarkDeep() var before, after []byte - afterUnknown := cty.False + var afterUnknown cty.Value + if changeV.Before != cty.NilVal { before, err = ctyjson.Marshal(changeV.Before, changeV.Before.Type()) if err != nil { @@ -455,8 +456,18 @@ func (p *plan) marshalOutputChanges(changes *plans.Changes) error { if err != nil { return err } + afterUnknown = cty.False } else { - afterUnknown = cty.True + filteredAfter := omitUnknowns(changeV.After) + if filteredAfter.IsNull() { + after = nil + } else { + after, err = ctyjson.Marshal(filteredAfter, filteredAfter.Type()) + if err != nil { + return err + } + } + afterUnknown = unknownAsBool(changeV.After) } } diff --git a/internal/command/testdata/show-json/unknown-output/main.tf b/internal/command/testdata/show-json/unknown-output/main.tf new file mode 100644 index 0000000000..d97891e224 --- /dev/null +++ b/internal/command/testdata/show-json/unknown-output/main.tf @@ -0,0 +1,19 @@ +output "foo" { + value = "hello" +} + +output "bar" { + value = tolist([ + "hello", + timestamp(), + "world", + ]) +} + +output "baz" { + value = { + greeting: "hello", + time: timestamp(), + subject: "world", + } +} diff --git a/internal/command/testdata/show-json/unknown-output/output.json b/internal/command/testdata/show-json/unknown-output/output.json new file mode 100644 index 0000000000..8a52b8dc57 --- /dev/null +++ b/internal/command/testdata/show-json/unknown-output/output.json @@ -0,0 +1,96 @@ +{ + "format_version": "1.1", + "terraform_version": "1.3.0-dev", + "planned_values": { + "outputs": { + "bar": { + "sensitive": false + }, + "baz": { + "sensitive": false + }, + "foo": { + "sensitive": false, + "type": "string", + "value": "hello" + } + }, + "root_module": {} + }, + "output_changes": { + "bar": { + "actions": [ + "create" + ], + "before": null, + "after": [ + "hello", + null, + "world" + ], + "after_unknown": [ + false, + true, + false + ], + "before_sensitive": false, + "after_sensitive": false + }, + "baz": { + "actions": [ + "create" + ], + "before": null, + "after": { + "greeting": "hello", + "subject": "world" + }, + "after_unknown": { + "time": true + }, + "before_sensitive": false, + "after_sensitive": false + }, + "foo": { + "actions": [ + "create" + ], + "before": null, + "after": "hello", + "after_unknown": false, + "before_sensitive": false, + "after_sensitive": false + } + }, + "prior_state": { + "format_version": "1.0", + "terraform_version": "1.3.0", + "values": { + "outputs": { + "foo": { + "sensitive": false, + "value": "hello", + "type": "string" + } + }, + "root_module": {} + } + }, + "configuration": { + "root_module": { + "outputs": { + "bar": { + "expression": {} + }, + "baz": { + "expression": {} + }, + "foo": { + "expression": { + "constant_value": "hello" + } + } + } + } + } +}