Skip to content

Commit

Permalink
Rewrite the code to handle handwritten resources (GoogleCloudPlatform…
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored and balanaguharsha committed May 2, 2024
1 parent 0115aa4 commit 105917d
Show file tree
Hide file tree
Showing 16 changed files with 1,063 additions and 1,391 deletions.
2 changes: 1 addition & 1 deletion mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type Resource struct {
//
// [Optional] (Api::Resource::IamPolicy) Configuration of a resource's
// resource-specific IAM Policy.
IamPolicy resource.IamPolicy `yaml:"iam_policy"`
IamPolicy *resource.IamPolicy `yaml:"iam_policy"`

// [Optional] If set to true, don't generate the resource itself; only
// generate the IAM policy.
Expand Down
30 changes: 29 additions & 1 deletion mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"golang.org/x/exp/slices"

Expand Down Expand Up @@ -68,6 +69,7 @@ func main() {
log.Fatalf("No product.yaml file found.")
}

startTime := time.Now()
log.Printf("Generating MM output to '%s'", *outputPath)
log.Printf("Using %s version", *version)

Expand All @@ -80,6 +82,7 @@ func main() {
return false
})

var productsForVersion []map[string]interface{}
for _, productName := range allProductFiles {
productYamlPath := path.Join(productName, "go_product.yaml")

Expand Down Expand Up @@ -137,7 +140,7 @@ func main() {
productApi.Validate()

// TODO Q2: set other providers via flag
providerToGenerate := provider.NewTerraform(productApi, *version)
providerToGenerate := provider.NewTerraform(productApi, *version, startTime)

if !slices.Contains(productsToGenerate, productName) {
log.Printf("%s not specified, skipping generation", productName)
Expand All @@ -146,8 +149,33 @@ func main() {

log.Printf("%s: Generating files", productName)
providerToGenerate.Generate(*outputPath, productName, generateCode, generateDocs)

// we need to preserve a single provider instance to use outside of this loop.
productsForVersion = append(productsForVersion, map[string]interface{}{
"Definitions": productApi,
"Provider": providerToGenerate,
})
}

// TODO Q2: copy common files
}

slices.SortFunc(productsForVersion, func(p1, p2 map[string]interface{}) int {
return strings.Compare(strings.ToLower(p1["Definitions"].(*api.Product).Name), strings.ToLower(p2["Definitions"].(*api.Product).Name))
})

// In order to only copy/compile files once per provider this must be called outside
// of the products loop. This will get called with the provider from the final iteration
// of the loop
finalProduct := productsForVersion[len(productsForVersion)-1]
provider := finalProduct["Provider"].(*provider.Terraform)

provider.CopyCommonFiles(*outputPath, generateCode, generateDocs)

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

// TODO Q2: product overrides
}
}
4 changes: 0 additions & 4 deletions mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ func (td *TemplateData) GenerateFile(filePath, templatePath string, input any, g
}

sourceByte := contents.Bytes()
// Replace import path based on version (beta/alpha)
if td.TerraformResourceDirectory != "google" {
sourceByte = bytes.Replace(sourceByte, []byte("github.com/hashicorp/terraform-provider-google/google"), []byte(td.TerraformProviderModule+"/"+td.TerraformResourceDirectory), -1)
}

// if goFormat {
// sourceByte, err = format.Source(sourceByte)
Expand Down
Loading

0 comments on commit 105917d

Please sign in to comment.