Skip to content

Commit

Permalink
Refactor: Reduce pfutils in runtime usage (#2076)
Browse files Browse the repository at this point in the history
Built on top of #2075

This PR removes `pfutil` from
`github.com/pulumi/pulumi-terraform-bridge/pf.ShimProvider` (introduced
in #2075). There should be no runtime effects.
  • Loading branch information
iwahbe authored Jun 10, 2024
1 parent 9cddce4 commit ed2a84b
Show file tree
Hide file tree
Showing 31 changed files with 243 additions and 173 deletions.
8 changes: 6 additions & 2 deletions pf/internal/pfutils/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ type LookupResult struct {
}

// Drills down a Schema with a given AttributePath to classify what is found at that path, see LookupResult.
func LookupTerraformPath(schema Schema, path *tftypes.AttributePath) (LookupResult, error) {
func LookupTerraformPath(
schema tftypes.AttributePathStepper, path *tftypes.AttributePath,
) (LookupResult, error) {
res, ok, err := tryLookupAttrOrBlock(schema, path)
if err != nil {
return res, err
Expand All @@ -157,7 +159,9 @@ func LookupTerraformPath(schema Schema, path *tftypes.AttributePath) (LookupResu
return LookupResult{IsMisc: true}, nil
}

func tryLookupAttrOrBlock(schema Schema, path *tftypes.AttributePath) (LookupResult, bool, error) {
func tryLookupAttrOrBlock(
schema tftypes.AttributePathStepper, path *tftypes.AttributePath,
) (LookupResult, bool, error) {
res, remaining, err := tftypes.WalkAttributePath(schema, path)
if err != nil {
return LookupResult{}, false, fmt.Errorf("%v still remains in the path: %w", remaining, err)
Expand Down
21 changes: 7 additions & 14 deletions pf/internal/pfutils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
)

func queryProviderMetadata(ctx context.Context, prov provider.Provider) *provider.MetadataResponse {
Expand Down Expand Up @@ -48,13 +49,13 @@ type entry[T any] struct {
diagnostics diag.Diagnostics
}

type collection[T any] map[TypeName]entry[T]
type collection[T any] map[runtypes.TypeName]entry[T]

func (c collection[T]) All() []TypeName {
func (c collection[T]) All() []runtypes.TypeName {
if c == nil {
return nil
}
var names []TypeName
var names []runtypes.TypeName
for name := range c {
names = append(names, name)
}
Expand All @@ -64,23 +65,15 @@ func (c collection[T]) All() []TypeName {
return names
}

func (c collection[T]) Has(name TypeName) bool {
func (c collection[T]) Has(name runtypes.TypeName) bool {
_, ok := c[name]
return ok
}

func (c collection[T]) Schema(name TypeName) Schema {
func (c collection[T]) Schema(name runtypes.TypeName) Schema {
return c[name].schema
}

func (c collection[T]) Diagnostics(name TypeName) diag.Diagnostics {
func (c collection[T]) Diagnostics(name runtypes.TypeName) diag.Diagnostics {
return c[name].diagnostics
}

func (c collection[T]) AllDiagnostics() diag.Diagnostics {
var diags diag.Diagnostics
for _, name := range c.All() {
diags.Append(c.Diagnostics(name)...)
}
return diags
}
27 changes: 14 additions & 13 deletions pf/internal/pfutils/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
)

// Represents all provider's datasources pre-indexed by TypeName.
type DataSources interface {
All() []TypeName
Has(TypeName) bool
Schema(TypeName) Schema
Diagnostics(TypeName) diag.Diagnostics
AllDiagnostics() diag.Diagnostics
}

func GatherDatasources(ctx context.Context, prov provider.Provider) (DataSources, error) {
func GatherDatasources[F func(Schema) shim.SchemaMap](
ctx context.Context, prov provider.Provider, f F,
) (runtypes.DataSources, error) {
provMetadata := queryProviderMetadata(ctx, prov)
ds := make(collection[func() datasource.DataSource])

Expand All @@ -53,16 +47,23 @@ func GatherDatasources(ctx context.Context, prov provider.Provider) (DataSources
return nil, fmt.Errorf("Resource %s GetSchema() error: %w", meta.TypeName, err)
}

ds[TypeName(meta.TypeName)] = entry[func() datasource.DataSource]{
ds[runtypes.TypeName(meta.TypeName)] = entry[func() datasource.DataSource]{
t: makeDataSource,
schema: FromDataSourceSchema(dataSourceSchema),
diagnostics: diag,
}
}

return &dataSources{collection: ds}, nil
return &dataSources{collection: ds, convert: f}, nil
}

type dataSources struct {
collection[func() datasource.DataSource]
convert func(Schema) shim.SchemaMap
}

func (r dataSources) Schema(t runtypes.TypeName) runtypes.Schema {
return runtypesSchemaAdapter{r.collection.Schema(t), r.convert}
}

func (dataSources) IsDataSources() {}
6 changes: 3 additions & 3 deletions pf/internal/pfutils/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (defaultEq) Equal(_ *tftypes.AttributePath, a, b tftypes.Value) (bool, erro
var DefaultEq Eq = defaultEq(0)

type nonComputedEq struct {
Schema Schema
Schema tftypes.AttributePathStepper
}

func (eq *nonComputedEq) Equal(p *tftypes.AttributePath, a, b tftypes.Value) (bool, error) {
Expand All @@ -49,11 +49,11 @@ func (eq *nonComputedEq) Equal(p *tftypes.AttributePath, a, b tftypes.Value) (bo
}

// Considers two tftype.Value values equal if all their non-computed attributes are equal.
func NonComputedEq(schema Schema) Eq {
func NonComputedEq(schema tftypes.AttributePathStepper) Eq {
return &nonComputedEq{schema}
}

func replaceComputedAttributesWithNull(schema Schema,
func replaceComputedAttributesWithNull(schema tftypes.AttributePathStepper,
offset *tftypes.AttributePath, val tftypes.Value) (tftypes.Value, error) {
return tftypes.Transform(val, func(p *tftypes.AttributePath, v tftypes.Value) (tftypes.Value, error) {
realPath := joinPaths(offset, p)
Expand Down
17 changes: 12 additions & 5 deletions pf/internal/pfutils/proposed_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"

"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand Down Expand Up @@ -45,13 +46,15 @@ import (
// When Pulumi programs retract attributes, config (checkedInputs) will have no entry for these, while priorState might
// have an entry. ProposedNewState must have a Null entry in this case for Diff to work properly and recognize an
// attribute deletion.
func ProposedNew(ctx context.Context, schema Schema, priorState, config tftypes.Value) (tftypes.Value, error) {
func ProposedNew(
ctx context.Context, schema runtypes.Schema, priorState, config tftypes.Value,
) (tftypes.Value, error) {
// If the config and prior are both null, return early here before populating the prior block. The prevents
// non-null blocks from appearing the proposed state value.
if config.IsNull() && priorState.IsNull() {
return priorState, nil
}
objectType := schema.Type().TerraformType(ctx).(tftypes.Object)
objectType := schema.Type(ctx).(tftypes.Object)
if priorState.IsNull() {
priorState = newObjectWithDefaults(objectType, func(t tftypes.Type) tftypes.Value {
return tftypes.NewValue(t, nil)
Expand Down Expand Up @@ -112,7 +115,7 @@ func ProposedNew(ctx context.Context, schema Schema, priorState, config tftypes.
return *joined, nil
}

func rewriteNullComputedAsUnknown(schema Schema,
func rewriteNullComputedAsUnknown(schema tftypes.AttributePathStepper,
offset *tftypes.AttributePath, val tftypes.Value) (tftypes.Value, error) {
return tftypes.Transform(val, func(p *tftypes.AttributePath, v tftypes.Value) (tftypes.Value, error) {
pt, err := getNearestEnclosingPathType(schema, joinPaths(offset, p))
Expand Down Expand Up @@ -151,7 +154,9 @@ const (
pathToMisc pathType = 7
)

func getPathType(schema Schema, path *tftypes.AttributePath) (pathType, error) {
func getPathType(
schema tftypes.AttributePathStepper, path *tftypes.AttributePath,
) (pathType, error) {
if len(path.Steps()) == 0 {
return pathToRoot, nil
}
Expand Down Expand Up @@ -186,7 +191,9 @@ func getPathType(schema Schema, path *tftypes.AttributePath) (pathType, error) {
//
// It starts from path and proceeds upward (path.WithoutLastStep()) to try to find the nearest enclosing Attr or Block.
// It skips over pathToMisc and pathToNestedObject. May return pathToRoot.
func getNearestEnclosingPathType(schema Schema, path *tftypes.AttributePath) (pathType, error) {
func getNearestEnclosingPathType(
schema tftypes.AttributePathStepper, path *tftypes.AttributePath,
) (pathType, error) {
for {
ty, err := getPathType(schema, path)
if err != nil {
Expand Down
Loading

0 comments on commit ed2a84b

Please sign in to comment.