Skip to content

Commit

Permalink
Add tgc_v6 provider (GoogleCloudPlatform#12341)
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored Dec 9, 2024
1 parent ec97af7 commit 7043b15
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 2 deletions.
7 changes: 5 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ clean-tgc:
rm -rf ./tfplan2cai/converters/google/resources;\
rm -rf ./cai2hcl/*;\
find ./tfplan2cai/test/** -type f -exec git rm {} \; > /dev/null;\
rm -rf ./v6/pkg/cai2hcl/*;\
rm -rf ./v6/pkg/tfplan2cai/*;\

tgc:
cd mmv1;\
go run . --version beta --provider tgc --output $(OUTPUT_PATH)/tfplan2cai $(mmv1_compile);\
go run . --version beta --provider tgc_cai2hcl --output $(OUTPUT_PATH)/cai2hcl $(mmv1_compile);\
go run . --version beta --provider tgc --output $(OUTPUT_PATH)/tfplan2cai $(mmv1_compile)\
&& go run . --version beta --provider tgc_cai2hcl --output $(OUTPUT_PATH)/cai2hcl $(mmv1_compile)\
&& go run . --version beta --provider tgc_v6 --output $(OUTPUT_PATH)/v6/pkg $(mmv1_compile);\

tf-oics:
cd mmv1;\
Expand Down
2 changes: 2 additions & 0 deletions mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ func setProvider(forceProvider, version string, productApi *api.Product, startTi
return provider.NewTerraformGoogleConversion(productApi, version, startTime)
case "tgc_cai2hcl":
return provider.NewCaiToTerraformConversion(productApi, version, startTime)
case "tgc_v6":
return provider.NewTerraformGoogleConversionV6(productApi, version, startTime)
case "oics":
return provider.NewTerraformOiCS(productApi, version, startTime)
default:
Expand Down
150 changes: 150 additions & 0 deletions mmv1/provider/terraform_tgc_v6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2024 Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generator for a library converting terraform state to gcp objects.

package provider

import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"time"

"github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
"github.com/otiai10/copy"
)

// This proivder is for both tfplan2cai and cai2hcl conversions,
// and copying other files, such as transport.go
type TerraformGoogleConversionV6 struct {
TargetVersionName string

Version product.Version

Product *api.Product

StartTime time.Time
}

func NewTerraformGoogleConversionV6(product *api.Product, versionName string, startTime time.Time) TerraformGoogleConversionV6 {
t := TerraformGoogleConversionV6{
Product: product,
TargetVersionName: versionName,
Version: *product.VersionObjOrClosest(versionName),
StartTime: startTime,
}

t.Product.SetPropertiesBasedOnVersion(&t.Version)
for _, r := range t.Product.Objects {
r.SetCompiler(ProviderName(t))
r.ImportPath = ImportPathFromVersion(versionName)
}

return t
}

func (tgc TerraformGoogleConversionV6) Generate(outputFolder, productPath, resourceToGenerate string, generateCode, generateDocs bool) {
tgc.GenerateTfToCaiObjects(outputFolder, resourceToGenerate, generateCode, generateDocs)
tgc.GenerateCaiToHclObjects(outputFolder, resourceToGenerate, generateCode, generateDocs)
}

func (tgc TerraformGoogleConversionV6) GenerateTfToCaiObjects(outputFolder, resourceToGenerate string, generateCode, generateDocs bool) {
}

func (tgc TerraformGoogleConversionV6) GenerateCaiToHclObjects(outputFolder, resourceToGenerate string, generateCode, generateDocs bool) {
}

func (tgc TerraformGoogleConversionV6) CompileCommonFiles(outputFolder string, products []*api.Product, overridePath string) {
tgc.CompileTfToCaiCommonFiles(outputFolder, products)
tgc.CompileCaiToHclCommonFiles(outputFolder, products)
}

func (tgc TerraformGoogleConversionV6) CompileTfToCaiCommonFiles(outputFolder string, products []*api.Product) {
log.Printf("Compiling common files for tgc v6 tfplan2cai.")

resourceConverters := map[string]string{
"tfplan2cai/converters/resource_converters.go": "templates/tgc_v6/tfplan2cai/resource_converters.go.tmpl",
}
templateData := NewTemplateData(outputFolder, tgc.TargetVersionName)
tgc.CompileFileList(outputFolder, resourceConverters, *templateData, products)
}

func (tgc TerraformGoogleConversionV6) CompileCaiToHclCommonFiles(outputFolder string, products []*api.Product) {
log.Printf("Compiling common files for tgc v6 tfplan2cai.")

resourceConverters := map[string]string{
"cai2hcl/converters/resource_converters.go": "templates/tgc_v6/cai2hcl/resource_converters.go.tmpl",
}
templateData := NewTemplateData(outputFolder, tgc.TargetVersionName)
tgc.CompileFileList(outputFolder, resourceConverters, *templateData, products)
}

func (tgc TerraformGoogleConversionV6) CompileFileList(outputFolder string, files map[string]string, fileTemplate TemplateData, products []*api.Product) {
if err := os.MkdirAll(outputFolder, os.ModePerm); err != nil {
log.Println(fmt.Errorf("error creating output directory %v: %v", outputFolder, err))
}

for target, source := range files {
targetFile := filepath.Join(outputFolder, target)
targetDir := filepath.Dir(targetFile)
if err := os.MkdirAll(targetDir, os.ModePerm); err != nil {
log.Println(fmt.Errorf("error creating output directory %v: %v", targetDir, err))
}

templates := []string{
source,
}

formatFile := filepath.Ext(targetFile) == ".go"

fileTemplate.GenerateFile(targetFile, source, tgc, formatFile, templates...)
tgc.replaceImportPath(outputFolder, target)
}
}

func (tgc TerraformGoogleConversionV6) CopyCommonFiles(outputFolder string, generateCode, generateDocs bool) {
if !generateCode {
return
}

log.Printf("Copying common files for tgc v6.")

if err := os.MkdirAll(outputFolder, os.ModePerm); err != nil {
log.Println(fmt.Errorf("error creating output directory %v: %v", outputFolder, err))
}

if err := copy.Copy("third_party/tgc_v6", outputFolder); err != nil {
log.Println(fmt.Errorf("error copying directory %v: %v", outputFolder, err))
}
}

func (tgc TerraformGoogleConversionV6) replaceImportPath(outputFolder, target string) {
// Replace import paths to reference the resources dir instead of the google provider
targetFile := filepath.Join(outputFolder, target)
sourceByte, err := os.ReadFile(targetFile)
if err != nil {
log.Fatalf("Cannot read file %s to replace import path: %s", targetFile, err)
}

// replace google to google-beta
gaImportPath := ImportPathFromVersion("ga")
sourceByte = bytes.Replace(sourceByte, []byte(gaImportPath), []byte(TERRAFORM_PROVIDER_BETA+"/"+RESOURCE_DIRECTORY_BETA), -1)
err = os.WriteFile(targetFile, sourceByte, 0644)
if err != nil {
log.Fatalf("Cannot write file %s to replace import path: %s", target, err)
}
}
28 changes: 28 additions & 0 deletions mmv1/templates/tgc_v6/cai2hcl/resource_converters.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{/* The license inside this block applies to this file
Copyright 2024 Google LLC. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */ -}}
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package converters
28 changes: 28 additions & 0 deletions mmv1/templates/tgc_v6/tfplan2cai/resource_converters.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{/* The license inside this block applies to this file
Copyright 2024 Google LLC. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */ -}}
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package converters

0 comments on commit 7043b15

Please sign in to comment.