Skip to content

Commit

Permalink
fix: Handle breaking change of Pulumi Terraform Bridge (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeckel authored Sep 22, 2023
1 parent cb40568 commit b890002
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"skip_git_init": "no",

"__terraform_bridge_version": "{{ get_latest_release_commit('pulumi-terraform-bridge') }}",
"__pulumi_sdk_version": "v3.66.0",
"__pulumi_sdk_version": "v3.81.0",
"__pulumi_ctl_version": "{{ get_latest_release('pulumictl') }}",
"__pulumi_java_version": "{{ get_latest_release('pulumi-java') }}",
"__go_version_major": "{{ cookiecutter.go_version | version_major }}",
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.provider}}/provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go {{ cookiecutter.__go_version_major }}.{{ cookiecutter.__go_version_minor }}
replace (
{% if cookiecutter.terraform_sdk_version != "plugin-framework" -%}
{% if cookiecutter.terraform_sdk_version == "2" -%}
github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20220725190814-23001ad6ec03
github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20230710100801-03a71d0fca3d
{% endif -%}
{% endif -%}
{% if cookiecutter.terraform_provider_package_name.startswith("internal") -%}
Expand Down
58 changes: 21 additions & 37 deletions {{cookiecutter.provider}}/provider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import (
"github.com/ettle/strcase"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
{%- if cookiecutter.terraform_sdk_version != "plugin-framework" %}
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
{%- if cookiecutter.terraform_sdk_version != "plugin-framework" %}
{% if cookiecutter.terraform_sdk_version == "1" -%}
shimv1 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v1"
{% else -%}
shimv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
{%- endif %}
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
{%- endif %}
{%- if cookiecutter.terraform_provider_package_name.startswith("internal") %}
shimprovider "{{ cookiecutter.terraform_provider_module }}/shim"
Expand All @@ -42,7 +42,6 @@ import (
{%- endif %}
{%- if cookiecutter.terraform_sdk_version == "plugin-framework" %}
pf "github.com/pulumi/pulumi-terraform-bridge/pf/tfbridge"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
{%- endif %}
"github.com/{{ cookiecutter.provider_github_organization }}/pulumi-{{ cookiecutter.terraform_provider_name }}/provider/pkg/version"
)
Expand Down Expand Up @@ -75,44 +74,42 @@ func makeResource(mod string, res string) tokens.Type {
return tfbridge.MakeResource("{{ cookiecutter.terraform_provider_name }}", mod, convertName(res))
}

{% if cookiecutter.terraform_sdk_version != "plugin-framework" -%}
// preConfigureCallback is called before the providerConfigure function of the underlying provider.
// It should validate that the provider can be configured, and provide actionable errors in the case
// it cannot be. Configuration variables can be read from `vars` using the `stringValue` function -
// for example `stringValue(vars, "accessKey")`.
func preConfigureCallback(vars resource.PropertyMap, c shim.ResourceConfig) error {
return nil
}
{% endif %}

// Provider returns additional overlaid schema and metadata associated with the provider..
{% if cookiecutter.terraform_sdk_version != "plugin-framework" -%}
func Provider() tfbridge.ProviderInfo {
{% else -%}
func Provider() pf.ProviderInfo {
{% endif -%}
// Instantiate the Terraform provider
{% if cookiecutter.terraform_sdk_version != "plugin-framework" -%}
// Instantiate the Terraform provider
{% if cookiecutter.terraform_provider_package_name.startswith("internal") -%}
{% if cookiecutter.terraform_sdk_version == "1" -%}
p := shimv1.NewProvider(shimprovider.NewProvider())
{% if cookiecutter.terraform_provider_package_name.startswith("internal") -%}
{% if cookiecutter.terraform_sdk_version == "1" -%}
p := shimv1.NewProvider(shimprovider.NewProvider())
{% else -%}
p := shimv2.NewProvider(shimprovider.NewProvider())
{% endif %}
{% else -%}
p := shimv2.NewProvider(shimprovider.NewProvider())
{% endif %}
{% if cookiecutter.terraform_sdk_version == "1" -%}
p := shimv1.NewProvider({{ cookiecutter.terraform_provider_package_name }}.Provider())
{% else -%}
p := shimv2.NewProvider({{ cookiecutter.terraform_provider_package_name }}.Provider())
{% endif %}
{%- endif -%}
{% else -%}
{% if cookiecutter.terraform_sdk_version == "1" -%}
p := shimv1.NewProvider({{ cookiecutter.terraform_provider_package_name }}.Provider())
{% else -%}
p := shimv2.NewProvider({{ cookiecutter.terraform_provider_package_name }}.Provider())
{% endif %}
{%- endif -%}
{%- if cookiecutter.terraform_provider_package_name.startswith("internal") -%}
p := pf.ShimProvider(shimprovider.NewProvider())
{%- else -%}
p := pf.ShimProvider({{ cookiecutter.terraform_provider_package_name }}.NewProvider())
{% endif -%}
{% endif -%}

// Create a Pulumi provider mapping
prov := tfbridge.ProviderInfo{
{% if cookiecutter.terraform_sdk_version != "plugin-framework" -%}
P: p,
{% endif -%}
Name: "{{ cookiecutter.terraform_provider_name }}",
// DisplayName is a way to be able to change the casing of the provider
// name when being displayed on the Pulumi registry
Expand Down Expand Up @@ -161,9 +158,7 @@ func Provider() pf.ProviderInfo {
// },
// },
},
{% if cookiecutter.terraform_sdk_version != "plugin-framework" -%}
PreConfigureCallback: preConfigureCallback,
{% endif -%}
Resources: map[string]*tfbridge.ResourceInfo{
// Map each resource in the Terraform provider to a Pulumi type. Two examples
// are below - the single line form is the common case. The multi-line form is
Expand Down Expand Up @@ -228,18 +223,7 @@ func Provider() pf.ProviderInfo {
},
}

{% if cookiecutter.terraform_sdk_version != "plugin-framework" %}
prov.SetAutonaming(255, "-")

return prov
{% elif cookiecutter.terraform_sdk_version == "plugin-framework" %}
return pf.ProviderInfo{
ProviderInfo: prov,
{%- if cookiecutter.terraform_provider_package_name.startswith("internal") %}
NewProvider: shimprovider.NewProvider(),
{%- else %}
{{ cookiecutter.terraform_provider_package_name }}.NewProvider()
{% endif -%}
}
{% endif -%}
}
4 changes: 1 addition & 3 deletions {{cookiecutter.provider}}/provider/shim/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (
tf "github.com/hashicorp/terraform-plugin-framework/provider"
)

func NewProvider() func() tf.Provider {
return func() tf.Provider {
func NewProvider() tf.Provider {
return provider.New()
}
}
{% endif -%}

0 comments on commit b890002

Please sign in to comment.