Skip to content

Commit f4964c1

Browse files
alethenoriofredrikaverpil
authored andcommitted
feat(sgterraform): add support for "forget" action in terraform summary
Terraform is able to forget about resources (meaning remove from state but leave real resource untouched) so we add support for this action.
1 parent 319d732 commit f4964c1

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

tools/sgterraform/tfplan.go

+12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const (
4545

4646
// ActionDelete denotes a delete operation.
4747
ActionDelete Action = "delete"
48+
49+
// ActionForget denotes a forget operation.
50+
ActionForget Action = "forget"
4851
)
4952

5053
// Actions denotes a valid change type.
@@ -124,3 +127,12 @@ func (a Actions) CreateBeforeDestroy() bool {
124127
func (a Actions) Replace() bool {
125128
return a.DestroyBeforeCreate() || a.CreateBeforeDestroy()
126129
}
130+
131+
// Forget is true if this set of Actions denotes a forget operation.
132+
func (a Actions) Forget() bool {
133+
if len(a) != 1 {
134+
return false
135+
}
136+
137+
return a[0] == ActionForget
138+
}

tools/sgterraform/tools.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func getCommentSummary(ctx context.Context, planFilePath string) (statusIcon, su
181181
destroy := TfChange{actionName: "Destroy", changes: make(map[string]int), actionCount: 0}
182182
update := TfChange{actionName: "Update", changes: make(map[string]int), actionCount: 0}
183183
replace := TfChange{actionName: "Replace", changes: make(map[string]int), actionCount: 0}
184+
forget := TfChange{actionName: "Forget", changes: make(map[string]int), actionCount: 0}
184185
for _, res := range jsonPlan.ResourceChanges {
185186
actions := res.Change.Actions
186187
resourceType := res.Type
@@ -193,13 +194,17 @@ func getCommentSummary(ctx context.Context, planFilePath string) (statusIcon, su
193194
update.add(resourceType)
194195
case actions.Replace():
195196
replace.add(resourceType)
197+
case actions.Forget():
198+
forget.add(resourceType)
196199
case
197200
actions.NoOp(),
198201
actions.Read():
199202
// Do nothing
200203
continue
201204
default:
202-
sg.Logger(ctx).Fatal(fmt.Errorf("unable to determine resource operation: %v", res))
205+
sg.Logger(ctx).Fatal(
206+
fmt.Errorf("unable to build a terraform plan summary due to unknown actions (%v) on resource: %v", actions, res),
207+
)
203208
}
204209
}
205210

@@ -212,14 +217,15 @@ func getCommentSummary(ctx context.Context, planFilePath string) (statusIcon, su
212217
}
213218

214219
summary = fmt.Sprintf(`
215-
Plan Summary: %d to create, %d to update, %d to replace, %d to destroy.
220+
Plan Summary: %d to create, %d to update, %d to replace, %d to destroy, %d to forget.
216221
<br/>
217222
%s
218223
`,
219224
create.actionCount,
220225
update.actionCount,
221226
replace.actionCount,
222227
destroy.actionCount,
228+
forget.actionCount,
223229
mapToHTMLList([]TfChange{create, destroy, update, replace}),
224230
)
225231

0 commit comments

Comments
 (0)