Skip to content

Commit

Permalink
Add package block generation to the terraform converter.
Browse files Browse the repository at this point in the history
This is the first step in supporting package blocks.  The next step is
supporting parameterization and detecting if the package should be
bridge with terraform-bridge or not.

Remote-Ref: brandonpollack23/3c0f4678
  • Loading branch information
brandonpollack23 committed Dec 6, 2024
1 parent 2881469 commit 2215b39
Show file tree
Hide file tree
Showing 8 changed files with 1,294 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### Improvements

- Add generation of pcl "package" blocks
### Bug Fixes
16 changes: 16 additions & 0 deletions pkg/convert/testdata/programs/required_providers/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
required_providers {
null = {
source = "hashicorp/null"
}
random = {
version = "~> 3.5.1"
source = "hashicorp/random"
}
}
required_version = ">= 1.3.5"
}

output "asdfasdfasdf" {
value = 123
}
12 changes: 12 additions & 0 deletions pkg/convert/testdata/programs/required_providers/pcl/main.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package "random" {
baseProviderName = "random"
}

package "null" {
baseProviderName = "null"
}


output "asdfasdfasdf" {
value = 123
}
77 changes: 73 additions & 4 deletions 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 @@ -2668,6 +2672,7 @@ func translateRemoteModule(
destinationRoot afero.Fs, // The root of the destination filesystem to write PCL to.
destinationDirectory string, // A path in destination to write the translated code to.
info il.ProviderInfoSource,
requiredProviders map[string]*configs.RequiredProvider,
) hcl.Diagnostics {
fetcher := getmodules.NewPackageFetcher()
tempPath, err := os.MkdirTemp("", "pulumi-tf-registry")
Expand Down Expand Up @@ -2711,6 +2716,8 @@ func translateRemoteModule(
sourceRoot, "/",
destinationRoot, destinationDirectory,
info,
requiredProviders,
/*topLevelModule*/false,
)
}

Expand All @@ -2721,6 +2728,8 @@ func translateModuleSourceCode(
destinationRoot afero.Fs, // The root of the destination filesystem to write PCL to.
destinationDirectory string, // A path in destination to write the translated code to.
info il.ProviderInfoSource,
requiredProviders map[string]*configs.RequiredProvider,
topLevelModule bool,
) hcl.Diagnostics {
sources, module, moduleDiagnostics := loadConfigDir(sourceRoot, sourceDirectory)
if moduleDiagnostics.HasErrors() {
Expand All @@ -2729,6 +2738,10 @@ func translateModuleSourceCode(
return moduleDiagnostics
}

for name, provider := range module.ProviderRequirements.RequiredProviders {
requiredProviders[name] = provider
}

scopes := newScopes(info)

state := &convertState{
Expand Down Expand Up @@ -2926,7 +2939,10 @@ func translateModuleSourceCode(
sourcePath,
destinationRoot,
destinationPath,
info)
info,
requiredProviders,
/*topLevelModule*/false,
)
state.diagnostics = append(state.diagnostics, diags...)
if diags.HasErrors() {
return state.diagnostics
Expand Down Expand Up @@ -2957,7 +2973,8 @@ func translateModuleSourceCode(
addr.Subdir,
destinationRoot,
destinationPath,
info)
info,
requiredProviders)
state.diagnostics = append(state.diagnostics, diags...)
if diags.HasErrors() {
return state.diagnostics
Expand Down Expand Up @@ -3081,7 +3098,8 @@ func translateModuleSourceCode(
remoteAddr.Subdir,
destinationRoot,
destinationPath,
info)
info,
requiredProviders)
state.diagnostics = append(state.diagnostics, diags...)
if diags.HasErrors() {
return state.diagnostics
Expand Down Expand Up @@ -3236,6 +3254,22 @@ func translateModuleSourceCode(

body := file.Body()

// Add package metadata to only the top level module.
// This should be true for only the entry point into the conversion, all
// recursive module translations should set this false.
// This guarantees that the package blocks are only written in one place (eg main.pp)
if topLevelModule {
for name, provider := range requiredProviders {
block, d := getPackageBlock(name, provider)
state.diagnostics = append(state.diagnostics, d...)
body.AppendBlock(block)
body.AppendUnstructuredTokens(hclwrite.TokensForIdentifier("\n"))
}

// Only write the package block once
topLevelModule = false
}

// First handle any inputs, these will be picked up by the "vars" scope
if item.variable != nil {
leading, block, trailing := convertVariable(state, scopes, item.variable)
Expand Down Expand Up @@ -3356,12 +3390,47 @@ func translateModuleSourceCode(
return state.diagnostics
}


// getPackageBlock returns package block in the following form:
//
// package <name> {
// baseProviderName = <name>
// baseProviderVersion = <version>
// baseProviderDownloadUrl = <url>
// parameterization {
// name = <name>
// version = <version>
// value = <base64-encoded-value>
// }
// }
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.

block := hclwrite.NewBlock("package", []string{name})
body := block.Body()

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

return block, diags
}


func TranslateModule(
source afero.Fs, sourceDirectory string,
destination afero.Fs, info il.ProviderInfoSource,
) hcl.Diagnostics {
modules := make(map[moduleKey]string)
return translateModuleSourceCode(modules, source, sourceDirectory, destination, "/", info)
return translateModuleSourceCode(modules,
source,
sourceDirectory,
destination,
"/",
info,
/*requiredProviders*/map[string]*configs.RequiredProvider{},
/*topLevelModule*/true,
)
}

func errorf(subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic {
Expand Down
5 changes: 5 additions & 0 deletions pkg/convert/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package convert

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -76,6 +77,10 @@ func (l *testLoader) LoadPackage(pkg string, version *semver.Version) (*schema.P
return schemaPackage, nil
}

func (l *testLoader) LoadPackageV2(ctx context.Context, descriptor *schema.PackageDescriptor) (*schema.Package, error) {
return l.LoadPackage(descriptor.Name, descriptor.Version)
}

func (l *testLoader) LoadPackageReference(pkg string, version *semver.Version) (schema.PackageReference, error) {
schemaPackage, err := l.LoadPackage(pkg, version)
if err != nil {
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

0 comments on commit 2215b39

Please sign in to comment.