From ae85b260266d605cea9b8dfcb4db5efebbd79934 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 7 Sep 2018 12:18:25 -0700 Subject: [PATCH] core: Move StateValueFromInstanceState shim from helper/schema This one doesn't depend on any helper/schema specific bits and it'll also be useful for the shims in our mock provider in core. --- helper/schema/shims.go | 25 ++++--------------------- terraform/state.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/helper/schema/shims.go b/helper/schema/shims.go index 89f6c6d7b4..cc19b0430b 100644 --- a/helper/schema/shims.go +++ b/helper/schema/shims.go @@ -3,12 +3,11 @@ package schema import ( "encoding/json" - "github.com/hashicorp/terraform/config/hcl2shim" - "github.com/hashicorp/terraform/configs/configschema" - "github.com/hashicorp/terraform/terraform" "github.com/zclconf/go-cty/cty" - ctyjson "github.com/zclconf/go-cty/cty/json" + + "github.com/hashicorp/terraform/configs/configschema" + "github.com/hashicorp/terraform/terraform" ) // DiffFromValues takes the current state and desired state as cty.Values and @@ -79,23 +78,7 @@ func JSONMapToStateValue(m map[string]interface{}, block *configschema.Block) (c // cty.Value as described by the provided cty.Type, and maintains the resource // ID as the "id" attribute. func StateValueFromInstanceState(is *terraform.InstanceState, ty cty.Type) (cty.Value, error) { - if is == nil { - // if the state is nil, we need to construct a complete cty.Value with - // null attributes, rather than a single cty.NullVal(ty) - is = &terraform.InstanceState{} - } - - // make sure ID is included in the attributes. The InstanceState.ID value - // takes precedent. - if is.Attributes == nil { - is.Attributes = map[string]string{} - } - - if is.ID != "" { - is.Attributes["id"] = is.ID - } - - return hcl2shim.HCL2ValueFromFlatmap(is.Attributes, ty) + return is.AttrsAsObjectValue(ty) } // InstanceStateFromStateValue converts a cty.Value to a diff --git a/terraform/state.go b/terraform/state.go index 2a11436829..bfb3490dff 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -1675,6 +1675,35 @@ func NewInstanceStateShimmedFromValue(state cty.Value, schemaVersion int) *Insta } } +// AttrsAsObjectValue shims from the legacy InstanceState representation to +// a new-style cty object value representation of the state attributes, using +// the given type for guidance. +// +// The given type must be the implied type of the schema of the resource type +// of the object whose state is being converted, or the result is undefined. +// +// This is for shimming from old components only and should not be used in +// new code. +func (s *InstanceState) AttrsAsObjectValue(ty cty.Type) (cty.Value, error) { + if s == nil { + // if the state is nil, we need to construct a complete cty.Value with + // null attributes, rather than a single cty.NullVal(ty) + s = &InstanceState{} + } + + if s.Attributes == nil { + s.Attributes = map[string]string{} + } + + // make sure ID is included in the attributes. The InstanceState.ID value + // takes precedence. + if s.ID != "" { + s.Attributes["id"] = s.ID + } + + return hcl2shim.HCL2ValueFromFlatmap(s.Attributes, ty) +} + // Copy all the Fields from another InstanceState func (s *InstanceState) Set(from *InstanceState) { s.Lock()