Skip to content

Commit 00afcf4

Browse files
committed
Move definition generator to gen folder
1 parent 502afa5 commit 00afcf4

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

openapiconverter/main.go renamed to gen/definition.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
// Copyright © 2024 Cisco Systems, Inc. and its affiliates.
2+
// All rights reserved.
3+
//
4+
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://mozilla.org/MPL/2.0/
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// SPDX-License-Identifier: MPL-2.0
17+
18+
//go:build ignore
19+
120
package main
221

322
import (
@@ -7,32 +26,32 @@ import (
726
"regexp"
827
"strings"
928

10-
"github.com/CiscoDevNet/terraform-provider-meraki/gen"
29+
"github.com/CiscoDevNet/terraform-provider-meraki/gen/yamlconfig"
1130
"github.com/tidwall/gjson"
1231
"gopkg.in/yaml.v3"
1332
)
1433

34+
const specPath = "./gen/models/spec3.json"
35+
1536
const usage = `
16-
Usage: openapiconverter <openapi_spec> <endpoint> <resource_name>
37+
Usage: go run gen/definition.go <endpoint> <resource_name>
1738
1839
Arguments:
19-
openapi_spec Path to the file containing the OpenAPI specification (YAML or JSON)
2040
endpoint The specific endpoint that is to be converted to generator specification
2141
resource_name The name that will be given to the resource
2242
2343
Example:
24-
openapiconverter ./api-spec.yaml "/networks/{networkId}/groupPolicies/{groupPolicyId}" "Network Group Policy"`
44+
go run gen/definition.go "/networks/{networkId}/groupPolicies/{groupPolicyId}" "Network Group Policy"`
2545

2646
func main() {
27-
if len(os.Args) < 4 {
47+
if len(os.Args) < 3 {
2848
fmt.Println("Error: Insufficient number of arguments")
2949
fmt.Println(usage)
3050
os.Exit(1)
3151
}
3252

33-
specPath := os.Args[1]
34-
endpointPath := os.Args[2]
35-
resourceName := os.Args[3]
53+
endpointPath := os.Args[1]
54+
resourceName := os.Args[2]
3655

3756
specData, err := os.ReadFile(specPath)
3857
if err != nil {
@@ -60,7 +79,7 @@ func main() {
6079
}
6180

6281
attributes := traverseProperties(schema["schema"].(map[string]interface{})["properties"].(map[string]interface{}), []string{}, "", string(exampleStr))
63-
config := gen.YamlConfig{}
82+
config := yamlconfig.YamlConfig{}
6483
urlResult := parseUrl(endpointPath)
6584
if urlResult.resultPath[len(urlResult.resultPath)-1] == '/' {
6685
urlResult.resultPath = urlResult.resultPath[:len(urlResult.resultPath)-1]
@@ -70,8 +89,8 @@ func main() {
7089
config.IdName = urlResult.idName[1 : len(urlResult.idName)-1]
7190
}
7291
for i, r := range urlResult.references {
73-
attr := gen.YamlConfigAttribute{}
74-
attr.TfName = gen.CamelToSnake(r[1 : len(r)-1])
92+
attr := yamlconfig.YamlConfigAttribute{}
93+
attr.TfName = yamlconfig.CamelToSnake(r[1 : len(r)-1])
7594
attr.Type = "String"
7695
attr.Reference = true
7796
if urlResult.oneToOne && i == len(urlResult.references)-1 {
@@ -133,8 +152,8 @@ func parseUrl(url string) parseUrlResult {
133152
return ret
134153
}
135154

136-
func traverseProperties(m map[string]interface{}, path []string, gjsonPath string, exampleStr string) []gen.YamlConfigAttribute {
137-
ret := []gen.YamlConfigAttribute{}
155+
func traverseProperties(m map[string]interface{}, path []string, gjsonPath string, exampleStr string) []yamlconfig.YamlConfigAttribute {
156+
ret := []yamlconfig.YamlConfigAttribute{}
138157
for propName, v := range m {
139158
propMap := v.(map[string]interface{})
140159
if propMap["type"] == "object" {
@@ -143,7 +162,7 @@ func traverseProperties(m map[string]interface{}, path []string, gjsonPath strin
143162
children := traverseProperties(propMap["properties"].(map[string]interface{}), childPath, childGjsonPath, exampleStr)
144163
ret = append(ret, children...)
145164
} else if propMap["type"] == "array" {
146-
attr := gen.YamlConfigAttribute{}
165+
attr := yamlconfig.YamlConfigAttribute{}
147166
attr.DataPath = path
148167
attr.Type = "List"
149168
attr.ModelName = propName
@@ -165,7 +184,7 @@ func traverseProperties(m map[string]interface{}, path []string, gjsonPath strin
165184
ret = append(ret, attr)
166185
} else {
167186
// primitive value
168-
attr := gen.YamlConfigAttribute{}
187+
attr := yamlconfig.YamlConfigAttribute{}
169188
attr.DataPath = path
170189
attr.Type = jsonTypes[propMap["type"].(string)]
171190
attr.ModelName = propName

gen/doc_category.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@ import (
2626
"strings"
2727

2828
"gopkg.in/yaml.v3"
29+
30+
"github.com/CiscoDevNet/terraform-provider-meraki/gen/yamlconfig"
2931
)
3032

3133
const (
3234
definitionsPath = "./gen/definitions/"
3335
)
3436

35-
type YamlConfig struct {
36-
Name string `yaml:"name"`
37-
DocCategory string `yaml:"doc_category"`
38-
NoResource bool `yaml:"no_resource"`
39-
NoDataSource bool `yaml:"no_data_source"`
40-
}
41-
4237
var docPaths = []string{"./docs/data-sources/", "./docs/resources/"}
4338

4439
var extraDocs = map[string]string{}
@@ -56,7 +51,7 @@ func SnakeCase(s string) string {
5651

5752
func main() {
5853
files, _ := os.ReadDir(definitionsPath)
59-
configs := make([]YamlConfig, len(files))
54+
configs := make([]yamlconfig.YamlConfig, len(files))
6055

6156
// Load configs
6257
for i, filename := range files {
@@ -65,7 +60,7 @@ func main() {
6560
log.Fatalf("Error reading file: %v", err)
6661
}
6762

68-
config := YamlConfig{}
63+
config := yamlconfig.YamlConfig{}
6964
err = yaml.Unmarshal(yamlFile, &config)
7065
if err != nil {
7166
log.Fatalf("Error parsing yaml: %v", err)

gen/generator.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333

3434
"gopkg.in/yaml.v3"
3535

36-
"github.com/CiscoDevNet/terraform-provider-meraki/gen"
36+
"github.com/CiscoDevNet/terraform-provider-meraki/gen/yamlconfig"
3737
)
3838

3939
const (
@@ -94,16 +94,16 @@ var templates = []t{
9494
},
9595
}
9696

97-
func NewYamlConfig(bytes []byte) (gen.YamlConfig, error) {
98-
var config gen.YamlConfig
97+
func NewYamlConfig(bytes []byte) (yamlconfig.YamlConfig, error) {
98+
var config yamlconfig.YamlConfig
9999

100100
if err := yaml.Unmarshal(bytes, &config); err != nil {
101101
return config, err
102102
}
103103

104104
for i := range config.Attributes {
105-
if err := config.Attributes[i].Init(gen.CamelCase(config.Name)); err != nil {
106-
return gen.YamlConfig{}, err
105+
if err := config.Attributes[i].Init(yamlconfig.CamelCase(config.Name)); err != nil {
106+
return yamlconfig.YamlConfig{}, err
107107
}
108108
}
109109
if config.DsDescription == "" {
@@ -164,7 +164,7 @@ func renderTemplate(templatePath, outputPath string, config interface{}) {
164164
temp = temp + scanner.Text() + "\n"
165165
}
166166

167-
template, err := template.New(path.Base(templatePath)).Funcs(gen.Functions).Parse(temp)
167+
template, err := template.New(path.Base(templatePath)).Funcs(yamlconfig.Functions).Parse(temp)
168168
if err != nil {
169169
log.Fatalf("Error parsing template: %v", err)
170170
}
@@ -215,7 +215,7 @@ func renderTemplate(templatePath, outputPath string, config interface{}) {
215215

216216
func main() {
217217
// Load configs
218-
var configs []gen.YamlConfig
218+
var configs []yamlconfig.YamlConfig
219219
files, _ := os.ReadDir(definitionsPath)
220220

221221
for _, filename := range files {
@@ -246,7 +246,7 @@ func main() {
246246
(configs[i].NoResource && t.path == "./gen/templates/import.sh") {
247247
continue
248248
}
249-
renderTemplate(t.path, t.prefix+gen.SnakeCase(configs[i].Name)+t.suffix, configs[i])
249+
renderTemplate(t.path, t.prefix+yamlconfig.SnakeCase(configs[i].Name)+t.suffix, configs[i])
250250
}
251251
providerConfig = append(providerConfig, configs[i].Name)
252252
}

gen/yamlconfig.go renamed to gen/yamlconfig/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package gen
1+
package yamlconfig
22

33
import (
44
"fmt"
@@ -36,7 +36,7 @@ type YamlConfigAttribute struct {
3636
TfName string `yaml:"tf_name,omitempty"`
3737
Type string `yaml:"type,omitempty"`
3838
ElementType string `yaml:"element_type,omitempty"`
39-
DataPath []string `yaml:"data_path,omitempty"`
39+
DataPath []string `yaml:"data_path,omitempty,flow"`
4040
Id bool `yaml:"id,omitempty"`
4141
Reference bool `yaml:"reference,omitempty"`
4242
RequiresReplace bool `yaml:"requires_replace,omitempty"`
@@ -47,22 +47,22 @@ type YamlConfigAttribute struct {
4747
ExcludeExample bool `yaml:"exclude_example,omitempty"`
4848
Description string `yaml:"description,omitempty"`
4949
Example string `yaml:"example,omitempty"`
50-
EnumValues []string `yaml:"enum_values,omitempty"`
50+
EnumValues []string `yaml:"enum_values,omitempty,flow"`
5151
MinList int64 `yaml:"min_list,omitempty"`
5252
MaxList int64 `yaml:"max_list,omitempty"`
5353
MinInt int64 `yaml:"min_int,omitempty"`
5454
MaxInt int64 `yaml:"max_int,omitempty"`
5555
MinFloat float64 `yaml:"min_float,omitempty"`
5656
MaxFloat float64 `yaml:"max_float,omitempty"`
5757
OrderedList bool `yaml:"ordered_list,omitempty"`
58-
StringPatterns []string `yaml:"string_patterns,omitempty"`
58+
StringPatterns []string `yaml:"string_patterns,omitempty,flow"`
5959
StringMinLength int64 `yaml:"string_min_length,omitempty"`
6060
StringMaxLength int64 `yaml:"string_max_length,omitempty"`
6161
DefaultValue string `yaml:"default_value,omitempty"`
6262
Value string `yaml:"value,omitempty"`
6363
TestValue string `yaml:"test_value,omitempty"`
6464
MinimumTestValue string `yaml:"minimum_test_value,omitempty"`
65-
TestTags []string `yaml:"test_tags,omitempty"`
65+
TestTags []string `yaml:"test_tags,omitempty,flow"`
6666
Attributes []YamlConfigAttribute `yaml:"attributes,omitempty"`
6767
GoTypeName string `yaml:"gotypename,omitempty"`
6868
}

0 commit comments

Comments
 (0)