-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
260 lines (215 loc) · 7.6 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 Orange
* SPDX-License-Identifier: Mozilla Public License 2.0
*
* This software is distributed under the MPL-2.0 license.
* the text of which is available at https://www.mozilla.org/en-US/MPL/2.0/
* or see the "LICENSE" file for more details.
*/
package main
import (
"bytes"
"html/template"
"os"
"path/filepath"
"strings"
"github.com/iancoleman/strcase"
"github.com/orange-cloudavenue/terraform-resource-templates/pkg/file"
_ "embed"
)
type templateDef struct {
CategoryName string
ResourceName string
Name string
PackageName string
LowerCamelName string
SnakeName string
CamelName string
Filename string
TestDir string
SchemaDir string
TemplateDocDir string
ExamplesDir string
FullSnakeResourceName string
FullCamelResourceName string
IsResource bool
}
//go:embed templates/datasource.go.tmpl
var templateDatasource string
//go:embed templates/resource.go.tmpl
var templateResource string
//go:embed templates/acc_test_resource.go.tmpl
var templateAccTestResource string
//go:embed templates/acc_test_datasource.go.tmpl
var templateAccTestDataSource string
//go:embed templates/schema.go.tmpl
var templateSchema string
//go:embed templates/unit_test_schema.go.tmpl
var templateUnitTestSchema string
//go:embed templates/base.go.tmpl
var templateBase string
//go:embed templates/types.go.tmpl
var templateTypes string
//go:embed templates/common_templates.go.tmpl
var templateCommonTemplates string
//go:embed templates/doc_resource.md.tmpl
var templateDocResource string
//go:embed templates/resource.tf.tmpl
var templateResourceTf string
//go:embed templates/datasource.tf.tmpl
var templateDataSourceTf string
//go:embed templates/import.sh.tmpl
var templateImportSh string
func genTemplateConf(categoryName, resourceName, packageName, testDir, fileName, schemaDir, templateDocDir, examplesDir string) templateDef {
t := templateDef{
CategoryName: categoryName,
ResourceName: resourceName,
PackageName: packageName,
LowerCamelName: strcase.ToLowerCamel(resourceName),
CamelName: strcase.ToCamel(resourceName),
SnakeName: strcase.ToSnake(resourceName),
Filename: fileName,
TestDir: testDir,
SchemaDir: schemaDir,
TemplateDocDir: templateDocDir,
ExamplesDir: examplesDir,
FullSnakeResourceName: categoryName + "_" + strcase.ToSnake(resourceName),
FullCamelResourceName: categoryName + "_" + strcase.ToCamel(resourceName),
IsResource: strings.Contains(fileName, "_resource"),
}
if resourceName == "" {
t.LowerCamelName = strcase.ToLowerCamel(categoryName)
t.CamelName = strcase.ToCamel(categoryName)
t.SnakeName = strcase.ToSnake(categoryName)
t.FullSnakeResourceName = strcase.ToSnake(categoryName)
t.FullCamelResourceName = strcase.ToCamel(categoryName)
}
return t
}
func (t templateDef) createTemplateFiles(tfTypes string) error {
templateS := templateDatasource
templateAccTest := templateAccTestDataSource
if tfTypes == "resource" {
templateS = templateResource
templateAccTest = templateAccTestResource
}
// * xx.go
if err := createTemplate(t, t.Filename, templateCommonTemplates+templateS); err != nil {
return err
}
// * testDir/categoryName_xx_test.go
// for acc test
if err := createTemplateIfNotExists(t, t.TestDir+"/"+t.CategoryName+"_"+fileNameWithoutExtAndPath(t.Filename)+"_test.go", templateCommonTemplates+templateAccTest); err != nil {
return err
}
// * xx_schema.go
// if file not already exists create schema file
if err := createTemplateIfNotExists(t, t.SchemaDir+"/"+fileNameWithoutResourceOrDataSource(t.Filename)+"_schema.go", templateCommonTemplates+templateSchema); err != nil {
return err
}
// * xx_schema_test.go
// if file not already exists create schema test file
if err := createTemplateIfNotExists(t, t.SchemaDir+"/"+fileNameWithoutResourceOrDataSource(t.Filename)+"_schema_test.go", templateCommonTemplates+templateUnitTestSchema); err != nil {
return err
}
// * xx_types.go
// if file not already exists create types file
if err := createTemplateIfNotExists(t, t.SchemaDir+"/"+fileNameWithoutResourceOrDataSource(t.Filename)+"_types.go", templateCommonTemplates+templateTypes); err != nil {
return err
}
// * base.go
// if base file not already exists create it
if err := createTemplateIfNotExists(t, t.SchemaDir+"/"+"base.go", templateCommonTemplates+templateBase); err != nil {
return err
}
// * TemplateDocDir/<resources|data-sources>/<packageName>_<resource_name>.md.tmpl
// if file not already exists create it
if err := createFileIfNotExists(
func() string {
ressOrData := "resources"
if !t.IsResource {
ressOrData = "data-sources"
}
return t.TemplateDocDir + "/" + ressOrData + "/" + t.PackageName + "_" + t.SnakeName + ".md.tmpl"
}(),
templateDocResource); err != nil {
return err
}
// * ExamplesDir/<resources|data-sources>/<resource_name>/resource.tf
// if file not already exists create it
if err := createTemplateIfNotExists(
t,
func() string {
ressOrData := "resources"
ressOrDataFile := "resource.tf"
if !t.IsResource {
ressOrData = "data-sources"
ressOrDataFile = "data-source.tf"
}
return t.ExamplesDir + "/" + ressOrData + "/cloudavenue_" + t.FullSnakeResourceName + "/" + ressOrDataFile
}(),
func() string {
ressOrDataTemplate := templateResourceTf
if !t.IsResource {
ressOrDataTemplate = templateDataSourceTf
}
return templateCommonTemplates + ressOrDataTemplate
}(),
); err != nil {
return err
}
// * ExamplesDir/resources/<resource_name>/import.sh
// if is resource and file not already exists create it
if t.IsResource {
if err := createTemplateIfNotExists(
t,
t.ExamplesDir+"/resources/cloudavenue_"+t.FullSnakeResourceName+"/import.sh",
templateCommonTemplates+templateImportSh,
); err != nil {
return err
}
}
return nil
}
func fileNameWithoutExtAndPath(fileName string) string {
f := filepath.Base(fileName)
return strings.TrimSuffix(f, filepath.Ext(f))
}
// fileNameWithoutResourceOrDataSource returns the filename without the resource or datasource prefix.
func fileNameWithoutResourceOrDataSource(fileName string) string {
f := fileNameWithoutExtAndPath(fileName)
f = strings.TrimSuffix(f, "_resource") // remove _resource
f = strings.TrimSuffix(f, "_datasource") // remove _datasource
return f
}
// createTemplateIfNotExists creates the template file if it does not exist.
func createTemplateIfNotExists(t templateDef, fileName, templateString string) error {
if !file.IsFileExists(fileName) {
return createTemplate(t, fileName, templateString)
}
return nil
}
// createFileIfNotExists creates the file if it does not exist.
func createFileIfNotExists(fileName, content string) error {
if !file.IsFileExists(fileName) {
return createFile(fileName, content)
}
return nil
}
// createTemplate creates the template file.
func createTemplate(t templateDef, fileName, templateString string) error {
var tplTypes bytes.Buffer
tmplTypes, err := template.New("template").Parse(templateString)
if err != nil {
return err
}
if err := tmplTypes.Execute(&tplTypes, t); err != nil {
return err
}
// 0o600 syntax https://stackoverflow.com/questions/5624359/write-file-with-specific-permissions-in-python/5624691#5624691
return os.WriteFile(fileName, tplTypes.Bytes(), 0o600)
}
// createFile creates the file.
func createFile(fileName, content string) error {
return os.WriteFile(fileName, []byte(content), 0o600)
}