Skip to content

Commit 63f29f1

Browse files
Refactor shim interfaces to not be implementable externally (#3100)
This PR is a pure refactor. The `shim` interfaces are not intended to be implemented externally. They change often and due to the fact they are public we have kept accruing new variants to keep the public interface backwards compatible which add unnecessary complexity. This PR adds `InternalInterface` interface with a private method which is only implemented by `internalinter.Internal`. This can be embedded in interfaces which are not intended to be implemented outside of the bridge. Implementers in the bridge can embed the implementation of `InternalInterface` but external users can not. This means that we are free to add methods to interfaces which are not intended to be implemented externally. This will in turn allow us to delete some of the `Schema` and `Resource` variants under `shim`.
1 parent 6534798 commit 63f29f1

36 files changed

+207
-78
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package internalinter
2+
3+
type InternalInterface interface {
4+
noimplement()
5+
}
6+
7+
type Internal struct{}
8+
9+
func (s Internal) noimplement() {}

pkg/pf/internal/schemashim/attr_schema.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
pfattr "github.com/hashicorp/terraform-plugin-framework/attr"
1919
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2020

21+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2122
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/pfutils"
2223
bridge "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
2324
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
@@ -26,6 +27,11 @@ import (
2627
type attrSchema struct {
2728
key string
2829
attr pfutils.Attr
30+
internalinter.Internal
31+
}
32+
33+
func newAttrSchema(key string, attr pfutils.Attr) *attrSchema {
34+
return &attrSchema{key, attr, internalinter.Internal{}}
2935
}
3036

3137
var _ shim.Schema = (*attrSchema)(nil)

pkg/pf/internal/schemashim/block_schema.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hashicorp/terraform-plugin-framework/attr"
2121
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2222

23+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2324
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/pfutils"
2425
bridge "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
2526
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
@@ -28,10 +29,11 @@ import (
2829
type blockSchema struct {
2930
key string
3031
block pfutils.Block
32+
internalinter.Internal
3133
}
3234

3335
func newBlockSchema(key string, block pfutils.Block) *blockSchema {
34-
return &blockSchema{key, block}
36+
return &blockSchema{key, block, internalinter.Internal{}}
3537
}
3638

3739
var _ shim.Schema = (*blockSchema)(nil)

pkg/pf/internal/schemashim/custom_type_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestCustomTypeEmbeddingObjectType(t *testing.T) {
5151
},
5252
}
5353

54-
shimmed := &blockSchema{"key", pfutils.FromBlockLike(raw)}
54+
shimmed := newBlockSchema("key", pfutils.FromBlockLike(raw))
5555
assert.Equal(t, shim.TypeMap, shimmed.Type())
5656
assert.NotNil(t, shimmed.Elem())
5757
_, isPseudoResource := shimmed.Elem().(shim.Resource)
@@ -80,7 +80,7 @@ func TestCustomListType(t *testing.T) {
8080
},
8181
}
8282

83-
shimmed := &blockSchema{"key", pfutils.FromBlockLike(raw)}
83+
shimmed := newBlockSchema("key", pfutils.FromBlockLike(raw))
8484
assert.Equal(t, shim.TypeList, shimmed.Type())
8585
assert.NotNil(t, shimmed.Elem())
8686
_, isPseudoResource := shimmed.Elem().(shim.Resource)
@@ -109,7 +109,7 @@ func TestCustomListAttribute(t *testing.T) {
109109
},
110110
}
111111

112-
shimmed := &attrSchema{"key", pfutils.FromAttrLike(raw)}
112+
shimmed := newAttrSchema("key", pfutils.FromAttrLike(raw))
113113
assert.Equal(t, shim.TypeList, shimmed.Type())
114114
assert.NotNil(t, shimmed.Elem())
115115
_, isPseudoResource := shimmed.Elem().(shim.Schema)
@@ -138,7 +138,7 @@ func TestCustomSetType(t *testing.T) {
138138
},
139139
}
140140

141-
shimmed := &blockSchema{"key", pfutils.FromBlockLike(raw)}
141+
shimmed := newBlockSchema("key", pfutils.FromBlockLike(raw))
142142
assert.Equal(t, shim.TypeSet, shimmed.Type())
143143
assert.NotNil(t, shimmed.Elem())
144144
_, isPseudoResource := shimmed.Elem().(shim.Resource)

pkg/pf/internal/schemashim/datasource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
package schemashim
1616

1717
import (
18+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
1819
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/runtypes"
1920
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
2021
)
2122

2223
type schemaOnlyDataSource struct {
2324
tf runtypes.Schema
25+
internalinter.Internal
2426
}
2527

2628
var _ shim.Resource = (*schemaOnlyDataSource)(nil)

pkg/pf/internal/schemashim/datasource_map.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package schemashim
1717
import (
1818
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
1919

20+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2021
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/runtypes"
2122
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
2223
)
@@ -27,7 +28,7 @@ func newSchemaOnlyDataSourceMap(dataSources runtypes.DataSources) schemaOnlyData
2728
for _, name := range dataSources.All() {
2829
key := string(name)
2930
v := dataSources.Schema(name)
30-
m[key] = &schemaOnlyDataSource{v}
31+
m[key] = &schemaOnlyDataSource{v, internalinter.Internal{}}
3132
}
3233
return m
3334
}

pkg/pf/internal/schemashim/object_pseudoresource.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/hashicorp/terraform-plugin-go/tftypes"
2626
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
2727

28+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2829
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/pfutils"
2930
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
3031
)
@@ -38,6 +39,7 @@ type objectPseudoResource struct {
3839
nestedAttrs map[string]pfutils.Attr
3940
nestedBlocks map[string]pfutils.Block // should have disjoint keys from nestedAttrs
4041
allAttrNames []string
42+
internalinter.Internal
4143
}
4244

4345
func newObjectPseudoResource(t basetypes.ObjectTypable,
@@ -127,7 +129,7 @@ func (r *objectPseudoResource) GetOk(key string) (shim.Schema, bool) {
127129
// and recurse using attrSchema. This information may be coming out of band from the ObjectTypeable value itself
128130
// when using blocks, see TestCustomTypeEmbeddingObjectType.
129131
if attr, ok := r.nestedAttrs[key]; ok {
130-
return &attrSchema{key, attr}, true
132+
return newAttrSchema(key, attr), true
131133
}
132134

133135
// Nested blocks are similar to attributes:
@@ -169,6 +171,7 @@ type tuplePseudoResource struct {
169171
schemaOnly
170172
attrs map[string]pfutils.Attr
171173
tuple attr.TypeWithElementTypes
174+
internalinter.Internal
172175
}
173176

174177
type tupElementAttr struct{ e attr.Type }

pkg/pf/internal/schemashim/object_type_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestObjectAttribute(t *testing.T) {
3737
"s": basetypes.StringType{},
3838
},
3939
}
40-
shimmed := &attrSchema{"key", pfutils.FromAttrLike(objectAttr)}
40+
shimmed := newAttrSchema("key", pfutils.FromAttrLike(objectAttr))
4141
assertIsObjectType(t, shimmed)
4242
s := shimmed.Elem().(shim.Resource).Schema().Get("s")
4343
assert.Equal(t, shim.TypeString, s.Type())
@@ -57,7 +57,7 @@ func TestSingleNestedBlock(t *testing.T) {
5757
b := schema.SingleNestedBlock{
5858
Attributes: simpleObjectAttributes(),
5959
}
60-
shimmed := &blockSchema{"key", pfutils.FromResourceBlock(b)}
60+
shimmed := newBlockSchema("key", pfutils.FromResourceBlock(b))
6161
assertIsObjectType(t, shimmed)
6262
assert.Equal(t, "obj[c=str,co=str,desc=str,o=str,r=str]", schemaLogicalType(shimmed).String())
6363
r, ok := shimmed.Elem().(shim.Resource)
@@ -72,7 +72,7 @@ func TestListNestedBlock(t *testing.T) {
7272
Attributes: simpleObjectAttributes(),
7373
},
7474
}
75-
shimmed := &blockSchema{"key", pfutils.FromResourceBlock(b)}
75+
shimmed := newBlockSchema("key", pfutils.FromResourceBlock(b))
7676
assert.Equal(t, "list[obj[c=str,co=str,desc=str,o=str,r=str]]", schemaLogicalType(shimmed).String())
7777
r, ok := shimmed.Elem().(shim.Resource)
7878
require.True(t, ok, "List-nested TF blocks should be represented as Elem() shim.Resource")
@@ -86,7 +86,7 @@ func TestSetNestedBlock(t *testing.T) {
8686
Attributes: simpleObjectAttributes(),
8787
},
8888
}
89-
shimmed := &blockSchema{"key", pfutils.FromResourceBlock(b)}
89+
shimmed := newBlockSchema("key", pfutils.FromResourceBlock(b))
9090
assert.Equal(t, "set[obj[c=str,co=str,desc=str,o=str,r=str]]", schemaLogicalType(shimmed).String())
9191
r, ok := shimmed.Elem().(shim.Resource)
9292
require.True(t, ok, "Set-nested TF blocks should be represented as Elem() shim.Resource")

pkg/pf/internal/schemashim/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
2424
"github.com/hashicorp/terraform-plugin-go/tftypes"
2525

26+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2627
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf"
2728
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/pfutils"
2829
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/runtypes"
@@ -36,6 +37,7 @@ type SchemaOnlyProvider struct {
3637
tf pfprovider.Provider
3738
resourceMap schemaOnlyResourceMap
3839
dataSourceMap schemaOnlyDataSourceMap
40+
internalinter.Internal
3941
}
4042

4143
func (p *SchemaOnlyProvider) Server(ctx context.Context) (tfprotov6.ProviderServer, error) {

pkg/pf/internal/schemashim/resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
package schemashim
1616

1717
import (
18+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
1819
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/runtypes"
1920
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
2021
)
2122

2223
type schemaOnlyResource struct {
2324
tf runtypes.Schema
25+
internalinter.Internal
26+
}
27+
28+
func newSchemaOnlyResource(tf runtypes.Schema) *schemaOnlyResource {
29+
return &schemaOnlyResource{tf, internalinter.Internal{}}
2430
}
2531

2632
var _ shim.Resource = (*schemaOnlyResource)(nil)

0 commit comments

Comments
 (0)