Skip to content

Commit d5bcb8c

Browse files
committed
Link schema type propagation together
1 parent 535136f commit d5bcb8c

File tree

20 files changed

+286
-18
lines changed

20 files changed

+286
-18
lines changed

pkg/pf/internal/schemashim/datasource.go

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

1717
import (
18+
"context"
19+
1820
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/runtypes"
1921
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
22+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
23+
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
2024
)
2125

2226
type schemaOnlyDataSource struct {
@@ -25,6 +29,12 @@ type schemaOnlyDataSource struct {
2529

2630
var _ shim.Resource = (*schemaOnlyDataSource)(nil)
2731

32+
func (r *schemaOnlyDataSource) SchemaType() valueshim.Type {
33+
protoSchema, err := r.tf.ResourceProtoSchema(context.Background())
34+
contract.AssertNoErrorf(err, "ResourceProtoSchema failed")
35+
return valueshim.FromTType(protoSchema.ValueType())
36+
}
37+
2838
func (r *schemaOnlyDataSource) Schema() shim.SchemaMap {
2939
return r.tf.Shim()
3040
}

pkg/pf/internal/schemashim/object_pseudoresource.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/pfutils"
2929
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
30+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
3031
)
3132

3233
// An Object type that masquerades as a Resource. This is a workaround to reusing tfgen code for generating schemas,
@@ -75,6 +76,10 @@ func (r *objectPseudoResource) Schema() shim.SchemaMap {
7576
return r
7677
}
7778

79+
func (r *objectPseudoResource) SchemaType() valueshim.Type {
80+
return valueshim.FromTType(tftypes.Object{})
81+
}
82+
7883
func (*objectPseudoResource) SchemaVersion() int {
7984
panic("This is an Object type encoded as a shim.Resource, and " +
8085
"SchemaVersion() should not be called on this entity during schema generation")
@@ -197,6 +202,10 @@ func newTuplePseudoResource(t attr.TypeWithElementTypes) shim.Resource {
197202
}
198203
}
199204

205+
func (r *tuplePseudoResource) SchemaType() valueshim.Type {
206+
return valueshim.FromTType(tftypes.Object{})
207+
}
208+
200209
func (*tuplePseudoResource) SchemaVersion() int { panic("TODO") }
201210
func (*tuplePseudoResource) DeprecationMessage() string { panic("TODO") }
202211

pkg/pf/internal/schemashim/resource.go

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

1717
import (
18+
"context"
19+
1820
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/runtypes"
1921
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
22+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
23+
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
2024
)
2125

2226
type schemaOnlyResource struct {
@@ -37,6 +41,12 @@ func (r *schemaOnlyResource) DeprecationMessage() string {
3741
return r.tf.DeprecationMessage()
3842
}
3943

44+
func (r *schemaOnlyResource) SchemaType() valueshim.Type {
45+
s, err := r.tf.ResourceProtoSchema(context.Background())
46+
contract.AssertNoErrorf(err, "failed to extract schema")
47+
return valueshim.FromTType(s.ValueType())
48+
}
49+
4050
func (*schemaOnlyResource) Importer() shim.ImportFunc {
4151
panic("schemaOnlyResource does not implement runtime operation ImporterFunc")
4252
}

pkg/pf/proto/element.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hashicorp/terraform-plugin-go/tftypes"
1919

2020
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
21+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
2122
)
2223

2324
var (
@@ -101,6 +102,10 @@ func (o elementObject) Schema() shim.SchemaMap {
101102
return elementObjectMap(o.typ)
102103
}
103104

105+
func (o elementObject) SchemaType() valueshim.Type {
106+
return valueshim.FromTType(o.typ)
107+
}
108+
104109
func (m elementObjectMap) Len() int { return len(m.AttributeTypes) }
105110

106111
func (m elementObjectMap) Get(key string) shim.Schema { return getSchemaMap(m, key) }

pkg/pf/proto/object.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
2020

2121
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
22+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
2223
)
2324

2425
var (
@@ -31,6 +32,11 @@ type object struct {
3132
obj tfprotov6.SchemaObject
3233
}
3334

35+
func (o object) SchemaType() valueshim.Type {
36+
ty := o.obj.ValueType()
37+
return valueshim.FromTType(ty)
38+
}
39+
3440
func (o object) Schema() shim.SchemaMap {
3541
contract.Assertf(o.obj.Nesting != tfprotov6.SchemaObjectNestingModeMap,
3642
"%T cannot be a map, since that would require `o` to represent a Map<Object> type", o)

pkg/pf/proto/resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
2020

2121
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
22+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
2223
)
2324

2425
var (
@@ -62,6 +63,11 @@ func (m resourceMap) Set(key string, value shim.Resource) {
6263

6364
type resource struct{ r *tfprotov6.Schema }
6465

66+
func (r resource) SchemaType() valueshim.Type {
67+
ty := r.r.Block.ValueType()
68+
return valueshim.FromTType(ty)
69+
}
70+
6571
func (r resource) Schema() shim.SchemaMap {
6672
return blockMap{r.r.Block}
6773
}

pkg/pf/proto/schema.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ package proto
1717
import (
1818
// "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
1919

20+
"github.com/hashicorp/terraform-plugin-go/tftypes"
2021
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
22+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
2123
)
2224

2325
// pseudoResource represents a type that must pretent to be a [shim.Resource], but does not represent a resource.
2426
type pseudoResource struct{}
2527

28+
func (pseudoResource) SchemaType() valueshim.Type {
29+
return valueshim.FromTType(tftypes.Object{}) // not a top-level resource
30+
}
31+
2632
func (pseudoResource) SchemaVersion() int { return 0 }
2733
func (pseudoResource) Importer() shim.ImportFunc { return nil }
2834
func (pseudoResource) Timeouts() *shim.ResourceTimeout { return nil }

pkg/pf/tfbridge/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ func (p *provider) returnTerraformConfig() (resource.PropertyMap, error) {
339339
}
340340
// use valueshim package to marshal tfConfigValue into raw json,
341341
// which can be unmarshaled into a map[string]interface{}
342-
configJSONMessage, err := valueshim.FromTValue(tfConfigValue).Marshal()
342+
value := valueshim.FromTValue(tfConfigValue)
343+
configJSONMessage, err := value.Marshal(value.Type())
343344
if err != nil {
344345
return nil, fmt.Errorf("error marshaling into raw JSON message: %v", err)
345346
}

pkg/pf/tfbridge/resource_state.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func insertRawStateDelta(ctx context.Context, rh *resourceHandle, pm resource.Pr
185185
schemaInfos := rh.pulumiResourceInfo.GetFields()
186186
v := valueshim.FromTValue(state)
187187

188-
delta, err := tfbridge.RawStateComputeDelta(ctx, rh.schema.Shim(), schemaInfos, pm, v)
188+
st := valueshim.FromTType(rh.schema.Type(ctx))
189+
delta, err := tfbridge.RawStateComputeDelta(ctx, rh.schema.Shim(), schemaInfos, pm, st, v)
189190
if err != nil {
190191
return err
191192
}

pkg/tfbridge/provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,8 @@ func (p *Provider) Create(ctx context.Context, req *pulumirpc.CreateRequest) (*p
13651365
}
13661366

13671367
if p.info.RawStateDeltaEnabled() {
1368-
if err := RawStateInjectDelta(ctx, res.TF.Schema(), res.Schema.Fields, props, newstate); err != nil {
1368+
s := res.TF.SchemaType()
1369+
if err := RawStateInjectDelta(ctx, res.TF.Schema(), res.Schema.Fields, props, s, newstate); err != nil {
13691370
return nil, err
13701371
}
13711372
}
@@ -1515,7 +1516,8 @@ func (p *Provider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*pulum
15151516
}
15161517

15171518
if p.info.RawStateDeltaEnabled() {
1518-
err := RawStateInjectDelta(ctx, res.TF.Schema(), res.Schema.Fields, props, newstate)
1519+
s := res.TF.SchemaType()
1520+
err := RawStateInjectDelta(ctx, res.TF.Schema(), res.Schema.Fields, props, s, newstate)
15191521
if err != nil {
15201522
return nil, err
15211523
}
@@ -1744,7 +1746,8 @@ func (p *Provider) Update(ctx context.Context, req *pulumirpc.UpdateRequest) (*p
17441746
}
17451747

17461748
if p.info.RawStateDeltaEnabled() {
1747-
if err := RawStateInjectDelta(ctx, res.TF.Schema(), res.Schema.Fields, props, newstate); err != nil {
1749+
s := res.TF.SchemaType()
1750+
if err := RawStateInjectDelta(ctx, res.TF.Schema(), res.Schema.Fields, props, s, newstate); err != nil {
17481751
return nil, err
17491752
}
17501753
}

0 commit comments

Comments
 (0)