Skip to content

Commit

Permalink
Rewrite iam policy template with Go (#10715)
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed May 16, 2024
1 parent b95a74e commit caee19a
Show file tree
Hide file tree
Showing 4 changed files with 572 additions and 104 deletions.
91 changes: 90 additions & 1 deletion mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,18 @@ func ImportIdFormats(importFormat, identity []string, baseUrl string) []string {
}
}

// TODO Q2: id_formats.uniq.reject(&:empty?).sort_by { |i| [i.count('/'), i.count('{{')] }.reverse
idFormats = google.Reject(slices.Compact(idFormats), func(i string) bool {
return i == ""
})
slices.SortFunc(idFormats, func(a, b string) int {
i := strings.Count(a, "/")
j := strings.Count(b, "/")
if i == j {
return strings.Count(a, "{{") - strings.Count(b, "{{")
}
return i - j
})
slices.Reverse(idFormats)
return idFormats
}

Expand Down Expand Up @@ -1003,6 +1014,84 @@ func (r Resource) IsInIdentity(t Type) bool {
return false
}

func (r Resource) IamParentResourceName() string {
var parentResourceName string

if r.IamPolicy != nil {
parentResourceName = r.IamPolicy.ParentResourceAttribute
}

if parentResourceName == "" {
parentResourceName = google.Underscore(r.Name)
}

return parentResourceName
}

func (r Resource) IamResourceUri() string {
var resourceUri string
if r.IamPolicy != nil {
resourceUri = r.IamPolicy.BaseUrl
}
if resourceUri == "" {
resourceUri = r.SelfLinkUri()
}
return resourceUri
}

func (r Resource) IamImportUrl() string {
r.IamResourceUri()
return regexp.MustCompile(`\{\{%?(\w+)\}\}`).ReplaceAllString(r.IamResourceUri(), "%s")
}

func (r Resource) IamResourceParams() []string {
resourceUri := strings.ReplaceAll(r.IamResourceUri(), "{{name}}", fmt.Sprintf("{{%s}}}", r.IamParentResourceName()))

return r.ExtractIdentifiers(resourceUri)
}

func (r Resource) IsInIamResourceParams(param string) bool {
return slices.Contains(r.IamResourceParams(), param)
}

func (r Resource) IamStringQualifiers() string {
var transformed []string
for _, param := range r.IamResourceParams() {
transformed = append(transformed, fmt.Sprintf("u.%s", google.Camelize(param, "lower")))
}
return strings.Join(transformed[:], ", ")
}

// def extract_identifiers(url)
func (r Resource) ExtractIdentifiers(url string) []string {
matches := regexp.MustCompile(`\{\{%?(\w+)\}\}`).FindAllStringSubmatch(url, -1)
var result []string
for _, match := range matches {
result = append(result, match[1])
}
return result
}

func (r Resource) ImportIdFormatsFromIam() string {
var importFormat, transformed []string

if r.IamPolicy != nil {
importFormat = r.IamPolicy.ImportFormat
}
if len(importFormat) == 0 {
importFormat = r.ImportFormat
}

importIdFormats := ImportIdFormats(importFormat, r.Identity, r.BaseUrl)
for _, s := range importIdFormats {
s = google.Format2Regex(s)
s = strings.ReplaceAll(s, "<name>", fmt.Sprintf("<%s>", r.IamParentResourceName()))
transformed = append(transformed, s)
}

return strings.Join(transformed[:], "\", \"")
}

func OrderProperties(props []*Type) []*Type {
req := google.Select(props, func(p *Type) bool {
return p.Required
Expand Down
18 changes: 18 additions & 0 deletions mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func wrapMultipleParams(params ...interface{}) (map[string]interface{}, error) {
return m, nil
}

// subtract returns the difference between a and b
// and used in Go templates
func subtract(a, b int) int {
return a - b
}

var TemplateFunctions = template.FuncMap{
"title": google.SpaceSeparatedTitle,
"replace": strings.Replace,
Expand All @@ -78,6 +84,7 @@ var TemplateFunctions = template.FuncMap{
"format2regex": google.Format2Regex,
"orderProperties": api.OrderProperties,
"hasPrefix": strings.HasPrefix,
"sub": subtract,
}

var GA_VERSION = "ga"
Expand Down Expand Up @@ -152,6 +159,17 @@ func (td *TemplateData) GenerateTestFile(filePath string, resource api.Resource)
td.GenerateFile(filePath, templatePath, tmplInput, true, templates...)
}

func (td *TemplateData) GenerateIamPolicyFile(filePath string, resource api.Resource) {
templatePath := "templates/terraform/iam_policy.go.tmpl"
templates := []string{
templatePath,
}
td.GenerateFile(filePath, templatePath, resource, true, templates...)
}

func (td *TemplateData) GenerateIamPolicyTestFile(filePath string, resource api.Resource) {
}

func (td *TemplateData) GenerateFile(filePath, templatePath string, input any, goFormat bool, templates ...string) {
log.Printf("Generating %s", filePath)

Expand Down

0 comments on commit caee19a

Please sign in to comment.