@@ -13,6 +13,7 @@ import (
1313
1414 "github.com/hashicorp/terraform/internal/addrs"
1515 "github.com/hashicorp/terraform/internal/command/jsonchecks"
16+ "github.com/hashicorp/terraform/internal/lang/ephemeral"
1617 "github.com/hashicorp/terraform/internal/lang/marks"
1718 "github.com/hashicorp/terraform/internal/states"
1819 "github.com/hashicorp/terraform/internal/states/statefile"
@@ -116,15 +117,21 @@ type Resource struct {
116117// resource, whose structure depends on the resource type schema.
117118type AttributeValues map [string ]json.RawMessage
118119
119- func marshalAttributeValues (value cty.Value ) (unmarkedVal cty.Value , marshalledVals AttributeValues , sensitivePaths []cty.Path , err error ) {
120+ func marshalAttributeValues (value cty.Value ) (unmarkedVal cty.Value , marshalledVals AttributeValues , sensitivePaths []cty.Path , ephemeralPaths []cty.Path , err error ) {
121+ // Every ephemeral value at this point must be set through a write_only attribute, otherwise
122+ // validation would have failed. For this reason we can safely remove all ephemeral values from the value.
123+ _ , pvms := value .UnmarkDeepWithPaths ()
124+ ephemeralPaths , _ = marks .PathsWithMark (pvms , marks .Ephemeral )
125+ value = ephemeral .RemoveEphemeralValuesForMarshaling (value )
126+
120127 // unmark our value to show all values
121128 value , sensitivePaths , err = unmarkValueForMarshaling (value )
122129 if err != nil {
123- return cty .NilVal , nil , nil , err
130+ return cty .NilVal , nil , nil , nil , err
124131 }
125132
126133 if value == cty .NilVal || value .IsNull () {
127- return value , nil , nil , nil
134+ return value , nil , nil , nil , nil
128135 }
129136
130137 ret := make (AttributeValues )
@@ -135,7 +142,7 @@ func marshalAttributeValues(value cty.Value) (unmarkedVal cty.Value, marshalledV
135142 vJSON , _ := ctyjson .Marshal (v , v .Type ())
136143 ret [k .AsString ()] = json .RawMessage (vJSON )
137144 }
138- return value , ret , sensitivePaths , nil
145+ return value , ret , sensitivePaths , ephemeralPaths , nil
139146}
140147
141148// newState() returns a minimally-initialized state
@@ -403,7 +410,7 @@ func marshalResources(resources map[string]*states.Resource, module addrs.Module
403410
404411 var value cty.Value
405412 var sensitivePaths []cty.Path
406- value , current .AttributeValues , sensitivePaths , err = marshalAttributeValues (riObj .Value )
413+ value , current .AttributeValues , sensitivePaths , _ , err = marshalAttributeValues (riObj .Value )
407414 if err != nil {
408415 return nil , fmt .Errorf ("preparing attribute values for %s: %w" , current .Address , err )
409416 }
@@ -455,7 +462,7 @@ func marshalResources(resources map[string]*states.Resource, module addrs.Module
455462
456463 var value cty.Value
457464 var sensitivePaths []cty.Path
458- value , deposed .AttributeValues , sensitivePaths , err = marshalAttributeValues (riObj .Value )
465+ value , deposed .AttributeValues , sensitivePaths , _ , err = marshalAttributeValues (riObj .Value )
459466 if err != nil {
460467 return nil , fmt .Errorf ("preparing attribute values for %s: %w" , current .Address , err )
461468 }
0 commit comments