Skip to content

Commit 53b31be

Browse files
committed
Simplify some of the templates and remove unused attributes
1 parent 4178515 commit 53b31be

15 files changed

+93
-421
lines changed

gen/generator.go

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ type YamlConfigAttribute struct {
120120
ElementType string `yaml:"element_type"`
121121
DataPath []string `yaml:"data_path"`
122122
Id bool `yaml:"id"`
123-
ResourceId bool `yaml:"resource_id"`
124123
Reference bool `yaml:"reference"`
125124
RequiresReplace bool `yaml:"requires_replace"`
126125
Mandatory bool `yaml:"mandatory"`
@@ -137,7 +136,6 @@ type YamlConfigAttribute struct {
137136
MaxInt int64 `yaml:"max_int"`
138137
MinFloat float64 `yaml:"min_float"`
139138
MaxFloat float64 `yaml:"max_float"`
140-
MapKeyExample string `yaml:"map_key_example"`
141139
OrderedList bool `yaml:"ordered_list"`
142140
StringPatterns []string `yaml:"string_patterns"`
143141
StringMinLength int64 `yaml:"string_min_length"`
@@ -229,21 +227,6 @@ func HasReference(attributes []YamlConfigAttribute) bool {
229227
return false
230228
}
231229

232-
// Templating helper function to return true if reference included in attributes
233-
func HasResourceId(attributes []YamlConfigAttribute) bool {
234-
for _, attr := range attributes {
235-
if attr.ResourceId {
236-
return true
237-
}
238-
if len(attr.Attributes) > 0 {
239-
if HasResourceId(attr.Attributes) {
240-
return true
241-
}
242-
}
243-
}
244-
return false
245-
}
246-
247230
// Templating helper function to return true if type is a list or set without nested elements
248231
func IsListSet(attribute YamlConfigAttribute) bool {
249232
if (attribute.Type == "List" || attribute.Type == "Set") && attribute.ElementType != "" {
@@ -284,14 +267,6 @@ func IsInt64ListSet(attribute YamlConfigAttribute) bool {
284267
return false
285268
}
286269

287-
// Templating helper function to return true if type is a list or a map or a set, anyway with nested elements
288-
func IsNestedListMapSet(attribute YamlConfigAttribute) bool {
289-
if (attribute.Type == "List" || attribute.Type == "Map" || attribute.Type == "Set") && attribute.ElementType == "" {
290-
return true
291-
}
292-
return false
293-
}
294-
295270
// Templating helper function to return true if type is a list or set with nested elements
296271
func IsNestedListSet(attribute YamlConfigAttribute) bool {
297272
if (attribute.Type == "List" || attribute.Type == "Set") && attribute.ElementType == "" {
@@ -308,14 +283,6 @@ func IsNestedList(attribute YamlConfigAttribute) bool {
308283
return false
309284
}
310285

311-
// Templating helper function to return true if type is a map with nested elements
312-
func IsNestedMap(attribute YamlConfigAttribute) bool {
313-
if attribute.Type == "Map" && attribute.ElementType == "" {
314-
return true
315-
}
316-
return false
317-
}
318-
319286
// Templating helper function to return true if type is a set with nested elements
320287
func IsNestedSet(attribute YamlConfigAttribute) bool {
321288
if attribute.Type == "Set" && attribute.ElementType == "" {
@@ -344,28 +311,25 @@ func Subtract(a, b int) int {
344311

345312
// Map of templating functions
346313
var functions = template.FuncMap{
347-
"toGoName": ToGoName,
348-
"camelCase": CamelCase,
349-
"snakeCase": SnakeCase,
350-
"sprintf": fmt.Sprintf,
351-
"errorf": Errorf,
352-
"toLower": strings.ToLower,
353-
"path": BuildPath,
354-
"hasId": HasId,
355-
"hasReference": HasReference,
356-
"hasResourceId": HasResourceId,
357-
"isListSet": IsListSet,
358-
"isList": IsList,
359-
"isSet": IsSet,
360-
"isStringListSet": IsStringListSet,
361-
"isInt64ListSet": IsInt64ListSet,
362-
"isNestedListMapSet": IsNestedListMapSet,
363-
"isNestedListSet": IsNestedListSet,
364-
"isNestedList": IsNestedList,
365-
"isNestedMap": IsNestedMap,
366-
"isNestedSet": IsNestedSet,
367-
"importParts": ImportParts,
368-
"subtract": Subtract,
314+
"toGoName": ToGoName,
315+
"camelCase": CamelCase,
316+
"snakeCase": SnakeCase,
317+
"sprintf": fmt.Sprintf,
318+
"errorf": Errorf,
319+
"toLower": strings.ToLower,
320+
"path": BuildPath,
321+
"hasId": HasId,
322+
"hasReference": HasReference,
323+
"isListSet": IsListSet,
324+
"isList": IsList,
325+
"isSet": IsSet,
326+
"isStringListSet": IsStringListSet,
327+
"isInt64ListSet": IsInt64ListSet,
328+
"isNestedListSet": IsNestedListSet,
329+
"isNestedList": IsNestedList,
330+
"isNestedSet": IsNestedSet,
331+
"importParts": ImportParts,
332+
"subtract": Subtract,
369333
}
370334

371335
func (attr *YamlConfigAttribute) init(parentGoTypeName string) error {

gen/schema/schema.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ attribute:
2222
element_type: enum('String', 'Int64', required=False) # only relevant if type is either 'List' or 'Set'
2323
data_path: list(str(), required=False) # Path to the attribute in the model structure
2424
id: bool(required=False) # Set to true if the attribute is part of the ID
25-
resource_id: bool(required=False) # Set to true if the attribute is a resource ID (and needs to be included in PUT payload)
2625
reference: bool(required=False) # Set to true if the attribute is a reference being used in the path (URL) of the REST endpoint
2726
requires_replace: bool(required=False) # Set to true if the attribute update forces Terraform to destroy/recreate the entire resource
2827
mandatory: bool(required=False) # Set to true if the attribute is mandatory
@@ -39,7 +38,6 @@ attribute:
3938
max_int: int(required=False) # Maximum value of an integer, only relevant if type is "Int64"
4039
min_float: num(required=False) # Minimum value of a float, only relevant if type is "Float"
4140
max_float: num(required=False) # Maximum value of a float, only relevant if type is "Float"
42-
map_key_example: str(required=False) # Example value of the map key, only relevant if type is "Map"
4341
ordered_list: bool(required=False) # Treat `type: List` as strictly ordered. If API ever returns different order, destroy and re-create elements.
4442
string_patterns: list(str(), required=False) # List of regular expressions that the string must match, only relevant if type is "String"
4543
string_min_length: int(required=False) # Minimum length of a string, only relevant if type is "String"

gen/templates/data-source.tf

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ data "meraki_{{snakeCase .Name}}" "example" {
44
{{- range .Attributes}}
55
{{- if .Reference}}
66
{{.TfName}} = {{if eq .Type "String"}}"{{.Example}}"{{else if isStringListSet .}}["{{.Example}}"]{{else if isInt64ListSet .}}[{{.Example}}]{{else}}{{.Example}}{{end}}
7-
{{- else if isNestedMap .}}
8-
{{- $map := .TfName}}
9-
{{- $mapkey := .MapKeyExample}}
10-
{{.TfName}} = {
11-
"{{.MapKeyExample}}" = {
12-
{{- range .Attributes}}
13-
{{- if .ResourceId}}
14-
{{.TfName}} = meraki_{{snakeCase $name}}.test.{{$map}}["{{$mapkey}}"].{{.TfName}}
15-
{{- end}}
16-
{{- end}}
17-
}
18-
}
197
{{- end}}
208
{{- end}}
219
}

gen/templates/data_source.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (d *{{camelCase .Name}}DataSource) Schema(ctx context.Context, req datasour
7575
},
7676
{{- range .Attributes}}
7777
{{- if not .Value}}
78-
"{{.TfName}}": schema.{{if isNestedListMapSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
78+
"{{.TfName}}": schema.{{if isNestedListSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
7979
MarkdownDescription: "{{.Description}}",
8080
{{- if isListSet .}}
8181
ElementType: types.{{.ElementType}}Type,
@@ -88,34 +88,34 @@ func (d *{{camelCase .Name}}DataSource) Schema(ctx context.Context, req datasour
8888
{{- end}}
8989
Computed: true,
9090
{{- end}}
91-
{{- if isNestedListMapSet .}}
91+
{{- if isNestedListSet .}}
9292
NestedObject: schema.NestedAttributeObject{
9393
Attributes: map[string]schema.Attribute{
9494
{{- range .Attributes}}
9595
{{- if not .Value}}
96-
"{{.TfName}}": schema.{{if isNestedListMapSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
96+
"{{.TfName}}": schema.{{if isNestedListSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
9797
MarkdownDescription: "{{.Description}}",
9898
{{- if isListSet .}}
9999
ElementType: types.{{.ElementType}}Type,
100100
{{- end}}
101101
Computed: true,
102-
{{- if isNestedListMapSet .}}
102+
{{- if isNestedListSet .}}
103103
NestedObject: schema.NestedAttributeObject{
104104
Attributes: map[string]schema.Attribute{
105105
{{- range .Attributes}}
106106
{{- if not .Value}}
107-
"{{.TfName}}": schema.{{if isNestedListMapSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
107+
"{{.TfName}}": schema.{{if isNestedListSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
108108
MarkdownDescription: "{{.Description}}",
109109
{{- if isListSet .}}
110110
ElementType: types.{{.ElementType}}Type,
111111
{{- end}}
112112
Computed: true,
113-
{{- if isNestedListMapSet .}}
113+
{{- if isNestedListSet .}}
114114
NestedObject: schema.NestedAttributeObject{
115115
Attributes: map[string]schema.Attribute{
116116
{{- range .Attributes}}
117117
{{- if not .Value}}
118-
"{{.TfName}}": schema.{{if isNestedListMapSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
118+
"{{.TfName}}": schema.{{if isNestedListSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
119119
MarkdownDescription: "{{.Description}}",
120120
{{- if isListSet .}}
121121
ElementType: types.{{.ElementType}}Type,
@@ -181,9 +181,9 @@ func (d *{{camelCase .Name}}DataSource) Read(ctx context.Context, req datasource
181181

182182
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.Id.String()))
183183

184-
{{- if .DataSourceNameQuery}}
185184
var res gjson.Result
186185
var err error
186+
{{- if .DataSourceNameQuery}}
187187
if config.Id.IsNull() && !config.Name.IsNull() {
188188
res, err = d.client.Get(config.getPath())
189189
if err != nil {
@@ -210,7 +210,7 @@ func (d *{{camelCase .Name}}DataSource) Read(ctx context.Context, req datasource
210210
{{- end}}
211211

212212
if !res.Exists() {
213-
{{- if .GetFromAll}}
213+
{{- if or .GetFromAll .PutCreate}}
214214
res, err = d.client.Get(config.getPath())
215215
{{- else}}
216216
res, err = d.client.Get(config.getPath() + "/" + url.QueryEscape(config.Id.ValueString()))

gen/templates/data_source_test.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestAccDataSourceMeraki{{camelCase .Name}}(t *testing.T) {
3838
var checks []resource.TestCheckFunc
3939
{{- $name := .Name }}
4040
{{- range .Attributes}}
41-
{{- if and (not .WriteOnly) (not .ExcludeTest) (not .Value) (not .TestValue) (not .ResourceId)}}
41+
{{- if and (not .WriteOnly) (not .ExcludeTest) (not .Value) (not .TestValue)}}
4242
{{- if isNestedListSet .}}
4343
{{- $list := .TfName }}
4444
{{- if len .TestTags}}
@@ -72,7 +72,7 @@ func TestAccDataSourceMeraki{{camelCase .Name}}(t *testing.T) {
7272
{{- if len .TestTags}}
7373
}
7474
{{- end}}
75-
{{- else if not (or (isSet .) (isNestedMap .))}}
75+
{{- else if not (isSet .)}}
7676
{{- if len .TestTags}}
7777
if {{range $i, $e := .TestTags}}{{if $i}} || {{end}}os.Getenv("{{$e}}") != ""{{end}} {
7878
checks = append(checks, resource.TestCheckResourceAttr("data.meraki_{{snakeCase $name}}.test", "{{$list}}.0.{{$clist}}.0.{{.TfName}}{{if isList .}}.0{{end}}", "{{.Example}}"))
@@ -86,7 +86,7 @@ func TestAccDataSourceMeraki{{camelCase .Name}}(t *testing.T) {
8686
{{- if len .TestTags}}
8787
}
8888
{{- end}}
89-
{{- else if not (or (isSet .) (isNestedMap .))}}
89+
{{- else if not (isSet .)}}
9090
{{- if len .TestTags}}
9191
if {{range $i, $e := .TestTags}}{{if $i}} || {{end}}os.Getenv("{{$e}}") != ""{{end}} {
9292
checks = append(checks, resource.TestCheckResourceAttr("data.meraki_{{snakeCase $name}}.test", "{{$list}}.0.{{.TfName}}{{if isList .}}.0{{end}}", "{{.Example}}"))
@@ -100,7 +100,7 @@ func TestAccDataSourceMeraki{{camelCase .Name}}(t *testing.T) {
100100
{{- if len .TestTags}}
101101
}
102102
{{- end}}
103-
{{- else if not (or (isSet .) (isNestedMap .))}}
103+
{{- else if not (isSet .)}}
104104
{{- if len .TestTags}}
105105
if {{range $i, $e := .TestTags}}{{if $i}} || {{end}}os.Getenv("{{$e}}") != ""{{end}} {
106106
checks = append(checks, resource.TestCheckResourceAttr("data.meraki_{{snakeCase $name}}.test", "{{.TfName}}{{if isList .}}.0{{end}}", "{{.Example}}"))
@@ -146,16 +146,12 @@ const testAccDataSourceMeraki{{camelCase .Name}}PrerequisitesConfig = `
146146
func testAccDataSourceMeraki{{camelCase .Name}}Config() string {
147147
config := `resource "meraki_{{snakeCase $name}}" "test" {` + "\n"
148148
{{- range .Attributes}}
149-
{{- if and (not .ExcludeTest) (not .Value) (not .ResourceId)}}
150-
{{- if isNestedListMapSet .}}
149+
{{- if and (not .ExcludeTest) (not .Value)}}
150+
{{- if isNestedListSet .}}
151151
{{- if len .TestTags}}
152152
if {{range $i, $e := .TestTags}}{{if $i}} || {{end}}os.Getenv("{{$e}}") != ""{{end}} {
153153
{{- end}}
154-
{{- if isNestedListSet .}}
155154
config += ` {{.TfName}} = [{` + "\n"
156-
{{- else if isNestedMap .}}
157-
config += ` {{.TfName}} = { "{{.MapKeyExample}}" = {` + "\n"
158-
{{- end}}
159155
{{- range .Attributes}}
160156
{{- if and (not .ExcludeTest) (not .Value)}}
161157
{{- if isNestedListSet .}}
@@ -211,11 +207,7 @@ func testAccDataSourceMeraki{{camelCase .Name}}Config() string {
211207
{{- end}}
212208
{{- end}}
213209
{{- end}}
214-
{{- if isNestedListSet .}}
215210
config += ` }]` + "\n"
216-
{{- else if isNestedMap .}}
217-
config += ` }}` + "\n"
218-
{{- end}}
219211
{{- if len .TestTags}}
220212
}
221213
{{- end}}
@@ -238,18 +230,6 @@ func testAccDataSourceMeraki{{camelCase .Name}}Config() string {
238230
{{- range .Attributes}}
239231
{{- if .Reference}}
240232
{{.TfName}} = {{if .TestValue}}{{.TestValue}}{{else}}{{if eq .Type "String"}}"{{.Example}}"{{else if isStringListSet .}}["{{.Example}}"]{{else if isInt64ListSet .}}[{{.Example}}]{{else}}{{.Example}}{{end}}{{end}}
241-
{{- else if isNestedMap .}}
242-
{{- $map := .TfName}}
243-
{{- $mapkey := .MapKeyExample}}
244-
{{.TfName}} = {
245-
"{{.MapKeyExample}}" = {
246-
{{- range .Attributes}}
247-
{{- if .ResourceId}}
248-
{{.TfName}} = meraki_{{snakeCase $name}}.test.{{$map}}["{{$mapkey}}"].{{.TfName}}
249-
{{- end}}
250-
{{- end}}
251-
}
252-
}
253233
{{- end}}
254234
{{- end}}
255235
}
@@ -261,7 +241,7 @@ func testAccDataSourceMeraki{{camelCase .Name}}Config() string {
261241
func testAccNamedDataSourceMeraki{{camelCase .Name}}Config() string {
262242
config := `resource "meraki_{{snakeCase $name}}" "test" {` + "\n"
263243
{{- range .Attributes}}
264-
{{- if and (not .ExcludeTest) (not .Value) (not .ResourceId)}}
244+
{{- if and (not .ExcludeTest) (not .Value)}}
265245
{{- if isNestedListSet .}}
266246
{{- if len .TestTags}}
267247
if {{range $i, $e := .TestTags}}{{if $i}} || {{end}}os.Getenv("{{$e}}") != ""{{end}} {

0 commit comments

Comments
 (0)