Skip to content

Commit

Permalink
Replace pfutils.DataSources with runtypes.DataSources
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Jun 10, 2024
1 parent d45e11c commit 3f87b8f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 27 deletions.
23 changes: 12 additions & 11 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() []runtypes.TypeName
Has(runtypes.TypeName) bool
Schema(runtypes.TypeName) Schema
Diagnostics(runtypes.TypeName) 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 Down Expand Up @@ -60,9 +54,16 @@ func GatherDatasources(ctx context.Context, prov provider.Provider) (DataSources
}
}

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() {}
2 changes: 2 additions & 0 deletions pf/internal/pfutils/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ func (r runtypesSchemaAdapter) Shim() shim.SchemaMap {
func (r resources) Schema(t runtypes.TypeName) runtypes.Schema {
return runtypesSchemaAdapter{r.collection.Schema(t), r.convert}
}

func (resources) IsResources() {}
15 changes: 13 additions & 2 deletions pf/internal/runtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@ type Schema interface {
// examplecloud_thing.
type TypeName string

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

// Represents all provider's resources pre-indexed by TypeName.
type Resources interface {
collection
IsResources()
}

// Represents all provider's datasources pre-indexed by TypeName.
type DataSources interface {
collection
IsDataSources()
}
6 changes: 3 additions & 3 deletions pf/internal/schemashim/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
package schemashim

import (
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
)

type schemaOnlyDataSource struct {
tf pfutils.Schema
tf runtypes.Schema
}

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

func (r *schemaOnlyDataSource) Schema() shim.SchemaMap {
return newSchemaMap(r.tf)
return r.tf.Shim()
}

func (*schemaOnlyDataSource) SchemaVersion() int {
Expand Down
3 changes: 1 addition & 2 deletions pf/internal/schemashim/datasource_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
package schemashim

import (
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
)

type schemaOnlyDataSourceMap struct {
dataSources pfutils.DataSources
dataSources runtypes.DataSources
}

var _ shim.ResourceMap = (*schemaOnlyDataSourceMap)(nil)
Expand Down
6 changes: 3 additions & 3 deletions pf/internal/schemashim/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (p *SchemaOnlyProvider) Resources(ctx context.Context) (runtypes.Resources,
return pfutils.GatherResources(ctx, p.tf, NewSchemaMap)
}

func (p *SchemaOnlyProvider) DataSources(ctx context.Context) (pfutils.DataSources, error) {
return pfutils.GatherDatasources(ctx, p.tf)
func (p *SchemaOnlyProvider) DataSources(ctx context.Context) (runtypes.DataSources, error) {
return pfutils.GatherDatasources(ctx, p.tf, NewSchemaMap)
}

func (p *SchemaOnlyProvider) Config(ctx context.Context) (tftypes.Object, error) {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (p *SchemaOnlyProvider) ResourcesMap() shim.ResourceMap {
}

func (p *SchemaOnlyProvider) DataSourcesMap() shim.ResourceMap {
dataSources, err := pfutils.GatherDatasources(context.TODO(), p.tf)
dataSources, err := pfutils.GatherDatasources(context.TODO(), p.tf, NewSchemaMap)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"

"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
)
Expand All @@ -33,6 +33,6 @@ type ShimProvider interface {

Server(context.Context) (tfprotov6.ProviderServer, error)
Resources(context.Context) (runtypes.Resources, error)
DataSources(context.Context) (pfutils.DataSources, error)
DataSources(context.Context) (runtypes.DataSources, error)
Config(context.Context) (tftypes.Object, error)
}
3 changes: 1 addition & 2 deletions pf/tfbridge/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"

"github.com/pulumi/pulumi-terraform-bridge/pf"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"
pl "github.com/pulumi/pulumi-terraform-bridge/pf/internal/plugin"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/schemashim"
Expand All @@ -52,7 +51,7 @@ type provider struct {
tfServer tfprotov6.ProviderServer
info tfbridge.ProviderInfo
resources runtypes.Resources
datasources pfutils.DataSources
datasources runtypes.DataSources
pulumiSchema []byte
encoding convert.Encoding
diagSink diag.Sink
Expand Down
3 changes: 1 addition & 2 deletions pf/tfbridge/provider_datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"

"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/runtypes"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/convert"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
Expand All @@ -31,7 +30,7 @@ import (
type datasourceHandle struct {
token tokens.ModuleMember
terraformDataSourceName string
schema pfutils.Schema
schema runtypes.Schema
encoder convert.Encoder
decoder convert.Decoder
schemaOnlyShim shim.Resource
Expand Down

0 comments on commit 3f87b8f

Please sign in to comment.