forked from adammck/terraform-inventory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
394 lines (346 loc) · 10.9 KB
/
parser.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
)
// TerraformVersion defines which version of Terraform state applies
type TerraformVersion int
const (
// TerraformVersionUnknown means unknown version
TerraformVersionUnknown TerraformVersion = 0
// TerraformVersionPre0dot12 means < 0.12
TerraformVersionPre0dot12 TerraformVersion = 1
// TerraformVersion0dot12 means >= 0.12
TerraformVersion0dot12 TerraformVersion = 2
)
type stateAnyTerraformVersion struct {
StatePre0dot12 state
State0dot12 stateTerraform0dot12
TerraformVersion TerraformVersion
}
// Terraform < v0.12
type state struct {
Modules []moduleState `json:"modules"`
}
type moduleState struct {
Path []string `json:"path"`
ResourceStates map[string]resourceState `json:"resources"`
Outputs map[string]interface{} `json:"outputs"`
}
type resourceState struct {
// Populated from statefile
Type string `json:"type"`
Primary instanceState `json:"primary"`
}
type instanceState struct {
ID string `json:"id"`
Attributes map[string]string `json:"attributes,omitempty"`
}
// Terraform <= v0.12
type stateTerraform0dot12 struct {
Values valuesStateTerraform0dot12 `json:"values"`
}
type valuesStateTerraform0dot12 struct {
RootModule *moduleStateTerraform0dot12 `json:"root_module"`
Outputs map[string]interface{} `json:"outputs"`
}
type moduleStateTerraform0dot12 struct {
ResourceStates []resourceStateTerraform0dot12 `json:"resources"`
ChildModules []moduleStateTerraform0dot12 `json:"child_modules"`
Address string `json:"address"` // empty for root module, else e.g. `module.mymodulename`
}
type resourceStateTerraform0dot12 struct {
Address string `json:"address"`
Index *interface{} `json:"index"` // only set by Terraform for counted resources
Name string `json:"name"`
RawValues map[string]interface{} `json:"values"`
Type string `json:"type"`
}
// read populates the state object from a statefile.
func (s *stateAnyTerraformVersion) read(stateFile io.Reader) error {
s.TerraformVersion = TerraformVersionUnknown
b, readErr := ioutil.ReadAll(stateFile)
if readErr != nil {
return readErr
}
err0dot12 := json.Unmarshal(b, &(*s).State0dot12)
if err0dot12 == nil && s.State0dot12.Values.RootModule != nil {
s.TerraformVersion = TerraformVersion0dot12
} else {
errPre0dot12 := json.Unmarshal(b, &(*s).StatePre0dot12)
if errPre0dot12 == nil && s.StatePre0dot12.Modules != nil {
s.TerraformVersion = TerraformVersionPre0dot12
} else {
return fmt.Errorf("0.12 format error: %v; pre-0.12 format error: %v (nil error means no content/modules found in the respective format)", err0dot12, errPre0dot12)
}
}
return nil
}
// outputs returns a slice of the Outputs found in the statefile.
func (s *state) outputs() []*Output {
inst := make([]*Output, 0)
for _, m := range s.Modules {
for k, v := range m.Outputs {
var o *Output
switch v := v.(type) {
case map[string]interface{}:
o, _ = NewOutput(k, v["value"])
case string:
o, _ = NewOutput(k, v)
default:
o, _ = NewOutput(k, "<error>")
}
inst = append(inst, o)
}
}
return inst
}
// outputs returns a slice of the Outputs found in the statefile.
func (s *stateTerraform0dot12) outputs() []*Output {
inst := make([]*Output, 0)
for k, v := range s.Values.Outputs {
var o *Output
switch v := v.(type) {
case map[string]interface{}:
o, _ = NewOutput(k, v["value"])
default: // not expected
o, _ = NewOutput(k, "<error>")
}
inst = append(inst, o)
}
return inst
}
// map of resource ID -> resource Name
func (s *state) mapResourceIDNames() map[string]string {
t := map[string]string{}
for _, m := range s.Modules {
for _, k := range m.resourceKeys() {
if m.ResourceStates[k].Primary.ID != "" && m.ResourceStates[k].Primary.Attributes["name"] != "" {
kk := strings.ToLower(m.ResourceStates[k].Primary.ID)
t[kk] = m.ResourceStates[k].Primary.Attributes["name"]
}
}
}
return t
}
// map of resource ID -> resource Name
func (s *stateTerraform0dot12) mapResourceIDNames() map[string]string {
t := map[string]string{}
for _, module := range s.getAllModules() {
for _, resourceState := range module.ResourceStates {
id, typeOk := resourceState.RawValues["id"].(string)
if typeOk && id != "" && resourceState.Name != "" {
k := strings.ToLower(id)
if val, ok := resourceState.RawValues["category_id"]; ok && resourceState.Type == "vsphere_tag" {
if categoryID, typeOk := val.(string); typeOk {
if categoryName := s.getResourceIDName(categoryID); categoryName != "" {
t[k] = fmt.Sprintf("%s_%s", s.getResourceIDName(categoryID), resourceState.Name)
continue
}
}
}
t[k] = resourceState.Name
}
}
}
return t
}
func (s *stateTerraform0dot12) getResourceIDName(matchingID string) string {
for _, module := range s.getAllModules() {
for _, resourceState := range module.ResourceStates {
id, typeOk := resourceState.RawValues["id"].(string)
if typeOk && id == matchingID {
return resourceState.Name
}
}
}
return ""
}
func (s *stateTerraform0dot12) getAllModules() []*moduleStateTerraform0dot12 {
var allModules []*moduleStateTerraform0dot12
allModules = append(allModules, s.Values.RootModule)
addChildModules(&allModules, s.Values.RootModule)
return allModules
}
// recursively adds all child modules to the slice
func addChildModules(out *[]*moduleStateTerraform0dot12, from *moduleStateTerraform0dot12) {
for i := range from.ChildModules {
addChildModules(out, &from.ChildModules[i])
*out = append(*out, &from.ChildModules[i])
}
}
// resources returns a slice of the Resources found in the statefile.
func (s *stateAnyTerraformVersion) resources() []*Resource {
switch s.TerraformVersion {
case TerraformVersionPre0dot12:
return s.StatePre0dot12.resources()
case TerraformVersion0dot12:
return s.State0dot12.resources()
case TerraformVersionUnknown:
}
panic("Unimplemented Terraform version enum")
}
// resources returns a slice of the Resources found in the statefile.
func (s *state) resources() []*Resource {
inst := make([]*Resource, 0)
for _, m := range s.Modules {
for _, k := range m.resourceKeys() {
if strings.HasPrefix(k, "data.") {
// This does not represent a host (e.g. AWS AMI)
continue
}
// If a module is used, the resource key may not be unique, for instance:
//
// The module cannot use dynamic resource naming and thus has to use some hardcoded name:
//
// resource "aws_instance" "host" { ... }
//
// The main file then uses the module twice:
//
// module "application1" { source = "./modules/mymodulename" }
// module "application2" { source = "./modules/mymodulename" }
//
// Avoid key clashes by prepending module name to the key. If path is ["root"], don't
// prepend anything.
//
// In the above example: `aws_instance.host` -> `aws_instance.application1_host`
fullKey := k
resourceNameIndex := strings.Index(fullKey, ".") + 1
if len(m.Path) > 1 && resourceNameIndex > 0 {
for i := len(m.Path) - 1; i >= 1; i-- {
fullKey = fullKey[:resourceNameIndex] + strings.Replace(m.Path[i], ".", "_", -1) + "_" + fullKey[resourceNameIndex:]
}
}
// Terraform stores resources in a name->map map, but we need the name to
// decide which groups to include the resource in. So wrap it in a higher-
// level object with both properties.
r, err := NewResource(fullKey, m.ResourceStates[k])
if err != nil {
asJSON, _ := json.Marshal(m.ResourceStates[k])
fmt.Fprintf(os.Stderr, "Warning: failed to parse resource %s (%v)\n", asJSON, err)
continue
}
if r.IsSupported() {
inst = append(inst, r)
}
}
}
return inst
}
func encodeTerraform0Dot12ValuesAsAttributes(rawValues *map[string]interface{}) map[string]string {
ret := make(map[string]string)
for k, v := range *rawValues {
switch v := v.(type) {
case map[string]interface{}:
ret[k+".#"] = strconv.Itoa(len(v))
for kk, vv := range v {
if str, typeOk := vv.(string); typeOk {
ret[k+"."+kk] = str
} else {
ret[k+"."+kk] = "<error>"
}
}
case []interface{}:
ret[k+".#"] = strconv.Itoa(len(v))
for kk, vv := range v {
switch o := vv.(type) {
case string:
ret[k+"."+strconv.Itoa(kk)] = o
case map[string]interface{}:
for kkk, vvv := range o {
if str, typeOk := vvv.(string); typeOk {
ret[k+"."+strconv.Itoa(kk)+"."+kkk] = str
} else {
ret[k+"."+strconv.Itoa(kk)+"."+kkk] = "<error>"
}
}
default:
ret[k+"."+strconv.Itoa(kk)] = "<error>"
}
}
case string:
ret[k] = v
default:
ret[k] = "<error>"
}
}
return ret
}
// resources returns a slice of the Resources found in the statefile.
func (s *stateTerraform0dot12) resources() []*Resource {
inst := make([]*Resource, 0)
for _, module := range s.getAllModules() {
for _, rs := range module.ResourceStates {
id, typeOk := rs.RawValues["id"].(string)
if !typeOk {
continue
}
if strings.HasPrefix(rs.Address, "data.") {
// This does not represent a host (e.g. AWS AMI)
continue
}
modulePrefix := ""
if module.Address != "" {
modulePrefix = strings.Replace(module.Address, ".", "_", -1) + "_"
}
resourceKeyName := rs.Type + "." + modulePrefix + rs.Name
if rs.Index != nil {
i := *rs.Index
switch v := i.(type) {
case int:
resourceKeyName += "." + strconv.Itoa(v)
case float64:
resourceKeyName += "." + strconv.Itoa(int(v))
case string:
resourceKeyName += "." + strings.Replace(v, ".", "_", -1)
default:
fmt.Fprintf(os.Stderr, "Warning: unknown index type %v\n", v)
}
}
// Terraform stores resources in a name->map map, but we need the name to
// decide which groups to include the resource in. So wrap it in a higher-
// level object with both properties.
//
// Convert to the pre-0.12 structure for backwards compatibility of code.
r, err := NewResource(resourceKeyName, resourceState{
Type: rs.Type,
Primary: instanceState{
ID: id,
Attributes: encodeTerraform0Dot12ValuesAsAttributes(&rs.RawValues),
},
})
if err != nil {
asJSON, _ := json.Marshal(rs)
fmt.Fprintf(os.Stderr, "Warning: failed to parse resource %s (%v)\n", asJSON, err)
continue
}
if r.IsSupported() {
inst = append(inst, r)
}
}
}
sort.Slice(inst, func(i, j int) bool {
return inst[i].baseName < inst[j].baseName
})
return inst
}
// resourceKeys returns a sorted slice of the key names of the resources in this
// module. Do this instead of range over ResourceStates, to ensure that the
// output is consistent.
func (ms *moduleState) resourceKeys() []string {
lk := len(ms.ResourceStates)
keys := make([]string, lk, lk)
i := 0
for k := range ms.ResourceStates {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}