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 sweepers auto-resolver code #12950

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 16 additions & 11 deletions mmv1/api/resource/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/url"
"path/filepath"
"regexp"
"runtime/debug"
"slices"
"strings"
"text/template"
Expand Down Expand Up @@ -302,26 +303,30 @@ func (e *Examples) SetHCLText() {
}

func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
moduleDir := google.GetModuleRoot()

// Convert template paths to be relative to module root
templates := []string{
templatePath,
"templates/terraform/expand_resource_ref.tmpl",
"templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl",
"templates/terraform/flatten_property_method.go.tmpl",
"templates/terraform/expand_property_method.go.tmpl",
"templates/terraform/update_mask.go.tmpl",
"templates/terraform/nested_query.go.tmpl",
"templates/terraform/unordered_list_customize_diff.go.tmpl",
filepath.Join(moduleDir, templatePath),
filepath.Join(moduleDir, "templates/terraform/expand_resource_ref.tmpl"),
filepath.Join(moduleDir, "templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl"),
filepath.Join(moduleDir, "templates/terraform/flatten_property_method.go.tmpl"),
filepath.Join(moduleDir, "templates/terraform/expand_property_method.go.tmpl"),
filepath.Join(moduleDir, "templates/terraform/update_mask.go.tmpl"),
filepath.Join(moduleDir, "templates/terraform/nested_query.go.tmpl"),
filepath.Join(moduleDir, "templates/terraform/unordered_list_customize_diff.go.tmpl"),
}
templateFileName := filepath.Base(templatePath)

templateFileName := filepath.Base(filepath.Join(moduleDir, templatePath))

tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions).ParseFiles(templates...)
if err != nil {
glog.Exit(err)
glog.Exitf("Error: %v\nStack trace:\n%s", err, debug.Stack())
}

contents := bytes.Buffer{}
if err = tmpl.ExecuteTemplate(&contents, templateFileName, e); err != nil {
glog.Exit(err)
glog.Exitf("Error: %v\nStack trace:\n%s", err, debug.Stack())
}

rs := contents.String()
Expand Down
37 changes: 37 additions & 0 deletions mmv1/google/module_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package google

import (
"os"
"path/filepath"
"runtime"
"runtime/debug"

"github.com/golang/glog"
)

var ModuleRoot = ""

func GetModuleRoot() string {
if ModuleRoot != "" {
return ModuleRoot
}

// First get the current file's path
_, currentFile, _, _ := runtime.Caller(0)
dir := filepath.Dir(currentFile)

// Walk up the directory tree until we find go.mod
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
ModuleRoot = dir
return ModuleRoot
}

parent := filepath.Dir(dir)
if parent == dir {
// We've reached the root without finding go.mod
glog.Exitf("Could not find module root (no go.mod file found)\nStack trace:\n%s", debug.Stack())
}
dir = parent
}
}
5 changes: 3 additions & 2 deletions mmv1/google/template_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"path/filepath"
"runtime/debug"
"strings"

"text/template"
Expand Down Expand Up @@ -108,12 +109,12 @@ func TrimTemplate(templatePath string, e any) string {

tmpl, err := template.New(templateFileName).Funcs(templateFunctions).ParseFiles(templates...)
if err != nil {
glog.Exit(err)
glog.Exitf("Error: %v\nStack trace:\n%s", err, debug.Stack())
}

contents := bytes.Buffer{}
if err = tmpl.ExecuteTemplate(&contents, templateFileName, e); err != nil {
glog.Exit(err)
glog.Exitf("Error: %v\nStack trace:\n%s", err, debug.Stack())
}

rs := contents.String()
Expand Down
3 changes: 2 additions & 1 deletion mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"
"sync"

Expand Down Expand Up @@ -232,7 +233,7 @@ func (td *TemplateData) GenerateFile(filePath, templatePath string, input any, g

err = os.WriteFile(filePath, sourceByte, 0644)
if err != nil {
glog.Exit(err)
glog.Exitf("Error: %v\nStack trace:\n%s", err, debug.Stack())
}
}

Expand Down
28 changes: 28 additions & 0 deletions tools/sweeper_resolver/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module github.com/GoogleCloudPlatform/magic-modules/tools/sweeper-resolver

go 1.23.3

replace github.com/GoogleCloudPlatform/magic-modules/tools/test-reader => ../test-reader

replace github.com/GoogleCloudPlatform/magic-modules/mmv1 => ../../mmv1

require (
github.com/GoogleCloudPlatform/magic-modules/mmv1 v0.0.0-00010101000000-000000000000
github.com/GoogleCloudPlatform/magic-modules/tools/test-reader v0.0.0-00010101000000-000000000000
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.20.1 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/zclconf/go-cty v1.13.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.18.0 // indirect
)
36 changes: 36 additions & 0 deletions tools/sweeper_resolver/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
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/hashicorp/hcl/v2 v2.20.1 h1:M6hgdyz7HYt1UN9e61j+qKJBqR3orTWbI1HKBJEdxtc=
github.com/hashicorp/hcl/v2 v2.20.1/go.mod h1:TZDqQ4kNKCbh1iJp99FdPiUaVDDUPivbqxZulxDYqL4=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/zclconf/go-cty v1.13.0 h1:It5dfKTTZHe9aeppbNOda3mN7Ag7sg6QkBNm6TkyFa0=
github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
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/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
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=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Loading
Loading