WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit a08282e

Browse files
committed
Retire resource.CustomTemplate in favor of customTemplate $.
Calling a templating method on resources means that the resource object currently means that Resources need to be aware of template locations, filesystem considerations, and in the future `fs.FS` objects. On the other hand if we move this to the `google` package, those "global" template functions will have a way to pass through `fs.FS` objects. Before removing the `CustomTemplate` method from Resource and Type, I've also double-checked that our private overrides do not use these. Note that I'm forced to change Resource.HasLabelsField and remove the pointer: without this I get a `F1205 17:17:34.717325 328294 template_data.go:335] error executing resource.go.tmpl for filepath ${override}/services/compute/resource_compute_address.go template: resource.go.tmpl:442:8: executing "resource.go.tmpl" at <customTemplate $ $.CustomCode.PostCreate false>: error calling customTemplate: template: labels.tmpl:1:8: executing "labels.tmpl" at <$.HasLabelsField>: can't evaluate field HasLabelsField in type api.Resource` error.
1 parent cc5b9ba commit a08282e

22 files changed

+155
-184
lines changed

mmv1/api/resource.go

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package api
1414

1515
import (
16-
"bytes"
1716
"fmt"
1817
"log"
1918
"maps"
@@ -22,9 +21,6 @@ import (
2221
"slices"
2322
"sort"
2423
"strings"
25-
"text/template"
26-
27-
"github.com/golang/glog"
2824

2925
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
3026
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/resource"
@@ -986,7 +982,7 @@ func (r *Resource) addLabelsFields(props []*Type, parent *Type, labels *Type) []
986982
return props
987983
}
988984

989-
func (r *Resource) HasLabelsField() bool {
985+
func (r Resource) HasLabelsField() bool {
990986
for _, p := range r.Properties {
991987
if p.Name == "labels" {
992988
return true
@@ -1976,46 +1972,6 @@ func (r Resource) FormatDocDescription(desc string, indent bool) string {
19761972
return strings.TrimSuffix(returnString, "\n")
19771973
}
19781974

1979-
func (r Resource) CustomTemplate(templatePath string, appendNewline bool) string {
1980-
output := ExecuteTemplate(&r, templatePath, appendNewline)
1981-
if !appendNewline {
1982-
output = strings.TrimSuffix(output, "\n")
1983-
}
1984-
return output
1985-
}
1986-
1987-
func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
1988-
templates := []string{
1989-
templatePath,
1990-
"templates/terraform/expand_resource_ref.tmpl",
1991-
"templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl",
1992-
"templates/terraform/flatten_property_method.go.tmpl",
1993-
"templates/terraform/expand_property_method.go.tmpl",
1994-
"templates/terraform/update_mask.go.tmpl",
1995-
"templates/terraform/nested_query.go.tmpl",
1996-
"templates/terraform/unordered_list_customize_diff.go.tmpl",
1997-
}
1998-
templateFileName := filepath.Base(templatePath)
1999-
2000-
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions).ParseFiles(templates...)
2001-
if err != nil {
2002-
glog.Exit(err)
2003-
}
2004-
2005-
contents := bytes.Buffer{}
2006-
if err = tmpl.ExecuteTemplate(&contents, templateFileName, e); err != nil {
2007-
glog.Exit(err)
2008-
}
2009-
2010-
rs := contents.String()
2011-
2012-
if !strings.HasSuffix(rs, "\n") && appendNewline {
2013-
rs = fmt.Sprintf("%s\n", rs)
2014-
}
2015-
2016-
return rs
2017-
}
2018-
20191975
// Returns the key of the list of resources in the List API response
20201976
// Used to get the list of resources to sweep
20211977
func (r Resource) ResourceListKey() string {

mmv1/api/type.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,10 +1374,6 @@ func (t Type) NamespaceProperty() string {
13741374
return fmt.Sprintf("%s%s%s", google.Camelize(t.ResourceMetadata.ProductMetadata.ApiName, "lower"), t.ResourceMetadata.Name, name)
13751375
}
13761376

1377-
func (t Type) CustomTemplate(templatePath string, appendNewline bool) string {
1378-
return ExecuteTemplate(&t, templatePath, appendNewline)
1379-
}
1380-
13811377
func (t *Type) GetIdFormat() string {
13821378
return t.ResourceMetadata.GetIdFormat()
13831379
}

mmv1/google/template_utils.go

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,30 @@ func plus(a, b int) int {
5454
return a + b
5555
}
5656

57-
var TemplateFunctions = template.FuncMap{
58-
"title": SpaceSeparatedTitle,
59-
"replace": strings.Replace,
60-
"replaceAll": strings.ReplaceAll,
61-
"camelize": Camelize,
62-
"underscore": Underscore,
63-
"plural": Plural,
64-
"contains": strings.Contains,
65-
"join": strings.Join,
66-
"lower": strings.ToLower,
67-
"upper": strings.ToUpper,
68-
"hasSuffix": strings.HasSuffix,
69-
"dict": wrapMultipleParams,
70-
"format2regex": Format2Regex,
71-
"hasPrefix": strings.HasPrefix,
72-
"sub": subtract,
73-
"plus": plus,
74-
"firstSentence": FirstSentence,
75-
"trimTemplate": TrimTemplate,
57+
var TemplateFunctions = templateFunctions()
58+
59+
func templateFunctions() template.FuncMap {
60+
return template.FuncMap{
61+
"title": SpaceSeparatedTitle,
62+
"replace": strings.Replace,
63+
"replaceAll": strings.ReplaceAll,
64+
"camelize": Camelize,
65+
"underscore": Underscore,
66+
"plural": Plural,
67+
"contains": strings.Contains,
68+
"join": strings.Join,
69+
"lower": strings.ToLower,
70+
"upper": strings.ToUpper,
71+
"hasSuffix": strings.HasSuffix,
72+
"dict": wrapMultipleParams,
73+
"format2regex": Format2Regex,
74+
"hasPrefix": strings.HasPrefix,
75+
"sub": subtract,
76+
"plus": plus,
77+
"firstSentence": FirstSentence,
78+
"trimTemplate": TrimTemplate,
79+
"customTemplate": executeCustomTemplate,
80+
}
7681
}
7782

7883
// Temporary function to simulate how Ruby MMv1's lines() function works
@@ -86,27 +91,7 @@ func TrimTemplate(templatePath string, e any) string {
8691

8792
// Need to remake TemplateFunctions, referencing it directly here
8893
// causes a declaration loop
89-
var templateFunctions = template.FuncMap{
90-
"title": SpaceSeparatedTitle,
91-
"replace": strings.Replace,
92-
"replaceAll": strings.ReplaceAll,
93-
"camelize": Camelize,
94-
"underscore": Underscore,
95-
"plural": Plural,
96-
"contains": strings.Contains,
97-
"join": strings.Join,
98-
"lower": strings.ToLower,
99-
"upper": strings.ToUpper,
100-
"dict": wrapMultipleParams,
101-
"format2regex": Format2Regex,
102-
"hasPrefix": strings.HasPrefix,
103-
"sub": subtract,
104-
"plus": plus,
105-
"firstSentence": FirstSentence,
106-
"trimTemplate": TrimTemplate,
107-
}
108-
109-
tmpl, err := template.New(templateFileName).Funcs(templateFunctions).ParseFiles(templates...)
94+
tmpl, err := template.New(templateFileName).Funcs(templateFunctions()).ParseFiles(templates...)
11095
if err != nil {
11196
glog.Exit(err)
11297
}
@@ -127,3 +112,37 @@ func TrimTemplate(templatePath string, e any) string {
127112
}
128113
return fmt.Sprintf("%s\n", rs)
129114
}
115+
116+
func executeCustomTemplate(e any, templatePath string, appendNewline bool) string {
117+
templates := []string{
118+
templatePath,
119+
"templates/terraform/expand_resource_ref.tmpl",
120+
"templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl",
121+
"templates/terraform/flatten_property_method.go.tmpl",
122+
"templates/terraform/expand_property_method.go.tmpl",
123+
"templates/terraform/update_mask.go.tmpl",
124+
"templates/terraform/nested_query.go.tmpl",
125+
"templates/terraform/unordered_list_customize_diff.go.tmpl",
126+
}
127+
templateFileName := filepath.Base(templatePath)
128+
129+
tmpl, err := template.New(templateFileName).Funcs(templateFunctions()).ParseFiles(templates...)
130+
if err != nil {
131+
glog.Exit(err)
132+
}
133+
134+
contents := bytes.Buffer{}
135+
if err = tmpl.ExecuteTemplate(&contents, templateFileName, e); err != nil {
136+
glog.Exit(err)
137+
}
138+
139+
rs := contents.String()
140+
141+
if !strings.HasSuffix(rs, "\n") && appendNewline {
142+
rs = fmt.Sprintf("%s\n", rs)
143+
}
144+
if !appendNewline {
145+
rs = strings.TrimSuffix(rs, "\n")
146+
}
147+
return rs
148+
}

mmv1/templates/terraform/datasource_iam.html.markdown.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ data "{{ $.IamTerraformName }}_policy" "policy" {
6363
provider = google-beta
6464
{{- end }}
6565
{{- if $.IamPolicy.ExampleConfigBody}}
66-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
66+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
6767
{{- end }}
6868

6969
{{- if $.IamPolicy.SampleConfigBody}}
70-
{{- $.CustomTemplate $.IamPolicy.SampleConfigBody false }}
70+
{{- customTemplate $ $.IamPolicy.SampleConfigBody false }}
7171
{{- end }}
7272
}
7373
```

mmv1/templates/terraform/examples/base_configs/iam_test_file.go.tmpl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ resource "{{ $.IamTerraformName }}_member" "foo" {
386386
{{- if eq $.MinVersionObj.Name "beta" }}
387387
provider = google-beta
388388
{{- end }}
389-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
389+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
390390
role = "%{role}"
391391
member = "user:[email protected]"
392392
}
@@ -416,15 +416,15 @@ resource "{{ $.IamTerraformName }}_policy" "foo" {
416416
{{- if eq $.MinVersionObj.Name "beta" }}
417417
provider = google-beta
418418
{{- end }}
419-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
419+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
420420
policy_data = data.google_iam_policy.foo.policy_data
421421
}
422422

423423
data "{{ $.IamTerraformName }}_policy" "foo" {
424424
{{- if eq $.MinVersionObj.Name "beta" }}
425425
provider = google-beta
426426
{{- end }}
427-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
427+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
428428
depends_on = [
429429
{{ $.IamTerraformName }}_policy.foo
430430
]
@@ -445,7 +445,7 @@ resource "{{ $.IamTerraformName }}_policy" "foo" {
445445
{{- if eq $.MinVersionObj.Name "beta" }}
446446
provider = google-beta
447447
{{- end }}
448-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
448+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
449449
policy_data = data.google_iam_policy.foo.policy_data
450450
}
451451
`, context)
@@ -458,7 +458,7 @@ resource "{{ $.IamTerraformName }}_binding" "foo" {
458458
{{- if eq $.MinVersionObj.Name "beta" }}
459459
provider = google-beta
460460
{{- end }}
461-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
461+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
462462
role = "%{role}"
463463
members = ["user:[email protected]"]
464464
}
@@ -472,7 +472,7 @@ resource "{{ $.IamTerraformName }}_binding" "foo" {
472472
{{- if eq $.MinVersionObj.Name "beta" }}
473473
provider = google-beta
474474
{{- end }}
475-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
475+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
476476
role = "%{role}"
477477
members = ["user:[email protected]", "user:[email protected]"]
478478
}
@@ -486,7 +486,7 @@ resource "{{ $.IamTerraformName }}_binding" "foo" {
486486
{{- if eq $.MinVersionObj.Name "beta" }}
487487
provider = google-beta
488488
{{- end }}
489-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
489+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
490490
role = "%{role}"
491491
members = ["user:[email protected]"]
492492
condition {
@@ -505,7 +505,7 @@ resource "{{ $.IamTerraformName }}_binding" "foo" {
505505
{{- if eq $.MinVersionObj.Name "beta" }}
506506
provider = google-beta
507507
{{- end }}
508-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
508+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
509509
role = "%{role}"
510510
members = ["user:[email protected]"]
511511
}
@@ -514,7 +514,7 @@ resource "{{ $.IamTerraformName }}_binding" "foo2" {
514514
{{- if eq $.MinVersionObj.Name "beta" }}
515515
provider = google-beta
516516
{{- end }}
517-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
517+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
518518
role = "%{role}"
519519
members = ["user:[email protected]"]
520520
condition {
@@ -528,7 +528,7 @@ resource "{{ $.IamTerraformName }}_binding" "foo3" {
528528
{{- if eq $.MinVersionObj.Name "beta" }}
529529
provider = google-beta
530530
{{- end }}
531-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
531+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
532532
role = "%{role}"
533533
members = ["user:[email protected]"]
534534
condition {
@@ -548,7 +548,7 @@ resource "{{ $.IamTerraformName }}_member" "foo" {
548548
{{- if eq $.MinVersionObj.Name "beta" }}
549549
provider = google-beta
550550
{{- end }}
551-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
551+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
552552
role = "%{role}"
553553
member = "user:[email protected]"
554554
condition {
@@ -567,7 +567,7 @@ resource "{{ $.IamTerraformName }}_member" "foo" {
567567
{{- if eq $.MinVersionObj.Name "beta" }}
568568
provider = google-beta
569569
{{- end }}
570-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
570+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
571571
role = "%{role}"
572572
member = "user:[email protected]"
573573
}
@@ -576,7 +576,7 @@ resource "{{ $.IamTerraformName }}_member" "foo2" {
576576
{{- if eq $.MinVersionObj.Name "beta" }}
577577
provider = google-beta
578578
{{- end }}
579-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
579+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
580580
role = "%{role}"
581581
member = "user:[email protected]"
582582
condition {
@@ -590,7 +590,7 @@ resource "{{ $.IamTerraformName }}_member" "foo3" {
590590
{{- if eq $.MinVersionObj.Name "beta" }}
591591
provider = google-beta
592592
{{- end }}
593-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
593+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
594594
role = "%{role}"
595595
member = "user:[email protected]"
596596
condition {
@@ -641,7 +641,7 @@ resource "{{ $.IamTerraformName }}_policy" "foo" {
641641
{{- if eq $.MinVersionObj.Name "beta" }}
642642
provider = google-beta
643643
{{- end }}
644-
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody false }}
644+
{{- customTemplate $ $.IamPolicy.ExampleConfigBody false }}
645645
policy_data = data.google_iam_policy.foo.policy_data
646646
}
647647
`, context)

mmv1/templates/terraform/examples/base_configs/test_file.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func testAccCheck{{ $.Res.ResourceName }}DestroyProducer(t *testing.T) func(s *t
134134
continue
135135
}
136136
{{ if $.Res.CustomCode.TestCheckDestroy }}
137-
{{ $.Res.CustomTemplate $.Res.CustomCode.TestCheckDestroy false -}}
137+
{{ customTemplate $.Res $.Res.CustomCode.TestCheckDestroy false -}}
138138
{{- else }}
139139

140140
config := acctest.GoogleProviderConfig(t)

mmv1/templates/terraform/expand_property_method.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License. */ -}}
1515
{{- define "expandPropertyMethod" }}
1616
{{- if $.CustomExpand }}
17-
{{ $.CustomTemplate $.CustomExpand true -}}
17+
{{ customTemplate $ $.CustomExpand true -}}
1818
{{- else }}{{/* if $.CustomExpand */}}
1919
{{- if $.IsA "Map" }}
2020
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) {

mmv1/templates/terraform/flatten_property_method.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{{- define "flattenPropertyMethod" }}
1616
{{- if or $.WriteOnlyLegacy $.WriteOnly }}
1717
{{- else if and $.CustomFlatten (not $.ShouldIgnoreCustomFlatten) }}
18-
{{- $.CustomTemplate $.CustomFlatten false -}}
18+
{{- customTemplate $ $.CustomFlatten false -}}
1919
{{- else -}}
2020
func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2121
{{- if or (and $.IgnoreRead (not $.ResourceMetadata.IsTgcCompiler)) $.ClientSide }}

mmv1/templates/terraform/iam_policy.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var {{ $.ResourceName }}IamSchema = map[string]*schema.Schema{
6565
}
6666

6767
{{- if and $.IamPolicy $.IamPolicy.CustomDiffSuppress }}
68-
{{ $.CustomTemplate $.IamPolicy.CustomDiffSuppress true }}
68+
{{ customTemplate $ $.IamPolicy.CustomDiffSuppress true }}
6969
{{- end }}
7070

7171
type {{ $.ResourceName }}IamUpdater struct {

mmv1/templates/terraform/nested_query.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func resource{{ $.ResourceName }}ListForPatch(d *schema.ResourceData, meta inter
262262
}
263263

264264
{{- if $.CustomCode.PostRead }}
265-
{{ $.CustomTemplate $.CustomCode.PostRead false -}}
265+
{{ customTemplate $ $.CustomCode.PostRead false -}}
266266
{{- end }}
267267
var v interface{}
268268
var ok bool

0 commit comments

Comments
 (0)