Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove v2 Instance State #2874

Draft
wants to merge 1 commit into
base: vvm/remove_v2_resource_and_resource_map
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/tfbridge/schema_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (f shimv2Factory) NewResource(r *schema.Resource) shim.Resource {
}

func (f shimv2Factory) NewInstanceState(id string) shim.InstanceState {
return shimv2.NewInstanceState(&terraformv2.InstanceState{
return shimv2.NewTestOnlyInstanceState(&terraformv2.InstanceState{
ID: id, Attributes: map[string]string{}, Meta: map[string]interface{}{},
})
}
Expand Down
118 changes: 0 additions & 118 deletions pkg/tfshim/sdk-v2/instance_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,11 @@ package sdkv2
import (
"bytes"
"encoding/json"
"strings"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
diff_reader "github.com/pulumi/terraform-diff-reader/sdk-v2"

shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
)

var _ = shim.InstanceState(v2InstanceState{})

type v2InstanceState struct {
resource *schema.Resource
tf *terraform.InstanceState
diff *terraform.InstanceDiff
}

func NewInstanceState(s *terraform.InstanceState) shim.InstanceState {
return v2InstanceState{tf: s}
}

func NewInstanceStateForResource(s *terraform.InstanceState, resource *schema.Resource) shim.InstanceState {
return v2InstanceState{
resource: resource,
tf: s,
diff: nil,
}
}

func IsInstanceState(s shim.InstanceState) (*terraform.InstanceState, bool) {
if is, ok := s.(v2InstanceState); ok {
return is.tf, true
}
return nil, false
}

func (s v2InstanceState) Type() string {
return s.tf.Ephemeral.Type
}

func (s v2InstanceState) ID() string {
return s.tf.ID
}

func (s v2InstanceState) Object(sch shim.SchemaMap) (map[string]interface{}, error) {
return s.objectV1(sch)
}

// This is needed because json.Unmarshal uses float64 for numbers by default which truncates int64 numbers.
func unmarshalJSON(data []byte, v interface{}) error {
dec := json.NewDecoder(bytes.NewReader(data))
Expand Down Expand Up @@ -94,76 +49,3 @@ func objectFromCtyValue(v cty.Value) map[string]interface{} {

return m
}

// The legacy version of Object used custom Pulumi code forked from TF sources.
func (s v2InstanceState) objectV1(sch shim.SchemaMap) (map[string]interface{}, error) {
obj := make(map[string]interface{})

schemaMap := map[string]*schema.Schema(sch.(v2SchemaMap))

attrs := s.tf.Attributes

var reader schema.FieldReader = &schema.MapFieldReader{
Schema: schemaMap,
Map: schema.BasicMapReader(attrs),
}

// If this is a state + a diff, use a diff reader rather than a map reader.
if s.diff != nil {
reader = &diff_reader.DiffFieldReader{
Diff: s.diff,
Schema: schemaMap,
Source: reader,
}
}

// Read each top-level field out of the attributes.
keys := make(map[string]bool)
readAttributeField := func(key string) error {
// Pull the top-level field out of this attribute key. If we've already read the top-level field, skip
// this key.
dot := strings.Index(key, ".")
if dot != -1 {
key = key[:dot]
}
if _, ok := keys[key]; ok {
return nil
}
keys[key] = true

// Read the top-level attribute for this key.
res, err := reader.ReadField([]string{key})
if err != nil {
return err
}
if res.Value != nil && !res.Computed {
obj[key] = res.Value
}
return nil
}

for key := range attrs {
if err := readAttributeField(key); err != nil {
return nil, err
}
}
if s.diff != nil {
for key := range s.diff.Attributes {
if err := readAttributeField(key); err != nil {
return nil, err
}
}
}

// Populate the "id" property if it is not set. Most schemas do not include this property, and leaving it out
// can cause unnecessary diffs when refreshing/updating resources after a provider upgrade.
if _, ok := obj["id"]; !ok {
obj["id"] = attrs["id"]
}

return obj, nil
}

func (s v2InstanceState) Meta() map[string]interface{} {
return s.tf.Meta
}
6 changes: 5 additions & 1 deletion pkg/tfshim/sdk-v2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func stateToShim(r *schema.Resource, s *terraform.InstanceState) shim.InstanceSt
if s == nil {
return nil
}
return NewInstanceStateForResource(s, r)
return &v2InstanceState2{
resourceType: s.Ephemeral.Type,
stateValue: s.RawState,
meta: s.Meta,
}
}

func diffFromShim(d shim.InstanceDiff) *terraform.InstanceDiff {
Expand Down
10 changes: 10 additions & 0 deletions pkg/tfshim/sdk-v2/provider2.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ func (r *v2Resource2) DecodeTimeouts(config shim.ResourceConfig) (*shim.Resource
}, nil
}

// NewTestOnlyInstanceState is a test-only constructor for v2InstanceState2.
// New tests should avoid using this and instead construct a v2 Provider with a TF schema.
func NewTestOnlyInstanceState(s *terraform.InstanceState) shim.InstanceState {
return &v2InstanceState2{
resourceType: s.Ephemeral.Type,
stateValue: s.RawState,
meta: s.Meta,
}
}

type v2InstanceState2 struct {
resourceType string
stateValue cty.Value
Expand Down
Loading