From 1140f3e750a47f15f89343e29c88a4c0d87a3093 Mon Sep 17 00:00:00 2001 From: Will Richard Date: Mon, 24 Jul 2023 13:16:28 -0400 Subject: [PATCH 1/2] 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. --- server/events/models/models.go | 13 +++++++------ server/events/models/models_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/server/events/models/models.go b/server/events/models/models.go index c5d89b99b0..6ad101d678 100644 --- a/server/events/models/models.go +++ b/server/events/models/models.go @@ -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).`) ) @@ -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 { @@ -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 diff --git a/server/events/models/models_test.go b/server/events/models/models_test.go index b698581d29..126d89c60a 100644 --- a/server/events/models/models_test.go +++ b/server/events/models/models_test.go @@ -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.", @@ -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, @@ -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 From fbeb52b0e25640c9b9f9903770941e8bffcc2cb4 Mon Sep 17 00:00:00 2001 From: William Richard Date: Mon, 24 Jul 2023 14:57:05 -0400 Subject: [PATCH 2/2] Use the ? Co-authored-by: Benjamin Ullian <5695343+bnu0@users.noreply.github.com> --- server/events/models/models.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/events/models/models.go b/server/events/models/models.go index 6ad101d678..4d9ccc60a1 100644 --- a/server/events/models/models.go +++ b/server/events/models/models.go @@ -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 import, )|(?:))(\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).`) )