From 4036e331551b06d5dab14d0248bf5a93e658c52a Mon Sep 17 00:00:00 2001 From: Scott Suarez Date: Mon, 3 Feb 2025 13:26:34 -0800 Subject: [PATCH] Add yaml source to metadata file (#12917) --- mmv1/api/resource.go | 45 ++++++++++++++++++++- mmv1/main.go | 1 + mmv1/provider/terraform.go | 38 +---------------- mmv1/templates/terraform/metadata.yaml.tmpl | 1 + 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/mmv1/api/resource.go b/mmv1/api/resource.go index 3fae2cd8a364..77e0824261e1 100644 --- a/mmv1/api/resource.go +++ b/mmv1/api/resource.go @@ -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 { @@ -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 +} diff --git a/mmv1/main.go b/mmv1/main.go index 214bfc49dbce..e4c8cfbb0427 100644 --- a/mmv1/main.go +++ b/mmv1/main.go @@ -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) diff --git a/mmv1/provider/terraform.go b/mmv1/provider/terraform.go index b25a0c9f389d..d8c58a65f1a2 100644 --- a/mmv1/provider/terraform.go +++ b/mmv1/provider/terraform.go @@ -24,7 +24,6 @@ import ( "os" "path" "path/filepath" - "regexp" "slices" "strings" "time" @@ -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 } diff --git a/mmv1/templates/terraform/metadata.yaml.tmpl b/mmv1/templates/terraform/metadata.yaml.tmpl index 75466ddd5586..2fb1b87ae05a 100644 --- a/mmv1/templates/terraform/metadata.yaml.tmpl +++ b/mmv1/templates/terraform/metadata.yaml.tmpl @@ -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 }}'