-
Notifications
You must be signed in to change notification settings - Fork 21
/
convert_json.go
247 lines (220 loc) · 7.05 KB
/
convert_json.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
package schema
import (
"fmt"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-registry-address"
"github.com/zclconf/go-cty/cty"
)
func ProviderSchemaFromJson(jsonSchema *tfjson.ProviderSchema, pAddr tfaddr.Provider) *ProviderSchema {
ps := &ProviderSchema{
Resources: map[string]*schema.BodySchema{},
DataSources: map[string]*schema.BodySchema{},
}
if jsonSchema.ConfigSchema != nil {
ps.Provider = bodySchemaFromJson(jsonSchema.ConfigSchema.Block)
ps.Provider.Detail = detailForSrcAddr(pAddr, nil)
ps.Provider.DocsLink = docsLinkForProvider(pAddr, nil)
}
for rName, rSchema := range jsonSchema.ResourceSchemas {
ps.Resources[rName] = bodySchemaFromJson(rSchema.Block)
ps.Resources[rName].Detail = detailForSrcAddr(pAddr, nil)
}
for dsName, dsSchema := range jsonSchema.DataSourceSchemas {
ps.DataSources[dsName] = bodySchemaFromJson(dsSchema.Block)
ps.DataSources[dsName].Detail = detailForSrcAddr(pAddr, nil)
}
return ps
}
func (ps *ProviderSchema) SetProviderVersion(pAddr tfaddr.Provider, v *version.Version) {
if ps.Provider != nil {
ps.Provider.Detail = detailForSrcAddr(pAddr, v)
ps.Provider.DocsLink = docsLinkForProvider(pAddr, v)
}
for _, rSchema := range ps.Resources {
rSchema.Detail = detailForSrcAddr(pAddr, v)
}
for _, dsSchema := range ps.DataSources {
dsSchema.Detail = detailForSrcAddr(pAddr, v)
}
}
func bodySchemaFromJson(schemaBlock *tfjson.SchemaBlock) *schema.BodySchema {
if schemaBlock == nil {
s := schema.NewBodySchema()
return s
}
return &schema.BodySchema{
Attributes: convertAttributesFromJson(schemaBlock.Attributes),
Blocks: convertBlocksFromJson(schemaBlock.NestedBlocks),
IsDeprecated: schemaBlock.Deprecated,
Description: markupContent(schemaBlock.Description, schemaBlock.DescriptionKind),
}
}
func convertBlocksFromJson(blocks map[string]*tfjson.SchemaBlockType) map[string]*schema.BlockSchema {
cBlocks := make(map[string]*schema.BlockSchema, len(blocks))
for name, jsonSchema := range blocks {
block := jsonSchema.Block
blockType := schema.BlockTypeNil
labels := []*schema.LabelSchema{}
switch jsonSchema.NestingMode {
case tfjson.SchemaNestingModeSingle:
blockType = schema.BlockTypeObject
case tfjson.SchemaNestingModeMap:
labels = []*schema.LabelSchema{
{Name: "name"},
}
blockType = schema.BlockTypeMap
case tfjson.SchemaNestingModeList:
blockType = schema.BlockTypeList
case tfjson.SchemaNestingModeSet:
blockType = schema.BlockTypeSet
}
cBlocks[name] = &schema.BlockSchema{
Description: markupContent(block.Description, block.DescriptionKind),
Type: blockType,
IsDeprecated: block.Deprecated,
MinItems: jsonSchema.MinItems,
MaxItems: jsonSchema.MaxItems,
Labels: labels,
Body: bodySchemaFromJson(block),
}
}
return cBlocks
}
func convertAttributesFromJson(attributes map[string]*tfjson.SchemaAttribute) map[string]*schema.AttributeSchema {
cAttrs := make(map[string]*schema.AttributeSchema, len(attributes))
for name, attr := range attributes {
cAttrs[name] = &schema.AttributeSchema{
Description: markupContent(attr.Description, attr.DescriptionKind),
IsDeprecated: attr.Deprecated,
IsComputed: attr.Computed,
IsOptional: attr.Optional,
IsRequired: attr.Required,
Expr: exprConstraintsFromAttribute(attr),
}
}
return cAttrs
}
func exprConstraintsFromAttribute(attr *tfjson.SchemaAttribute) schema.ExprConstraints {
var expr schema.ExprConstraints
if attr.AttributeType != cty.NilType {
return schema.ExprConstraints{
schema.TraversalExpr{OfType: attr.AttributeType},
schema.LiteralTypeExpr{Type: attr.AttributeType},
}
}
if attr.AttributeNestedType != nil {
switch attr.AttributeNestedType.NestingMode {
case tfjson.SchemaNestingModeSingle:
return schema.ExprConstraints{
convertJsonAttributesToObjectExprAttr(attr.AttributeNestedType.Attributes),
}
case tfjson.SchemaNestingModeList:
return schema.ExprConstraints{
schema.ListExpr{
Elem: schema.ExprConstraints{
convertJsonAttributesToObjectExprAttr(attr.AttributeNestedType.Attributes),
},
MinItems: attr.AttributeNestedType.MinItems,
MaxItems: attr.AttributeNestedType.MaxItems,
},
}
case tfjson.SchemaNestingModeSet:
return schema.ExprConstraints{
schema.SetExpr{
Elem: schema.ExprConstraints{
convertJsonAttributesToObjectExprAttr(attr.AttributeNestedType.Attributes),
},
MinItems: attr.AttributeNestedType.MinItems,
MaxItems: attr.AttributeNestedType.MaxItems,
},
}
case tfjson.SchemaNestingModeMap:
return schema.ExprConstraints{
schema.MapExpr{
Elem: schema.ExprConstraints{
convertJsonAttributesToObjectExprAttr(attr.AttributeNestedType.Attributes),
},
MinItems: attr.AttributeNestedType.MinItems,
MaxItems: attr.AttributeNestedType.MaxItems,
},
}
}
}
return expr
}
func convertJsonAttributesToObjectExprAttr(attrs map[string]*tfjson.SchemaAttribute) schema.ObjectExpr {
attributes := make(schema.ObjectExprAttributes, len(attrs))
for name, attr := range attrs {
attributes[name] = &schema.AttributeSchema{
Description: markupContent(attr.Description, attr.DescriptionKind),
IsDeprecated: attr.Deprecated,
IsComputed: attr.Computed,
IsOptional: attr.Optional,
IsRequired: attr.Required,
Expr: exprConstraintsFromAttribute(attr),
}
}
return schema.ObjectExpr{
Attributes: attributes,
}
}
func markupContent(value string, kind tfjson.SchemaDescriptionKind) lang.MarkupContent {
if value == "" {
return lang.MarkupContent{}
}
switch kind {
case tfjson.SchemaDescriptionKindMarkdown:
return lang.Markdown(value)
case tfjson.SchemaDescriptionKindPlain:
return lang.PlainText(value)
}
// backwards compatibility with v0.12
return lang.PlainText(value)
}
func docsLinkForProvider(addr tfaddr.Provider, v *version.Version) *schema.DocsLink {
if !providerHasDocs(addr) {
return nil
}
ver := "latest"
if v != nil {
ver = v.String()
}
return &schema.DocsLink{
URL: fmt.Sprintf("https://registry.terraform.io/providers/%s/%s/%s/docs",
addr.Namespace, addr.Type, ver),
Tooltip: fmt.Sprintf("%s Documentation", addr.ForDisplay()),
}
}
func providerHasDocs(addr tfaddr.Provider) bool {
if addr.IsBuiltIn() {
// Ideally this should point to versioned TF core docs
// but there aren't any for the built-in provider yet
return false
}
if addr.IsLegacy() {
// The Registry does know where legacy providers live
// but it doesn't provide stable (legacy) URLs
return false
}
if addr.Hostname != "registry.terraform.io" {
// docs URLs outside of the official Registry aren't standardized yet
return false
}
return true
}
func detailForSrcAddr(addr tfaddr.Provider, v *version.Version) string {
if addr.IsBuiltIn() {
if v == nil {
return "(builtin)"
}
return fmt.Sprintf("(builtin %s)", v.String())
}
detail := addr.ForDisplay()
if v != nil {
detail += " " + v.String()
}
return detail
}