Skip to content

Commit

Permalink
Remove v2Resource and v2ResourceMap
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Jan 28, 2025
1 parent 2513b14 commit 8947c33
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 180 deletions.
2 changes: 1 addition & 1 deletion pkg/tfbridge/schema_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (f shimv2Factory) newResource(r shim.Resource) *schemav2.Resource {
}

func (f shimv2Factory) NewResource(r *schema.Resource) shim.Resource {
return shimv2.NewResource(f.newResource(r.Shim()))
return shimv2.NewTestOnlyResource(f.newResource(r.Shim()))
}

func (f shimv2Factory) NewInstanceState(id string) shim.InstanceState {
Expand Down
6 changes: 2 additions & 4 deletions pkg/tfbridge/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3057,7 +3057,6 @@ func TestMakeTerraformInputsOnMapNestedObjects(t *testing.T) {
},
}

shimmedR := shimv2.NewResource(r)
type testCase struct {
name string
ps map[string]*SchemaInfo
Expand Down Expand Up @@ -3132,7 +3131,7 @@ func TestMakeTerraformInputsOnMapNestedObjects(t *testing.T) {
tc := tc

t.Run(tc.name, func(t *testing.T) {
i, _, err := makeTerraformInputsForConfig(tc.olds, tc.news, shimmedR.Schema(), tc.ps)
i, _, err := makeTerraformInputsForConfig(tc.olds, tc.news, shimv2.NewSchemaMap(r.Schema), tc.ps)
require.NoError(t, err)
require.Equal(t, tc.expect, i)
})
Expand Down Expand Up @@ -3161,7 +3160,6 @@ func TestRegress940(t *testing.T) {
},
},
}
shimmedR := shimv2.NewResource(r)

var olds, news resource.PropertyMap

Expand All @@ -3175,7 +3173,7 @@ func TestRegress940(t *testing.T) {
}),
}

result, _, err := makeTerraformInputsForConfig(olds, news, shimmedR.Schema(), map[string]*SchemaInfo{})
result, _, err := makeTerraformInputsForConfig(olds, news, shimv2.NewSchemaMap(r.Schema), map[string]*SchemaInfo{})

t.Run("no error with empty keys", func(t *testing.T) {
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/tfshim/sdk-v2/instance_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestToInstanceState(t *testing.T) {
t.Parallel()
res := NewResource(&schema.Resource{
res := newElemResource(&schema.Resource{
Schema: map[string]*schema.Schema{
"nil_property_value": {Type: schema.TypeMap},
"bool_property_value": {Type: schema.TypeBool},
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestToInstanceState(t *testing.T) {
expected := writer.Map()

// Build the same using makeTerraformAttributesFromInputs.
res = NewResource(&schema.Resource{Schema: sharedSchema})
res = newElemResource(&schema.Resource{Schema: sharedSchema})
state, err = res.InstanceState("id", sharedInputs, nil)
assert.NoError(t, err)
assert.Equal(t, expected, state.(v2InstanceState).tf.Attributes)
Expand All @@ -165,7 +165,7 @@ func TestToInstanceState(t *testing.T) {
// Test that an unset list still generates a length attribute.
func TestEmptyListAttribute(t *testing.T) {
t.Parallel()
res := NewResource(&schema.Resource{
res := newElemResource(&schema.Resource{
Schema: map[string]*schema.Schema{
"list_property": {Type: schema.TypeList, Optional: true},
},
Expand All @@ -180,7 +180,7 @@ func TestEmptyListAttribute(t *testing.T) {

func TestObjectFromInstanceDiff(t *testing.T) {
t.Parallel()
res := NewResource(&schema.Resource{
res := newElemResource(&schema.Resource{
Schema: map[string]*schema.Schema{
"nil_property_value": {Type: schema.TypeMap},
"bool_property_value": {Type: schema.TypeBool},
Expand Down
48 changes: 39 additions & 9 deletions pkg/tfshim/sdk-v2/provider2.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ type v2Resource2 struct {
resourceType string
}

// NewTestOnlyResource is a test-only constructor for v2Resource2.
// New tests should avoid using this and instead construct a v2 Provider with a TF schema.
func NewTestOnlyResource(r *schema.Resource) shim.Resource {
return &v2Resource2{r, nil, ""}
}

func newElemResource(r *schema.Resource) shim.Resource {
return &v2Resource2{r, nil, ""}
}

var _ shim.Resource = (*v2Resource2)(nil)

func (r *v2Resource2) Schema() shim.SchemaMap {
Expand Down Expand Up @@ -82,11 +92,31 @@ func (r *v2Resource2) DeprecationMessage() string {
}

func (r *v2Resource2) Timeouts() *shim.ResourceTimeout {
return v2Resource{r.tf}.Timeouts()
if r.tf.Timeouts == nil {
return nil
}
return &shim.ResourceTimeout{
Create: r.tf.Timeouts.Create,
Read: r.tf.Timeouts.Read,
Update: r.tf.Timeouts.Update,
Delete: r.tf.Timeouts.Delete,
Default: r.tf.Timeouts.Default,
}
}

func (r *v2Resource2) DecodeTimeouts(config shim.ResourceConfig) (*shim.ResourceTimeout, error) {
return v2Resource{r.tf}.DecodeTimeouts(config)
v2Timeouts := &schema.ResourceTimeout{}
if err := v2Timeouts.ConfigDecode(r.tf, configFromShim(config)); err != nil {
return nil, err
}

return &shim.ResourceTimeout{
Create: v2Timeouts.Create,
Read: v2Timeouts.Read,
Update: v2Timeouts.Update,
Delete: v2Timeouts.Delete,
Default: v2Timeouts.Default,
}, nil
}

type v2InstanceState2 struct {
Expand Down Expand Up @@ -260,7 +290,12 @@ func (p *planResourceChangeImpl) ResourcesMap() shim.ResourceMap {
}

func (p *planResourceChangeImpl) DataSourcesMap() shim.ResourceMap {
return v2ResourceMap(p.tf.DataSourcesMap)
return &v2ResourceCustomMap{
resources: p.tf.DataSourcesMap,
pack: func(token string, res *schema.Resource) shim.Resource {
return &v2Resource2{res, nil, token}
},
}
}

func (p *planResourceChangeImpl) InternalValidate() error {
Expand Down Expand Up @@ -1038,12 +1073,7 @@ func (m *v2ResourceCustomMap) Range(each func(key string, value shim.Resource) b
}

func (m *v2ResourceCustomMap) Set(key string, value shim.Resource) {
switch r := value.(type) {
case v2Resource:
m.resources[key] = r.tf
case *v2Resource2:
m.resources[key] = r.tf
}
m.resources[key] = value.(*v2Resource2).tf
}

func NewProvider(p *schema.Provider, opts ...providerOption) shim.Provider {
Expand Down
161 changes: 0 additions & 161 deletions pkg/tfshim/sdk-v2/resource.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/tfshim/sdk-v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s v2Schema) StateFunc() shim.SchemaStateFunc {
func (s v2Schema) Elem() interface{} {
switch e := s.tf.Elem.(type) {
case *schema.Resource:
return v2Resource{e}
return newElemResource(e)
case *schema.Schema:
return v2Schema{e}
default:
Expand Down

0 comments on commit 8947c33

Please sign in to comment.