forked from im2nguyen/rover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rso.go
221 lines (185 loc) · 6.42 KB
/
rso.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
package main
import (
"fmt"
"regexp"
"strings"
tfjson "github.com/hashicorp/terraform-json"
)
// ResourcesOverview represents the root module
type ResourcesOverview struct {
Variables map[string]*tfjson.PlanVariable `json:"variables,omitempty"`
Outputs map[string]*OutputOverview `json:"output,omitempty"`
Resources map[string]*ResourceOverview `json:"resources,omitempty"`
}
// ResourceOverview is a modified tfjson.Plan
type ResourceOverview struct {
// ChangeAction tfjson.Actions `json:change_action`
PriorState map[string]interface{} `json:"prior_state,omitempty"`
PlannedState map[string]interface{} `json:"planned_state,omitempty"`
Change tfjson.Change `json:"change,omitempty"`
Config tfjson.ConfigResource `json:"config,omitempty"`
ModuleConfig *tfjson.ModuleCall `json:"module_config,omitempty"`
DependsOn []string `json:"depends_on,omitempty"`
Children map[string]*ResourceOverview `json:"children,omitempty"`
}
// OutputOverview is a modified tfjson.Change with Outputs
type OutputOverview struct {
// ChangeAction tfjson.Actions `json:change_action`
Change *tfjson.Change `json:"change"`
Config *tfjson.ConfigOutput `json:"config,omitempty"`
}
// GenerateResourceOverview - Overview of files and their resources
// Groups different resource types together
func GenerateResourceOverview(plan *tfjson.Plan) *ResourcesOverview {
rso := &ResourcesOverview{}
rso.Variables = plan.Variables
// Loop through outputs
oo := make(map[string]*OutputOverview)
// Loop through output configs
for outputName, output := range plan.Config.RootModule.Outputs {
if _, ok := oo[outputName]; !ok {
oo[outputName] = &OutputOverview{}
}
oo[outputName].Config = output
}
// Loop through output changes
for outputName, output := range plan.OutputChanges {
if _, ok := oo[outputName]; !ok {
oo[outputName] = &OutputOverview{}
}
oo[outputName].Change = output
}
rso.Outputs = oo
rs := make(map[string]*ResourceOverview)
// reIsChild := regexp.MustCompile(`^\w+\.\w+[\.\[]`)
// reGetParent := regexp.MustCompile(`^\w+\.\w+`)
reIsChild := regexp.MustCompile(`^\w+\.[\w-]+[\.\[]`)
reGetParent := regexp.MustCompile(`^\w+\.[\w-]+`)
// Loop through each resource type and populate graph
for _, rc := range plan.Config.RootModule.Resources {
if _, ok := rs[rc.Address]; !ok {
rs[rc.Address] = &ResourceOverview{}
}
rs[rc.Address].Config = *rc
rs[rc.Address].DependsOn = rc.DependsOn
}
// Add modules
for moduleName, m := range plan.Config.RootModule.ModuleCalls {
mn := fmt.Sprintf("module.%s", moduleName)
if _, ok := rs[mn]; !ok {
rs[mn] = &ResourceOverview{}
}
rs[mn].ModuleConfig = m
}
// Loop through resource changes
for _, rc := range plan.ResourceChanges {
id := rc.Address
var parent string
// Check if resource has parent
// part of module, resource w/ count or for_each
if reIsChild.MatchString(id) {
parent = reGetParent.FindString(id)
// If resource has parent, create parent if doesn't exist
if _, ok := rs[parent]; !ok {
rs[parent] = &ResourceOverview{}
}
if rs[parent].Children == nil {
rs[parent].Children = make(map[string]*ResourceOverview)
}
}
if rc.Change != nil {
// Add resource to parent
if parent != "" {
// Create resource if doesn't exist
if _, ok := rs[parent].Children[id]; !ok {
rs[parent].Children[id] = &ResourceOverview{}
}
rs[parent].Children[id].Change = *rc.Change
// Add type and name since it's missing
// TODO: Find long term fix
rs[parent].Children[id].Config.Name = strings.ReplaceAll(rc.Address, fmt.Sprintf("%s.%s.", parent, rc.Type), "")
rs[parent].Children[id].Config.Type = rc.Type
} else {
rs[rc.Address].Change = *rc.Change
}
}
}
// Populate prior state
if plan.PriorState != nil {
if plan.PriorState.Values != nil {
if plan.PriorState.Values.RootModule != nil {
for _, rst := range plan.PriorState.Values.RootModule.Resources {
id := rst.Address
var parent string
// Check if resource has parent
// part of module, resource w/ count or for_each
if reIsChild.MatchString(id) {
parent = reGetParent.FindString(id)
// If resource has parent, create parent if doesn't exist
if _, ok := rs[parent]; !ok {
rs[parent] = &ResourceOverview{}
}
if rs[parent].Children == nil {
rs[parent].Children = make(map[string]*ResourceOverview)
}
}
if rst.AttributeValues != nil {
// Add resource to parent
if parent != "" {
// Create resource if doesn't exist
if _, ok := rs[parent].Children[id]; !ok {
rs[parent].Children[id] = &ResourceOverview{}
}
rs[parent].Children[id].PriorState = rst.AttributeValues
// Add type and name since it's missing
// TODO: Find long term fix
rs[parent].Children[id].Config.Name = strings.ReplaceAll(rst.Address, fmt.Sprintf("%s.%s.", parent, rst.Type), "")
rs[parent].Children[id].Config.Type = rst.Type
} else {
rs[rst.Address].PriorState = rst.AttributeValues
}
}
}
}
}
}
// Populate planned state
if plan.PlannedValues != nil {
if plan.PlannedValues.RootModule != nil {
for _, rps := range plan.PlannedValues.RootModule.Resources {
id := rps.Address
var parent string
// Check if resource has parent
// part of module, resource w/ count or for_each
if reIsChild.MatchString(id) {
parent = reGetParent.FindString(id)
// If resource has parent, create parent if doesn't exist
if _, ok := rs[parent]; !ok {
rs[parent] = &ResourceOverview{}
}
if rs[parent].Children == nil {
rs[parent].Children = make(map[string]*ResourceOverview)
}
}
if rps.AttributeValues != nil {
// Add resource to parent
if parent != "" {
// Create resource if doesn't exist
if _, ok := rs[parent].Children[id]; !ok {
rs[parent].Children[id] = &ResourceOverview{}
}
rs[parent].Children[id].PlannedState = rps.AttributeValues
// Add type and name since it's missing
// TODO: Find long term fix
rs[parent].Children[id].Config.Name = strings.ReplaceAll(rps.Address, fmt.Sprintf("%s.%s.", parent, rps.Type), "")
rs[parent].Children[id].Config.Type = rps.Type
} else {
rs[rps.Address].PlannedState = rps.AttributeValues
}
}
}
}
}
rso.Resources = rs
return rso
}