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.
This commit is contained in:
Martin Atkins 2018-09-07 12:18:25 -07:00
parent 8003b3408f
commit 33151f5011
2 changed files with 33 additions and 21 deletions

View File

@ -3,12 +3,11 @@ package schema
import ( import (
"encoding/json" "encoding/json"
"github.com/hashicorp/terraform/config/hcl2shim" "github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
) )
// DiffFromValues takes the current state and desired state as cty.Values and // 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 // cty.Value as described by the provided cty.Type, and maintains the resource
// ID as the "id" attribute. // ID as the "id" attribute.
func StateValueFromInstanceState(is *terraform.InstanceState, ty cty.Type) (cty.Value, error) { func StateValueFromInstanceState(is *terraform.InstanceState, ty cty.Type) (cty.Value, error) {
if is == nil { return is.AttrsAsObjectValue(ty)
// 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)
} }
// InstanceStateFromStateValue converts a cty.Value to a // InstanceStateFromStateValue converts a cty.Value to a

View File

@ -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 // Copy all the Fields from another InstanceState
func (s *InstanceState) Set(from *InstanceState) { func (s *InstanceState) Set(from *InstanceState) {
s.Lock() s.Lock()