-
Notifications
You must be signed in to change notification settings - Fork 69
/
changeset.go
177 lines (165 loc) · 4.37 KB
/
changeset.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
package ls
import (
"encoding/json"
"fmt"
"strings"
"github.com/appscode/jsonpatch"
"github.com/aws-cloudformation/rain/cft/diff"
"github.com/aws-cloudformation/rain/internal/aws/cfn"
"github.com/aws-cloudformation/rain/internal/config"
"github.com/aws-cloudformation/rain/internal/console"
"github.com/aws-cloudformation/rain/internal/console/spinner"
"github.com/aws-cloudformation/rain/internal/ui"
)
// valueForPath finds the value for the given path and returns it as a string
func valueForPath(path string, j map[string]any) string {
tokens := strings.Split(path, "/")
for i, token := range tokens {
if token == "" {
continue
}
if i == len(tokens)-1 {
return fmt.Sprintf("%v", j[token])
} else {
v := j[token]
if v == nil {
config.Debugf("unexpected valueForPath %s, %s is nil?", path, token)
config.Debugf("j: %v", j)
return "??"
}
j = j[token].(map[string]any)
}
}
return "?"
}
func showChangeset(stackName, changeSetName string) {
spinner.Push("Fetching changeset details")
cs, err := cfn.GetChangeSet(stackName, changeSetName)
if err != nil {
panic(ui.Errorf(err, "failed to get changeset '%s'", changeSetName))
}
config.Debugf("ChangeSet response: %+v", cs)
if cs == nil {
return
}
out := ""
out += fmt.Sprintf("Arn: %v\n", *cs.ChangeSetId)
out += fmt.Sprintf("Created: %v\n", cs.CreationTime)
descr := ""
if cs.Description != nil {
descr = *cs.Description
}
out += fmt.Sprintf("Description: %v\n", descr)
reason := ""
if cs.StatusReason != nil {
reason = "(" + *cs.StatusReason + ")"
}
out += fmt.Sprintf("Status: %v/%v %v\n",
ui.ColouriseStatus(string(cs.ExecutionStatus)),
ui.ColouriseStatus(string(cs.Status)),
reason)
out += "Parameters: "
if len(cs.Parameters) == 0 {
out += "(None)\n"
} else {
out += "\n"
}
for _, p := range cs.Parameters {
k, v := "", ""
if p.ParameterKey != nil {
k = *p.ParameterKey
}
if p.ParameterValue != nil {
v = *p.ParameterValue
}
out += fmt.Sprintf(" %s: %s\n", k, v)
}
out += "Changes: \n"
for _, csch := range cs.Changes {
if csch.ResourceChange == nil {
continue
}
change := csch.ResourceChange
rid := ""
if change.LogicalResourceId != nil {
rid = *change.LogicalResourceId
}
rt := ""
if change.ResourceType != nil {
rt = *change.ResourceType
}
pid := ""
if change.PhysicalResourceId != nil {
pid = *change.PhysicalResourceId
}
replace := ""
switch string(change.Replacement) {
case "True":
replace = " [Replace]"
case "Conditional":
replace = " [Might replace]"
}
if change.Action == "Add" {
replace = ""
}
changeMsg := fmt.Sprintf(" %s%s: %s (%s) %s\n",
string(change.Action),
replace,
rid,
rt,
pid)
switch change.Action {
case "Add":
out += console.Green(changeMsg)
case "Modify":
out += console.Blue(changeMsg)
case "Remove":
out += console.Red(changeMsg)
default:
out += changeMsg
}
for _, detail := range change.Details {
config.Debugf("Detail: %+v", detail)
}
// Compare properties to see what has changed
config.Debugf("Change: %+v", change)
var before, after string
if change.BeforeContext != nil {
config.Debugf("Before: %v", *change.BeforeContext)
before = *change.BeforeContext
}
if change.AfterContext != nil {
config.Debugf("After: %v", *change.AfterContext)
after = *change.AfterContext
}
if before != "" && after != "" {
var beforeJson, afterJson map[string]any
if err := json.Unmarshal([]byte(before), &beforeJson); err != nil {
config.Debugf("%v", err)
} else {
if err = json.Unmarshal([]byte(after), &afterJson); err != nil {
config.Debugf("%v", err)
} else {
diff := diff.CompareMaps(beforeJson, afterJson)
config.Debugf(diff.Format(true))
// jsonpatch is a little easier to work with than Diff
ops, err := jsonpatch.CreatePatch([]byte(before), []byte(after))
if err != nil {
config.Debugf("%v", err)
} else {
for _, op := range ops {
path := strings.Replace(op.Path, "/", ".", -1)
path = strings.Replace(path, ".", "", 1) // 1st instance of .
out += console.Blue(fmt.Sprintf(" %s\n", path))
was := valueForPath(op.Path, beforeJson)
out += console.Blue(fmt.Sprintf(" before: %s\n", was))
out += console.Blue(fmt.Sprintf(" after: %s\n", op.Value))
}
}
}
}
}
}
spinner.Pop()
fmt.Println(out)
}