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
+
1
20
package main
2
21
3
22
import (
@@ -7,32 +26,32 @@ import (
7
26
"regexp"
8
27
"strings"
9
28
10
- "github.com/CiscoDevNet/terraform-provider-meraki/gen"
29
+ "github.com/CiscoDevNet/terraform-provider-meraki/gen/yamlconfig "
11
30
"github.com/tidwall/gjson"
12
31
"gopkg.in/yaml.v3"
13
32
)
14
33
34
+ const specPath = "./gen/models/spec3.json"
35
+
15
36
const usage = `
16
- Usage: openapiconverter <openapi_spec> <endpoint> <resource_name>
37
+ Usage: go run gen/definition.go <endpoint> <resource_name>
17
38
18
39
Arguments:
19
- openapi_spec Path to the file containing the OpenAPI specification (YAML or JSON)
20
40
endpoint The specific endpoint that is to be converted to generator specification
21
41
resource_name The name that will be given to the resource
22
42
23
43
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"`
25
45
26
46
func main () {
27
- if len (os .Args ) < 4 {
47
+ if len (os .Args ) < 3 {
28
48
fmt .Println ("Error: Insufficient number of arguments" )
29
49
fmt .Println (usage )
30
50
os .Exit (1 )
31
51
}
32
52
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 ]
36
55
37
56
specData , err := os .ReadFile (specPath )
38
57
if err != nil {
@@ -60,7 +79,7 @@ func main() {
60
79
}
61
80
62
81
attributes := traverseProperties (schema ["schema" ].(map [string ]interface {})["properties" ].(map [string ]interface {}), []string {}, "" , string (exampleStr ))
63
- config := gen .YamlConfig {}
82
+ config := yamlconfig .YamlConfig {}
64
83
urlResult := parseUrl (endpointPath )
65
84
if urlResult .resultPath [len (urlResult .resultPath )- 1 ] == '/' {
66
85
urlResult .resultPath = urlResult .resultPath [:len (urlResult .resultPath )- 1 ]
@@ -70,8 +89,8 @@ func main() {
70
89
config .IdName = urlResult .idName [1 : len (urlResult .idName )- 1 ]
71
90
}
72
91
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 ])
75
94
attr .Type = "String"
76
95
attr .Reference = true
77
96
if urlResult .oneToOne && i == len (urlResult .references )- 1 {
@@ -133,8 +152,8 @@ func parseUrl(url string) parseUrlResult {
133
152
return ret
134
153
}
135
154
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 {}
138
157
for propName , v := range m {
139
158
propMap := v .(map [string ]interface {})
140
159
if propMap ["type" ] == "object" {
@@ -143,7 +162,7 @@ func traverseProperties(m map[string]interface{}, path []string, gjsonPath strin
143
162
children := traverseProperties (propMap ["properties" ].(map [string ]interface {}), childPath , childGjsonPath , exampleStr )
144
163
ret = append (ret , children ... )
145
164
} else if propMap ["type" ] == "array" {
146
- attr := gen .YamlConfigAttribute {}
165
+ attr := yamlconfig .YamlConfigAttribute {}
147
166
attr .DataPath = path
148
167
attr .Type = "List"
149
168
attr .ModelName = propName
@@ -165,7 +184,7 @@ func traverseProperties(m map[string]interface{}, path []string, gjsonPath strin
165
184
ret = append (ret , attr )
166
185
} else {
167
186
// primitive value
168
- attr := gen .YamlConfigAttribute {}
187
+ attr := yamlconfig .YamlConfigAttribute {}
169
188
attr .DataPath = path
170
189
attr .Type = jsonTypes [propMap ["type" ].(string )]
171
190
attr .ModelName = propName
0 commit comments