-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_reader.go
346 lines (288 loc) · 11.5 KB
/
tf_reader.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
log "github.com/Sirupsen/logrus"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// file loading
func loadModule(terraformRoot string, moduleRoot string, awsResources []Resource, state *HierarchyState) error {
modulePath := filepath.Join(terraformRoot, moduleRoot)
log.Info("loading module: ", modulePath)
module := state.NewModule(getModuleName(terraformRoot, moduleRoot))
files, err := ioutil.ReadDir(modulePath)
if err != nil {
return fmt.Errorf("error reading directory: ", err)
}
for _, file := range files {
if file.IsDir() {
log.Info("load module: ", filepath.Join(moduleRoot, file.Name()))
err := loadModule(*rootDir, filepath.Join(moduleRoot, file.Name()), awsResources, state)
if err != nil {
log.Errorf("error reading file '%s' (SKIPPED): %v", file.Name(), err)
}
} else {
moduleFile := filepath.Join(modulePath, file.Name())
log.Debug("moduleFile = ", moduleFile)
_, err = loadModuleFile(module, moduleFile, awsResources, state)
if err != nil {
log.Errorf("error reading file '%s' (SKIPPED): %v", moduleFile, err)
}
}
}
return nil
}
func loadModuleFile(module *Module, filePath string, awsResources []Resource, state *HierarchyState) (*HierarchyState, error) {
re := regexp.MustCompile(".*\\.tf")
if !re.MatchString(filePath) {
return state, nil
}
log.Info("module file loading: ", filePath)
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("module file loading (%s): %v", filePath, err)
}
hclFile, err := hcl.Parse(string(bytes))
if err != nil {
return nil, fmt.Errorf("module file loading (%s): unmarshalling from hcl: %v", filePath, err)
}
objects := hclFile.Node.(*ast.ObjectList)
for _, objItem := range objects.Items {
_, err = processModuleObject(module, objItem, awsResources, state)
if nil != err {
log.Warningf("module file loading (%s): error processing module object: %v", filePath, err)
}
}
log.Debugf("module file loading (%s): loaded module: %+v", filePath, module)
return state, nil
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// process one of file root objects
func processModuleObject(module *Module, object *ast.ObjectItem, awsResources []Resource, state *HierarchyState) (*HierarchyState, error) {
var strKeys []string
for _, key := range object.Keys {
strKeys = append(strKeys, key.Token.Text)
}
if len(strKeys) < 2 {
return nil, fmt.Errorf("process module object: wrong number of object keys (expected at least 2)")
}
switch strKeys[0] {
case "variable":
moduleInput := state.NewInput(module, VariableID(unquote(strKeys[1])))
moduleInput.IsLoaded = true
case "output":
moduleOutput := state.NewOutput(module, VariableID(unquote(strKeys[1])))
moduleOutput.IsLoaded = true
processOutput(module, object.Val.(*ast.ObjectType), Map(strKeys[1:], unquote), awsResources, state)
case "resource":
processResource(module, object.Val.(*ast.ObjectType), Map(strKeys[1:], unquote), awsResources, state)
case "module":
processModule(module, object.Val.(*ast.ObjectType), Map(strKeys[1:], unquote), awsResources, state)
default:
log.Warning("process module object: unknown item type: ", strKeys[0])
}
return state, nil
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// process resource
func processResource(module *Module, object *ast.ObjectType, resourceName []string, awsResources []Resource, state *HierarchyState) {
if nil != object.List && nil != object.List.Items {
for _, i := range object.List.Items {
if len(i.Keys) != 1 {
log.Error("process resource: wrong number of keys, expected 1")
return
}
fieldResourceName := resourceName
for _, k := range i.Keys {
fieldResourceName = append(fieldResourceName, k.Token.Text)
}
switch value := i.Val.(type) {
case *ast.LiteralType:
findInputVariableAsArgumentUsages(value.Token.Text, module, fieldResourceName, awsResources, state)
//findModuleOutputUsages(value.Token.Text, module, fieldResourceName, awsResources, state)
default:
log.Warningf("process resource: unsupported value type for resourceName: %v value: %+v", fieldResourceName, value)
}
}
}
}
func findModuleOutputUsages(token string, module *Module, fieldResourceName []string, awsResources []Resource, state *HierarchyState) {
log.Fatal("NOT IMPLEMENTED!")
}
func findInputVariableAsArgumentUsages(token string, module *Module, fieldResourceName []string, awsResources []Resource, state *HierarchyState) {
variableUsages := findAllVariables(token)
resourceName := fieldResourceName[0]
resourceFieldName := fieldResourceName[2]
for i := 0; i < len(variableUsages); i++ {
variableName := variableUsages[i]
awsArgument := getArgumentByName(resourceName, resourceFieldName, awsResources)
state.ConnectInputToArgument(module, variableName, fieldResourceName, awsArgument)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// process module instance
func processModule(module *Module, object *ast.ObjectType, resourceName []string, awsResources []Resource, state *HierarchyState) {
instanceName := resourceName[0]
log.Info("Processing module instance: ", instanceName)
if nil != object.List && nil != object.List.Items {
for _, i := range object.List.Items {
if len(i.Keys) != 1 {
log.Error("process resource: wrong number of keys, expected 1")
return
}
fieldResourceName := resourceName
for _, k := range i.Keys {
fieldResourceName = append(fieldResourceName, k.Token.Text)
}
switch value := i.Val.(type) {
case *ast.LiteralType:
registerInstance(value.Token.Text, module, fieldResourceName, instanceName, state)
findInputVariableModuleInputUsages(value.Token.Text, module, fieldResourceName, awsResources, state)
//findModuleOutputUsages(value.Token.Text, module, fieldResourceName, awsResources, state)
default:
log.Warningf("process resource: unsupported value type for resourceName: %v value: %+v", fieldResourceName, value)
}
}
}
}
func registerInstance(token string, module *Module, fieldResourceName []string, instanceName string, state *HierarchyState) {
fieldName := fieldResourceName[1]
if fieldName == "source" {
module.NewInstance(instanceName, token, module)
}
}
func findInputVariableModuleInputUsages(token string, module *Module, fieldResourceName []string, awsResources []Resource, state *HierarchyState) {
variableUsages := findAllVariables(token)
for i := 0; i < len(variableUsages); i++ {
variableName := variableUsages[i]
if "" != variableName {
moduleInstanceName := fieldResourceName[0]
//awsArgument := getArgumentByName([]string{fieldResourceName[0], fieldResourceName[2]}, awsResources)
moduleInstance := module.FindModuleInstance(moduleInstanceName)
state.ConnectInputToModuleInput(module, variableName, fieldResourceName, moduleInstance)
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// process module output
func processOutput(module *Module, object *ast.ObjectType, resourceName []string, awsResources []Resource, state *HierarchyState) {
if nil != object.List && nil != object.List.Items {
for _, i := range object.List.Items {
if len(i.Keys) != 1 {
log.Error("process resource: wrong number of keys, expected 1")
return
}
fieldResourceName := resourceName
for _, k := range i.Keys {
fieldResourceName = append(fieldResourceName, k.Token.Text)
}
switch value := i.Val.(type) {
case *ast.LiteralType:
findModuleOutputValues(value.Token.Text, module, fieldResourceName, awsResources, state)
default:
log.Warningf("process resource: unsupported value type for resourceName: %v value: %+v", fieldResourceName, value)
}
}
}
}
func findModuleOutputValues(token string, module *Module, fieldResourceName []string, awsResources []Resource, state *HierarchyState) {
if "value" != fieldResourceName[1] {
return
}
resourceFields := findAllResourceFields(token)
moduleOutputName := VariableID(fieldResourceName[0])
for _, resourceField := range resourceFields {
awsAttribute := getAttributeByName(resourceField.Name, resourceField.FieldName, awsResources)
state.ConnectOutputToAttribute(module, moduleOutputName, awsAttribute)
}
moduleFields := findAllModuleFields(token)
for _, moduleField := range moduleFields {
moduleInstance := module.FindModuleInstance(moduleField.InstanceName)
state.ConnectOutputToModuleOutput(moduleInstance, moduleOutputName, moduleField)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// regex utilities
func findAllVariables(token string) []VariableID {
re := regexp.MustCompile("var\\.([-a-zA-Z0-9_]*)")
matches := re.FindAllStringSubmatch(token, -1)
result := make([]VariableID, 0)
for i := 0; i < len(matches); i++ {
if len(matches[i]) == 2 {
result = append(result, VariableID(matches[i][1]))
}
}
return result
}
func findAllResourceFields(token string) []ResourceFieldID {
re := regexp.MustCompile("(aws_[-a-zA-Z_0-9]*)\\.([-a-zA-Z_0-9]*)\\.([-a-zA-Z_0-9]*)")
matches := re.FindAllStringSubmatch(token, -1)
result := make([]ResourceFieldID, 0)
for i := 0; i < len(matches); i++ {
if len(matches[i]) == 4 {
result = append(result, ResourceFieldID{Name: matches[i][1], InstanceName: matches[i][2], FieldName: matches[i][3]})
}
}
return result
}
func findAllModuleFields(token string) []ModuleFieldID {
re := regexp.MustCompile("module\\.([-a-zA-Z_0-9]*)\\.([-a-zA-Z_0-9]*)")
matches := re.FindAllStringSubmatch(token, -1)
result := make([]ModuleFieldID, 0)
for i := 0; i < len(matches); i++ {
if len(matches[i]) == 3 {
result = append(result, ModuleFieldID{InstanceName: matches[i][1], FieldName: matches[i][2]})
}
}
return result
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// aws resource description
func loadResources(path string) ([]Resource, error) {
var resources []Resource
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("resource loading: %v", err)
}
err = json.Unmarshal(bytes, &resources)
if err != nil {
return nil, fmt.Errorf("resource loading: error unmarshalling resources: %v", err)
}
return resources, nil
}
func getArgumentByName(resourceName string, fieldName string, awsResources []Resource) *ResourceArgument {
s1 := unquote(resourceName)
s2 := unquote(fieldName)
for _, res := range awsResources {
if res.Name == s1 {
for _, arg := range res.Arguments {
if arg.Name == s2 {
return &arg
}
}
break
}
}
return nil
}
func getAttributeByName(resourceName string, fieldName string, awsResources []Resource) *ResourceAttribute {
s1 := unquote(resourceName)
s2 := unquote(fieldName)
for _, res := range awsResources {
if res.Name == s1 {
for _, arg := range res.Attributes {
if arg.Name == s2 {
return &arg
}
}
break
}
}
return nil
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////