-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
hcl2.go
235 lines (220 loc) · 8.05 KB
/
hcl2.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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package terraform
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
)
// writeValue writes a cty Value to a body with a given key
// It detects the value type and uses the more specific functions below
func writeValue(body *hclwrite.Body, key string, value cty.Value) {
if value.IsNull() {
return
}
if value.Type().IsListType() {
if value.LengthInt() == 0 {
return
}
literals := make([]*terraformWriter.Literal, 0)
for _, val := range value.AsValueSlice() {
refLiteral := reflect.New(reflect.TypeOf(terraformWriter.Literal{}))
err := gocty.FromCtyValue(val, refLiteral.Interface())
if literal, ok := refLiteral.Interface().(*terraformWriter.Literal); err == nil && ok {
literals = append(literals, literal)
}
}
if len(literals) > 0 {
writeLiteralList(body, key, literals)
} else {
// We assume that if the first element of a list is an object
// then all of the elements are objects
firstElement := value.Index(cty.NumberIntVal(0))
if firstElement.Type().IsObjectType() {
it := value.ElementIterator()
for it.Next() {
_, v := it.Element()
childBlock := body.AppendNewBlock(key, nil)
childBody := childBlock.Body()
keys := make([]string, 0)
for k := range v.Type().AttributeTypes() {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
writeValue(childBody, k, v.GetAttr(k))
}
}
} else {
body.SetAttributeValue(key, value)
}
}
} else {
refLiteral := reflect.New(reflect.TypeOf(terraformWriter.Literal{}))
err := gocty.FromCtyValue(value, refLiteral.Interface())
if literal, ok := refLiteral.Interface().(*terraformWriter.Literal); err == nil && ok {
writeLiteral(body, key, literal)
} else if value.Type().IsObjectType() {
childBlock := body.AppendNewBlock(key, nil)
childBody := childBlock.Body()
keys := make([]string, 0)
for k := range value.Type().AttributeTypes() {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
writeValue(childBody, k, value.GetAttr(k))
}
} else if value.Type().IsMapType() {
writeMap(body, key, value.AsValueMap())
} else {
body.SetAttributeValue(key, value)
}
}
}
// writeLiteral will write a literal attribute to a body
// Examples:
// key = "value1"
// key = res_type.res_name.res_prop
// key = file("${module.path}/foo")
func writeLiteral(body *hclwrite.Body, key string, literal *terraformWriter.Literal) {
if literal.FnName != "" {
tokens := hclwrite.Tokens{
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(fmt.Sprintf("%v(%v)", literal.FnName, strings.Join(literal.FnArgs, ", "))),
},
}
body.SetAttributeRaw(key, tokens)
} else if len(literal.Tokens) == 0 {
body.SetAttributeValue(key, cty.StringVal(literal.Value))
} else {
traversal := hcl.Traversal{
hcl.TraverseRoot{Name: literal.Tokens[0]},
}
for i := 1; i < len(literal.Tokens); i++ {
token := literal.Tokens[i]
traversal = append(traversal, hcl.TraverseAttr{Name: token})
}
body.SetAttributeTraversal(key, traversal)
}
}
// literalListTokens returns the tokens of a list of literals
// Example:
// key = [type1.name1.attr1, type2.name2.attr2, "stringliteral"]
func literalListTokens(literals []*terraformWriter.Literal) hclwrite.Tokens {
tokens := hclwrite.Tokens{
{Type: hclsyntax.TokenOBrack, Bytes: []byte("["), SpacesBefore: 1},
}
for i, literal := range literals {
if len(literal.Tokens) == 0 {
tokens = append(tokens, []*hclwrite.Token{
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(literal.Value)},
{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
}...)
} else {
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenStringLit, Bytes: []byte(literal.Tokens[0]), SpacesBefore: 1})
for i := 1; i < len(literal.Tokens); i++ {
token := literal.Tokens[i]
tokens = append(tokens, []*hclwrite.Token{
{Type: hclsyntax.TokenDot, Bytes: []byte(".")},
{Type: hclsyntax.TokenStringLit, Bytes: []byte(token)},
}...)
}
}
if i < len(literals)-1 {
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenComma, Bytes: []byte(",")})
}
}
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenCBrack, Bytes: []byte("]")})
return tokens
}
// writeLiteralList writes a list of literals to a body
// Example:
// key = [type1.name1.attr1, type2.name2.attr2, "stringliteral"]
//
// The HCL2 library does not support this natively. See https://github.com/hashicorp/hcl/issues/347
func writeLiteralList(body *hclwrite.Body, key string, literals []*terraformWriter.Literal) {
body.SetAttributeRaw(key, literalListTokens(literals))
}
// writeMap writes a map's key-value pairs to a body spready across multiple lines.
// Example:
// key = {
// "key1" = "value1"
// "key2" = "value2"
// }
//
// The HCL2 library does not support this natively. See https://github.com/hashicorp/hcl/issues/356
func writeMap(body *hclwrite.Body, key string, values map[string]cty.Value) {
if len(values) == 0 {
return
}
tokens := hclwrite.Tokens{
{Type: hclsyntax.TokenOBrace, Bytes: []byte("{"), SpacesBefore: 1},
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")},
}
keys := make([]string, 0, len(values))
for k := range values {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
tokens = append(tokens, []*hclwrite.Token{
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(k)},
{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
{Type: hclsyntax.TokenEqual, Bytes: []byte("="), SpacesBefore: 1},
}...)
v := values[k]
refLiteral := reflect.New(reflect.TypeOf(terraformWriter.Literal{}))
errLiteral := gocty.FromCtyValue(v, refLiteral.Interface())
refLiteralSlice := reflect.New(reflect.TypeOf([]*terraformWriter.Literal{}))
errLiteralSlice := gocty.FromCtyValue(v, refLiteralSlice.Interface())
// If this is a map of literals then do not surround the value with quotes
if literal, ok := refLiteral.Interface().(*terraformWriter.Literal); errLiteral == nil && ok {
if literal.FnName != "" {
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenIdent,
Bytes: []byte(fmt.Sprintf("%v(%v)", literal.FnName, strings.Join(literal.FnArgs, ", "))),
})
} else if literal.Value != "" {
tokens = append(tokens, []*hclwrite.Token{
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(literal.Value)},
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
}...)
}
} else if literals, ok := refLiteralSlice.Interface().(*[]*terraformWriter.Literal); errLiteralSlice == nil && ok {
tokens = append(tokens, literalListTokens(*literals)...)
} else {
tokens = append(tokens, []*hclwrite.Token{
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(v.AsString())},
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
}...)
}
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")})
}
tokens = append(tokens,
&hclwrite.Token{Type: hclsyntax.TokenCBrace, Bytes: []byte("}")},
)
body.SetAttributeRaw(key, tokens)
}