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 usage of plugin framework types in favor of interface types #2184

Merged
merged 3 commits into from
Jul 17, 2024
Merged
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
4 changes: 2 additions & 2 deletions pf/internal/schemashim/convert_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"

pfattr "github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"

shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
Expand All @@ -34,7 +34,7 @@ func convertType(typ pfattr.Type) (shim.ValueType, error) {
switch {
case is(tftypes.Bool):
return shim.TypeBool, nil
case typ.Equal(types.Int64Type):
case typ.Equal(basetypes.Int64Type{}):
// We special case int, since it is a stable type but not present on the wire.
return shim.TypeInt, nil
case is(tftypes.Number):
Expand Down
5 changes: 2 additions & 3 deletions pf/internal/schemashim/custom_object_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/stretchr/testify/assert"
Expand All @@ -35,12 +34,12 @@ func TestCustomObject(t *testing.T) {

obj := newObjectPseudoResource(NewObjectTypeOf[SomeType](ctx), nil, nil)

s := obj.Schema().Get("s")
s := obj.Schema().Get("sample_string")
assert.Equal(t, shim.TypeString, s.Type())
}

type SomeType struct {
S types.String `tfsdk:"s"`
SampleString basetypes.StringValue `tfsdk:"sample_string"`
}

// --- custom object machinery ---
Expand Down
19 changes: 9 additions & 10 deletions pf/internal/schemashim/custom_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/stretchr/testify/assert"

Expand All @@ -31,7 +30,7 @@ import (
// This example is taken from aws:resourceexplorer/index:Index "timeouts" property.
func TestCustomTypeEmbeddingObjectType(t *testing.T) {
type timeoutsType struct {
types.ObjectType
basetypes.ObjectType
}

raw := schema.SingleNestedBlock{
Expand All @@ -41,11 +40,11 @@ func TestCustomTypeEmbeddingObjectType(t *testing.T) {
"update": schema.StringAttribute{},
},
CustomType: timeoutsType{
ObjectType: types.ObjectType{
ObjectType: basetypes.ObjectType{
AttrTypes: map[string]attr.Type{
"create": types.StringType,
"read": types.StringType,
"update": types.StringType,
"create": basetypes.StringType{},
"read": basetypes.StringType{},
"update": basetypes.StringType{},
},
},
},
Expand All @@ -65,7 +64,7 @@ func TestCustomListType(t *testing.T) {
ctx := context.Background()

raw := schema.ListNestedBlock{
CustomType: newListNestedObjectTypeOf[searchFilterModel](ctx, types.ObjectType{
CustomType: newListNestedObjectTypeOf[searchFilterModel](ctx, basetypes.ObjectType{
AttrTypes: map[string]attr.Type{
"filter_string": basetypes.StringType{},
},
Expand Down Expand Up @@ -93,7 +92,7 @@ func TestCustomListAttribute(t *testing.T) {
ctx := context.Background()

raw := schema.ListNestedAttribute{
CustomType: newListNestedObjectTypeOf[searchFilterModel](ctx, types.ObjectType{
CustomType: newListNestedObjectTypeOf[searchFilterModel](ctx, basetypes.ObjectType{
AttrTypes: map[string]attr.Type{
"filter_string": basetypes.StringType{},
},
Expand Down Expand Up @@ -121,7 +120,7 @@ func TestCustomSetType(t *testing.T) {
ctx := context.Background()

raw := schema.SetNestedBlock{
CustomType: newSetNestedObjectTypeOf[searchFilterModel](ctx, types.ObjectType{
CustomType: newSetNestedObjectTypeOf[searchFilterModel](ctx, basetypes.ObjectType{
AttrTypes: map[string]attr.Type{
"filter_string": basetypes.StringType{},
},
Expand All @@ -146,7 +145,7 @@ func TestCustomSetType(t *testing.T) {
}

type searchFilterModel struct {
FilterString types.String `tfsdk:"filter_string"`
FilterString basetypes.StringType `tfsdk:"filter_string"`
}

type listNestedObjectTypeOf[T any] struct {
Expand Down
6 changes: 3 additions & 3 deletions pf/internal/schemashim/object_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -33,7 +33,7 @@ import (
func TestObjectAttribute(t *testing.T) {
objectAttr := schema.ObjectAttribute{
AttributeTypes: map[string]attr.Type{
"s": types.StringType,
"s": basetypes.StringType{},
},
}
shimmed := &attrSchema{"key", pfutils.FromAttrLike(objectAttr)}
Expand All @@ -44,7 +44,7 @@ func TestObjectAttribute(t *testing.T) {

func TestTypeSchemaDescriptionIsEmpty(t *testing.T) {
shimmedType := &typeSchema{
t: types.StringType,
t: basetypes.StringType{},
nested: nil,
}
assert.Equal(t, shimmedType.Description(), "")
Expand Down
11 changes: 3 additions & 8 deletions pf/internal/schemashim/type_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package schemashim

import (
pfattr "github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

Expand Down Expand Up @@ -62,16 +61,12 @@ func (s *typeSchema) Elem() interface{} {
case basetypes.ObjectTypable:
var pseudoResource shim.Resource = newObjectPseudoResource(tt, s.nested, nil)
return pseudoResource
case basetypes.SetTypable, basetypes.ListTypable:
case basetypes.SetTypable, basetypes.ListTypable, basetypes.MapTypable:
typeWithElementType, ok := s.t.(pfattr.TypeWithElementType)
contract.Assertf(ok, "List or Set type %T expect to implement TypeWithElementType", s.t)
contract.Assertf(ok, "List, Set or Map type %T expect to implement TypeWithElementType", s.t)
contract.Assertf(s.nested == nil || len(s.nested) == 0,
"s.t==SetTypable should not have any s.nested attrs")
"s.t==%T should not have any s.nested attrs", s.t)
return newTypeSchema(typeWithElementType.ElementType(), nil)
case types.MapType:
contract.Assertf(s.nested == nil || len(s.nested) == 0,
"s.t==MapType should not have any s.nested attrs")
return newTypeSchema(tt.ElemType, nil)
case pfattr.TypeWithElementTypes:
var pseudoResource shim.Resource = newTuplePseudoResource(tt)
return pseudoResource
Expand Down
29 changes: 29 additions & 0 deletions pf/internal/schemashim/type_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package schemashim

import (
"testing"

"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/stretchr/testify/assert"

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

func TestMapAttribute(t *testing.T) {
mapAttr := schema.MapAttribute{
Optional: true,
ElementType: basetypes.StringType{},
}
shimmed := &typeSchema{mapAttr.GetType(), nil}
assertIsMapType(t, shimmed)
s := shimmed.Elem().(*typeSchema)
assert.Equal(t, shim.TypeString, s.Type())
}

func assertIsMapType(t *testing.T, shimmed shim.Schema) {
assert.Equal(t, shim.TypeMap, shimmed.Type())
assert.NotNil(t, shimmed.Elem())
schema, isTypeSchema := shimmed.Elem().(*typeSchema)
assert.Truef(t, isTypeSchema, "expected shim.Elem() to be of type %T", *schema)
}
8 changes: 4 additions & 4 deletions pf/tests/provider_checkconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"testing"

"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hexops/autogold/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestCheckConfig(t *testing.T) {
},
"scopes": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
ElementType: basetypes.StringType{},
},
},
}
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestCheckConfig(t *testing.T) {
Attributes: map[string]schema.Attribute{
"scopes": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
ElementType: basetypes.StringType{},
},
},
Blocks: map[string]schema.Block{
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestCheckConfig(t *testing.T) {
Attributes: map[string]schema.Attribute{
"scopes": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
ElementType: basetypes.StringType{},
},
},
Blocks: map[string]schema.Block{
Expand Down
5 changes: 2 additions & 3 deletions pf/tests/provider_nested_custom_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/pulumi/pulumi-terraform-bridge/pf/tests/internal/providerbuilder"
"github.com/pulumi/pulumi-terraform-bridge/pf/tfbridge"
Expand Down Expand Up @@ -41,7 +40,7 @@ func TestNestedCustomTypeEncoding(t *testing.T) {
Validators: []validator.List{
listvalidator.SizeAtMost(1),
},
ElementType: types.ObjectType{
ElementType: basetypes.ObjectType{
AttrTypes: AttributeTypesMust[promptOverrideConfigurationModel](context.Background()),
},
},
Expand Down Expand Up @@ -78,7 +77,7 @@ type promptOverrideConfigurationModel struct {
}

type promptConfigurationModel struct {
BasePromptTemplate types.String `tfsdk:"base_prompt_template"`
BasePromptTemplate basetypes.StringType `tfsdk:"base_prompt_template"`
}

// Implementation for set, list, and object typables.
Expand Down