From 98a040ee0ab869332ded2f8f9a72d8ddfe82a432 Mon Sep 17 00:00:00 2001 From: d-lau Date: Thu, 28 May 2020 11:27:11 +0900 Subject: [PATCH] explicitly say AddOrUpdateOnly --- README.md | 2 +- config/config.go | 14 +-- config/config_test.go | 2 +- ...th-destroy-and-result-labels.tfnotify.yaml | 2 +- main.go | 2 +- notifier/github/notify.go | 2 +- terraform/parser.go | 30 +++--- terraform/parser_test.go | 96 +++++++++---------- 8 files changed, 75 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 02f12a9..ff004d1 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ terraform:
{{ .Body }}
       
- when_add_or_update: + when_add_or_update_only: label: "add-or-update" when_destroy: label: "destroy" diff --git a/config/config.go b/config/config.go index fd51399..d4411d9 100644 --- a/config/config.go +++ b/config/config.go @@ -81,15 +81,15 @@ type Fmt struct { // Plan is a terraform plan config type Plan struct { - Template string `yaml:"template"` - WhenAddOrUpdate WhenAddOrUpdate `yaml:"when_add_or_update,omitempty"` - WhenDestroy WhenDestroy `yaml:"when_destroy,omitempty"` - WhenNoChanges WhenNoChanges `yaml:"when_no_changes,omitempty"` - WhenPlanError WhenPlanError `yaml:"when_plan_error,omitempty"` + Template string `yaml:"template"` + WhenAddOrUpdateOnly WhenAddOrUpdateOnly `yaml:"when_add_or_update_only,omitempty"` + WhenDestroy WhenDestroy `yaml:"when_destroy,omitempty"` + WhenNoChanges WhenNoChanges `yaml:"when_no_changes,omitempty"` + WhenPlanError WhenPlanError `yaml:"when_plan_error,omitempty"` } -// WhenAddOrUpdate is a configuration to notify the plan result contains new or updated in place resources -type WhenAddOrUpdate struct { +// WhenAddOrUpdateOnly is a configuration to notify the plan result contains new or updated in place resources +type WhenAddOrUpdateOnly struct { Label string `yaml:"label,omitempty"` } diff --git a/config/config_test.go b/config/config_test.go index 490801c..e296373 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -93,7 +93,7 @@ func TestLoadFile(t *testing.T) { }, Plan: Plan{ Template: "{{ .Title }}\n{{ .Message }}\n{{if .Result}}\n
{{ .Result }}\n
\n{{end}}\n
Details (Click me)\n\n
{{ .Body }}\n
\n", - WhenAddOrUpdate: WhenAddOrUpdate{ + WhenAddOrUpdateOnly: WhenAddOrUpdateOnly{ Label: "add-or-update", }, WhenDestroy: WhenDestroy{ diff --git a/example-with-destroy-and-result-labels.tfnotify.yaml b/example-with-destroy-and-result-labels.tfnotify.yaml index b1e7fad..04b791b 100644 --- a/example-with-destroy-and-result-labels.tfnotify.yaml +++ b/example-with-destroy-and-result-labels.tfnotify.yaml @@ -18,7 +18,7 @@ terraform:
{{ .Body }}
       
- when_add_or_update: + when_add_or_update_only: label: "add-or-update" when_destroy: label: "destroy" diff --git a/main.go b/main.go index d5d901e..dd0436f 100644 --- a/main.go +++ b/main.go @@ -116,7 +116,7 @@ func (t *tfnotify) Run() error { DestroyWarningTemplate: t.destroyWarningTemplate, WarnDestroy: t.warnDestroy, ResultLabels: github.ResultLabels{ - AddOrUpdateLabel: t.config.Terraform.Plan.WhenAddOrUpdate.Label, + AddOrUpdateLabel: t.config.Terraform.Plan.WhenAddOrUpdateOnly.Label, DestroyLabel: t.config.Terraform.Plan.WhenDestroy.Label, NoChangesLabel: t.config.Terraform.Plan.WhenNoChanges.Label, PlanErrorLabel: t.config.Terraform.Plan.WhenPlanError.Label, diff --git a/notifier/github/notify.go b/notifier/github/notify.go index a114740..212d2b3 100644 --- a/notifier/github/notify.go +++ b/notifier/github/notify.go @@ -39,7 +39,7 @@ func (g *NotifyService) Notify(body string) (exit int, err error) { } var labelToAdd string - if result.HasAddOrUpdate { + if result.HasAddOrUpdateOnly { labelToAdd = cfg.ResultLabels.AddOrUpdateLabel } else if result.HasDestroy { labelToAdd = cfg.ResultLabels.DestroyLabel diff --git a/terraform/parser.go b/terraform/parser.go index 7b44b21..80687dc 100644 --- a/terraform/parser.go +++ b/terraform/parser.go @@ -13,13 +13,13 @@ type Parser interface { // ParseResult represents the result of parsed terraform execution type ParseResult struct { - Result string - HasAddOrUpdate bool - HasDestroy bool - HasNoChanges bool - HasPlanError bool - ExitCode int - Error error + Result string + HasAddOrUpdateOnly bool + HasDestroy bool + HasNoChanges bool + HasPlanError bool + ExitCode int + Error error } // DefaultParser is a parser for terraform commands @@ -130,16 +130,16 @@ func (p *PlanParser) Parse(body string) ParseResult { hasDestroy := p.HasDestroy.MatchString(line) hasNoChanges := p.HasNoChanges.MatchString(line) - HasAddOrUpdate := !hasNoChanges && !hasDestroy && !hasPlanError + HasAddOrUpdateOnly := !hasNoChanges && !hasDestroy && !hasPlanError return ParseResult{ - Result: result, - HasAddOrUpdate: HasAddOrUpdate, - HasDestroy: hasDestroy, - HasNoChanges: hasNoChanges, - HasPlanError: hasPlanError, - ExitCode: exitCode, - Error: nil, + Result: result, + HasAddOrUpdateOnly: HasAddOrUpdateOnly, + HasDestroy: hasDestroy, + HasNoChanges: hasNoChanges, + HasPlanError: hasPlanError, + ExitCode: exitCode, + Error: nil, } } diff --git a/terraform/parser_test.go b/terraform/parser_test.go index 1d92020..6f13626 100644 --- a/terraform/parser_test.go +++ b/terraform/parser_test.go @@ -405,26 +405,26 @@ func TestPlanParserParse(t *testing.T) { name: "plan ok pattern", body: planSuccessResult, result: ParseResult{ - Result: "Plan: 1 to add, 0 to change, 0 to destroy.", - HasAddOrUpdate: true, - HasDestroy: false, - HasNoChanges: false, - HasPlanError: false, - ExitCode: 0, - Error: nil, + Result: "Plan: 1 to add, 0 to change, 0 to destroy.", + HasAddOrUpdateOnly: true, + HasDestroy: false, + HasNoChanges: false, + HasPlanError: false, + ExitCode: 0, + Error: nil, }, }, { name: "no stdin", body: "", result: ParseResult{ - Result: "", - HasAddOrUpdate: false, - HasDestroy: false, - HasNoChanges: false, - HasPlanError: false, - ExitCode: 1, - Error: errors.New("cannot parse plan result"), + Result: "", + HasAddOrUpdateOnly: false, + HasDestroy: false, + HasNoChanges: false, + HasPlanError: false, + ExitCode: 1, + Error: errors.New("cannot parse plan result"), }, }, { @@ -437,64 +437,64 @@ func TestPlanParserParse(t *testing.T) { * google_sql_database.main: google_sql_database.main: Error reading SQL Database "main" in instance "main-master-instance": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState * google_sql_user.proxyuser_main: 1 error(s) occurred:`, - HasAddOrUpdate: false, - HasDestroy: false, - HasNoChanges: false, - HasPlanError: true, - ExitCode: 1, - Error: nil, + HasAddOrUpdateOnly: false, + HasDestroy: false, + HasNoChanges: false, + HasPlanError: true, + ExitCode: 1, + Error: nil, }, }, { name: "plan no changes", body: planNoChanges, result: ParseResult{ - Result: "No changes. Infrastructure is up-to-date.", - HasAddOrUpdate: false, - HasDestroy: false, - HasNoChanges: true, - HasPlanError: false, - ExitCode: 0, - Error: nil, + Result: "No changes. Infrastructure is up-to-date.", + HasAddOrUpdateOnly: false, + HasDestroy: false, + HasNoChanges: true, + HasPlanError: false, + ExitCode: 0, + Error: nil, }, }, { name: "plan has destroy", body: planHasDestroy, result: ParseResult{ - Result: "Plan: 0 to add, 0 to change, 1 to destroy.", - HasAddOrUpdate: false, - HasDestroy: true, - HasNoChanges: false, - HasPlanError: false, - ExitCode: 0, - Error: nil, + Result: "Plan: 0 to add, 0 to change, 1 to destroy.", + HasAddOrUpdateOnly: false, + HasDestroy: true, + HasNoChanges: false, + HasPlanError: false, + ExitCode: 0, + Error: nil, }, }, { name: "plan has add and destroy", body: planHasAddAndDestroy, result: ParseResult{ - Result: "Plan: 1 to add, 0 to change, 1 to destroy.", - HasAddOrUpdate: false, - HasDestroy: true, - HasNoChanges: false, - HasPlanError: false, - ExitCode: 0, - Error: nil, + Result: "Plan: 1 to add, 0 to change, 1 to destroy.", + HasAddOrUpdateOnly: false, + HasDestroy: true, + HasNoChanges: false, + HasPlanError: false, + ExitCode: 0, + Error: nil, }, }, { name: "plan has add and update in place", body: planHasAddAndUpdateInPlace, result: ParseResult{ - Result: "Plan: 1 to add, 1 to change, 0 to destroy.", - HasAddOrUpdate: true, - HasDestroy: false, - HasNoChanges: false, - HasPlanError: false, - ExitCode: 0, - Error: nil, + Result: "Plan: 1 to add, 1 to change, 0 to destroy.", + HasAddOrUpdateOnly: true, + HasDestroy: false, + HasNoChanges: false, + HasPlanError: false, + ExitCode: 0, + Error: nil, }, }, }