add relevant_attributes to the json plan format

Add the resource instances and individual attributes which may have
contributed to the planned changes to the json format of the plan. We
use the existing path encoding for individual attributes, which is
already used in the replace_paths change field.
This commit is contained in:
James Bardin 2022-02-08 10:58:10 -05:00
parent f0cd8be66f
commit 8c5e11d41a
1 changed files with 54 additions and 25 deletions

View File

@ -38,6 +38,7 @@ type plan struct {
OutputChanges map[string]change `json:"output_changes,omitempty"` OutputChanges map[string]change `json:"output_changes,omitempty"`
PriorState json.RawMessage `json:"prior_state,omitempty"` PriorState json.RawMessage `json:"prior_state,omitempty"`
Config json.RawMessage `json:"configuration,omitempty"` Config json.RawMessage `json:"configuration,omitempty"`
RelevantAttributes []resourceAttr `json:"relevant_attributes,omitempty"`
} }
func newPlan() *plan { func newPlan() *plan {
@ -46,6 +47,13 @@ func newPlan() *plan {
} }
} }
// resourceAttr contains the address and attribute of an external for the
// RelevantAttributes in the plan.
type resourceAttr struct {
Resource string `json:"resource"`
Attr json.RawMessage `json:"attribute"`
}
// Change is the representation of a proposed change for an object. // Change is the representation of a proposed change for an object.
type change struct { type change struct {
// Actions are the actions that will be taken on the object selected by the // Actions are the actions that will be taken on the object selected by the
@ -151,6 +159,10 @@ func Marshal(
} }
} }
if err := output.marshalRelevantAttrs(p); err != nil {
return nil, fmt.Errorf("error marshaling relevant attributes for external changes: %s", err)
}
// output.ResourceChanges // output.ResourceChanges
if p.Changes != nil { if p.Changes != nil {
output.ResourceChanges, err = output.marshalResourceChanges(p.Changes.Resources, schemas) output.ResourceChanges, err = output.marshalResourceChanges(p.Changes.Resources, schemas)
@ -482,6 +494,19 @@ func (p *plan) marshalPlannedValues(changes *plans.Changes, schemas *terraform.S
return nil return nil
} }
func (p *plan) marshalRelevantAttrs(plan *plans.Plan) error {
for _, ra := range plan.RelevantAttributes {
addr := ra.Resource.String()
path, err := encodePath(ra.Attr)
if err != nil {
return err
}
p.RelevantAttributes = append(p.RelevantAttributes, resourceAttr{addr, path})
}
return nil
}
// omitUnknowns recursively walks the src cty.Value and returns a new cty.Value, // omitUnknowns recursively walks the src cty.Value and returns a new cty.Value,
// omitting any unknowns. // omitting any unknowns.
// //
@ -655,6 +680,17 @@ func encodePaths(pathSet cty.PathSet) (json.RawMessage, error) {
jsonPaths := make([]json.RawMessage, 0, len(pathList)) jsonPaths := make([]json.RawMessage, 0, len(pathList))
for _, path := range pathList { for _, path := range pathList {
jsonPath, err := encodePath(path)
if err != nil {
return nil, err
}
jsonPaths = append(jsonPaths, jsonPath)
}
return json.Marshal(jsonPaths)
}
func encodePath(path cty.Path) (json.RawMessage, error) {
steps := make([]json.RawMessage, 0, len(path)) steps := make([]json.RawMessage, 0, len(path))
for _, step := range path { for _, step := range path {
switch s := step.(type) { switch s := step.(type) {
@ -674,12 +710,5 @@ func encodePaths(pathSet cty.PathSet) (json.RawMessage, error) {
return nil, fmt.Errorf("Unsupported path step %#v (%t)", step, step) return nil, fmt.Errorf("Unsupported path step %#v (%t)", step, step)
} }
} }
jsonPath, err := json.Marshal(steps) return json.Marshal(steps)
if err != nil {
return nil, err
}
jsonPaths = append(jsonPaths, jsonPath)
}
return json.Marshal(jsonPaths)
} }