Skip to content

Commit

Permalink
Go rewrite tgc handwritten files (GoogleCloudPlatform#11768)
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored Sep 20, 2024
1 parent a25e4a1 commit 3a42912
Show file tree
Hide file tree
Showing 9 changed files with 802 additions and 29 deletions.
4 changes: 4 additions & 0 deletions mmv1/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ require (
)

require github.com/golang/glog v1.2.0

require github.com/otiai10/copy v1.9.0

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
8 changes: 8 additions & 0 deletions mmv1/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68=
github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/otiai10/copy v1.9.0 h1:7KFNiCgZ91Ru4qW4CWPf/7jqtxLagGRmIxWldPP9VY4=
github.com/otiai10/copy v1.9.0/go.mod h1:hsfX19wcn0UWIHUQ3/4fHuehhk2UyArQ9dVFAn3FczI=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.4.0/go.mod h1:gifjb2MYOoULtKLqUAEILUG/9KONW6f7YsJ6vQLTlFI=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
3 changes: 2 additions & 1 deletion mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func main() {
providerToGenerate = setProvider(*forceProvider, *version, productsForVersion[0], startTime)
providerToGenerate.CopyCommonFiles(*outputPath, generateCode, generateDocs)

log.Printf("Compiling common files for terraform")
if generateCode {
providerToGenerate.CompileCommonFiles(*outputPath, productsForVersion, "")

Expand Down Expand Up @@ -236,6 +235,8 @@ func setProvider(forceProvider, version string, productApi *api.Product, startTi
switch forceProvider {
case "tgc":
return provider.NewTerraformGoogleConversion(productApi, version, startTime)
case "tgc_cai2hcl":
return provider.NewCaiToTerraformConversion(productApi, version, startTime)
default:
return provider.NewTerraform(productApi, version, startTime)
}
Expand Down
2 changes: 1 addition & 1 deletion mmv1/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ProviderName(t Provider) string {
return reflect.TypeOf(t).Name()
}

func ImportPathFromVersion(t Provider, v string) string {
func ImportPathFromVersion(v string) string {
var tpg, dir string
switch v {
case "ga":
Expand Down
6 changes: 3 additions & 3 deletions mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewTerraform(product *api.Product, versionName string, startTime time.Time)
t.Product.SetPropertiesBasedOnVersion(&t.Version)
for _, r := range t.Product.Objects {
r.SetCompiler(ProviderName(t))
r.ImportPath = ImportPathFromVersion(t, versionName)
r.ImportPath = ImportPathFromVersion(versionName)
}

return t
Expand Down Expand Up @@ -557,8 +557,8 @@ func (t Terraform) replaceImportPath(outputFolder, target string) {

data := string(sourceByte)

gaImportPath := ImportPathFromVersion(t, "ga")
betaImportPath := ImportPathFromVersion(t, "beta")
gaImportPath := ImportPathFromVersion("ga")
betaImportPath := ImportPathFromVersion("beta")

if strings.Contains(data, betaImportPath) {
log.Fatalf("Importing a package from module %s is not allowed in file %s. Please import a package from module %s.", betaImportPath, filepath.Base(target), gaImportPath)
Expand Down
191 changes: 167 additions & 24 deletions mmv1/provider/terraform_tgc.go

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions mmv1/provider/terraform_tgc_cai2hcl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.

package provider

import (
"fmt"
"log"
"os"
"time"

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

// Code generator for a library converting GCP CAI objects to Terraform state.
type CaiToTerraformConversion struct {
TargetVersionName string

Version product.Version

Product *api.Product

StartTime time.Time
}

func NewCaiToTerraformConversion(product *api.Product, versionName string, startTime time.Time) CaiToTerraformConversion {
t := CaiToTerraformConversion{
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(t, versionName)
}

return t
}

func (cai2hcl CaiToTerraformConversion) Generate(outputFolder, productPath, resourceToGenerate string, generateCode, generateDocs bool) {
}

func (cai2hcl CaiToTerraformConversion) CompileCommonFiles(outputFolder string, products []*api.Product, overridePath string) {
}

func (cai2hcl CaiToTerraformConversion) CopyCommonFiles(outputFolder string, generateCode, generateDocs bool) {
if !generateCode {
return
}
log.Printf("Coping cai2hcl common files")

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/cai2hcl", outputFolder); err != nil {
log.Println(fmt.Errorf("error copying directory %v: %v", outputFolder, err))
}
}
Loading

0 comments on commit 3a42912

Please sign in to comment.