Skip to content

Commit 73fd6c0

Browse files
committed
Move ExecuteTemplate to be examples specific
1 parent f0562c2 commit 73fd6c0

File tree

3 files changed

+59
-35
lines changed

3 files changed

+59
-35
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: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -199,25 +199,9 @@ func (e *Examples) Validate(rName string) {
199199
if e.Name == "" {
200200
log.Fatalf("Missing `name` for one example in resource %s", rName)
201201
}
202-
e.ValidateVariables()
203202
e.ValidateExternalProviders()
204203
}
205204

206-
func (e *Examples) ValidateVariables() {
207-
// Read file as raw text to pull variables
208-
content, err := os.ReadFile(e.ConfigPath)
209-
if err != nil {
210-
glog.Exit(err)
211-
}
212-
213-
fileContentString := string(content)
214-
215-
envVarRegex := regexp.MustCompile(`{{index \$\.TestEnvVars "([a-zA-Z_]*)"}}`)
216-
validateRegexForContents(envVarRegex, fileContentString, e.ConfigPath, "test_env_vars", e.TestEnvVars)
217-
varRegex := regexp.MustCompile(`{{index \$\.Vars "([a-zA-Z_]*)"}}`)
218-
validateRegexForContents(varRegex, fileContentString, e.ConfigPath, "vars", e.Vars)
219-
}
220-
221205
func validateRegexForContents(r *regexp.Regexp, contents string, configPath string, objName string, vars map[string]string) {
222206
matches := r.FindAllStringSubmatch(contents, -1)
223207
for _, v := range matches {
@@ -282,7 +266,7 @@ func (e *Examples) SetHCLText() {
282266
docTestEnvVars[key] = docs_defaults[e.TestEnvVars[key]]
283267
}
284268
e.TestEnvVars = docTestEnvVars
285-
e.DocumentationHCLText = ExecuteTemplate(e, e.ConfigPath, true)
269+
e.DocumentationHCLText = e.ExecuteTemplate()
286270
e.DocumentationHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(e.DocumentationHCLText, "\n")
287271

288272
// Remove region tags
@@ -323,7 +307,7 @@ func (e *Examples) SetHCLText() {
323307

324308
e.Vars = testVars
325309
e.TestEnvVars = testTestEnvVars
326-
e.TestHCLText = ExecuteTemplate(e, e.ConfigPath, true)
310+
e.TestHCLText = e.ExecuteTemplate()
327311
e.TestHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(e.TestHCLText, "\n")
328312
// Remove region tags
329313
e.TestHCLText = re1.ReplaceAllString(e.TestHCLText, "")
@@ -335,20 +319,23 @@ func (e *Examples) SetHCLText() {
335319
e.TestEnvVars = originalTestEnvVars
336320
}
337321

338-
func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
339-
templates := []string{
340-
templatePath,
341-
"templates/terraform/expand_resource_ref.tmpl",
342-
"templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl",
343-
"templates/terraform/flatten_property_method.go.tmpl",
344-
"templates/terraform/expand_property_method.go.tmpl",
345-
"templates/terraform/update_mask.go.tmpl",
346-
"templates/terraform/nested_query.go.tmpl",
347-
"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)
348326
}
349-
templateFileName := filepath.Base(templatePath)
350327

351-
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)
352339
if err != nil {
353340
glog.Exit(err)
354341
}
@@ -360,7 +347,7 @@ func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
360347

361348
rs := contents.String()
362349

363-
if !strings.HasSuffix(rs, "\n") && appendNewline {
350+
if !strings.HasSuffix(rs, "\n") {
364351
rs = fmt.Sprintf("%s\n", rs)
365352
}
366353

@@ -434,7 +421,7 @@ func (e *Examples) SetOiCSHCLText() {
434421
}
435422

436423
e.Vars = testVars
437-
e.OicsHCLText = ExecuteTemplate(e, e.ConfigPath, true)
424+
e.OicsHCLText = e.ExecuteTemplate()
438425
e.OicsHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(e.OicsHCLText, "\n")
439426

440427
// 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 {

0 commit comments

Comments
 (0)