This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
generate-schema.go
272 lines (233 loc) · 6.48 KB
/
generate-schema.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
261
262
263
264
265
266
267
268
269
270
271
272
package main
//noinspection GoUnresolvedReference
import (
"github.com/hashicorp/hcl/v2/ext/customdecode"
"github.com/zclconf/go-cty/cty"
cty_json "github.com/zclconf/go-cty/cty/json"
prvdr "__REPOSITORY__/__PKG_NAME__"
"__REPOSITORY__/tfplugin5"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
)
// ExportSchema should be called to export the structure
// of the provider.
func Export(p *tfplugin5.GetProviderSchema_Response) *ResourceProviderSchema {
result := new(ResourceProviderSchema)
result.SchemaVersion = "2"
result.SDKType = "terraform-tfplugin5"
result.Name = "__NAME__"
result.Type = "provider"
result.Version = "__REVISION__"
result.Provider = ExportSchema(p.Provider)
result.Resources = make(map[string]SchemaInfo)
result.DataSources = make(map[string]SchemaInfo)
for k, r := range p.ResourceSchemas {
result.Resources[k] = ExportSchema(r)
}
for k, ds := range p.DataSourceSchemas {
result.DataSources[k] = ExportSchema(ds)
}
return result
}
//region helpers for new cty.Type schemas
func exportAttribute(v *tfplugin5.Schema_Attribute) SchemaDefinition {
item := SchemaDefinition{}
unmarshalType, _ := cty_json.UnmarshalType(v.Type)
item.Type = exportType(unmarshalType)
item.Optional = v.Optional
item.Required = v.Required
item.Description = v.Description
item.Computed = v.Computed
item.IsBlock = false
item.ConfigImplicitMode = "Attr"
return item
}
func exportType(t cty.Type) string {
if t.IsPrimitiveType() {
return shortenType(t.GoString())
}
if t.IsListType() {
et := t.ListElementType()
if et != nil {
return "List(" + exportType(*et) + ")"
}
}
if t.IsSetType() {
et := t.SetElementType()
if et != nil {
return "Set(" + exportType(*et) + ")"
}
}
if t.IsMapType() {
et := t.MapElementType()
if et != nil {
return "Map(" + exportType(*et) + ")"
}
}
if t.IsObjectType() {
attributeTypes := t.AttributeTypes()
names := make([]string, 0)
for k, _ := range attributeTypes {
names = append(names, k)
}
sort.Strings(names)
ret := "Object({"
first := true
for _, name := range names {
if first {
first = true
} else {
ret += ","
}
ret += " " + name + "=" + exportType(attributeTypes[name])
}
ret += "})"
return ret
}
if t == cty.DynamicPseudoType {
return "Any"
}
//noinspection GoUnresolvedReference
if t == customdecode.ExpressionClosureType {
return "Expression"
}
if t == cty.NilType {
return "NilType"
}
// TODO: Implement other cases
panic(fmt.Errorf("invalid type %#v", t))
}
func exportBlock(block *tfplugin5.Schema_Block) SchemaInfo {
result := make(SchemaInfo)
for _, attr := range block.Attributes {
result[attr.Name] = exportAttribute(attr)
}
for _, blk := range block.BlockTypes {
result[blk.TypeName] = exportNestedBlock(blk)
}
return result
}
func exportNestedBlock(v *tfplugin5.Schema_NestedBlock) SchemaDefinition {
item := SchemaDefinition{}
item.MinItems = v.MinItems
item.MaxItems = v.MaxItems
switch v.Nesting {
case tfplugin5.Schema_NestedBlock_SINGLE:
item.Type = "NestingSingle"
case tfplugin5.Schema_NestedBlock_GROUP:
item.Type = "NestingGroup"
case tfplugin5.Schema_NestedBlock_LIST:
item.Type = "NestingList"
case tfplugin5.Schema_NestedBlock_SET:
item.Type = "NestingSet"
case tfplugin5.Schema_NestedBlock_MAP:
item.Type = "NestingMap"
}
item.Elem = &SchemaElement{Type: "SchemaInfo", Info: exportBlock(v.Block)}
item.IsBlock = true
item.ConfigImplicitMode = "Block"
return item
}
//endregion
/*
func (m schemaMap) Export() SchemaInfo {
result := make(SchemaInfo)
for k, v := range m {
item := export(v)
result[k] = item
}
return result
}
*/
func ExportSchema(m *tfplugin5.Schema) SchemaInfo {
return exportBlock(m.Block)
}
func shortenType(value string) string {
if len(value) > 4 && value[0:4] == "Type" {
return value[4:]
}
if len(value) > 4 && value[0:4] == "cty." {
return value[4:]
}
return value
}
func Generate(provider tfplugin5.ProviderServer, name string, outputPath string) {
outputFilePath := filepath.Join(outputPath, name+`.json`)
if err := DoGenerate(provider, outputFilePath); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
os.Exit(255)
}
}
func DoGenerate(provider tfplugin5.ProviderServer, outputFilePath string) error {
schemaResponse, err := (provider).GetSchema(nil, nil)
if err != nil {
return err
}
providerJson, err := json.MarshalIndent(Export(schemaResponse), "", " ")
if err != nil {
return err
}
file, err := os.Create(outputFilePath)
if err != nil {
return err
}
//noinspection GoUnhandledErrorResult
defer file.Close()
_, err = file.Write(providerJson)
if err != nil {
return err
}
return file.Sync()
}
type SchemaElement struct {
// One of "schema.ValueType" or "SchemaElements" or "SchemaInfo"
Type string `json:",omitempty"`
// Set for simple types (from ValueType)
Value string `json:",omitempty"`
// Set if Type == "SchemaElements"
ElementsType string `json:",omitempty"`
// Set if Type == "SchemaInfo"
Info SchemaInfo `json:",omitempty"`
}
type SchemaDefinition struct {
Type string `json:",omitempty"`
Optional bool `json:",omitempty"`
Required bool `json:",omitempty"`
Description string `json:",omitempty"`
InputDefault string `json:",omitempty"`
Computed bool `json:",omitempty"`
MaxItems int64 `json:",omitempty"`
MinItems int64 `json:",omitempty"`
PromoteSingle bool `json:",omitempty"`
DefaultFunc string `json:",omitempty"`
ComputedWhen []string `json:",omitempty"`
ConflictsWith []string `json:",omitempty"`
Deprecated string `json:",omitempty"`
Removed string `json:",omitempty"`
IsBlock bool `json:",omitempty"`
ConfigExplicitMode string `json:",omitempty"`
ConfigImplicitMode string `json:",omitempty"`
Default *SchemaElement `json:",omitempty"`
Elem *SchemaElement `json:",omitempty"`
}
type SchemaInfo map[string]SchemaDefinition
// ResourceProviderSchema
type ResourceProviderSchema struct {
SchemaVersion string `json:".schema_version"`
SDKType string `json:".sdk_type"`
Name string `json:"name"`
Type string `json:"type"`
Version string `json:"version"`
Provider SchemaInfo `json:"provider"`
Resources map[string]SchemaInfo `json:"resources"`
DataSources map[string]SchemaInfo `json:"data-sources"`
}
func main() {
var provider tfplugin5.ProviderServer
//noinspection GoUnresolvedReference
provider = &prvdr.RawProviderServer{}
Generate(provider, "__NAME__", "__OUT__")
}