Skip to content

Commit

Permalink
Add parameterization to pulumi package blocks.
Browse files Browse the repository at this point in the history
This adds parameterization for terraform providers which require dynamic
bridging (and do not have pulumiverse or official pulumi supported
packages already).
  • Loading branch information
brandonpollack23 committed Dec 3, 2024
1 parent 948be02 commit a46c993
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Improvements

- Add parameterization block to "package" blocks
- Add generation of pcl "package" blocks
- Add EOT (heredoc) style string delimiter handling.
- Add template join expression to convert expression
Expand All @@ -12,4 +13,4 @@

- Fix using a module multiple times via different constraints

- Fix conversion of object blocks
- Fix conversion of object blocks
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module github.com/pulumi/pulumi-converter-terraform

go 1.22.0

toolchain go1.23.3

replace github.com/opentofu/opentofu/shim => ./pkg/internal/shim

require (
github.com/apparentlymart/go-versions v1.0.2
github.com/blang/semver v3.5.1+incompatible
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/hcl/v2 v2.22.0
github.com/hashicorp/terraform-svchost v0.1.1
github.com/opentofu/opentofu/shim v0.0.0-00010101000000-000000000000
github.com/pulumi/pulumi-terraform-bridge/v3 v3.96.0
github.com/pulumi/pulumi/pkg/v3 v3.142.0
github.com/pulumi/pulumi/sdk/v3 v3.142.0
Expand Down Expand Up @@ -41,7 +41,6 @@ require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/apparentlymart/go-versions v1.0.2 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aws/aws-sdk-go v1.50.36 // indirect
Expand Down Expand Up @@ -126,6 +125,7 @@ require (
github.com/muesli/termenv v0.15.2 // indirect
github.com/natefinch/atomic v1.0.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opentofu/registry-address v0.0.0-20230922120653-901b9ae4061a // indirect
github.com/opentracing/basictracer-go v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pgavlin/fx v0.1.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/opentofu/registry-address v0.0.0-20230922120653-901b9ae4061a h1:NyM/PPbc+kxxv2d4OKfE32C5fLtVTLceyg4YKKCYO9Y=
github.com/opentofu/registry-address v0.0.0-20230922120653-901b9ae4061a/go.mod h1:HzQhpVo/NJnGmN+7FPECCVCA5ijU7AUcvf39enBKYOc=
github.com/opentracing/basictracer-go v1.1.0 h1:Oa1fTSBvAl8pa3U+IJYqrKm0NALwH9OsgwOqDv4xJW0=
github.com/opentracing/basictracer-go v1.1.0/go.mod h1:V2HZueSJEp879yv285Aap1BS69fQMD+MNP1mRs6mBQc=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Expand Down
109 changes: 109 additions & 0 deletions pkg/convert/pulumiverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2024, Pulumi Corporation. All rights reserved.


package convert

var pulumiSupportedProviders = []string{
"acme",
"aiven",
"akamai",
"alicloud",
"aquasec",
"archive",
"artifactory",
"astra",
"auth0",
"aws",
"aws-eksa",
"azure",
"azuread",
"azuredevops",
"buildkite",
"civo",
"cloudamqp",
"cloudflare",
"cloudinit",
"cloudngfwaws",
"concourse",
"configcat",
"confluentcloud",
"consul",
"databricks",
"datadog",
"dbtcloud",
"digitalocean",
"dnsimple",
"docker",
"doppler",
"ec",
"exoscale",
"external",
"f5bigip",
"fastly",
"gandi",
"gcp",
"github",
"github-credentials",
"gitlab",
"googleworkspace",
"harbor",
"harness",
"hcloud",
"hcp",
"heroku",
"http",
"ise",
"junipermist",
"kafka",
"keycloak",
"kong",
"libvirt",
"linode",
"local",
"mailgun",
"matchbox",
"meraki",
"minio",
"mongodbatlas",
"mssql",
"mysql",
"newrelic",
"ngrok",
"nomad",
"ns1",
"null",
"oci",
"okta",
"onelogin",
"openstack",
"opsgenie",
"pagerduty",
"postgresql",
"purrl",
"rabbitmq",
"rancher2",
"random",
"rke",
"scm",
"sdwan",
"sentry",
"signalfx",
"slack",
"snowflake",
"splunk",
"spotinst",
"statuscake",
"sumologic",
"tailscale",
"tf-provider-boilerplate",
"time",
"tls",
"unifi",
"vault",
"venafi",
"vra",
"vsphere",
"wavefront",
"xyz",
"zitadel",
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
baseProviderName = "null"
}


output "asdfasdfasdf" {
value = 123
}
13 changes: 13 additions & 0 deletions pkg/convert/testdata/programs/required_providers_tf/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
boundary = {
source = "hashicorp/boundary"
version = "1.1.9"
}
}
required_version = ">= 1.3.5"
}

output "asdfasdfasdf" {
value = 123
}
14 changes: 14 additions & 0 deletions pkg/convert/testdata/programs/required_providers_tf/pcl/main.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package "boundary" {
baseProviderName = "terraform-provider"
baseProviderVersion = "0.3.0"
parameterization "name" "version" "value" {
version = "1.1.9"
name = "boundary"
value = "eyJyZW1vdGUiOnsidXJsIjoiaGFzaGljb3JwL2JvdW5kYXJ5IiwidmVyc2lvbiI6IjEuMS45In19"
}
}


output "asdfasdfasdf" {
value = 123
}
30 changes: 29 additions & 1 deletion pkg/convert/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ package convert
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"math"
"os"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"

"github.com/apparentlymart/go-versions/versions"
version "github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform-svchost/disco"
"github.com/opentofu/opentofu/shim"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tf2pulumi/il"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
Expand Down Expand Up @@ -3407,11 +3411,35 @@ func getPackageBlock(name string, prov *configs.RequiredProvider) (*hclwrite.Blo
body := block.Body()

diags := hcl.Diagnostics{}
body.SetAttributeValue("baseProviderName", cty.StringVal(name))

if isTerraformProvider(name) {
body.SetAttributeValue("baseProviderName", cty.StringVal("terraform-provider"))
body.SetAttributeValue("baseProviderVersion", cty.StringVal("0.3.0"))

var desiredVersion versions.Version
desiredVersion, diags = shim.FindTfPackageVersion(prov)

innerValue := fmt.Sprintf(`{"remote":{"url":"%s","version":"%s"}}`,
prov.Source,
desiredVersion.String())
encoded := base64.StdEncoding.EncodeToString([]byte(innerValue))

paramBlock := hclwrite.NewBlock("parameterization", []string{"name", "version", "value"})
body.AppendBlock(paramBlock)
paramBlockBody := paramBlock.Body()
paramBlockBody.SetAttributeValue("version", cty.StringVal(desiredVersion.String()))
paramBlockBody.SetAttributeValue("name", cty.StringVal(name))
paramBlockBody.SetAttributeValue("value", cty.StringVal(encoded))
} else {
body.SetAttributeValue("baseProviderName", cty.StringVal(name))
}

return block, diags
}

func isTerraformProvider(name string) bool {
return !slices.Contains(pulumiSupportedProviders, name)
}

func TranslateModule(
source afero.Fs, sourceDirectory string,
Expand Down

0 comments on commit a46c993

Please sign in to comment.