Skip to content

Commit 328eb4f

Browse files
committed
Export some internal functions.
These will be consumed by the bridge.
1 parent 249055a commit 328eb4f

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

helper/resource/exports.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package resource
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/go-cty/cty"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/plugin"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plans/objchange"
11+
)
12+
13+
func UpgradeFlatmapState(ctx context.Context, version int, m map[string]string, res *schema.Resource, meta interface{}) (map[string]interface{}, int, error) {
14+
return plugin.UpgradeFlatmapState(ctx, version, m, res, meta)
15+
}
16+
17+
func UpgradeJSONState(ctx context.Context, version int, m map[string]interface{}, res *schema.Resource, meta interface{}) (map[string]interface{}, error) {
18+
return plugin.UpgradeJSONState(ctx, version, m, res, meta)
19+
}
20+
21+
func RemoveAttributes(v interface{}, ty cty.Type) {
22+
plugin.RemoveAttributes(v, ty)
23+
}
24+
25+
func NormalizeObjectFromLegacySDK(val cty.Value, schema *configschema.Block) cty.Value {
26+
return objchange.NormalizeObjectFromLegacySDK(val, schema)
27+
}

internal/helper/plugin/grpc_provider.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func (s *GRPCProviderServer) UpgradeResourceState(ctx context.Context, req *prot
338338
// map[string]interface{}.
339339
// upgradeFlatmapState returns the json map along with the corresponding schema
340340
// version.
341-
func (s *GRPCProviderServer) upgradeFlatmapState(ctx context.Context, version int, m map[string]string, res *schema.Resource) (map[string]interface{}, int, error) {
341+
func UpgradeFlatmapState(ctx context.Context, version int, m map[string]string, res *schema.Resource, meta interface{}) (map[string]interface{}, int, error) {
342342
// this will be the version we've upgraded so, defaulting to the given
343343
// version in case no migration was called.
344344
upgradedVersion := version
@@ -370,7 +370,7 @@ func (s *GRPCProviderServer) upgradeFlatmapState(ctx context.Context, version in
370370
"schema_version": strconv.Itoa(version),
371371
},
372372
}
373-
is, err := res.MigrateState(version, is, s.provider.Meta())
373+
is, err := res.MigrateState(version, is, meta)
374374
if err != nil {
375375
return nil, 0, err
376376
}
@@ -410,15 +410,19 @@ func (s *GRPCProviderServer) upgradeFlatmapState(ctx context.Context, version in
410410
return jsonMap, upgradedVersion, err
411411
}
412412

413-
func (s *GRPCProviderServer) upgradeJSONState(ctx context.Context, version int, m map[string]interface{}, res *schema.Resource) (map[string]interface{}, error) {
413+
func (s *GRPCProviderServer) upgradeFlatmapState(ctx context.Context, version int, m map[string]string, res *schema.Resource) (map[string]interface{}, int, error) {
414+
return UpgradeFlatmapState(ctx, version, m, res, s.provider.Meta())
415+
}
416+
417+
func UpgradeJSONState(ctx context.Context, version int, m map[string]interface{}, res *schema.Resource, meta interface{}) (map[string]interface{}, error) {
414418
var err error
415419

416420
for _, upgrader := range res.StateUpgraders {
417421
if version != upgrader.Version {
418422
continue
419423
}
420424

421-
m, err = upgrader.Upgrade(ctx, m, s.provider.Meta())
425+
m, err = upgrader.Upgrade(ctx, m, meta)
422426
if err != nil {
423427
return nil, err
424428
}
@@ -428,9 +432,13 @@ func (s *GRPCProviderServer) upgradeJSONState(ctx context.Context, version int,
428432
return m, nil
429433
}
430434

435+
func (s *GRPCProviderServer) upgradeJSONState(ctx context.Context, version int, m map[string]interface{}, res *schema.Resource) (map[string]interface{}, error) {
436+
return UpgradeJSONState(ctx, version, m, res, s.provider.Meta())
437+
}
438+
431439
// Remove any attributes no longer present in the schema, so that the json can
432440
// be correctly decoded.
433-
func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
441+
func RemoveAttributes(v interface{}, ty cty.Type) {
434442
// we're only concerned with finding maps that corespond to object
435443
// attributes
436444
switch v := v.(type) {
@@ -439,7 +447,7 @@ func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
439447
if ty.IsListType() || ty.IsSetType() {
440448
eTy := ty.ElementType()
441449
for _, eV := range v {
442-
s.removeAttributes(eV, eTy)
450+
RemoveAttributes(eV, eTy)
443451
}
444452
}
445453
return
@@ -448,7 +456,7 @@ func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
448456
if ty.IsMapType() {
449457
eTy := ty.ElementType()
450458
for _, eV := range v {
451-
s.removeAttributes(eV, eTy)
459+
RemoveAttributes(eV, eTy)
452460
}
453461
return
454462
}
@@ -474,11 +482,15 @@ func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
474482
continue
475483
}
476484

477-
s.removeAttributes(attrV, attrTy)
485+
RemoveAttributes(attrV, attrTy)
478486
}
479487
}
480488
}
481489

490+
func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
491+
RemoveAttributes(v, ty)
492+
}
493+
482494
func (s *GRPCProviderServer) Stop(_ context.Context, _ *proto.Stop_Request) (*proto.Stop_Response, error) {
483495
s.stopMu.Lock()
484496
defer s.stopMu.Unlock()

0 commit comments

Comments
 (0)