From 28adf4ddc03131204651a968f97a97d309181640 Mon Sep 17 00:00:00 2001 From: Venelin Date: Tue, 28 Jan 2025 14:32:18 +0000 Subject: [PATCH] Remove v2 Instance State --- pkg/tfbridge/schema_provider_test.go | 2 +- pkg/tfshim/sdk-v2/instance_state.go | 118 --------------------------- pkg/tfshim/sdk-v2/provider.go | 6 +- pkg/tfshim/sdk-v2/provider2.go | 10 +++ 4 files changed, 16 insertions(+), 120 deletions(-) diff --git a/pkg/tfbridge/schema_provider_test.go b/pkg/tfbridge/schema_provider_test.go index 75808bb0a..692f5aa16 100644 --- a/pkg/tfbridge/schema_provider_test.go +++ b/pkg/tfbridge/schema_provider_test.go @@ -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{}{}, }) } diff --git a/pkg/tfshim/sdk-v2/instance_state.go b/pkg/tfshim/sdk-v2/instance_state.go index b4def4cf1..67af80a5c 100644 --- a/pkg/tfshim/sdk-v2/instance_state.go +++ b/pkg/tfshim/sdk-v2/instance_state.go @@ -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)) @@ -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 -} diff --git a/pkg/tfshim/sdk-v2/provider.go b/pkg/tfshim/sdk-v2/provider.go index 36a52ca00..98bbdb0ef 100644 --- a/pkg/tfshim/sdk-v2/provider.go +++ b/pkg/tfshim/sdk-v2/provider.go @@ -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 { diff --git a/pkg/tfshim/sdk-v2/provider2.go b/pkg/tfshim/sdk-v2/provider2.go index 93104413d..6118fabc4 100644 --- a/pkg/tfshim/sdk-v2/provider2.go +++ b/pkg/tfshim/sdk-v2/provider2.go @@ -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