Skip to content

Commit e706e96

Browse files
authored
decoupling from filesystem ops: phase 2 (#15833)
1 parent 513c50b commit e706e96

File tree

14 files changed

+215
-171
lines changed

14 files changed

+215
-171
lines changed

mmv1/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
visibility = ["//visibility:private"],
88
deps = [
99
"//mmv1/api",
10+
"//mmv1/google",
1011
"//mmv1/loader",
1112
"//mmv1/openapi_generate",
1213
"//mmv1/provider",

mmv1/api/compiler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import (
2121
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
2222
)
2323

24-
func Compile(yamlPath string, obj interface{}, overrideDir string) {
24+
func Compile(yamlPath string, obj interface{}) {
2525
objYaml, err := os.ReadFile(yamlPath)
2626

2727
if err != nil {
2828
log.Fatalf("Cannot open the file: %s", yamlPath)
2929
}
3030

31-
if overrideDir != "" {
32-
objYaml = bytes.ReplaceAll(objYaml, []byte("{{override_path}}"), []byte(overrideDir))
33-
}
31+
// TODO: retire {{override_path}} from private overrides repositories,
32+
// and remove this later.
33+
objYaml = bytes.ReplaceAll(objYaml, []byte("{{override_path}}/"), []byte(""))
3434

3535
yamlValidator := google.YamlValidator{}
3636
yamlValidator.Parse(objYaml, obj, yamlPath)

mmv1/api/resource.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package api
1414

1515
import (
1616
"fmt"
17+
"io/fs"
1718
"log"
1819
"maps"
1920
"path/filepath"
@@ -2090,7 +2091,7 @@ func (r Resource) TestSamples() []*resource.Sample {
20902091
})
20912092
}
20922093

2093-
func (r Resource) TestSampleSetUp() {
2094+
func (r Resource) TestSampleSetUp(sysfs fs.FS) {
20942095
res := make(map[string]string)
20952096
for _, sample := range r.Samples {
20962097
sample.TargetVersionName = r.TargetVersionName
@@ -2106,7 +2107,7 @@ func (r Resource) TestSampleSetUp() {
21062107
if step.ConfigPath == "" {
21072108
step.ConfigPath = fmt.Sprintf("templates/terraform/samples/services/%s/%s.tf.tmpl", packageName, step.Name)
21082109
}
2109-
step.SetHCLText()
2110+
step.SetHCLText(sysfs)
21102111
configName := step.Name
21112112
if _, ok := res[step.Name]; !ok {
21122113
res[configName] = sample.Name

mmv1/api/resource/examples.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ package resource
1616
import (
1717
"bytes"
1818
"fmt"
19+
"io/fs"
1920
"log"
2021
"net/url"
21-
"os"
2222
"path/filepath"
2323
"regexp"
2424
"slices"
@@ -257,7 +257,7 @@ func (e *Examples) ValidateExternalProviders() error {
257257
}
258258

259259
// Executes example templates for documentation and tests
260-
func (e *Examples) LoadHCLText(baseDir string) (err error) {
260+
func (e *Examples) LoadHCLText(sysfs fs.FS) (err error) {
261261
originalVars := e.Vars
262262
originalTestEnvVars := e.TestEnvVars
263263
docTestEnvVars := make(map[string]string)
@@ -284,7 +284,7 @@ func (e *Examples) LoadHCLText(baseDir string) (err error) {
284284
docTestEnvVars[key] = docs_defaults[e.TestEnvVars[key]]
285285
}
286286
e.TestEnvVars = docTestEnvVars
287-
e.DocumentationHCLText, err = e.ExecuteTemplate(baseDir)
287+
e.DocumentationHCLText, err = e.ExecuteTemplate(sysfs)
288288
if err != nil {
289289
return err
290290
}
@@ -328,7 +328,7 @@ func (e *Examples) LoadHCLText(baseDir string) (err error) {
328328

329329
e.Vars = testVars
330330
e.TestEnvVars = testTestEnvVars
331-
e.TestHCLText, err = e.ExecuteTemplate(baseDir)
331+
e.TestHCLText, err = e.ExecuteTemplate(sysfs)
332332
if err != nil {
333333
return err
334334
}
@@ -344,8 +344,8 @@ func (e *Examples) LoadHCLText(baseDir string) (err error) {
344344
return nil
345345
}
346346

347-
func (e *Examples) ExecuteTemplate(baseDir string) (string, error) {
348-
templateContent, err := os.ReadFile(filepath.Join(baseDir, e.ConfigPath))
347+
func (e *Examples) ExecuteTemplate(sysfs fs.FS) (string, error) {
348+
templateContent, err := fs.ReadFile(sysfs, e.ConfigPath)
349349
if err != nil {
350350
return "", err
351351
}
@@ -359,7 +359,7 @@ func (e *Examples) ExecuteTemplate(baseDir string) (string, error) {
359359
validateRegexForContents(varRegex, fileContentString, e.ConfigPath, "vars", e.Vars)
360360

361361
templateFileName := filepath.Base(e.ConfigPath)
362-
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions).Parse(fileContentString)
362+
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions(sysfs)).Parse(fileContentString)
363363
if err != nil {
364364
return "", err
365365
}
@@ -408,7 +408,7 @@ func (e *Examples) ResourceType(terraformName string) string {
408408
}
409409

410410
// Executes example templates for documentation and tests
411-
func (e *Examples) SetOiCSHCLText() {
411+
func (e *Examples) SetOiCSHCLText(sysfs fs.FS) {
412412
var err error
413413
originalVars := e.Vars
414414
originalTestEnvVars := e.TestEnvVars
@@ -430,7 +430,7 @@ func (e *Examples) SetOiCSHCLText() {
430430
e.Vars = testVars
431431
// SetOiCSHCLText is generated from the provider, assume base directory is
432432
// always relative for this case
433-
e.OicsHCLText, err = e.ExecuteTemplate("")
433+
e.OicsHCLText, err = e.ExecuteTemplate(sysfs)
434434
if err != nil {
435435
log.Fatal(err)
436436
}

mmv1/api/resource/step.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ package resource
1616
import (
1717
"bytes"
1818
"fmt"
19+
"io/fs"
1920
"log"
2021
"net/url"
21-
"os"
2222
"path/filepath"
2323
"regexp"
2424
"strings"
@@ -123,7 +123,6 @@ func (s *Step) Validate(rName, sName string) {
123123
if s.Name == "" {
124124
log.Fatalf("Missing `name` for one step in test sample %s in resource %s", sName, rName)
125125
}
126-
127126
}
128127

129128
func validateRegexForContents(r *regexp.Regexp, contents string, configPath string, objName string, vars map[string]string) {
@@ -143,7 +142,7 @@ func validateRegexForContents(r *regexp.Regexp, contents string, configPath stri
143142
}
144143

145144
// Executes step configuration templates for documentation and tests
146-
func (s *Step) SetHCLText() {
145+
func (s *Step) SetHCLText(sysfs fs.FS) {
147146
originalPrefixedVars := s.PrefixedVars
148147
// originalVars := s.Vars
149148
originalTestEnvVars := s.TestEnvVars
@@ -171,7 +170,7 @@ func (s *Step) SetHCLText() {
171170
docTestEnvVars[key] = docs_defaults[s.TestEnvVars[key]]
172171
}
173172
s.TestEnvVars = docTestEnvVars
174-
s.DocumentationHCLText = s.ExecuteTemplate()
173+
s.DocumentationHCLText = s.ExecuteTemplate(sysfs)
175174
s.DocumentationHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(s.DocumentationHCLText, "\n")
176175

177176
// Remove region tags
@@ -215,7 +214,7 @@ func (s *Step) SetHCLText() {
215214

216215
s.PrefixedVars = testPrefixedVars
217216
s.TestEnvVars = testTestEnvVars
218-
s.TestHCLText = s.ExecuteTemplate()
217+
s.TestHCLText = s.ExecuteTemplate(sysfs)
219218
s.TestHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(s.TestHCLText, "\n")
220219
// Remove region tags
221220
s.TestHCLText = re1.ReplaceAllString(s.TestHCLText, "")
@@ -227,8 +226,8 @@ func (s *Step) SetHCLText() {
227226
s.TestEnvVars = originalTestEnvVars
228227
}
229228

230-
func (s *Step) ExecuteTemplate() string {
231-
templateContent, err := os.ReadFile(s.ConfigPath)
229+
func (s *Step) ExecuteTemplate(sysfs fs.FS) string {
230+
templateContent, err := fs.ReadFile(sysfs, s.ConfigPath)
232231
if err != nil {
233232
glog.Exit(err)
234233
}
@@ -245,7 +244,7 @@ func (s *Step) ExecuteTemplate() string {
245244

246245
templateFileName := filepath.Base(s.ConfigPath)
247246

248-
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions).Parse(fileContentString)
247+
tmpl, err := template.New(templateFileName).Funcs(google.TemplateFunctions(sysfs)).Parse(fileContentString)
249248
if err != nil {
250249
glog.Exit(err)
251250
}
@@ -300,7 +299,7 @@ func SubstituteTestPaths(config string) string {
300299
}
301300

302301
// Executes step configuration templates for documentation and tests
303-
func (s *Step) SetOiCSHCLText() {
302+
func (s *Step) SetOiCSHCLText(sysfs fs.FS) {
304303
originalPrefixedVars := s.PrefixedVars
305304

306305
// // Remove region tags
@@ -318,7 +317,7 @@ func (s *Step) SetOiCSHCLText() {
318317
}
319318

320319
s.PrefixedVars = testPrefixedVars
321-
s.OicsHCLText = s.ExecuteTemplate()
320+
s.OicsHCLText = s.ExecuteTemplate(sysfs)
322321
s.OicsHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(s.OicsHCLText, "\n")
323322

324323
// Remove region tags

mmv1/google/template_utils.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"bytes"
1818
"errors"
1919
"fmt"
20+
"io/fs"
2021
"path/filepath"
2122
"reflect"
2223
"strings"
@@ -55,9 +56,15 @@ func plus(a, b int) int {
5556
return a + b
5657
}
5758

58-
var TemplateFunctions = templateFunctions()
59+
func TemplateFunctions(templateFs fs.FS) template.FuncMap {
60+
return functionsData{templateFS: templateFs}.templateFunctions()
61+
}
62+
63+
type functionsData struct {
64+
templateFS fs.FS
65+
}
5966

60-
func templateFunctions() template.FuncMap {
67+
func (t functionsData) templateFunctions() template.FuncMap {
6168
return template.FuncMap{
6269
"title": SpaceSeparatedTitle,
6370
"replace": strings.Replace,
@@ -76,8 +83,8 @@ func templateFunctions() template.FuncMap {
7683
"sub": subtract,
7784
"plus": plus,
7885
"firstSentence": FirstSentence,
79-
"trimTemplate": TrimTemplate,
80-
"customTemplate": executeCustomTemplate,
86+
"trimTemplate": t.trimTemplate,
87+
"customTemplate": t.customTemplate,
8188
}
8289
}
8390

@@ -95,38 +102,36 @@ func structToPtr(e any) reflect.Value {
95102

96103
// Temporary function to simulate how Ruby MMv1's lines() function works
97104
// for nested documentation. Can replace with normal "template" after switchover
98-
func TrimTemplate(templatePath string, e any) string {
105+
func (t *functionsData) trimTemplate(templatePath string, e any) (string, error) {
99106
templates := []string{
100107
fmt.Sprintf("templates/terraform/%s", templatePath),
101108
"templates/terraform/expand_resource_ref.tmpl",
102109
}
103110
templateFileName := filepath.Base(templatePath)
104111

105-
// Need to remake TemplateFunctions, referencing it directly here
106-
// causes a declaration loop
107-
tmpl, err := template.New(templateFileName).Funcs(templateFunctions()).ParseFiles(templates...)
112+
tmpl, err := template.New(templateFileName).Funcs(t.templateFunctions()).ParseFS(t.templateFS, templates...)
108113
if err != nil {
109-
glog.Exit(err)
114+
return "", err
110115
}
111116

112117
contents := bytes.Buffer{}
113118
if err = tmpl.ExecuteTemplate(&contents, templateFileName, structToPtr(e)); err != nil {
114-
glog.Exit(err)
119+
return "", err
115120
}
116121

117122
rs := contents.String()
118123

119124
if rs == "" {
120-
return rs
125+
return "", nil
121126
}
122127

123128
for strings.HasSuffix(rs, "\n") {
124129
rs = strings.TrimSuffix(rs, "\n")
125130
}
126-
return fmt.Sprintf("%s\n", rs)
131+
return fmt.Sprintf("%s\n", rs), nil
127132
}
128133

129-
func executeCustomTemplate(e any, templatePath string, appendNewline bool) string {
134+
func (t functionsData) customTemplate(e any, templatePath string, appendNewline bool) (string, error) {
130135
templates := []string{
131136
templatePath,
132137
"templates/terraform/expand_resource_ref.tmpl",
@@ -139,9 +144,9 @@ func executeCustomTemplate(e any, templatePath string, appendNewline bool) strin
139144
}
140145
templateFileName := filepath.Base(templatePath)
141146

142-
tmpl, err := template.New(templateFileName).Funcs(templateFunctions()).ParseFiles(templates...)
147+
tmpl, err := template.New(templateFileName).Funcs(t.templateFunctions()).ParseFS(t.templateFS, templates...)
143148
if err != nil {
144-
glog.Exit(err)
149+
return "", err
145150
}
146151

147152
contents := bytes.Buffer{}
@@ -157,5 +162,5 @@ func executeCustomTemplate(e any, templatePath string, appendNewline bool) strin
157162
if !appendNewline {
158163
rs = strings.TrimSuffix(rs, "\n")
159164
}
160-
return rs
165+
return rs, nil
161166
}

0 commit comments

Comments
 (0)