Skip to content

Commit

Permalink
fix: Include information about imports in PlanSuccessStats (#3621)
Browse files Browse the repository at this point in the history
* Include information about imports in PlanSuccessStats

Terraform 1.5 introduced a feature to import existing resources into
state via code.

https://developer.hashicorp.com/terraform/language/import

When you run `terraform plan` with imports, the output includes information
about the imported resources, which breaks the current regex for parsing
plan changes.

This PR updates the regex to include the, optional, import information.

* Use the ?

Co-authored-by: Benjamin Ullian <5695343+bnu0@users.noreply.github.com>

---------

Co-authored-by: Will Richard <wrichard@seatgeek.com>
Co-authored-by: Benjamin Ullian <5695343+bnu0@users.noreply.github.com>
Co-authored-by: Dylan Page <dylan.page@autodesk.com>
  • Loading branch information
4 people committed Jul 30, 2023
1 parent 75b4b06 commit 3ca5d82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
13 changes: 7 additions & 6 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ type PolicySetStatus struct {
// Summary regexes
var (
reChangesOutside = regexp.MustCompile(`Note: Objects have changed outside of Terraform`)
rePlanChanges = regexp.MustCompile(`Plan: (\d+) to add, (\d+) to change, (\d+) to destroy.`)
rePlanChanges = regexp.MustCompile(`Plan: (?:(\d+) to import, )?(\d+) to add, (\d+) to change, (\d+) to destroy.`)
reNoChanges = regexp.MustCompile(`No changes. (Infrastructure is up-to-date|Your infrastructure matches the configuration).`)
)

Expand Down Expand Up @@ -631,8 +631,8 @@ type WorkflowHookCommandContext struct {

// PlanSuccessStats holds stats for a plan.
type PlanSuccessStats struct {
Add, Change, Destroy int
Changes, ChangesOutside bool
Import, Add, Change, Destroy int
Changes, ChangesOutside bool
}

func NewPlanSuccessStats(output string) PlanSuccessStats {
Expand All @@ -647,9 +647,10 @@ func NewPlanSuccessStats(output string) PlanSuccessStats {
// We can skip checking the error here as we can assume
// Terraform output will always render an integer on these
// blocks.
s.Add, _ = strconv.Atoi(m[1])
s.Change, _ = strconv.Atoi(m[2])
s.Destroy, _ = strconv.Atoi(m[3])
s.Import, _ = strconv.Atoi(m[1])
s.Add, _ = strconv.Atoi(m[2])
s.Change, _ = strconv.Atoi(m[3])
s.Destroy, _ = strconv.Atoi(m[4])
}

return s
Expand Down
24 changes: 24 additions & 0 deletions server/events/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ func TestPlanSuccess_Summary(t *testing.T) {
"dummy\nPlan: 100 to add, 111 to change, 222 to destroy.",
"Plan: 100 to add, 111 to change, 222 to destroy.",
},
{
"dummy\nPlan: 42 to import, 53 to add, 64 to change, 75 to destroy.",
"Plan: 42 to import, 53 to add, 64 to change, 75 to destroy.",
},
{
"Note: Objects have changed outside of Terraform\ndummy\nNo changes. Infrastructure is up-to-date.",
"\n**Note: Objects have changed outside of Terraform**\nNo changes. Infrastructure is up-to-date.",
Expand Down Expand Up @@ -629,6 +633,7 @@ func TestPlanSuccessStats(t *testing.T) {
Plan: 1 to add, 3 to change, 2 to destroy.`,
models.PlanSuccessStats{
Changes: true,

Add: 1,
Change: 3,
Destroy: 2,
Expand All @@ -651,6 +656,25 @@ func TestPlanSuccessStats(t *testing.T) {
ChangesOutside: true,
},
},
{
"with imports",
`Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
~ update in-place
- destroy
Terraform will perform the following actions:
- null_resource.hi[1]
Plan: 42 to import, 31 to add, 20 to change, 1 to destroy.`,
models.PlanSuccessStats{
Changes: true,

Import: 42,
Add: 31,
Change: 20,
Destroy: 1,
},
},
{
"changes and changes outside",
`Note: Objects have changed outside of Terraform
Expand Down

0 comments on commit 3ca5d82

Please sign in to comment.