From b890002f1e2d861b2121598511375625b47ee158 Mon Sep 17 00:00:00 2001 From: Thomas Meckel <14177833+tmeckel@users.noreply.github.com> Date: Fri, 22 Sep 2023 13:22:13 +0200 Subject: [PATCH] fix: Handle breaking change of Pulumi Terraform Bridge (#20) --- cookiecutter.json | 2 +- {{cookiecutter.provider}}/provider/go.mod | 2 +- .../provider/resources.go | 58 +++++++------------ .../provider/shim/shim.go | 4 +- 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/cookiecutter.json b/cookiecutter.json index 710e3ba..2d20083 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -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 }}", diff --git a/{{cookiecutter.provider}}/provider/go.mod b/{{cookiecutter.provider}}/provider/go.mod index 4b1eb9d..ea72233 100644 --- a/{{cookiecutter.provider}}/provider/go.mod +++ b/{{cookiecutter.provider}}/provider/go.mod @@ -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") -%} diff --git a/{{cookiecutter.provider}}/provider/resources.go b/{{cookiecutter.provider}}/provider/resources.go index f3ea503..7c8fcd8 100644 --- a/{{cookiecutter.provider}}/provider/resources.go +++ b/{{cookiecutter.provider}}/provider/resources.go @@ -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" @@ -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" ) @@ -75,7 +74,6 @@ 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 - @@ -83,36 +81,35 @@ func makeResource(mod string, res string) tokens.Type { 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 @@ -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 @@ -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 -%} } diff --git a/{{cookiecutter.provider}}/provider/shim/shim.go b/{{cookiecutter.provider}}/provider/shim/shim.go index 44f8592..b1a1691 100644 --- a/{{cookiecutter.provider}}/provider/shim/shim.go +++ b/{{cookiecutter.provider}}/provider/shim/shim.go @@ -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 -%}