Skip to content

Commit c82e81a

Browse files
feat: listing imported or moved resources even if they are changed or replaced (#932)
* feat: listing imported or moved resources even if they are changed or replaced * test: add test * test: disable linter * fix: check array index
1 parent 61efb4c commit c82e81a

File tree

2 files changed

+345
-5
lines changed

2 files changed

+345
-5
lines changed

pkg/terraform/parser.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type PlanParser struct {
4646
ReplaceOption *regexp.Regexp
4747
Move *regexp.Regexp
4848
Import *regexp.Regexp
49+
ImportedFrom *regexp.Regexp
50+
MovedFrom *regexp.Regexp
4951
}
5052

5153
// ApplyParser is a parser for terraform apply
@@ -71,6 +73,8 @@ func NewPlanParser() *PlanParser {
7173
ReplaceOption: regexp.MustCompile(`^ *# (.*?) will be replaced, as requested$`),
7274
Move: regexp.MustCompile(`^ *# (.*?) has moved to (.*?)$`),
7375
Import: regexp.MustCompile(`^ *# (.*?) will be imported$`),
76+
ImportedFrom: regexp.MustCompile(`^ *# \(imported from (.*?)\)$`),
77+
MovedFrom: regexp.MustCompile(`^ *# \(moved from (.*?)\)$`),
7478
}
7579
}
7680

@@ -175,8 +179,25 @@ func (p *PlanParser) Parse(body string) ParseResult { //nolint:cyclop
175179
replacedResources = append(replacedResources, rsc)
176180
} else if rsc := extractResource(p.Import, line); rsc != "" {
177181
importedResources = append(importedResources, rsc)
182+
} else if rsc := extractResource(p.ImportedFrom, line); rsc != "" {
183+
if i == 0 {
184+
continue
185+
}
186+
if rsc := p.changedResources(lines[i-1]); rsc != "" {
187+
importedResources = append(importedResources, rsc)
188+
}
178189
} else if rsc := extractMovedResource(p.Move, line); rsc != nil {
179190
movedResources = append(movedResources, rsc)
191+
} else if fromRsc := extractResource(p.MovedFrom, line); fromRsc != "" {
192+
if i == 0 {
193+
continue
194+
}
195+
if toRsc := p.changedResources(lines[i-1]); toRsc != "" {
196+
movedResources = append(movedResources, &MovedResource{
197+
Before: fromRsc,
198+
After: toRsc,
199+
})
200+
}
180201
}
181202
}
182203
var hasPlanError bool
@@ -237,6 +258,17 @@ func (p *PlanParser) Parse(body string) ParseResult { //nolint:cyclop
237258
}
238259
}
239260

261+
func (p *PlanParser) changedResources(line string) string {
262+
if rsc := extractResource(p.Update, line); rsc != "" {
263+
return rsc
264+
} else if rsc := extractResource(p.Replace, line); rsc != "" {
265+
return rsc
266+
} else if rsc := extractResource(p.ReplaceOption, line); rsc != "" {
267+
return rsc
268+
}
269+
return ""
270+
}
271+
240272
type MovedResource struct {
241273
Before string
242274
After string

0 commit comments

Comments
 (0)