Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/hcl/v2 v2.24.0
github.com/hashicorp/hil v0.0.0-20190212132231-97b3a9cdfa93
github.com/hashicorp/terraform-plugin-framework v1.15.0
github.com/hashicorp/terraform-plugin-framework v1.16.1
github.com/hashicorp/terraform-plugin-framework-validators v0.17.0
github.com/hashicorp/terraform-plugin-mux v0.20.0
github.com/hashicorp/terraform-plugin-sdk v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2049,8 +2049,8 @@ github.com/hashicorp/terraform-exec v0.24.0/go.mod h1:lluc/rDYfAhYdslLJQg3J0oDqo
github.com/hashicorp/terraform-json v0.4.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-json v0.27.1 h1:zWhEracxJW6lcjt/JvximOYyc12pS/gaKSy/wzzE7nY=
github.com/hashicorp/terraform-json v0.27.1/go.mod h1:GzPLJ1PLdUG5xL6xn1OXWIjteQRT2CNT9o/6A9mi9hE=
github.com/hashicorp/terraform-plugin-framework v1.15.0 h1:LQ2rsOfmDLxcn5EeIwdXFtr03FVsNktbbBci8cOKdb4=
github.com/hashicorp/terraform-plugin-framework v1.15.0/go.mod h1:hxrNI/GY32KPISpWqlCoTLM9JZsGH3CyYlir09bD/fI=
github.com/hashicorp/terraform-plugin-framework v1.16.1 h1:1+zwFm3MEqd/0K3YBB2v9u9DtyYHyEuhVOfeIXbteWA=
github.com/hashicorp/terraform-plugin-framework v1.16.1/go.mod h1:0xFOxLy5lRzDTayc4dzK/FakIgBhNf/lC4499R9cV4Y=
github.com/hashicorp/terraform-plugin-framework-validators v0.17.0 h1:0uYQcqqgW3BMyyve07WJgpKorXST3zkpzvrOnf3mpbg=
github.com/hashicorp/terraform-plugin-framework-validators v0.17.0/go.mod h1:VwdfgE/5Zxm43flraNa0VjcvKQOGVrcO4X8peIri0T0=
github.com/hashicorp/terraform-plugin-go v0.29.0 h1:1nXKl/nSpaYIUBU1IG/EsDOX0vv+9JxAltQyDMpq5mU=
Expand Down
1 change: 1 addition & 0 deletions pkg/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Encoding interface {
NewResourceEncoder(resource string, resourceType tftypes.Object) (Encoder, error)
NewDataSourceDecoder(dataSource string, dataSourceType tftypes.Object) (Decoder, error)
NewDataSourceEncoder(dataSource string, dataSourceType tftypes.Object) (Encoder, error)
NewActionEncoder(action string, actionType tftypes.Object) (Encoder, error)
}

// Like PropertyNames but specialized to either a type by token or config property.
Expand Down
11 changes: 11 additions & 0 deletions pkg/convert/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ func (e *encoding) NewDataSourceDecoder(
return dec, nil
}

func (e *encoding) NewActionEncoder(
dataSource string, objectType tftypes.Object,
) (Encoder, error) {
mctx := newActionSchemaMapContext(dataSource, e.SchemaOnlyProvider, e.ProviderInfo)
enc, err := NewObjectEncoder(ObjectSchema{mctx.schemaMap, mctx.schemaInfos, &objectType})
if err != nil {
return nil, fmt.Errorf("cannot derive an encoder for action %q: %w", dataSource, err)
}
return enc, nil
}

func buildPropertyEncoders(
mctx *schemaMapContext, objectType tftypes.Object,
) (map[terraformPropertyName]Encoder, error) {
Expand Down
17 changes: 16 additions & 1 deletion pkg/convert/schema_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/walk"
)

Expand Down Expand Up @@ -73,6 +73,21 @@ func newDataSourceSchemaMapContext(
return newSchemaMapContext(sm, fields)
}

func newActionSchemaMapContext(
action string,
schemaOnlyProvider shim.Provider,
providerInfo *tfbridge.ProviderInfo,
) *schemaMapContext {
r := schemaOnlyProvider.ActionsMap().Get(action)
contract.Assertf(r != nil, "no action %q found in ActionsMap", action)
sm := r.Schema()
var fields map[string]*tfbridge.SchemaInfo
if providerInfo != nil {
fields = providerInfo.Actions[action].GetFields()
}
return newSchemaMapContext(sm, fields)
}

func (sc *schemaMapContext) PropertyKey(tfname terraformPropertyName, _ tftypes.Type) resource.PropertyKey {
return sc.ToPropertyKey(tfname)
}
Expand Down
74 changes: 74 additions & 0 deletions pkg/pf/internal/pfutils/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pfutils

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/action"
"github.com/hashicorp/terraform-plugin-framework/provider"

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

func GatherActions[F func(Schema) shim.SchemaMap](
ctx context.Context, prov provider.Provider, f F,
) (runtypes.Actions, error) {
provMetadata := queryProviderMetadata(ctx, prov)
ds := make(collection[func() action.Action])

aprov, is := prov.(provider.ProviderWithActions)
if is {
for _, makeAction := range aprov.Actions(ctx) {
act := makeAction()

meta := action.MetadataResponse{}
act.Metadata(ctx, action.MetadataRequest{
ProviderTypeName: provMetadata.TypeName,
}, &meta)

schemaResponse := &action.SchemaResponse{}
act.Schema(ctx, action.SchemaRequest{}, schemaResponse)

actionSchema := schemaResponse.Schema
diag := schemaResponse.Diagnostics
if err := checkDiagsForErrors(diag); err != nil {
return nil, fmt.Errorf("Action %s GetSchema() error: %w", meta.TypeName, err)
}

ds[runtypes.TypeOrRenamedEntityName(meta.TypeName)] = entry[func() action.Action]{
t: makeAction,
schema: FromActionSchema(actionSchema),
tfName: runtypes.TypeName(meta.TypeName),
}
}
}

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

type actions struct {
collection[func() action.Action]
convert func(Schema) shim.SchemaMap
}

func (r actions) Schema(t runtypes.TypeOrRenamedEntityName) runtypes.Schema {
entry := r.collection[t]
return runtypesSchemaAdapter{entry.schema, r.convert, entry.tfName}
}

func (actions) IsActions() {}
5 changes: 5 additions & 0 deletions pkg/pf/internal/pfutils/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pfutils
import (
"fmt"

aschema "github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/attr"
dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
prschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
Expand Down Expand Up @@ -65,6 +66,10 @@ func FromResourceAttribute(x rschema.Attribute) Attr {
return FromAttrLike(x)
}

func FromActionAttribute(x aschema.Attribute) Attr {
return FromAttrLike(x)
}

func FromAttrLike(attrLike AttrLike) Attr {
nested, nestingMode := extractNestedAttributes(attrLike)
hasDefault := hasDefault(attrLike)
Expand Down
5 changes: 5 additions & 0 deletions pkg/pf/internal/pfutils/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"regexp"
"strconv"

aschema "github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/attr"
dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
prschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
Expand Down Expand Up @@ -58,6 +59,10 @@ func FromResourceBlock(x rschema.Block) Block {
return FromBlockLike(x)
}

func FromActionBlock(x aschema.Block) Block {
return FromBlockLike(x)
}

func FromBlockLike(x BlockLike) Block {
minItems, maxItems, _ := detectSizeConstraints(x)
attrs, blocks, mode := extractBlockNesting(x)
Expand Down
7 changes: 7 additions & 0 deletions pkg/pf/internal/pfutils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pfutils
import (
"context"

aschema "github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/attr"
dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
prschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
Expand Down Expand Up @@ -66,6 +67,12 @@ func FromResourceSchema(x rschema.Schema) Schema {
return newSchemaAdapter(x, x.Type(), x.DeprecationMessage, attrs, blocks, &x)
}

func FromActionSchema(x aschema.Schema) Schema {
attrs := convertMap(FromActionAttribute, x.Attributes)
blocks := convertMap(FromActionBlock, x.Blocks)
return newSchemaAdapter(x, x.Type(), x.DeprecationMessage, attrs, blocks, nil)
}

type schemaAdapter struct {
tftypes.AttributePathStepper
attrType attr.Type
Expand Down
6 changes: 6 additions & 0 deletions pkg/pf/internal/runtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ type DataSources interface {
collection
IsDataSources()
}

// Represents all provider's actions pre-indexed by TypeOrRenamedEntityName.
type Actions interface {
collection
IsActions()
}
52 changes: 52 additions & 0 deletions pkg/pf/internal/schemashim/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package schemashim

import (
"context"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/internalinter"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/runtypes"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/valueshim"
)

type schemaOnlyAction struct {
tf runtypes.Schema
internalinter.Internal
}

var _ shim.Action = (*schemaOnlyAction)(nil)

func (r *schemaOnlyAction) SchemaType() valueshim.Type {
protoSchema, err := r.tf.ResourceProtoSchema(context.Background())
contract.AssertNoErrorf(err, "ResourceProtoSchema failed")
return valueshim.FromTType(protoSchema.ValueType())
}

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

func (r *schemaOnlyAction) Metadata() string {
panic("schemaOnlyAction does not implement runtime operation Metadata")
}

func (r *schemaOnlyAction) Invoke(context.Context, resource.PropertyMap) (resource.PropertyMap, error) {
panic("schemaOnlyAction does not implement runtime operation Invoke")
}
87 changes: 87 additions & 0 deletions pkg/pf/internal/schemashim/actions_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package schemashim

import (
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

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

// Data Source map needs to support Set (mutability) for RenameAction.
func newSchemaOnlyActionMap(actions runtypes.Actions) schemaOnlyActionMap {
m := schemaOnlyActionMap{}
for _, name := range actions.All() {
key := string(name)
v := actions.Schema(name)
m[key] = &schemaOnlyAction{v, internalinter.Internal{}}
}
return m
}

type schemaOnlyActionMap map[string]*schemaOnlyAction

var (
_ shim.ActionMap = schemaOnlyActionMap{}
_ runtypes.Actions = schemaOnlyActionMap{}
)

func (m schemaOnlyActionMap) Len() int {
return len(m)
}

func (m schemaOnlyActionMap) Get(key string) shim.Action {
return m[key]
}

func (m schemaOnlyActionMap) GetOk(key string) (shim.Action, bool) {
v, ok := m[key]
return v, ok
}

func (m schemaOnlyActionMap) Range(each func(key string, value shim.Action) bool) {
for k, v := range m {
if !each(k, v) {
return
}
}
}

func (m schemaOnlyActionMap) Set(key string, value shim.Action) {
v, ok := value.(*schemaOnlyAction)
contract.Assertf(ok, "Set must be a %T, found a %T", v, value)
m[key] = v
}

func (m schemaOnlyActionMap) All() []runtypes.TypeOrRenamedEntityName {
arr := make([]runtypes.TypeOrRenamedEntityName, 0, len(m))
for k := range m {
arr = append(arr, runtypes.TypeOrRenamedEntityName(k))
}
return arr
}

func (m schemaOnlyActionMap) Has(key runtypes.TypeOrRenamedEntityName) bool {
_, ok := m[string(key)]
return ok
}

func (m schemaOnlyActionMap) Schema(key runtypes.TypeOrRenamedEntityName) runtypes.Schema {
return m[string(key)].tf
}

func (m schemaOnlyActionMap) IsActions() {}
9 changes: 9 additions & 0 deletions pkg/pf/internal/schemashim/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SchemaOnlyProvider struct {
tf pfprovider.Provider
resourceMap schemaOnlyResourceMap
dataSourceMap schemaOnlyDataSourceMap
actionsMap schemaOnlyActionMap
internalinter.Internal
}

Expand All @@ -62,6 +63,10 @@ func (p *SchemaOnlyProvider) DataSources(ctx context.Context) (runtypes.DataSour
return p.dataSourceMap, nil
}

func (p *SchemaOnlyProvider) Actions(ctx context.Context) (runtypes.Actions, error) {
return p.actionsMap, nil
}

func (p *SchemaOnlyProvider) Config(ctx context.Context) (tftypes.Object, error) {
schemaResponse := &pfprovider.SchemaResponse{}
p.tf.Schema(ctx, pfprovider.SchemaRequest{}, schemaResponse)
Expand Down Expand Up @@ -93,6 +98,10 @@ func (p *SchemaOnlyProvider) DataSourcesMap() shim.ResourceMap {
return p.dataSourceMap
}

func (p *SchemaOnlyProvider) ActionsMap() shim.ActionMap {
return p.actionsMap
}

func (p *SchemaOnlyProvider) InternalValidate() error {
return nil
}
Expand Down
Loading
Loading