-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_tfplanjson.go
226 lines (185 loc) · 5.56 KB
/
generate_tfplanjson.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
package tentez
import (
"encoding/json"
"fmt"
)
type TerraformPlanJson struct {
FormatVersion string `json:"format_version"`
ResourceChanges []ResourceChange `json:"resource_changes"`
}
type ResourceChange struct {
Address string `json:"address"`
Type string `json:"type"`
Change Change `json:"change"`
}
type Change struct {
Actions ChangeActions `json:"actions"`
Before json.RawMessage `json:"before"`
After json.RawMessage `json:"after"`
}
type ChangeActions []ChangeAction
type ChangeAction string
type ChangeValueInterface interface {
GetTarget() (string, error)
GetSwitchTarget() (string, error)
}
type ChangeValueAwsListener struct {
Arn string `json:"arn"`
DefaultActions []ChangeValueElbV2TargetAction `json:"default_action"`
}
type ChangeValueAwsListenerRule struct {
Arn string `json:"arn"`
Actions []ChangeValueElbV2TargetAction `json:"action"`
}
type ChangeValueElbV2TargetAction struct {
TargetGroupArn string `json:"target_group_arn"`
Forward []ChangeValueElbV2TargetActionForward `json:"forward"`
}
type ChangeValueElbV2TargetActionForward struct {
TargetGroups []ChangeValueElbV2TargetActionForwardTargetGroups `json:"target_group"`
}
type ChangeValueElbV2TargetActionForwardTargetGroups struct {
Arn string `json:"arn"`
Weight int `json:"weight"`
}
func (c ChangeValueAwsListener) GetSwitchTarget() (string, error) {
if len(c.DefaultActions) != 1 {
return "", fmt.Errorf("cannot get default_action; %+v", c)
}
if len(c.DefaultActions) == 1 && c.DefaultActions[0].TargetGroupArn != "" {
return c.DefaultActions[0].TargetGroupArn, nil
}
if len(c.DefaultActions[0].Forward) == 1 {
for _, tg := range c.DefaultActions[0].Forward[0].TargetGroups {
if tg.Weight > 0 {
return tg.Arn, nil
}
}
}
return "", fmt.Errorf("cannot find target group")
}
func (c ChangeValueAwsListener) GetTarget() (string, error) {
return c.Arn, nil
}
func (c ChangeValueAwsListenerRule) GetSwitchTarget() (string, error) {
if len(c.Actions) != 1 {
return "", fmt.Errorf("cannot get action; %+v", c)
}
if len(c.Actions) == 1 && c.Actions[0].TargetGroupArn != "" {
return c.Actions[0].TargetGroupArn, nil
}
if len(c.Actions[0].Forward) == 1 {
for _, tg := range c.Actions[0].Forward[0].TargetGroups {
if tg.Weight > 0 {
return tg.Arn, nil
}
}
}
return "", fmt.Errorf("cannot find target group")
}
func (c ChangeValueAwsListenerRule) GetTarget() (string, error) {
return c.Arn, nil
}
const (
ChangeActionNoop ChangeAction = "no-op"
ChangeActionCreate ChangeAction = "create"
ChangeActionRead ChangeAction = "read"
ChangeActionUpdate ChangeAction = "update"
ChangeActionDelete ChangeAction = "delete"
)
func (c ChangeActions) IsUpdate() bool {
return len(c) == 1 && c[0] == ChangeActionUpdate
}
func GenerateConfigFromTerraformPlanJsons(jsons []TerraformPlanJson) (YamlStruct, error) {
allAwsListeners := []AwsListener{}
allAwsListenerRules := []AwsListenerRule{}
for _, json := range jsons {
awsListeners, err := getAwsListenersFromTerraformJson(json)
if err != nil {
return YamlStruct{}, err
}
allAwsListeners = append(allAwsListeners, awsListeners...)
awsListenerRules, err := getAwsListenerRulesFromTerraformJson(json)
if err != nil {
return YamlStruct{}, err
}
allAwsListenerRules = append(allAwsListenerRules, awsListenerRules...)
}
return YamlStruct{
Steps: defaultSteps,
AwsListeners: allAwsListeners,
AwsListenerRules: allAwsListenerRules,
}, nil
}
func getAwsListenersFromTerraformJson(tfplanjson TerraformPlanJson) ([]AwsListener, error) {
awsListeners := []AwsListener{}
for _, resourceChange := range tfplanjson.ResourceChanges {
if !(resourceChange.Change.Actions.IsUpdate() && resourceChange.Type == "aws_lb_listener") {
continue
}
var before, after ChangeValueAwsListener
if err := json.Unmarshal(resourceChange.Change.Before, &before); err != nil {
return nil, err
}
if err := json.Unmarshal(resourceChange.Change.After, &after); err != nil {
return nil, err
}
target, err := before.GetTarget()
if err != nil {
return nil, err
}
switchOld, err := before.GetSwitchTarget()
if err != nil {
return nil, err
}
switchAfter, err := after.GetSwitchTarget()
if err != nil {
return nil, err
}
awsListeners = append(awsListeners, AwsListener{
Name: resourceChange.Address,
Target: target,
Switch: Switch{
Old: switchOld,
New: switchAfter,
},
})
}
return awsListeners, nil
}
func getAwsListenerRulesFromTerraformJson(tfplanjson TerraformPlanJson) ([]AwsListenerRule, error) {
awsListenerRules := []AwsListenerRule{}
for _, resourceChange := range tfplanjson.ResourceChanges {
if !(resourceChange.Change.Actions.IsUpdate() && resourceChange.Type == "aws_lb_listener_rule") {
continue
}
var before, after ChangeValueAwsListenerRule
if err := json.Unmarshal(resourceChange.Change.Before, &before); err != nil {
return nil, err
}
if err := json.Unmarshal(resourceChange.Change.After, &after); err != nil {
return nil, err
}
target, err := before.GetTarget()
if err != nil {
return nil, err
}
switchOld, err := before.GetSwitchTarget()
if err != nil {
return nil, err
}
switchAfter, err := after.GetSwitchTarget()
if err != nil {
return nil, err
}
awsListenerRules = append(awsListenerRules, AwsListenerRule{
Name: resourceChange.Address,
Target: target,
Switch: Switch{
Old: switchOld,
New: switchAfter,
},
})
}
return awsListenerRules, nil
}