Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paramaterization to package block generation #240

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
1 change: 1 addition & 0 deletions 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

### Bug Fixes
28 changes: 16 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ 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.6.0
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
github.com/pulumi/terraform v0.12.1-0.20230322133416-a268cd0892c9
github.com/spf13/afero v1.9.5
github.com/pulumi/terraform v1.4.0
github.com/spf13/afero v1.11.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/zclconf/go-cty v1.14.2
github.com/zclconf/go-cty v1.14.4
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8
google.golang.org/grpc v1.67.1
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -39,7 +43,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.1 // 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 @@ -124,6 +127,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 Expand Up @@ -166,16 +170,16 @@ require (
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.22.0 // indirect
golang.org/x/tools v0.26.0 // indirect
google.golang.org/api v0.169.0 // indirect
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
Expand Down
64 changes: 26 additions & 38 deletions go.sum

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions pkg/convert/pulumiverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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",
}
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 {
tf-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 {
version = "1.1.9"
name = "boundary"
value = "eyJyZW1vdGUiOnsidXJsIjoiaGFzaGljb3JwL2JvdW5kYXJ5IiwidmVyc2lvbiI6IjEuMS45In19"
}
}


output "asdfasdfasdf" {
value = 123
}
40 changes: 39 additions & 1 deletion pkg/convert/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package convert
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"math"
Expand All @@ -27,11 +28,13 @@ import (
"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 @@ -3409,6 +3412,9 @@ func translateModuleSourceCode(
// }
// }
func getPackageBlock(name string, prov *configs.RequiredProvider) (*hclwrite.Block, hcl.Diagnostics) {
// if the name is one of our known providers just write it, if it is not it
// must be tf so write a paramaterized package.

packageNameParts := strings.Split(prov.Source, "/")
packageName := packageNameParts[len(packageNameParts)-1]

Expand All @@ -3420,11 +3426,43 @@ func getPackageBlock(name string, prov *configs.RequiredProvider) (*hclwrite.Blo
body := block.Body()

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

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

// Right now we use the shim of the opentofu implementation of getting the
// TF Package version to access an internal API.
// This is a bit of a hack and a future option may be to depend on the TF
// bridge to provide this functionality.
var desiredVersion versions.Version
desiredVersion, diags = shim.FindTfPackageVersion(prov)

// Right now this json string is just a remote source with url and version,
// but it could be more complex in the future.
// Once it becomes more complex it may be worthwhile to export a function
// from pulumi-terraform-bridge that encodes this value and use it here.
innerValue := fmt.Sprintf(`{"remote":{"url":"%s","version":"%s"}}`,
prov.Source,
desiredVersion.String())
encoded := base64.StdEncoding.EncodeToString([]byte(innerValue))

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

return block, diags
}

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

func TranslateModule(
source afero.Fs, sourceDirectory string,
Expand Down
83 changes: 83 additions & 0 deletions pkg/internal/shim/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module github.com/opentofu/opentofu/shim

go 1.22.0

toolchain go1.23.1

require (
github.com/apparentlymart/go-versions v1.0.2
github.com/hashicorp/hcl/v2 v2.22.0
github.com/hashicorp/terraform-registry-address v0.2.3
github.com/hashicorp/terraform-svchost v0.1.1
github.com/opentofu/registry-address v0.0.0-20230922120653-901b9ae4061a
github.com/pulumi/terraform v1.4.0
)

require (
cloud.google.com/go v0.112.1 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/storage v1.39.1 // indirect
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/aws/aws-sdk-go v1.50.36 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bmatcuk/doublestar v1.1.5 // indirect
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
github.com/zclconf/go-cty-yaml v1.0.3 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.26.0 // indirect
google.golang.org/api v0.169.0 // indirect
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/grpc v1.66.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
Loading
Loading