-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
parser.go
259 lines (240 loc) · 7.75 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
package terraform
import (
"errors"
"regexp"
"strings"
)
// Parser is an interface for parsing terraform execution result
type Parser interface {
Parse(body string) ParseResult
}
// ParseResult represents the result of parsed terraform execution
type ParseResult struct {
Result string
OutsideTerraform string
ChangedResult string
Warning string
HasAddOrUpdateOnly bool
HasDestroy bool
HasNoChanges bool
HasPlanError bool
HasParseError bool
ExitCode int
Error error
CreatedResources []string
UpdatedResources []string
DeletedResources []string
ReplacedResources []string
}
// DefaultParser is a parser for terraform commands
type DefaultParser struct{}
// PlanParser is a parser for terraform plan
type PlanParser struct {
Pass *regexp.Regexp
Fail *regexp.Regexp
HasDestroy *regexp.Regexp
HasNoChanges *regexp.Regexp
Create *regexp.Regexp
Update *regexp.Regexp
Delete *regexp.Regexp
Replace *regexp.Regexp
ReplaceOption *regexp.Regexp
}
// ApplyParser is a parser for terraform apply
type ApplyParser struct {
Pass *regexp.Regexp
Fail *regexp.Regexp
}
// NewDefaultParser is DefaultParser initializer
func NewDefaultParser() *DefaultParser {
return &DefaultParser{}
}
// NewPlanParser is PlanParser initialized with its Regexp
func NewPlanParser() *PlanParser {
return &PlanParser{
Pass: regexp.MustCompile(`(?m)^(Plan: \d|No changes.)`),
Fail: regexp.MustCompile(`(?m)^(Error: )`),
// "0 to destroy" should be treated as "no destroy"
HasDestroy: regexp.MustCompile(`(?m)([1-9][0-9]* to destroy.)`),
HasNoChanges: regexp.MustCompile(`(?m)^(No changes.)`),
Create: regexp.MustCompile(`^ *# (.*) will be created$`),
Update: regexp.MustCompile(`^ *# (.*) will be updated in-place$`),
Delete: regexp.MustCompile(`^ *# (.*) will be destroyed$`),
Replace: regexp.MustCompile(`^ *# (.*?)(?: is tainted, so)? must be replaced$`),
ReplaceOption: regexp.MustCompile(`^ *# (.*?) will be replaced, as requested$`),
}
}
// NewApplyParser is ApplyParser initialized with its Regexp
func NewApplyParser() *ApplyParser {
return &ApplyParser{
Pass: regexp.MustCompile(`(?m)^(Apply complete!)`),
Fail: regexp.MustCompile(`(?m)^(Error: )`),
}
}
// Parse returns ParseResult related with terraform commands
func (p *DefaultParser) Parse(body string) ParseResult {
return ParseResult{
Result: body,
ExitCode: ExitPass,
Error: nil,
}
}
func extractResource(pattern *regexp.Regexp, line string) string {
if arr := pattern.FindStringSubmatch(line); len(arr) == 2 { //nolint:gomnd
return arr[1]
}
return ""
}
// Parse returns ParseResult related with terraform plan
func (p *PlanParser) Parse(body string) ParseResult { //nolint:cyclop
var exitCode int
switch {
case p.Pass.MatchString(body):
exitCode = ExitPass
case p.Fail.MatchString(body):
exitCode = ExitFail
default:
return ParseResult{
Result: "",
HasParseError: true,
ExitCode: ExitFail,
Error: errors.New("cannot parse plan result"),
}
}
lines := strings.Split(body, "\n")
firstMatchLineIndex := -1
var result, firstMatchLine string
var createdResources, updatedResources, deletedResources, replacedResources []string
startOutsideTerraform := -1
endOutsideTerraform := -1
startChangeOutput := -1
endChangeOutput := -1
startWarning := -1
endWarning := -1
for i, line := range lines {
if line == "Note: Objects have changed outside of Terraform" { // https://github.com/hashicorp/terraform/blob/332045a4e4b1d256c45f98aac74e31102ace7af7/internal/command/views/plan.go#L403
startOutsideTerraform = i + 1
}
if startOutsideTerraform != -1 && endOutsideTerraform == -1 && strings.HasPrefix(line, "Unless you have made equivalent changes to your configuration") { // https://github.com/hashicorp/terraform/blob/332045a4e4b1d256c45f98aac74e31102ace7af7/internal/command/views/plan.go#L110
endOutsideTerraform = i + 1
}
if line == "Terraform will perform the following actions:" { // https://github.com/hashicorp/terraform/blob/332045a4e4b1d256c45f98aac74e31102ace7af7/internal/command/views/plan.go#L252
startChangeOutput = i + 1
}
if startChangeOutput != -1 && endChangeOutput == -1 && strings.HasPrefix(line, "Plan: ") { // https://github.com/hashicorp/terraform/blob/dfc12a6a9e1cff323829026d51873c1b80200757/internal/command/views/plan.go#L306
endChangeOutput = i + 1
}
if strings.HasPrefix(line, "Warning:") && startWarning == -1 {
startWarning = i
}
if strings.HasPrefix(line, "─────") && startWarning != -1 && endWarning == -1 {
endWarning = i
}
if firstMatchLineIndex == -1 {
if p.Pass.MatchString(line) || p.Fail.MatchString(line) {
firstMatchLineIndex = i
firstMatchLine = line
}
}
if rsc := extractResource(p.Create, line); rsc != "" {
createdResources = append(createdResources, rsc)
} else if rsc := extractResource(p.Update, line); rsc != "" {
updatedResources = append(updatedResources, rsc)
} else if rsc := extractResource(p.Delete, line); rsc != "" {
deletedResources = append(deletedResources, rsc)
} else if rsc := extractResource(p.Replace, line); rsc != "" {
replacedResources = append(replacedResources, rsc)
} else if rsc := extractResource(p.ReplaceOption, line); rsc != "" {
replacedResources = append(replacedResources, rsc)
}
}
var hasPlanError bool
switch {
case p.Pass.MatchString(firstMatchLine):
result = lines[firstMatchLineIndex]
case p.Fail.MatchString(firstMatchLine):
hasPlanError = true
result = strings.Join(trimLastNewline(lines[firstMatchLineIndex:]), "\n")
}
hasDestroy := p.HasDestroy.MatchString(firstMatchLine)
hasNoChanges := p.HasNoChanges.MatchString(firstMatchLine)
HasAddOrUpdateOnly := !hasNoChanges && !hasDestroy && !hasPlanError
outsideTerraform := ""
if startOutsideTerraform != -1 {
outsideTerraform = strings.Join(lines[startOutsideTerraform:endOutsideTerraform], "\n")
}
changeResult := ""
if startChangeOutput != -1 {
changeResult = strings.Join(lines[startChangeOutput:endChangeOutput], "\n")
}
warnings := ""
if startWarning != -1 {
if endWarning == -1 {
warnings = strings.Join(lines[startWarning:], "\n")
} else {
warnings = strings.Join(lines[startWarning:endWarning], "\n")
}
}
return ParseResult{
Result: result,
ChangedResult: changeResult,
OutsideTerraform: outsideTerraform,
Warning: warnings,
HasAddOrUpdateOnly: HasAddOrUpdateOnly,
HasDestroy: hasDestroy,
HasNoChanges: hasNoChanges,
HasPlanError: hasPlanError,
ExitCode: exitCode,
Error: nil,
CreatedResources: createdResources,
UpdatedResources: updatedResources,
DeletedResources: deletedResources,
ReplacedResources: replacedResources,
}
}
// Parse returns ParseResult related with terraform apply
func (p *ApplyParser) Parse(body string) ParseResult {
var exitCode int
switch {
case p.Pass.MatchString(body):
exitCode = ExitPass
case p.Fail.MatchString(body):
exitCode = ExitFail
default:
return ParseResult{
Result: "",
ExitCode: ExitFail,
HasParseError: true,
Error: errors.New("cannot parse apply result"),
}
}
lines := strings.Split(body, "\n")
var i int
var result, line string
for i, line = range lines {
if p.Pass.MatchString(line) || p.Fail.MatchString(line) {
break
}
}
switch {
case p.Pass.MatchString(line):
result = lines[i]
case p.Fail.MatchString(line):
result = strings.Join(trimLastNewline(lines[i:]), "\n")
}
return ParseResult{
Result: result,
ExitCode: exitCode,
Error: nil,
}
}
func trimLastNewline(s []string) []string {
if len(s) == 0 {
return s
}
last := len(s) - 1
if s[last] == "" {
return s[:last]
}
return s
}