2018-12-19 20:08:25 +01:00
|
|
|
package jsonplan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-01-12 00:13:55 +01:00
|
|
|
"sort"
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2019-01-25 18:17:40 +01:00
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:07:38 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonconfig"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonstate"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/statefile"
|
2021-05-17 21:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2019-02-01 22:47:18 +01:00
|
|
|
"github.com/hashicorp/terraform/version"
|
2018-12-19 20:08:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// FormatVersion represents the version of the json format and will be
|
|
|
|
// incremented for any change to this format that requires changes to a
|
|
|
|
// consuming parser.
|
2021-09-01 17:17:13 +02:00
|
|
|
const FormatVersion = "1.0"
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
// Plan is the top-level representation of the json format of a plan. It includes
|
|
|
|
// the complete config and current state.
|
|
|
|
type plan struct {
|
2019-02-01 22:47:18 +01:00
|
|
|
FormatVersion string `json:"format_version,omitempty"`
|
|
|
|
TerraformVersion string `json:"terraform_version,omitempty"`
|
2019-02-11 22:17:03 +01:00
|
|
|
Variables variables `json:"variables,omitempty"`
|
2019-02-01 22:47:18 +01:00
|
|
|
PlannedValues stateValues `json:"planned_values,omitempty"`
|
2021-05-07 00:22:48 +02:00
|
|
|
// ResourceDrift and ResourceChanges are sorted in a user-friendly order
|
|
|
|
// that is undefined at this time, but consistent.
|
|
|
|
ResourceDrift []resourceChange `json:"resource_drift,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
ResourceChanges []resourceChange `json:"resource_changes,omitempty"`
|
|
|
|
OutputChanges map[string]change `json:"output_changes,omitempty"`
|
|
|
|
PriorState json.RawMessage `json:"prior_state,omitempty"`
|
|
|
|
Config json.RawMessage `json:"configuration,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPlan() *plan {
|
|
|
|
return &plan{
|
|
|
|
FormatVersion: FormatVersion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change is the representation of a proposed change for an object.
|
|
|
|
type change struct {
|
|
|
|
// Actions are the actions that will be taken on the object selected by the
|
|
|
|
// properties below. Valid actions values are:
|
|
|
|
// ["no-op"]
|
|
|
|
// ["create"]
|
|
|
|
// ["read"]
|
|
|
|
// ["update"]
|
|
|
|
// ["delete", "create"]
|
|
|
|
// ["create", "delete"]
|
|
|
|
// ["delete"]
|
|
|
|
// The two "replace" actions are represented in this way to allow callers to
|
|
|
|
// e.g. just scan the list for "delete" to recognize all three situations
|
|
|
|
// where the object will be deleted, allowing for any new deletion
|
|
|
|
// combinations that might be added in future.
|
|
|
|
Actions []string `json:"actions,omitempty"`
|
|
|
|
|
|
|
|
// Before and After are representations of the object value both before and
|
|
|
|
// after the action. For ["create"] and ["delete"] actions, either "before"
|
|
|
|
// or "after" is unset (respectively). For ["no-op"], the before and after
|
|
|
|
// values are identical. The "after" value will be incomplete if there are
|
|
|
|
// values within it that won't be known until after apply.
|
2021-03-25 16:41:49 +01:00
|
|
|
Before json.RawMessage `json:"before,omitempty"`
|
|
|
|
After json.RawMessage `json:"after,omitempty"`
|
|
|
|
|
|
|
|
// AfterUnknown is an object value with similar structure to After, but
|
|
|
|
// with all unknown leaf values replaced with true, and all known leaf
|
|
|
|
// values omitted. This can be combined with After to reconstruct a full
|
|
|
|
// value after the action, including values which will only be known after
|
|
|
|
// apply.
|
2018-12-20 23:30:18 +01:00
|
|
|
AfterUnknown json.RawMessage `json:"after_unknown,omitempty"`
|
2021-03-25 16:41:49 +01:00
|
|
|
|
|
|
|
// BeforeSensitive and AfterSensitive are object values with similar
|
|
|
|
// structure to Before and After, but with all sensitive leaf values
|
|
|
|
// replaced with true, and all non-sensitive leaf values omitted. These
|
|
|
|
// objects should be combined with Before and After to prevent accidental
|
|
|
|
// display of sensitive values in user interfaces.
|
|
|
|
BeforeSensitive json.RawMessage `json:"before_sensitive,omitempty"`
|
|
|
|
AfterSensitive json.RawMessage `json:"after_sensitive,omitempty"`
|
2021-05-04 22:51:51 +02:00
|
|
|
|
|
|
|
// ReplacePaths is an array of arrays representing a set of paths into the
|
|
|
|
// object value which resulted in the action being "replace". This will be
|
|
|
|
// omitted if the action is not replace, or if no paths caused the
|
|
|
|
// replacement (for example, if the resource was tainted). Each path
|
|
|
|
// consists of one or more steps, each of which will be a number or a
|
|
|
|
// string.
|
|
|
|
ReplacePaths json.RawMessage `json:"replace_paths,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type output struct {
|
2019-01-09 17:59:11 +01:00
|
|
|
Sensitive bool `json:"sensitive"`
|
2018-12-19 20:08:25 +01:00
|
|
|
Value json.RawMessage `json:"value,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-02-11 22:17:03 +01:00
|
|
|
// variables is the JSON representation of the variables provided to the current
|
|
|
|
// plan.
|
|
|
|
type variables map[string]*variable
|
|
|
|
|
|
|
|
type variable struct {
|
|
|
|
Value json.RawMessage `json:"value,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
// Marshal returns the json encoding of a terraform plan.
|
|
|
|
func Marshal(
|
|
|
|
config *configs.Config,
|
|
|
|
p *plans.Plan,
|
2019-01-25 00:28:53 +01:00
|
|
|
sf *statefile.File,
|
2018-12-19 20:08:25 +01:00
|
|
|
schemas *terraform.Schemas,
|
|
|
|
) ([]byte, error) {
|
|
|
|
output := newPlan()
|
2019-02-01 22:47:18 +01:00
|
|
|
output.TerraformVersion = version.String()
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2021-12-21 01:38:52 +01:00
|
|
|
err := output.marshalPlanVariables(p.VariableValues, config.Module.Variables)
|
2019-02-11 22:17:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error in marshalPlanVariables: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-12-20 23:30:18 +01:00
|
|
|
// output.PlannedValues
|
2019-02-11 22:17:03 +01:00
|
|
|
err = output.marshalPlannedValues(p.Changes, schemas)
|
2018-12-19 20:08:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error in marshalPlannedValues: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
// output.ResourceDrift
|
2021-09-17 19:18:34 +02:00
|
|
|
if len(p.DriftedResources) > 0 {
|
|
|
|
// In refresh-only mode, we render all resources marked as drifted,
|
|
|
|
// including those which have moved without other changes. In other plan
|
|
|
|
// modes, move-only changes will be included in the planned changes, so
|
|
|
|
// we skip them here.
|
|
|
|
var driftedResources []*plans.ResourceInstanceChangeSrc
|
|
|
|
if p.UIMode == plans.RefreshOnlyMode {
|
|
|
|
driftedResources = p.DriftedResources
|
|
|
|
} else {
|
|
|
|
for _, dr := range p.DriftedResources {
|
|
|
|
if dr.Action != plans.NoOp {
|
|
|
|
driftedResources = append(driftedResources, dr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.ResourceDrift, err = output.marshalResourceChanges(driftedResources, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error in marshaling resource drift: %s", err)
|
|
|
|
}
|
2021-05-07 00:22:48 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
// output.ResourceChanges
|
2021-09-13 23:30:16 +02:00
|
|
|
if p.Changes != nil {
|
|
|
|
output.ResourceChanges, err = output.marshalResourceChanges(p.Changes.Resources, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error in marshaling resource changes: %s", err)
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// output.OutputChanges
|
|
|
|
err = output.marshalOutputChanges(p.Changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error in marshaling output changes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// output.PriorState
|
2019-03-14 22:52:07 +01:00
|
|
|
if sf != nil && !sf.State.Empty() {
|
2019-06-05 13:29:02 +02:00
|
|
|
output.PriorState, err = jsonstate.Marshal(sf, schemas)
|
2019-03-14 22:52:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error marshaling prior state: %s", err)
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// output.Config
|
|
|
|
output.Config, err = jsonconfig.Marshal(config, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error marshaling config: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-03-11 15:29:36 +01:00
|
|
|
ret, err := json.Marshal(output)
|
2018-12-19 20:08:25 +01:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2021-12-21 01:38:52 +01:00
|
|
|
func (p *plan) marshalPlanVariables(vars map[string]plans.DynamicValue, decls map[string]*configs.Variable) error {
|
2019-02-11 22:17:03 +01:00
|
|
|
p.Variables = make(variables, len(vars))
|
|
|
|
|
|
|
|
for k, v := range vars {
|
|
|
|
val, err := v.Decode(cty.DynamicPseudoType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
valJSON, err := ctyjson.Marshal(val, val.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Variables[k] = &variable{
|
|
|
|
Value: valJSON,
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 01:38:52 +01:00
|
|
|
|
|
|
|
// In Terraform v1.1 and earlier we had some confusion about which subsystem
|
|
|
|
// of Terraform was the one responsible for substituting in default values
|
|
|
|
// for unset module variables, with root module variables being handled in
|
|
|
|
// three different places while child module variables were only handled
|
|
|
|
// during the Terraform Core graph walk.
|
|
|
|
//
|
|
|
|
// For Terraform v1.2 and later we rationalized that by having the Terraform
|
|
|
|
// Core graph walk always be responsible for selecting defaults regardless
|
|
|
|
// of root vs. child module, but unfortunately our earlier accidental
|
|
|
|
// misbehavior bled out into the public interface by making the defaults
|
|
|
|
// show up in the "vars" map to this function. Those are now correctly
|
|
|
|
// omitted (so that the plan file only records the variables _actually_
|
|
|
|
// set by the caller) but consumers of the JSON plan format may be depending
|
|
|
|
// on our old behavior and so we'll fake it here just in time so that
|
|
|
|
// outside consumers won't see a behavior change.
|
|
|
|
for name, decl := range decls {
|
|
|
|
if _, ok := p.Variables[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if val := decl.Default; val != cty.NilVal {
|
|
|
|
valJSON, err := ctyjson.Marshal(val, val.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Variables[name] = &variable{
|
|
|
|
Value: valJSON,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.Variables) == 0 {
|
|
|
|
p.Variables = nil // omit this property if there are no variables to describe
|
|
|
|
}
|
|
|
|
|
2019-02-11 22:17:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 23:30:16 +02:00
|
|
|
func (p *plan) marshalResourceChanges(resources []*plans.ResourceInstanceChangeSrc, schemas *terraform.Schemas) ([]resourceChange, error) {
|
|
|
|
var ret []resourceChange
|
2021-05-07 00:22:48 +02:00
|
|
|
|
2021-09-13 23:30:16 +02:00
|
|
|
for _, rc := range resources {
|
2018-12-19 20:08:25 +01:00
|
|
|
var r resourceChange
|
|
|
|
addr := rc.Addr
|
|
|
|
r.Address = addr.String()
|
2021-09-17 19:18:34 +02:00
|
|
|
if !addr.Equal(rc.PrevRunAddr) {
|
|
|
|
r.PreviousAddress = rc.PrevRunAddr.String()
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
dataSource := addr.Resource.Resource.Mode == addrs.DataResourceMode
|
|
|
|
// We create "delete" actions for data resources so we can clean up
|
|
|
|
// their entries in state, but this is an implementation detail that
|
|
|
|
// users shouldn't see.
|
|
|
|
if dataSource && rc.Action == plans.Delete {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:27:49 +01:00
|
|
|
schema, _ := schemas.ResourceTypeConfig(
|
2020-02-13 21:32:58 +01:00
|
|
|
rc.ProviderAddr.Provider,
|
2019-02-20 23:27:49 +01:00
|
|
|
addr.Resource.Resource.Mode,
|
|
|
|
addr.Resource.Resource.Type,
|
|
|
|
)
|
2018-12-19 20:08:25 +01:00
|
|
|
if schema == nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, fmt.Errorf("no schema found for %s (in provider %s)", r.Address, rc.ProviderAddr.Provider)
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
changeV, err := rc.Decode(schema.ImpliedType())
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
2021-05-12 17:10:03 +02:00
|
|
|
// We drop the marks from the change, as decoding is only an
|
|
|
|
// intermediate step to re-encode the values as json
|
|
|
|
changeV.Before, _ = changeV.Before.UnmarkDeep()
|
|
|
|
changeV.After, _ = changeV.After.UnmarkDeep()
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
var before, after []byte
|
2021-03-25 16:41:49 +01:00
|
|
|
var beforeSensitive, afterSensitive []byte
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
var afterUnknown cty.Value
|
2021-05-12 17:10:03 +02:00
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
if changeV.Before != cty.NilVal {
|
|
|
|
before, err = ctyjson.Marshal(changeV.Before, changeV.Before.Type())
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
2021-04-26 22:26:47 +02:00
|
|
|
marks := rc.BeforeValMarks
|
|
|
|
if schema.ContainsSensitive() {
|
|
|
|
marks = append(marks, schema.ValueMarks(changeV.Before, nil)...)
|
|
|
|
}
|
2021-06-14 15:19:13 +02:00
|
|
|
bs := jsonstate.SensitiveAsBool(changeV.Before.MarkWithPaths(marks))
|
2021-03-25 16:41:49 +01:00
|
|
|
beforeSensitive, err = ctyjson.Marshal(bs, bs.Type())
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
2021-03-25 16:41:49 +01:00
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
if changeV.After != cty.NilVal {
|
|
|
|
if changeV.After.IsWhollyKnown() {
|
|
|
|
after, err = ctyjson.Marshal(changeV.After, changeV.After.Type())
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
2019-06-03 14:14:23 +02:00
|
|
|
afterUnknown = cty.EmptyObjectVal
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
} else {
|
|
|
|
filteredAfter := omitUnknowns(changeV.After)
|
2019-01-24 01:14:34 +01:00
|
|
|
if filteredAfter.IsNull() {
|
|
|
|
after = nil
|
|
|
|
} else {
|
|
|
|
after, err = ctyjson.Marshal(filteredAfter, filteredAfter.Type())
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
2019-01-24 01:14:34 +01:00
|
|
|
}
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
|
|
|
afterUnknown = unknownAsBool(changeV.After)
|
2018-12-20 23:30:18 +01:00
|
|
|
}
|
2021-04-26 22:26:47 +02:00
|
|
|
marks := rc.AfterValMarks
|
|
|
|
if schema.ContainsSensitive() {
|
|
|
|
marks = append(marks, schema.ValueMarks(changeV.After, nil)...)
|
|
|
|
}
|
2021-06-14 15:19:13 +02:00
|
|
|
as := jsonstate.SensitiveAsBool(changeV.After.MarkWithPaths(marks))
|
2021-03-25 16:41:49 +01:00
|
|
|
afterSensitive, err = ctyjson.Marshal(as, as.Type())
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
2021-03-25 16:41:49 +01:00
|
|
|
}
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
2018-12-20 23:30:18 +01:00
|
|
|
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
a, err := ctyjson.Marshal(afterUnknown, afterUnknown.Type())
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
2021-05-04 22:51:51 +02:00
|
|
|
replacePaths, err := encodePaths(rc.RequiredReplace)
|
|
|
|
if err != nil {
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, err
|
2021-05-04 22:51:51 +02:00
|
|
|
}
|
2018-12-20 23:30:18 +01:00
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
r.Change = change{
|
2021-03-25 16:41:49 +01:00
|
|
|
Actions: actionString(rc.Action.String()),
|
|
|
|
Before: json.RawMessage(before),
|
|
|
|
After: json.RawMessage(after),
|
|
|
|
AfterUnknown: a,
|
|
|
|
BeforeSensitive: json.RawMessage(beforeSensitive),
|
|
|
|
AfterSensitive: json.RawMessage(afterSensitive),
|
2021-05-04 22:51:51 +02:00
|
|
|
ReplacePaths: replacePaths,
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
2018-12-20 23:30:18 +01:00
|
|
|
|
2019-02-19 22:55:49 +01:00
|
|
|
if rc.DeposedKey != states.NotDeposed {
|
|
|
|
r.Deposed = rc.DeposedKey.String()
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
key := addr.Resource.Key
|
|
|
|
if key != nil {
|
|
|
|
r.Index = key
|
|
|
|
}
|
|
|
|
|
|
|
|
switch addr.Resource.Resource.Mode {
|
|
|
|
case addrs.ManagedResourceMode:
|
|
|
|
r.Mode = "managed"
|
|
|
|
case addrs.DataResourceMode:
|
|
|
|
r.Mode = "data"
|
|
|
|
default:
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, fmt.Errorf("resource %s has an unsupported mode %s", r.Address, addr.Resource.Resource.Mode.String())
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
r.ModuleAddress = addr.Module.String()
|
|
|
|
r.Name = addr.Resource.Resource.Name
|
|
|
|
r.Type = addr.Resource.Resource.Type
|
2020-04-02 18:58:44 +02:00
|
|
|
r.ProviderName = rc.ProviderAddr.Provider.String()
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2021-04-28 21:02:34 +02:00
|
|
|
switch rc.ActionReason {
|
|
|
|
case plans.ResourceInstanceChangeNoReason:
|
|
|
|
r.ActionReason = "" // will be omitted in output
|
|
|
|
case plans.ResourceInstanceReplaceBecauseCannotUpdate:
|
|
|
|
r.ActionReason = "replace_because_cannot_update"
|
|
|
|
case plans.ResourceInstanceReplaceBecauseTainted:
|
|
|
|
r.ActionReason = "replace_because_tainted"
|
|
|
|
case plans.ResourceInstanceReplaceByRequest:
|
|
|
|
r.ActionReason = "replace_by_request"
|
2021-09-23 02:58:41 +02:00
|
|
|
case plans.ResourceInstanceDeleteBecauseNoResourceConfig:
|
|
|
|
r.ActionReason = "delete_because_no_resource_config"
|
|
|
|
case plans.ResourceInstanceDeleteBecauseWrongRepetition:
|
|
|
|
r.ActionReason = "delete_because_wrong_repetition"
|
|
|
|
case plans.ResourceInstanceDeleteBecauseCountIndex:
|
|
|
|
r.ActionReason = "delete_because_count_index"
|
|
|
|
case plans.ResourceInstanceDeleteBecauseEachKey:
|
|
|
|
r.ActionReason = "delete_because_each_key"
|
|
|
|
case plans.ResourceInstanceDeleteBecauseNoModule:
|
|
|
|
r.ActionReason = "delete_because_no_module"
|
2021-04-28 21:02:34 +02:00
|
|
|
default:
|
2021-09-13 23:30:16 +02:00
|
|
|
return nil, fmt.Errorf("resource %s has an unsupported action reason %s", r.Address, rc.ActionReason)
|
2021-04-28 21:02:34 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 23:30:16 +02:00
|
|
|
ret = append(ret, r)
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-13 23:30:16 +02:00
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
return ret[i].Address < ret[j].Address
|
2019-01-12 00:13:55 +01:00
|
|
|
})
|
|
|
|
|
2021-09-13 23:30:16 +02:00
|
|
|
return ret, nil
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *plan) marshalOutputChanges(changes *plans.Changes) error {
|
|
|
|
if changes == nil {
|
|
|
|
// Nothing to do!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.OutputChanges = make(map[string]change, len(changes.Outputs))
|
|
|
|
for _, oc := range changes.Outputs {
|
|
|
|
changeV, err := oc.Decode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-12 17:10:03 +02:00
|
|
|
// We drop the marks from the change, as decoding is only an
|
|
|
|
// intermediate step to re-encode the values as json
|
|
|
|
changeV.Before, _ = changeV.Before.UnmarkDeep()
|
|
|
|
changeV.After, _ = changeV.After.UnmarkDeep()
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
var before, after []byte
|
2018-12-20 23:30:18 +01:00
|
|
|
afterUnknown := cty.False
|
2018-12-19 20:08:25 +01:00
|
|
|
if changeV.Before != cty.NilVal {
|
|
|
|
before, err = ctyjson.Marshal(changeV.Before, changeV.Before.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if changeV.After != cty.NilVal {
|
|
|
|
if changeV.After.IsWhollyKnown() {
|
|
|
|
after, err = ctyjson.Marshal(changeV.After, changeV.After.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-20 23:30:18 +01:00
|
|
|
} else {
|
|
|
|
afterUnknown = cty.True
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 00:21:40 +01:00
|
|
|
// The only information we have in the plan about output sensitivity is
|
|
|
|
// a boolean which is true if the output was or is marked sensitive. As
|
|
|
|
// a result, BeforeSensitive and AfterSensitive will be identical, and
|
|
|
|
// either false or true.
|
|
|
|
outputSensitive := cty.False
|
|
|
|
if oc.Sensitive {
|
|
|
|
outputSensitive = cty.True
|
|
|
|
}
|
|
|
|
sensitive, err := ctyjson.Marshal(outputSensitive, outputSensitive.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-20 23:30:18 +01:00
|
|
|
a, _ := ctyjson.Marshal(afterUnknown, afterUnknown.Type())
|
|
|
|
|
|
|
|
c := change{
|
2021-03-27 00:21:40 +01:00
|
|
|
Actions: actionString(oc.Action.String()),
|
|
|
|
Before: json.RawMessage(before),
|
|
|
|
After: json.RawMessage(after),
|
|
|
|
AfterUnknown: a,
|
|
|
|
BeforeSensitive: json.RawMessage(sensitive),
|
|
|
|
AfterSensitive: json.RawMessage(sensitive),
|
2018-12-20 23:30:18 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
p.OutputChanges[oc.Addr.OutputValue.Name] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *plan) marshalPlannedValues(changes *plans.Changes, schemas *terraform.Schemas) error {
|
|
|
|
// marshal the planned changes into a module
|
2018-12-20 23:30:18 +01:00
|
|
|
plan, err := marshalPlannedValues(changes, schemas)
|
2018-12-19 20:08:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.PlannedValues.RootModule = plan
|
|
|
|
|
|
|
|
// marshalPlannedOutputs
|
2018-12-20 23:30:18 +01:00
|
|
|
outputs, err := marshalPlannedOutputs(changes)
|
2018-12-19 20:08:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.PlannedValues.Outputs = outputs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
|
|
|
|
// omitUnknowns recursively walks the src cty.Value and returns a new cty.Value,
|
|
|
|
// omitting any unknowns.
|
2019-05-25 01:30:58 +02:00
|
|
|
//
|
|
|
|
// The result also normalizes some types: all sequence types are turned into
|
|
|
|
// tuple types and all mapping types are converted to object types, since we
|
|
|
|
// assume the result of this is just going to be serialized as JSON (and thus
|
|
|
|
// lose those distinctions) anyway.
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
func omitUnknowns(val cty.Value) cty.Value {
|
|
|
|
ty := val.Type()
|
|
|
|
switch {
|
|
|
|
case val.IsNull():
|
|
|
|
return val
|
|
|
|
case !val.IsKnown():
|
|
|
|
return cty.NilVal
|
2019-05-25 01:30:58 +02:00
|
|
|
case ty.IsPrimitiveType():
|
|
|
|
return val
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
case ty.IsListType() || ty.IsTupleType() || ty.IsSetType():
|
|
|
|
var vals []cty.Value
|
|
|
|
it := val.ElementIterator()
|
|
|
|
for it.Next() {
|
|
|
|
_, v := it.Element()
|
|
|
|
newVal := omitUnknowns(v)
|
|
|
|
if newVal != cty.NilVal {
|
|
|
|
vals = append(vals, newVal)
|
|
|
|
} else if newVal == cty.NilVal && ty.IsListType() {
|
|
|
|
// list length may be significant, so we will turn unknowns into nulls
|
|
|
|
vals = append(vals, cty.NullVal(v.Type()))
|
|
|
|
}
|
|
|
|
}
|
2019-05-25 01:30:58 +02:00
|
|
|
// We use tuple types always here, because the work we did above
|
|
|
|
// may have caused the individual elements to have different types,
|
|
|
|
// and we're doing this work to produce JSON anyway and JSON marshalling
|
|
|
|
// represents all of these sequence types as an array.
|
|
|
|
return cty.TupleVal(vals)
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
case ty.IsMapType() || ty.IsObjectType():
|
|
|
|
vals := make(map[string]cty.Value)
|
|
|
|
it := val.ElementIterator()
|
|
|
|
for it.Next() {
|
|
|
|
k, v := it.Element()
|
|
|
|
newVal := omitUnknowns(v)
|
|
|
|
if newVal != cty.NilVal {
|
|
|
|
vals[k.AsString()] = newVal
|
|
|
|
}
|
|
|
|
}
|
2019-05-25 01:30:58 +02:00
|
|
|
// We use object types always here, because the work we did above
|
|
|
|
// may have caused the individual elements to have different types,
|
|
|
|
// and we're doing this work to produce JSON anyway and JSON marshalling
|
|
|
|
// represents both of these mapping types as an object.
|
|
|
|
return cty.ObjectVal(vals)
|
|
|
|
default:
|
|
|
|
// Should never happen, since the above should cover all types
|
|
|
|
panic(fmt.Sprintf("omitUnknowns cannot handle %#v", val))
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 01:30:58 +02:00
|
|
|
// recursively iterate through a cty.Value, replacing unknown values (including
|
|
|
|
// null) with cty.True and known values with cty.False.
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
//
|
2019-05-25 01:30:58 +02:00
|
|
|
// The result also normalizes some types: all sequence types are turned into
|
|
|
|
// tuple types and all mapping types are converted to object types, since we
|
|
|
|
// assume the result of this is just going to be serialized as JSON (and thus
|
|
|
|
// lose those distinctions) anyway.
|
2021-03-25 16:41:49 +01:00
|
|
|
//
|
|
|
|
// For map/object values, all known attribute values will be omitted instead of
|
|
|
|
// returning false, as this results in a more compact serialization.
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
func unknownAsBool(val cty.Value) cty.Value {
|
|
|
|
ty := val.Type()
|
|
|
|
switch {
|
|
|
|
case val.IsNull():
|
|
|
|
return cty.False
|
|
|
|
case !val.IsKnown():
|
|
|
|
if ty.IsPrimitiveType() || ty.Equals(cty.DynamicPseudoType) {
|
|
|
|
return cty.True
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case ty.IsPrimitiveType():
|
|
|
|
return cty.BoolVal(!val.IsKnown())
|
|
|
|
case ty.IsListType() || ty.IsTupleType() || ty.IsSetType():
|
|
|
|
length := val.LengthInt()
|
|
|
|
if length == 0 {
|
|
|
|
// If there are no elements then we can't have unknowns
|
2019-05-25 01:30:58 +02:00
|
|
|
return cty.EmptyTupleVal
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
|
|
|
vals := make([]cty.Value, 0, length)
|
|
|
|
it := val.ElementIterator()
|
|
|
|
for it.Next() {
|
|
|
|
_, v := it.Element()
|
|
|
|
vals = append(vals, unknownAsBool(v))
|
|
|
|
}
|
2019-05-25 01:30:58 +02:00
|
|
|
// The above transform may have changed the types of some of the
|
|
|
|
// elements, so we'll always use a tuple here in case we've now made
|
|
|
|
// different elements have different types. Our ultimate goal is to
|
|
|
|
// marshal to JSON anyway, and all of these sequence types are
|
|
|
|
// indistinguishable in JSON.
|
|
|
|
return cty.TupleVal(vals)
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
case ty.IsMapType() || ty.IsObjectType():
|
|
|
|
var length int
|
|
|
|
switch {
|
|
|
|
case ty.IsMapType():
|
|
|
|
length = val.LengthInt()
|
|
|
|
default:
|
|
|
|
length = len(val.Type().AttributeTypes())
|
|
|
|
}
|
|
|
|
if length == 0 {
|
|
|
|
// If there are no elements then we can't have unknowns
|
2019-05-25 01:30:58 +02:00
|
|
|
return cty.EmptyObjectVal
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
|
|
|
vals := make(map[string]cty.Value)
|
|
|
|
it := val.ElementIterator()
|
|
|
|
for it.Next() {
|
|
|
|
k, v := it.Element()
|
2019-05-25 01:30:58 +02:00
|
|
|
vAsBool := unknownAsBool(v)
|
2021-03-25 16:41:49 +01:00
|
|
|
// Omit all of the "false"s for known values for more compact
|
|
|
|
// serialization
|
|
|
|
if !vAsBool.RawEquals(cty.False) {
|
2019-05-25 01:30:58 +02:00
|
|
|
vals[k.AsString()] = unknownAsBool(v)
|
|
|
|
}
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
2019-05-25 01:30:58 +02:00
|
|
|
// The above transform may have changed the types of some of the
|
|
|
|
// elements, so we'll always use an object here in case we've now made
|
|
|
|
// different elements have different types. Our ultimate goal is to
|
|
|
|
// marshal to JSON anyway, and all of these mapping types are
|
|
|
|
// indistinguishable in JSON.
|
|
|
|
return cty.ObjectVal(vals)
|
|
|
|
default:
|
|
|
|
// Should never happen, since the above should cover all types
|
|
|
|
panic(fmt.Sprintf("unknownAsBool cannot handle %#v", val))
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func actionString(action string) []string {
|
|
|
|
switch {
|
|
|
|
case action == "NoOp":
|
|
|
|
return []string{"no-op"}
|
|
|
|
case action == "Create":
|
|
|
|
return []string{"create"}
|
|
|
|
case action == "Delete":
|
|
|
|
return []string{"delete"}
|
|
|
|
case action == "Update":
|
|
|
|
return []string{"update"}
|
|
|
|
case action == "CreateThenDelete":
|
|
|
|
return []string{"create", "delete"}
|
|
|
|
case action == "Read":
|
|
|
|
return []string{"read"}
|
|
|
|
case action == "DeleteThenCreate":
|
|
|
|
return []string{"delete", "create"}
|
|
|
|
default:
|
|
|
|
return []string{action}
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 22:51:51 +02:00
|
|
|
|
|
|
|
// encodePaths lossily encodes a cty.PathSet into an array of arrays of step
|
|
|
|
// values, such as:
|
|
|
|
//
|
|
|
|
// [["length"],["triggers",0,"value"]]
|
|
|
|
//
|
|
|
|
// The lossiness is that we cannot distinguish between an IndexStep with string
|
|
|
|
// key and a GetAttr step. This is fine with JSON output, because JSON's type
|
|
|
|
// system means that those two steps are equivalent anyway: both are object
|
|
|
|
// indexes.
|
|
|
|
//
|
|
|
|
// JavaScript (or similar dynamic language) consumers of these values can
|
|
|
|
// recursively apply the steps to a given object using an index operation for
|
|
|
|
// each step.
|
|
|
|
func encodePaths(pathSet cty.PathSet) (json.RawMessage, error) {
|
|
|
|
if pathSet.Empty() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pathList := pathSet.List()
|
|
|
|
jsonPaths := make([]json.RawMessage, 0, len(pathList))
|
|
|
|
|
|
|
|
for _, path := range pathList {
|
|
|
|
steps := make([]json.RawMessage, 0, len(path))
|
|
|
|
for _, step := range path {
|
|
|
|
switch s := step.(type) {
|
|
|
|
case cty.IndexStep:
|
|
|
|
key, err := ctyjson.Marshal(s.Key, s.Key.Type())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to marshal index step key %#v: %s", s.Key, err)
|
|
|
|
}
|
|
|
|
steps = append(steps, key)
|
|
|
|
case cty.GetAttrStep:
|
|
|
|
name, err := json.Marshal(s.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to marshal get attr step name %#v: %s", s.Name, err)
|
|
|
|
}
|
|
|
|
steps = append(steps, name)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported path step %#v (%t)", step, step)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jsonPath, err := json.Marshal(steps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jsonPaths = append(jsonPaths, jsonPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(jsonPaths)
|
|
|
|
}
|