Skip to content

Commit

Permalink
feat: Implement Provider Muxer (#1605)
Browse files Browse the repository at this point in the history
This PR contains changes to Pulumi Terraform Bridge which allows the
specification of provider mixins to add or replace functions or
resources to a wrapped Terraform Provider without the need to change the
code of upstream provider.

---------

Co-authored-by: Thomas Meckel <tmeckel@users.noreply.github.com>
Co-authored-by: Ian Wahbe <ian@wahbe.com>
  • Loading branch information
3 people authored Jan 5, 2024
1 parent e9ec87d commit 6e4059f
Show file tree
Hide file tree
Showing 22 changed files with 1,088 additions and 85 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,14 @@ pin_upstream_sdk:
find . -name go.mod -and \
-not -path '*/x/muxer/*' -and \
-execdir go mod edit -replace ${UpstreamPluginSDK}=${OurPluginSDK}@${PluginSDKVersion} \;

.PHONY: go.work
go.work::
@cd $(PROJECT_DIR)
ifeq (,$(wildcard $(PROJECT_DIR)/go.work))
@echo "Initializing go.work..."
@go work init
else
@echo "Updating go.work..."
endif
@go work use -r .
77 changes: 77 additions & 0 deletions internal/testprovider/schema_mini_muxed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2016-2024, 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 testprovider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Subset of pulumi-random provider.
func ProviderMiniMuxed() *schema.Provider {
resourceInteger := func() *schema.Resource {
return &schema.Resource{
Description: "The resource `minimuxed_integer` generates random values from a given range, described " +
"by the `min` and `max` attributes of a given resource.\n" +
"\n" +
"This resource can be used in conjunction with resources that have the `create_before_destroy` " +
"lifecycle flag set, to avoid conflicts with unique names during the brief period where both the " +
"old and new resources exist concurrently.",

Schema: map[string]*schema.Schema{
"keepers": {
Description: "Arbitrary map of values that, when changed, will trigger recreation of " +
"resource. See [the main provider documentation](../index.html) for more information.",
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},

"min": {
Description: "The minimum inclusive value of the range.",
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},

"max": {
Description: "The maximum inclusive value of the range.",
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},

"seed": {
Description: "A custom seed to always produce the same value.",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"result": {
Description: "The random integer result.",
Type: schema.TypeInt,
Computed: true,
},
},
}
}

return &schema.Provider{
Schema: map[string]*schema.Schema{},
ResourcesMap: map[string]*schema.Resource{
"minimuxed_integer": resourceInteger(),
},
}
}
5 changes: 4 additions & 1 deletion pf/tfbridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func handleFlags(
//
// This is an experimental API.
func MainWithMuxer(ctx context.Context, pkg string, info tfbridge.ProviderInfo, schema []byte) {
if len(info.MuxWith) > 0 {
panic("mixin providers via tfbridge.ProviderInfo.MuxWith is currently not supported")
}
handleFlags(ctx, info.Version, func() (*tfbridge.MarshallableProviderInfo, error) {
info := info
return tfbridge.MarshalProviderInfo(&info), nil
Expand Down Expand Up @@ -154,7 +157,7 @@ func MakeMuxedServer(
}
m := muxer.Main{
DispatchTable: dispatchTable,
Schema: string(schema),
Schema: schema,
GetMappingHandler: map[string]muxer.MultiMappingHandler{
"tf": getTFMapping,
"terraform": getTFMapping,
Expand Down
4 changes: 4 additions & 0 deletions pf/tfgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func Main(provider string, info sdkBridge.ProviderInfo) {
//
// [Pulumi Package Schema]: https://www.pulumi.com/docs/guides/pulumi-packages/schema/
func MainWithMuxer(provider string, info sdkBridge.ProviderInfo) {
if len(info.MuxWith) > 0 {
panic("mixin providers via tfbridge.ProviderInfo.MuxWith is currently not supported")
}

shim, ok := info.P.(*pfmuxer.ProviderShim)
contract.Assertf(ok, "MainWithMuxer must have a ProviderInfo.P created with AugmentShimWithPF")

Expand Down
31 changes: 29 additions & 2 deletions pkg/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,53 @@ require (

require (
dario.cat/mergo v1.0.0 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/charmbracelet/bubbles v0.16.1 // indirect
github.com/charmbracelet/bubbletea v0.24.2 // indirect
github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/deckarep/golang-set/v2 v2.5.0 // indirect
github.com/frankban/quicktest v1.14.3 // indirect
github.com/ettle/strcase v0.1.1 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/hil v0.0.0-20190212132231-97b3a9cdfa93 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mitchellh/cli v1.1.5 // indirect
github.com/mitchellh/hashstructure v1.0.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pgavlin/fx v0.1.6 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231 // indirect
github.com/pulumi/esc v0.6.2 // indirect
github.com/pulumi/pulumi-java/pkg v0.9.8 // indirect
github.com/pulumi/pulumi-yaml v1.4.4 // indirect
github.com/pulumi/schema-tools v0.1.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
Expand Down Expand Up @@ -209,7 +236,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/grpc v1.57.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.31.0
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 6e4059f

Please sign in to comment.