Skip to content

Commit 5c89fe1

Browse files
t0yv0VenelinMartinov
authored andcommitted
Link schema type propagation together
1 parent b3281fc commit 5c89fe1

File tree

19 files changed

+280
-18
lines changed

19 files changed

+280
-18
lines changed

pkg/pf/internal/schemashim/datasource.go

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

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

2327
type schemaOnlyDataSource struct {
@@ -27,6 +31,12 @@ type schemaOnlyDataSource struct {
2731

2832
var _ shim.Resource = (*schemaOnlyDataSource)(nil)
2933

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

pkg/pf/internal/schemashim/object_pseudoresource.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2929
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/pfutils"
3030
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
31+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
3132
)
3233

3334
// An Object type that masquerades as a Resource. This is a workaround to reusing tfgen code for generating schemas,
@@ -77,6 +78,10 @@ func (r *objectPseudoResource) Schema() shim.SchemaMap {
7778
return r
7879
}
7980

81+
func (r *objectPseudoResource) SchemaType() valueshim.Type {
82+
return valueshim.FromTType(tftypes.Object{})
83+
}
84+
8085
func (*objectPseudoResource) SchemaVersion() int {
8186
panic("This is an Object type encoded as a shim.Resource, and " +
8287
"SchemaVersion() should not be called on this entity during schema generation")
@@ -200,6 +205,10 @@ func newTuplePseudoResource(t attr.TypeWithElementTypes) shim.Resource {
200205
}
201206
}
202207

208+
func (r *tuplePseudoResource) SchemaType() valueshim.Type {
209+
return valueshim.FromTType(tftypes.Object{})
210+
}
211+
203212
func (*tuplePseudoResource) SchemaVersion() int { panic("TODO") }
204213
func (*tuplePseudoResource) DeprecationMessage() string { panic("TODO") }
205214

pkg/pf/internal/schemashim/resource.go

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

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

2327
type schemaOnlyResource struct {
@@ -43,6 +47,12 @@ func (r *schemaOnlyResource) DeprecationMessage() string {
4347
return r.tf.DeprecationMessage()
4448
}
4549

50+
func (r *schemaOnlyResource) SchemaType() valueshim.Type {
51+
s, err := r.tf.ResourceProtoSchema(context.Background())
52+
contract.AssertNoErrorf(err, "failed to extract schema")
53+
return valueshim.FromTType(s.ValueType())
54+
}
55+
4656
func (*schemaOnlyResource) Importer() shim.ImportFunc {
4757
panic("schemaOnlyResource does not implement runtime operation ImporterFunc")
4858
}

pkg/pf/proto/element.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
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 (
@@ -113,6 +114,10 @@ func (o elementObject) Schema() shim.SchemaMap {
113114
return elementObjectMap(o.typ)
114115
}
115116

117+
func (o elementObject) SchemaType() valueshim.Type {
118+
return valueshim.FromTType(o.typ)
119+
}
120+
116121
func (m elementObjectMap) Len() int { return len(m.AttributeTypes) }
117122

118123
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
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2222
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
23+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
2324
)
2425

2526
var (
@@ -33,6 +34,11 @@ type object struct {
3334
internalinter.Internal
3435
}
3536

37+
func (o object) SchemaType() valueshim.Type {
38+
ty := o.obj.ValueType()
39+
return valueshim.FromTType(ty)
40+
}
41+
3642
func (o object) Schema() shim.SchemaMap {
3743
contract.Assertf(o.obj.Nesting != tfprotov6.SchemaObjectNestingModeMap,
3844
"%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
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
2222
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
23+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
2324
)
2425

2526
var (
@@ -70,6 +71,11 @@ func newResource(r *tfprotov6.Schema) *resource {
7071
return &resource{r, internalinter.Internal{}}
7172
}
7273

74+
func (r resource) SchemaType() valueshim.Type {
75+
ty := r.r.Block.ValueType()
76+
return valueshim.FromTType(ty)
77+
}
78+
7379
func (r resource) Schema() shim.SchemaMap {
7480
return blockMap{r.r.Block}
7581
}

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)