-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathrender.go
573 lines (488 loc) · 13.9 KB
/
render.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package schemamd
import (
"fmt"
"io"
"sort"
"strings"
tfjson "github.com/hashicorp/terraform-json"
"github.com/zclconf/go-cty/cty"
)
// Render writes a Markdown formatted Schema definition to the specified writer.
// A Schema contains a Version and the root Block, for example:
//
// "aws_accessanalyzer_analyzer": {
// "block": {
// },
// "version": 0
// },
func Render(schema *tfjson.Schema, w io.Writer) error {
_, err := io.WriteString(w, "## Schema\n\n")
if err != nil {
return err
}
err = writeRootBlock(w, schema.Block)
if err != nil {
return fmt.Errorf("unable to render schema: %w", err)
}
return nil
}
// Group by Attribute/Block characteristics.
type groupFilter struct {
topLevelTitle string
nestedTitle string
filterAttribute func(att *tfjson.SchemaAttribute) bool
filterBlock func(block *tfjson.SchemaBlockType) bool
}
var (
// Attributes and Blocks are in one of 3 characteristic groups:
// * Required
// * Optional
// * Read-Only
groupFilters = []groupFilter{
{"### Required", "Required:", childAttributeIsRequired, childBlockIsRequired},
{"### Optional", "Optional:", childAttributeIsOptional, childBlockIsOptional},
{"### Read-Only", "Read-Only:", childAttributeIsReadOnly, childBlockIsReadOnly},
}
)
type nestedType struct {
anchorID string
pathTitle string
path []string
block *tfjson.SchemaBlock
object *cty.Type
attrs *tfjson.SchemaNestedAttributeType
group groupFilter
}
func writeAttribute(w io.Writer, path []string, att *tfjson.SchemaAttribute, group groupFilter) ([]nestedType, error) {
name := path[len(path)-1]
_, err := io.WriteString(w, "- `"+name+"` ")
if err != nil {
return nil, err
}
if att.AttributeNestedType == nil {
err = WriteAttributeDescription(w, att, false)
} else {
err = WriteNestedAttributeTypeDescription(w, att, false)
}
if err != nil {
return nil, err
}
if att.AttributeType.IsTupleType() {
return nil, fmt.Errorf("TODO: tuples are not yet supported")
}
anchorID := "nestedatt--" + strings.Join(path, "--")
pathTitle := strings.Join(path, ".")
nestedTypes := []nestedType{}
switch {
case att.AttributeNestedType != nil:
_, err = io.WriteString(w, " (see [below for nested schema](#"+anchorID+"))")
if err != nil {
return nil, err
}
nestedTypes = append(nestedTypes, nestedType{
anchorID: anchorID,
pathTitle: pathTitle,
path: path,
attrs: att.AttributeNestedType,
group: group,
})
case att.AttributeType.IsObjectType():
_, err = io.WriteString(w, " (see [below for nested schema](#"+anchorID+"))")
if err != nil {
return nil, err
}
nestedTypes = append(nestedTypes, nestedType{
anchorID: anchorID,
pathTitle: pathTitle,
path: path,
object: &att.AttributeType,
group: group,
})
case att.AttributeType.IsCollectionType() && att.AttributeType.ElementType().IsObjectType():
_, err = io.WriteString(w, " (see [below for nested schema](#"+anchorID+"))")
if err != nil {
return nil, err
}
nt := att.AttributeType.ElementType()
nestedTypes = append(nestedTypes, nestedType{
anchorID: anchorID,
pathTitle: pathTitle,
path: path,
object: &nt,
group: group,
})
}
_, err = io.WriteString(w, "\n")
if err != nil {
return nil, err
}
return nestedTypes, nil
}
func writeBlockType(w io.Writer, path []string, block *tfjson.SchemaBlockType) ([]nestedType, error) {
name := path[len(path)-1]
_, err := io.WriteString(w, "- `"+name+"` ")
if err != nil {
return nil, err
}
err = WriteBlockTypeDescription(w, block)
if err != nil {
return nil, fmt.Errorf("unable to write block description for %q: %w", name, err)
}
anchorID := "nestedblock--" + strings.Join(path, "--")
pathTitle := strings.Join(path, ".")
nt := nestedType{
anchorID: anchorID,
pathTitle: pathTitle,
path: path,
block: block.Block,
}
_, err = io.WriteString(w, " (see [below for nested schema](#"+anchorID+"))")
if err != nil {
return nil, err
}
_, err = io.WriteString(w, "\n")
if err != nil {
return nil, err
}
return []nestedType{nt}, nil
}
func writeRootBlock(w io.Writer, block *tfjson.SchemaBlock) error {
return writeBlockChildren(w, nil, block, true)
}
// A Block contains:
// * Attributes (arbitrarily nested)
// * Nested Blocks (with nesting mode, max and min items)
// * Description(Kind)
// * Deprecated flag
// For example:
//
// "block": {
// "attributes": {
// "certificate_arn": {
// "description_kind": "plain",
// "required": true,
// "type": "string"
// }
// },
// "block_types": {
// "timeouts": {
// "block": {
// "attributes": {
// },
// "description_kind": "plain"
// },
// "nesting_mode": "single"
// }
// },
// "description_kind": "plain"
// },
func writeBlockChildren(w io.Writer, parents []string, block *tfjson.SchemaBlock, root bool) error {
names := []string{}
for n := range block.Attributes {
names = append(names, n)
}
for n := range block.NestedBlocks {
names = append(names, n)
}
groups := map[int][]string{}
// Group Attributes/Blocks by characteristics.
nameLoop:
for _, n := range names {
if childBlock, ok := block.NestedBlocks[n]; ok {
for i, gf := range groupFilters {
if gf.filterBlock(childBlock) {
groups[i] = append(groups[i], n)
continue nameLoop
}
}
} else if childAtt, ok := block.Attributes[n]; ok {
for i, gf := range groupFilters {
// By default, the attribute `id` is place in the "Read-Only" group
// if the provider schema contained no `.Description` for it.
//
// If a `.Description` is provided instead, the behaviour will be the
// same as for every other attribute.
if strings.ToLower(n) == "id" && len(parents) == 0 && childAtt.Description == "" {
if strings.Contains(gf.topLevelTitle, "Read-Only") {
childAtt.Description = "The ID of this resource."
groups[i] = append(groups[i], n)
continue nameLoop
}
} else if gf.filterAttribute(childAtt) {
groups[i] = append(groups[i], n)
continue nameLoop
}
}
}
return fmt.Errorf("no match for %q, this can happen if you have incompatible schema defined, for example an "+
"optional block where all the child attributes are computed, in which case the block itself should also "+
"be marked computed", n)
}
nestedTypes := []nestedType{}
// For each characteristic group
// If Attribute
// Write out summary including characteristic and type (if primitive type or collection of primitives)
// If NestedAttribute type, Object type or collection of Objects, add to list of nested types
// ElseIf Block
// Write out summary including characteristic
// Add block to list of nested types
// End
// End
// For each nested type:
// Write out heading
// If Block
// Recursively call this function (writeBlockChildren)
// ElseIf Object
// Call writeObjectChildren, which
// For each Object Attribute
// Write out summary including characteristic and type (if primitive type or collection of primitives)
// If Object type or collection of Objects, add to list of nested types
// End
// Recursively do nested type functionality
// ElseIf NestedAttribute
// Call writeNestedAttributeChildren, which
// For each nested Attribute
// Write out summary including characteristic and type (if primitive type or collection of primitives)
// If NestedAttribute type, Object type or collection of Objects, add to list of nested types
// End
// Recursively do nested type functionality
// End
// End
for i, gf := range groupFilters {
sortedNames := groups[i]
if len(sortedNames) == 0 {
continue
}
sort.Strings(sortedNames)
groupTitle := gf.topLevelTitle
if !root {
groupTitle = gf.nestedTitle
}
_, err := io.WriteString(w, groupTitle+"\n\n")
if err != nil {
return err
}
for _, name := range sortedNames {
if childBlock, ok := block.NestedBlocks[name]; ok {
if childBlockContainsWriteOnly(childBlock) {
_, err := io.WriteString(w,
"> **NOTE**: [Write-only arguments](https://developer.hashicorp.com/terraform/language/resources/ephemeral#write-only-arguments) are supported in Terraform 1.11 and later.\n\n")
if err != nil {
return err
}
break
}
} else if childAtt, ok := block.Attributes[name]; ok {
if childAttributeIsWriteOnly(childAtt) {
_, err := io.WriteString(w,
"> **NOTE**: [Write-only arguments](https://developer.hashicorp.com/terraform/language/resources/ephemeral#write-only-arguments) are supported in Terraform 1.11 and later.\n\n")
if err != nil {
return err
}
break
}
}
}
for _, name := range sortedNames {
path := make([]string, len(parents), len(parents)+1)
copy(path, parents)
path = append(path, name)
if childBlock, ok := block.NestedBlocks[name]; ok {
nt, err := writeBlockType(w, path, childBlock)
if err != nil {
return fmt.Errorf("unable to render block %q: %w", name, err)
}
nestedTypes = append(nestedTypes, nt...)
continue
} else if childAtt, ok := block.Attributes[name]; ok {
nt, err := writeAttribute(w, path, childAtt, gf)
if err != nil {
return fmt.Errorf("unable to render attribute %q: %w", name, err)
}
nestedTypes = append(nestedTypes, nt...)
continue
}
return fmt.Errorf("unexpected name in schema render %q", name)
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
}
err := writeNestedTypes(w, nestedTypes)
if err != nil {
return err
}
return nil
}
func writeNestedTypes(w io.Writer, nestedTypes []nestedType) error {
for _, nt := range nestedTypes {
_, err := io.WriteString(w, "<a id=\""+nt.anchorID+"\"></a>\n")
if err != nil {
return err
}
_, err = io.WriteString(w, "### Nested Schema for `"+nt.pathTitle+"`\n\n")
if err != nil {
return err
}
switch {
case nt.block != nil:
err = writeBlockChildren(w, nt.path, nt.block, false)
if err != nil {
return err
}
case nt.object != nil:
err = writeObjectChildren(w, nt.path, *nt.object, nt.group)
if err != nil {
return err
}
case nt.attrs != nil:
err = writeNestedAttributeChildren(w, nt.path, nt.attrs, nt.group)
if err != nil {
return err
}
default:
return fmt.Errorf("missing information on nested block: %s", strings.Join(nt.path, "."))
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
}
return nil
}
func writeObjectAttribute(w io.Writer, path []string, att cty.Type, group groupFilter) ([]nestedType, error) {
name := path[len(path)-1]
_, err := io.WriteString(w, "- `"+name+"` (")
if err != nil {
return nil, err
}
err = WriteType(w, att)
if err != nil {
return nil, err
}
_, err = io.WriteString(w, ")")
if err != nil {
return nil, err
}
if att.IsTupleType() {
return nil, fmt.Errorf("TODO: tuples are not yet supported")
}
anchorID := "nestedobjatt--" + strings.Join(path, "--")
pathTitle := strings.Join(path, ".")
nestedTypes := []nestedType{}
switch {
case att.IsObjectType():
_, err = io.WriteString(w, " (see [below for nested schema](#"+anchorID+"))")
if err != nil {
return nil, err
}
nestedTypes = append(nestedTypes, nestedType{
anchorID: anchorID,
pathTitle: pathTitle,
path: path,
object: &att,
group: group,
})
case att.IsCollectionType() && att.ElementType().IsObjectType():
_, err = io.WriteString(w, " (see [below for nested schema](#"+anchorID+"))")
if err != nil {
return nil, err
}
nt := att.ElementType()
nestedTypes = append(nestedTypes, nestedType{
anchorID: anchorID,
pathTitle: pathTitle,
path: path,
object: &nt,
group: group,
})
}
_, err = io.WriteString(w, "\n")
if err != nil {
return nil, err
}
return nestedTypes, nil
}
func writeObjectChildren(w io.Writer, parents []string, ty cty.Type, group groupFilter) error {
_, err := io.WriteString(w, group.nestedTitle+"\n\n")
if err != nil {
return err
}
atts := ty.AttributeTypes()
sortedNames := []string{}
for n := range atts {
sortedNames = append(sortedNames, n)
}
sort.Strings(sortedNames)
nestedTypes := []nestedType{}
for _, name := range sortedNames {
att := atts[name]
path := make([]string, len(parents), len(parents)+1)
copy(path, parents)
path = append(path, name)
nt, err := writeObjectAttribute(w, path, att, group)
if err != nil {
return fmt.Errorf("unable to render attribute %q: %w", name, err)
}
nestedTypes = append(nestedTypes, nt...)
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
err = writeNestedTypes(w, nestedTypes)
if err != nil {
return err
}
return nil
}
func writeNestedAttributeChildren(w io.Writer, parents []string, nestedAttributes *tfjson.SchemaNestedAttributeType, group groupFilter) error {
sortedNames := []string{}
for n := range nestedAttributes.Attributes {
sortedNames = append(sortedNames, n)
}
sort.Strings(sortedNames)
groups := map[int][]string{}
for _, name := range sortedNames {
att := nestedAttributes.Attributes[name]
for i, gf := range groupFilters {
if gf.filterAttribute(att) {
groups[i] = append(groups[i], name)
}
}
}
nestedTypes := []nestedType{}
for i, gf := range groupFilters {
names, ok := groups[i]
if !ok || len(names) == 0 {
continue
}
_, err := io.WriteString(w, gf.nestedTitle+"\n\n")
if err != nil {
return err
}
for _, name := range names {
att := nestedAttributes.Attributes[name]
path := make([]string, len(parents), len(parents)+1)
copy(path, parents)
path = append(path, name)
nt, err := writeAttribute(w, path, att, group)
if err != nil {
return fmt.Errorf("unable to render attribute %q: %w", name, err)
}
nestedTypes = append(nestedTypes, nt...)
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
}
err := writeNestedTypes(w, nestedTypes)
if err != nil {
return err
}
return nil
}