Skip to content

Commit

Permalink
Add yaml source to metadata file (#12917)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottSuarez authored Feb 3, 2025
1 parent 110b49b commit 4036e33
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
45 changes: 44 additions & 1 deletion mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ type Resource struct {
// fine-grained resources and legacy resources.
ApiResourceTypeKind string `yaml:"api_resource_type_kind,omitempty"`

ImportPath string `yaml:"-"`
ImportPath string `yaml:"-"`
SourceYamlFile string `yaml:"-"`
}

func (r *Resource) UnmarshalYAML(unmarshal func(any) error) error {
Expand Down Expand Up @@ -1787,3 +1788,45 @@ func (r Resource) CaiIamAssetNameTemplate(productBackendName string) string {
}
return fmt.Sprintf("//%s.googleapis.com/%s/{{%s}}", productBackendName, caiBaseUrl, r.IamParentResourceName())
}

func urlContainsOnlyAllowedKeys(templateURL string, allowedKeys []string) bool {
// Create regex to match anything between {{ and }}
re := regexp.MustCompile(`{{\s*([^}]+)\s*}}`)

// Find all matches in the template URL
matches := re.FindAllStringSubmatch(templateURL, -1)

// Create a map of allowed keys for O(1) lookup
allowedKeysMap := make(map[string]bool)
for _, key := range allowedKeys {
allowedKeysMap[key] = true
}

// Check each found key against the allowed keys
for _, match := range matches {
if len(match) < 2 {
continue
}

// Trim spaces from the key
key := strings.TrimSpace(match[1])

// If the key isn't in our allowed list, return false
if !allowedKeysMap[key] {
return false
}
}

return true
}

func (r Resource) ShouldGenerateSweepers() bool {
allowedKeys := []string{"project", "region", "location", "zone", "billing_account"}
if !urlContainsOnlyAllowedKeys(r.ListUrlTemplate(), allowedKeys) {
return false
}
if r.ExcludeSweeper || r.CustomCode.CustomDelete != "" || r.CustomCode.PreDelete != "" || r.CustomCode.PostDelete != "" || r.ExcludeDelete {
return false
}
return true
}
1 change: 1 addition & 0 deletions mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func GenerateProduct(productChannel chan string, providerToGenerate provider.Pro

resource := &api.Resource{}
api.Compile(resourceYamlPath, resource, overrideDirectory)
resource.SourceYamlFile = resourceYamlPath

resource.TargetVersionName = *version
resource.Properties = resource.AddLabelsRelatedFields(resource.PropertiesWithExcluded(), nil)
Expand Down
38 changes: 1 addition & 37 deletions mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -173,43 +172,8 @@ func (t *Terraform) GenerateResourceTests(object api.Resource, templateData Temp
templateData.GenerateTestFile(targetFilePath, object)
}

func urlContainsOnlyAllowedKeys(templateURL string, allowedKeys []string) bool {
// Create regex to match anything between {{ and }}
re := regexp.MustCompile(`{{\s*([^}]+)\s*}}`)

// Find all matches in the template URL
matches := re.FindAllStringSubmatch(templateURL, -1)

// Create a map of allowed keys for O(1) lookup
allowedKeysMap := make(map[string]bool)
for _, key := range allowedKeys {
allowedKeysMap[key] = true
}

// Check each found key against the allowed keys
for _, match := range matches {
if len(match) < 2 {
continue
}

// Trim spaces from the key
key := strings.TrimSpace(match[1])

// If the key isn't in our allowed list, return false
if !allowedKeysMap[key] {
return false
}
}

return true
}

func (t *Terraform) GenerateResourceSweeper(object api.Resource, templateData TemplateData, outputFolder string) {
allowedKeys := []string{"project", "region", "location", "zone", "billing_account"}
if !urlContainsOnlyAllowedKeys(object.ListUrlTemplate(), allowedKeys) {
return
}
if object.ExcludeSweeper || object.CustomCode.CustomDelete != "" || object.CustomCode.PreDelete != "" || object.CustomCode.PostDelete != "" || object.ExcludeDelete {
if !object.ShouldGenerateSweepers() {
return
}

Expand Down
1 change: 1 addition & 0 deletions mmv1/templates/terraform/metadata.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resource: '{{ $.TerraformName }}'
generation_type: 'mmv1'
source_file: '{{ $.SourceYamlFile }}'
api_service_name: '{{ $.ProductMetadata.ServiceName }}'
api_version: '{{ or $.ProductMetadata.ServiceVersion $.ServiceVersion }}'
api_resource_type_kind: '{{ or $.ApiResourceTypeKind $.Name }}'
Expand Down

0 comments on commit 4036e33

Please sign in to comment.