2019-01-09 17:59:11 +01:00
|
|
|
package jsonstate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2021-05-17 21:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2019-01-09 17:59:11 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalOutputs(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Outputs map[string]*states.OutputValue
|
|
|
|
Want map[string]output
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]*states.OutputValue{
|
|
|
|
"test": {
|
|
|
|
Sensitive: true,
|
|
|
|
Value: cty.StringVal("sekret"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]output{
|
|
|
|
"test": {
|
|
|
|
Sensitive: true,
|
|
|
|
Value: json.RawMessage(`"sekret"`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]*states.OutputValue{
|
|
|
|
"test": {
|
|
|
|
Sensitive: false,
|
|
|
|
Value: cty.StringVal("not_so_sekret"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]output{
|
|
|
|
"test": {
|
|
|
|
Sensitive: false,
|
|
|
|
Value: json.RawMessage(`"not_so_sekret"`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got, err := marshalOutputs(test.Outputs)
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
eq := reflect.DeepEqual(got, test.Want)
|
|
|
|
if !eq {
|
|
|
|
// printing the output isn't terribly useful, but it does help indicate which test case failed
|
|
|
|
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalAttributeValues(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-10-28 20:11:45 +01:00
|
|
|
Attr cty.Value
|
|
|
|
Want attributeValues
|
2019-01-09 17:59:11 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.NilVal,
|
|
|
|
nil,
|
|
|
|
},
|
2019-11-25 21:01:38 +01:00
|
|
|
{
|
|
|
|
cty.NullVal(cty.String),
|
|
|
|
nil,
|
|
|
|
},
|
2019-01-09 17:59:11 +01:00
|
|
|
{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
}),
|
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
|
|
|
attributeValues{"foo": json.RawMessage(`"bar"`)},
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"foo": cty.NullVal(cty.String),
|
|
|
|
}),
|
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
|
|
|
attributeValues{"foo": json.RawMessage(`null`)},
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"bar": cty.MapVal(map[string]cty.Value{
|
|
|
|
"hello": cty.StringVal("world"),
|
|
|
|
}),
|
|
|
|
"baz": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("goodnight"),
|
|
|
|
cty.StringVal("moon"),
|
|
|
|
}),
|
|
|
|
}),
|
2020-10-28 20:11:45 +01:00
|
|
|
attributeValues{
|
|
|
|
"bar": json.RawMessage(`{"hello":"world"}`),
|
|
|
|
"baz": json.RawMessage(`["goodnight","moon"]`),
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
2020-10-28 20:11:45 +01:00
|
|
|
},
|
|
|
|
// Marked values
|
|
|
|
{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"bar": cty.MapVal(map[string]cty.Value{
|
|
|
|
"hello": cty.StringVal("world"),
|
|
|
|
}),
|
|
|
|
"baz": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("goodnight"),
|
|
|
|
cty.StringVal("moon").Mark("sensitive"),
|
|
|
|
}),
|
|
|
|
}),
|
2019-01-09 17:59:11 +01:00
|
|
|
attributeValues{
|
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
|
|
|
"bar": json.RawMessage(`{"hello":"world"}`),
|
|
|
|
"baz": json.RawMessage(`["goodnight","moon"]`),
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2020-10-28 20:11:45 +01:00
|
|
|
got := marshalAttributeValues(test.Attr)
|
2019-01-09 17:59:11 +01:00
|
|
|
eq := reflect.DeepEqual(got, test.Want)
|
|
|
|
if !eq {
|
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
|
|
|
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
|
2019-01-09 17:59:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalResources(t *testing.T) {
|
2019-10-08 19:42:34 +02:00
|
|
|
deposedKey := states.NewDeposedKey()
|
|
|
|
tests := map[string]struct {
|
2019-01-09 17:59:11 +01:00
|
|
|
Resources map[string]*states.Resource
|
|
|
|
Schemas *terraform.Schemas
|
|
|
|
Want []resource
|
|
|
|
Err bool
|
|
|
|
}{
|
2019-10-08 19:42:34 +02:00
|
|
|
"nil": {
|
2019-01-09 17:59:11 +01:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
2019-10-08 19:42:34 +02:00
|
|
|
"single resource": {
|
|
|
|
map[string]*states.Resource{
|
|
|
|
"test_thing.baz": {
|
2020-03-16 21:50:48 +01:00
|
|
|
Addr: addrs.AbsResource{
|
|
|
|
Resource: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2020-03-05 14:13:45 +01:00
|
|
|
},
|
|
|
|
Instances: map[addrs.InstanceKey]*states.ResourceInstance{
|
|
|
|
addrs.NoKey: {
|
|
|
|
Current: &states.ResourceInstanceObjectSrc{
|
2021-02-23 16:19:24 +01:00
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
2020-03-05 14:13:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ProviderConfig: addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-03-05 14:13:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testSchemas(),
|
|
|
|
[]resource{
|
|
|
|
resource{
|
2021-02-23 16:19:24 +01:00
|
|
|
Address: "test_thing.bar",
|
|
|
|
Mode: "managed",
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
Index: addrs.InstanceKey(nil),
|
|
|
|
ProviderName: "registry.terraform.io/hashicorp/test",
|
2020-03-05 14:13:45 +01:00
|
|
|
AttributeValues: attributeValues{
|
|
|
|
"foozles": json.RawMessage(`null`),
|
|
|
|
"woozles": json.RawMessage(`"confuzles"`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2021-02-23 16:19:24 +01:00
|
|
|
"single resource wrong schema": {
|
|
|
|
map[string]*states.Resource{
|
|
|
|
"test_thing.baz": {
|
|
|
|
Addr: addrs.AbsResource{
|
|
|
|
Resource: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Instances: map[addrs.InstanceKey]*states.ResourceInstance{
|
|
|
|
addrs.NoKey: {
|
|
|
|
Current: &states.ResourceInstanceObjectSrc{
|
|
|
|
SchemaVersion: 1,
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":["confuzles"]}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ProviderConfig: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testSchemas(),
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2020-03-05 14:13:45 +01:00
|
|
|
"resource with count": {
|
|
|
|
map[string]*states.Resource{
|
|
|
|
"test_thing.bar": {
|
2020-03-16 21:50:48 +01:00
|
|
|
Addr: addrs.AbsResource{
|
|
|
|
Resource: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2019-10-08 19:42:34 +02:00
|
|
|
},
|
|
|
|
Instances: map[addrs.InstanceKey]*states.ResourceInstance{
|
|
|
|
addrs.IntKey(0): {
|
|
|
|
Current: &states.ResourceInstanceObjectSrc{
|
2021-02-23 16:19:24 +01:00
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
2019-10-08 19:42:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderConfig: addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-10-08 19:42:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
testSchemas(),
|
|
|
|
[]resource{
|
|
|
|
resource{
|
2021-02-23 16:19:24 +01:00
|
|
|
Address: "test_thing.bar[0]",
|
|
|
|
Mode: "managed",
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
Index: addrs.IntKey(0),
|
|
|
|
ProviderName: "registry.terraform.io/hashicorp/test",
|
2019-10-08 19:42:34 +02:00
|
|
|
AttributeValues: attributeValues{
|
|
|
|
"foozles": json.RawMessage(`null`),
|
|
|
|
"woozles": json.RawMessage(`"confuzles"`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2020-03-05 14:13:45 +01:00
|
|
|
"resource with for_each": {
|
|
|
|
map[string]*states.Resource{
|
|
|
|
"test_thing.bar": {
|
2020-03-16 21:50:48 +01:00
|
|
|
Addr: addrs.AbsResource{
|
|
|
|
Resource: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2020-03-05 14:13:45 +01:00
|
|
|
},
|
|
|
|
Instances: map[addrs.InstanceKey]*states.ResourceInstance{
|
|
|
|
addrs.StringKey("rockhopper"): {
|
|
|
|
Current: &states.ResourceInstanceObjectSrc{
|
2021-02-23 16:19:24 +01:00
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
2020-03-05 14:13:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ProviderConfig: addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-03-05 14:13:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testSchemas(),
|
|
|
|
[]resource{
|
|
|
|
resource{
|
2021-02-23 16:19:24 +01:00
|
|
|
Address: "test_thing.bar[\"rockhopper\"]",
|
|
|
|
Mode: "managed",
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
Index: addrs.StringKey("rockhopper"),
|
|
|
|
ProviderName: "registry.terraform.io/hashicorp/test",
|
2020-03-05 14:13:45 +01:00
|
|
|
AttributeValues: attributeValues{
|
|
|
|
"foozles": json.RawMessage(`null`),
|
|
|
|
"woozles": json.RawMessage(`"confuzles"`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2019-10-08 19:42:34 +02:00
|
|
|
"deposed resource": {
|
|
|
|
map[string]*states.Resource{
|
|
|
|
"test_thing.baz": {
|
2020-03-16 21:50:48 +01:00
|
|
|
Addr: addrs.AbsResource{
|
|
|
|
Resource: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2019-10-08 19:42:34 +02:00
|
|
|
},
|
|
|
|
Instances: map[addrs.InstanceKey]*states.ResourceInstance{
|
2020-03-05 14:13:45 +01:00
|
|
|
addrs.NoKey: {
|
2019-10-08 19:42:34 +02:00
|
|
|
Deposed: map[states.DeposedKey]*states.ResourceInstanceObjectSrc{
|
|
|
|
states.DeposedKey(deposedKey): &states.ResourceInstanceObjectSrc{
|
2021-02-23 16:19:24 +01:00
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
2019-10-08 19:42:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderConfig: addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-10-08 19:42:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
testSchemas(),
|
|
|
|
[]resource{
|
|
|
|
resource{
|
|
|
|
Address: "test_thing.bar",
|
|
|
|
Mode: "managed",
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
2020-03-05 14:13:45 +01:00
|
|
|
Index: addrs.InstanceKey(nil),
|
2020-04-02 18:58:44 +02:00
|
|
|
ProviderName: "registry.terraform.io/hashicorp/test",
|
2019-10-08 19:42:34 +02:00
|
|
|
DeposedKey: deposedKey.String(),
|
|
|
|
AttributeValues: attributeValues{
|
|
|
|
"foozles": json.RawMessage(`null`),
|
|
|
|
"woozles": json.RawMessage(`"confuzles"`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"deposed and current resource": {
|
2019-01-09 17:59:11 +01:00
|
|
|
map[string]*states.Resource{
|
|
|
|
"test_thing.baz": {
|
2020-03-16 21:50:48 +01:00
|
|
|
Addr: addrs.AbsResource{
|
|
|
|
Resource: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
Instances: map[addrs.InstanceKey]*states.ResourceInstance{
|
2020-03-05 14:13:45 +01:00
|
|
|
addrs.NoKey: {
|
2019-10-08 19:42:34 +02:00
|
|
|
Deposed: map[states.DeposedKey]*states.ResourceInstanceObjectSrc{
|
|
|
|
states.DeposedKey(deposedKey): &states.ResourceInstanceObjectSrc{
|
2021-02-23 16:19:24 +01:00
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
2019-10-08 19:42:34 +02:00
|
|
|
},
|
|
|
|
},
|
2019-01-09 17:59:11 +01:00
|
|
|
Current: &states.ResourceInstanceObjectSrc{
|
2021-02-23 16:19:24 +01:00
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderConfig: addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
testSchemas(),
|
|
|
|
[]resource{
|
|
|
|
resource{
|
2021-02-23 16:19:24 +01:00
|
|
|
Address: "test_thing.bar",
|
|
|
|
Mode: "managed",
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
Index: addrs.InstanceKey(nil),
|
|
|
|
ProviderName: "registry.terraform.io/hashicorp/test",
|
2019-01-09 17:59:11 +01:00
|
|
|
AttributeValues: attributeValues{
|
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
|
|
|
"foozles": json.RawMessage(`null`),
|
|
|
|
"woozles": json.RawMessage(`"confuzles"`),
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
},
|
2019-10-08 19:42:34 +02:00
|
|
|
resource{
|
|
|
|
Address: "test_thing.bar",
|
|
|
|
Mode: "managed",
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
2020-03-05 14:13:45 +01:00
|
|
|
Index: addrs.InstanceKey(nil),
|
2020-04-02 18:58:44 +02:00
|
|
|
ProviderName: "registry.terraform.io/hashicorp/test",
|
2019-10-08 19:42:34 +02:00
|
|
|
DeposedKey: deposedKey.String(),
|
|
|
|
AttributeValues: attributeValues{
|
|
|
|
"foozles": json.RawMessage(`null`),
|
|
|
|
"woozles": json.RawMessage(`"confuzles"`),
|
|
|
|
},
|
|
|
|
},
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:42:34 +02:00
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-03-05 14:13:45 +01:00
|
|
|
got, err := marshalResources(test.Resources, addrs.RootModuleInstance, test.Schemas)
|
2019-10-08 19:42:34 +02:00
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2019-01-09 17:59:11 +01:00
|
|
|
}
|
2019-10-08 19:42:34 +02:00
|
|
|
eq := reflect.DeepEqual(got, test.Want)
|
|
|
|
if !eq {
|
|
|
|
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
2019-01-09 17:59:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:57:14 +01:00
|
|
|
func TestMarshalModules_basic(t *testing.T) {
|
|
|
|
childModule, _ := addrs.ParseModuleInstanceStr("module.child")
|
|
|
|
subModule, _ := addrs.ParseModuleInstanceStr("module.submodule")
|
|
|
|
testState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-03-09 20:57:14 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(childModule),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: childModule.Module(),
|
2020-03-09 20:57:14 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(subModule),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: subModule.Module(),
|
2020-03-09 20:57:14 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
moduleMap := make(map[string][]addrs.ModuleInstance)
|
|
|
|
moduleMap[""] = []addrs.ModuleInstance{childModule, subModule}
|
|
|
|
|
|
|
|
got, err := marshalModules(testState, testSchemas(), moduleMap[""], moduleMap)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != 2 {
|
|
|
|
t.Fatalf("wrong result! got %d modules, expected 2", len(got))
|
|
|
|
}
|
|
|
|
|
|
|
|
if got[0].Address != "module.child" || got[1].Address != "module.submodule" {
|
|
|
|
t.Fatalf("wrong result! got %#v\n", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalModules_nested(t *testing.T) {
|
|
|
|
childModule, _ := addrs.ParseModuleInstanceStr("module.child")
|
|
|
|
subModule, _ := addrs.ParseModuleInstanceStr("module.child.module.submodule")
|
|
|
|
testState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-03-09 20:57:14 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(childModule),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: childModule.Module(),
|
2020-03-09 20:57:14 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(subModule),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: subModule.Module(),
|
2020-03-09 20:57:14 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
moduleMap := make(map[string][]addrs.ModuleInstance)
|
|
|
|
moduleMap[""] = []addrs.ModuleInstance{childModule}
|
|
|
|
moduleMap[childModule.String()] = []addrs.ModuleInstance{subModule}
|
|
|
|
|
|
|
|
got, err := marshalModules(testState, testSchemas(), moduleMap[""], moduleMap)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != 1 {
|
|
|
|
t.Fatalf("wrong result! got %d modules, expected 1", len(got))
|
|
|
|
}
|
|
|
|
|
|
|
|
if got[0].Address != "module.child" {
|
|
|
|
t.Fatalf("wrong result! got %#v\n", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got[0].ChildModules[0].Address != "module.child.module.submodule" {
|
|
|
|
t.Fatalf("wrong result! got %#v\n", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:31:20 +01:00
|
|
|
func TestMarshalModules_parent_no_resources(t *testing.T) {
|
|
|
|
subModule, _ := addrs.ParseModuleInstanceStr("module.child.module.submodule")
|
|
|
|
testState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(subModule),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: subModule.Module(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
got, err := marshalRootModule(testState, testSchemas())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got.ChildModules) != 1 {
|
|
|
|
t.Fatalf("wrong result! got %d modules, expected 1", len(got.ChildModules))
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ChildModules[0].Address != "module.child" {
|
|
|
|
t.Fatalf("wrong result! got %#v\n", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ChildModules[0].ChildModules[0].Address != "module.child.module.submodule" {
|
|
|
|
t.Fatalf("wrong result! got %#v\n", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 17:59:11 +01:00
|
|
|
func testSchemas() *terraform.Schemas {
|
|
|
|
return &terraform.Schemas{
|
2020-02-03 14:18:04 +01:00
|
|
|
Providers: map[addrs.Provider]*terraform.ProviderSchema{
|
2020-04-02 18:58:44 +02:00
|
|
|
addrs.NewDefaultProvider("test"): &terraform.ProviderSchema{
|
2019-01-09 17:59:11 +01:00
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_thing": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"woozles": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"foozles": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
2020-03-09 20:57:14 +01:00
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
"bar": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
2019-01-09 17:59:11 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|