Skip to content

Commit 860a8ee

Browse files
slevenickNandiniAgrawal15
authored andcommitted
Check example vars (GoogleCloudPlatform#13938)
1 parent 017ad8e commit 860a8ee

25 files changed

+112
-30
lines changed

mmv1/api/resource.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@
1313
package api
1414

1515
import (
16+
"bytes"
1617
"fmt"
1718
"log"
1819
"maps"
20+
"path/filepath"
1921
"regexp"
22+
"slices"
2023
"sort"
2124
"strings"
25+
"text/template"
26+
27+
"github.com/golang/glog"
2228

2329
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
2430
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/resource"
2531
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/utils"
2632
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
27-
"golang.org/x/exp/slices"
2833
)
2934

3035
const RELATIVE_MAGICIAN_LOCATION = "mmv1/"
@@ -1590,13 +1595,45 @@ func (r Resource) FormatDocDescription(desc string, indent bool) string {
15901595
}
15911596

15921597
func (r Resource) CustomTemplate(templatePath string, appendNewline bool) string {
1593-
output := resource.ExecuteTemplate(&r, templatePath, appendNewline)
1598+
output := ExecuteTemplate(&r, templatePath, appendNewline)
15941599
if !appendNewline {
15951600
output = strings.TrimSuffix(output, "\n")
15961601
}
15971602
return output
15981603
}
15991604

1605+
func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
1606+
templates := []string{
1607+
templatePath,
1608+
"templates/terraform/expand_resource_ref.tmpl",
1609+
"templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl",
1610+
"templates/terraform/flatten_property_method.go.tmpl",
1611+
"templates/terraform/expand_property_method.go.tmpl",
1612+
"templates/terraform/update_mask.go.tmpl",
1613+
"templates/terraform/nested_query.go.tmpl",
1614+
"templates/terraform/unordered_list_customize_diff.go.tmpl",
1615+
}
1616+
templateFileName := filepath.Base(templatePath)
1617+
1618+
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions).ParseFiles(templates...)
1619+
if err != nil {
1620+
glog.Exit(err)
1621+
}
1622+
1623+
contents := bytes.Buffer{}
1624+
if err = tmpl.ExecuteTemplate(&contents, templateFileName, e); err != nil {
1625+
glog.Exit(err)
1626+
}
1627+
1628+
rs := contents.String()
1629+
1630+
if !strings.HasSuffix(rs, "\n") && appendNewline {
1631+
rs = fmt.Sprintf("%s\n", rs)
1632+
}
1633+
1634+
return rs
1635+
}
1636+
16001637
// Returns the key of the list of resources in the List API response
16011638
// Used to get the list of resources to sweep
16021639
func (r Resource) ResourceListKey() string {

mmv1/api/resource/examples.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"log"
2020
"net/url"
21+
"os"
2122
"path/filepath"
2223
"regexp"
2324
"slices"
@@ -201,6 +202,22 @@ func (e *Examples) Validate(rName string) {
201202
e.ValidateExternalProviders()
202203
}
203204

205+
func validateRegexForContents(r *regexp.Regexp, contents string, configPath string, objName string, vars map[string]string) {
206+
matches := r.FindAllStringSubmatch(contents, -1)
207+
for _, v := range matches {
208+
found := false
209+
for k, _ := range vars {
210+
if k == v[1] {
211+
found = true
212+
break
213+
}
214+
}
215+
if !found {
216+
log.Fatalf("Failed to find %s environment variable defined in YAML file when validating the file %s. Please define this in %s", v[1], configPath, objName)
217+
}
218+
}
219+
}
220+
204221
func (e *Examples) ValidateExternalProviders() {
205222
// Official providers supported by HashiCorp
206223
// https://registry.terraform.io/search/providers?namespace=hashicorp&tier=official
@@ -249,7 +266,7 @@ func (e *Examples) SetHCLText() {
249266
docTestEnvVars[key] = docs_defaults[e.TestEnvVars[key]]
250267
}
251268
e.TestEnvVars = docTestEnvVars
252-
e.DocumentationHCLText = ExecuteTemplate(e, e.ConfigPath, true)
269+
e.DocumentationHCLText = e.ExecuteTemplate()
253270
e.DocumentationHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(e.DocumentationHCLText, "\n")
254271

255272
// Remove region tags
@@ -290,7 +307,7 @@ func (e *Examples) SetHCLText() {
290307

291308
e.Vars = testVars
292309
e.TestEnvVars = testTestEnvVars
293-
e.TestHCLText = ExecuteTemplate(e, e.ConfigPath, true)
310+
e.TestHCLText = e.ExecuteTemplate()
294311
e.TestHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(e.TestHCLText, "\n")
295312
// Remove region tags
296313
e.TestHCLText = re1.ReplaceAllString(e.TestHCLText, "")
@@ -302,20 +319,23 @@ func (e *Examples) SetHCLText() {
302319
e.TestEnvVars = originalTestEnvVars
303320
}
304321

305-
func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
306-
templates := []string{
307-
templatePath,
308-
"templates/terraform/expand_resource_ref.tmpl",
309-
"templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl",
310-
"templates/terraform/flatten_property_method.go.tmpl",
311-
"templates/terraform/expand_property_method.go.tmpl",
312-
"templates/terraform/update_mask.go.tmpl",
313-
"templates/terraform/nested_query.go.tmpl",
314-
"templates/terraform/unordered_list_customize_diff.go.tmpl",
322+
func (e *Examples) ExecuteTemplate() string {
323+
templateContent, err := os.ReadFile(e.ConfigPath)
324+
if err != nil {
325+
glog.Exit(err)
315326
}
316-
templateFileName := filepath.Base(templatePath)
317327

318-
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions).ParseFiles(templates...)
328+
fileContentString := string(templateContent)
329+
330+
// Check that any variables in Vars or TestEnvVars used in the example are defined via YAML
331+
envVarRegex := regexp.MustCompile(`{{index \$\.TestEnvVars "([a-zA-Z_]*)"}}`)
332+
validateRegexForContents(envVarRegex, fileContentString, e.ConfigPath, "test_env_vars", e.TestEnvVars)
333+
varRegex := regexp.MustCompile(`{{index \$\.Vars "([a-zA-Z_]*)"}}`)
334+
validateRegexForContents(varRegex, fileContentString, e.ConfigPath, "vars", e.Vars)
335+
336+
templateFileName := filepath.Base(e.ConfigPath)
337+
338+
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions).Parse(fileContentString)
319339
if err != nil {
320340
glog.Exit(err)
321341
}
@@ -327,7 +347,7 @@ func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
327347

328348
rs := contents.String()
329349

330-
if !strings.HasSuffix(rs, "\n") && appendNewline {
350+
if !strings.HasSuffix(rs, "\n") {
331351
rs = fmt.Sprintf("%s\n", rs)
332352
}
333353

@@ -401,7 +421,7 @@ func (e *Examples) SetOiCSHCLText() {
401421
}
402422

403423
e.Vars = testVars
404-
e.OicsHCLText = ExecuteTemplate(e, e.ConfigPath, true)
424+
e.OicsHCLText = e.ExecuteTemplate()
405425
e.OicsHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(e.OicsHCLText, "\n")
406426

407427
// Remove region tags

mmv1/api/type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ func (t Type) NamespaceProperty() string {
10781078
}
10791079

10801080
func (t Type) CustomTemplate(templatePath string, appendNewline bool) string {
1081-
return resource.ExecuteTemplate(&t, templatePath, appendNewline)
1081+
return ExecuteTemplate(&t, templatePath, appendNewline)
10821082
}
10831083

10841084
func (t *Type) GetIdFormat() string {

mmv1/products/accesscontextmanager/AuthorizedOrgsDesc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ examples:
6161
- name: 'access_context_manager_authorized_orgs_desc_basic'
6262
primary_resource_id: 'authorized-orgs-desc'
6363
exclude_test: true
64+
test_env_vars:
65+
org_id: 'ORG_ID'
6466
parameters:
6567
- name: 'parent'
6668
type: String

mmv1/products/apigee/EnvgroupAttachment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ examples:
5252
project_id: 'my-project'
5353
envgroup_name: 'my-envgroup'
5454
environment_name: 'my-environment'
55+
test_env_vars:
56+
org_id: 'ORG_ID'
57+
billing_account: 'BILLING_ACCT'
5558
exclude_test: true
5659
- name: 'apigee_environment_group_attachment_basic_test'
5760
primary_resource_id: 'apigee_environment_group_attachment'

mmv1/products/apigee/InstanceAttachment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ examples:
5151
project_id: 'my-project'
5252
instance_name: 'my-instance-name'
5353
environment_name: 'my-environment-name'
54+
test_env_vars:
55+
org_id: 'ORG_ID'
56+
billing_account: 'BILLING_ACCT'
5457
exclude_test: true
5558
# This is a more verbose version of the above that creates all
5659
# the resources needed for the acceptance test.

mmv1/products/bigquerydatatransfer/Config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ examples:
6060
dataset_id: 'example_dataset'
6161
key_name: 'example-key'
6262
keyring_name: 'example-keyring'
63+
display_name: 'display-name'
6364
exclude_test: true
6465
- name: 'bigquerydatatransfer_config_salesforce'
6566
primary_resource_id: 'salesforce_config'

mmv1/products/cloudbuild/Trigger.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ examples:
8282
cloudbuild_trigger_name: 'manual-trigger'
8383
- name: 'cloudbuild_trigger_manual_github_enterprise'
8484
primary_resource_id: 'manual-ghe-trigger'
85+
vars:
86+
cloudbuild_trigger_name: 'my-trigger'
8587
exclude_test: true
8688
- name: 'cloudbuild_trigger_manual_bitbucket_server'
8789
primary_resource_id: 'manual-bitbucket-trigger'

mmv1/products/colab/Schedule.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ examples:
7676
dataform_repository: 'dataform-repository'
7777
start_time: '2014-10-02T15:01:23Z'
7878
end_time: '2014-10-10T15:01:23Z'
79+
key_name: 'my-key'
7980
test_env_vars:
8081
project_id: 'PROJECT_NAME'
8182
location: 'REGION'

mmv1/products/datastream/Stream.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ examples:
216216
- name: 'datastream_stream_salesforce'
217217
primary_resource_id: 'default'
218218
vars:
219+
stream_id: 'sf-stream'
219220
source_connection_profile_id: 'source-profile'
220221
destination_connection_profile_id: 'destination-profile'
221222
exclude_test: true

0 commit comments

Comments
 (0)