From e9eb8e04cc4aa75a7793f5e8543f373c68d74832 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 13 Apr 2020 16:37:59 -0400 Subject: [PATCH] add AbsOutputAddrs to state outputs We need all module instance outputs to build the objects for evaluation, but there is no need to copy all the resource instances along with that. This allows us to only return the output states, with enough information to connect them with their module instances. --- states/module.go | 6 ++++++ states/output_value.go | 2 ++ states/state_deepcopy.go | 1 + states/state_test.go | 16 ++++++++++++++++ states/statefile/version4.go | 8 +++++++- 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/states/module.go b/states/module.go index acce10129..ddfada9ae 100644 --- a/states/module.go +++ b/states/module.go @@ -259,6 +259,12 @@ func (ms *Module) maybeRestoreResourceInstanceDeposed(addr addrs.ResourceInstanc // existing value of the same name. func (ms *Module) SetOutputValue(name string, value cty.Value, sensitive bool) *OutputValue { os := &OutputValue{ + Addr: addrs.AbsOutputValue{ + Module: ms.Addr, + OutputValue: addrs.OutputValue{ + Name: name, + }, + }, Value: value, Sensitive: sensitive, } diff --git a/states/output_value.go b/states/output_value.go index d232b76d4..268420cf4 100644 --- a/states/output_value.go +++ b/states/output_value.go @@ -1,6 +1,7 @@ package states import ( + "github.com/hashicorp/terraform/addrs" "github.com/zclconf/go-cty/cty" ) @@ -9,6 +10,7 @@ import ( // It is not valid to mutate an OutputValue object once it has been created. // Instead, create an entirely new OutputValue to replace the previous one. type OutputValue struct { + Addr addrs.AbsOutputValue Value cty.Value Sensitive bool } diff --git a/states/state_deepcopy.go b/states/state_deepcopy.go index 1c5aee0a9..817e1c19d 100644 --- a/states/state_deepcopy.go +++ b/states/state_deepcopy.go @@ -226,6 +226,7 @@ func (os *OutputValue) DeepCopy() *OutputValue { } return &OutputValue{ + Addr: os.Addr, Value: os.Value, Sensitive: os.Sensitive, } diff --git a/states/state_test.go b/states/state_test.go index 2a38a9c53..8fe191d57 100644 --- a/states/state_test.go +++ b/states/state_test.go @@ -53,10 +53,20 @@ func TestState(t *testing.T) { }, OutputValues: map[string]*OutputValue{ "bar": { + Addr: addrs.AbsOutputValue{ + OutputValue: addrs.OutputValue{ + Name: "bar", + }, + }, Value: cty.StringVal("bar value"), Sensitive: false, }, "secret": { + Addr: addrs.AbsOutputValue{ + OutputValue: addrs.OutputValue{ + Name: "secret", + }, + }, Value: cty.StringVal("secret value"), Sensitive: true, }, @@ -92,6 +102,12 @@ func TestState(t *testing.T) { LocalValues: map[string]cty.Value{}, OutputValues: map[string]*OutputValue{ "pizza": { + Addr: addrs.AbsOutputValue{ + Module: addrs.RootModuleInstance.Child("child", addrs.NoKey), + OutputValue: addrs.OutputValue{ + Name: "pizza", + }, + }, Value: cty.StringVal("hawaiian"), Sensitive: false, }, diff --git a/states/statefile/version4.go b/states/statefile/version4.go index c49599d82..0cb0ae9b0 100644 --- a/states/statefile/version4.go +++ b/states/statefile/version4.go @@ -281,7 +281,13 @@ func prepareStateV4(sV4 *stateV4) (*File, tfdiags.Diagnostics) { { rootModule := state.RootModule() for name, fos := range sV4.RootOutputs { - os := &states.OutputValue{} + os := &states.OutputValue{ + Addr: addrs.AbsOutputValue{ + OutputValue: addrs.OutputValue{ + Name: name, + }, + }, + } os.Sensitive = fos.Sensitive ty, err := ctyjson.UnmarshalType([]byte(fos.ValueTypeRaw))