Skip to content

Commit

Permalink
Merge pull request #105 from KeisukeYamashita/support-terraform-0-15-…
Browse files Browse the repository at this point in the history
…or-later

Support Terraform `0.15` or later
  • Loading branch information
drlau authored Jan 21, 2022
2 parents 04274c9 + 823ebf5 commit d912440
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
4 changes: 2 additions & 2 deletions terraform/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewFmtParser() *FmtParser {
func NewPlanParser() *PlanParser {
return &PlanParser{
Pass: regexp.MustCompile(`(?m)^(Plan: \d|No changes.)`),
Fail: regexp.MustCompile(`(?m)^(Error: )`),
Fail: regexp.MustCompile(`(?m)^(\|\s{1})?(Error: )`),
// "0 to destroy" should be treated as "no destroy"
HasDestroy: regexp.MustCompile(`(?m)([1-9][0-9]* to destroy.)`),
HasNoChanges: regexp.MustCompile(`(?m)^(No changes. Infrastructure is up-to-date.)`),
Expand All @@ -73,7 +73,7 @@ func NewPlanParser() *PlanParser {
func NewApplyParser() *ApplyParser {
return &ApplyParser{
Pass: regexp.MustCompile(`(?m)^(Apply complete!)`),
Fail: regexp.MustCompile(`(?m)^(Error: )`),
Fail: regexp.MustCompile(`(?m)^(\|\s{1})?(Error: )`),
}
}

Expand Down
88 changes: 82 additions & 6 deletions terraform/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
`

const planFailureResult = `
const planFailureResult0_12 = `
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
Expand All @@ -99,6 +99,19 @@ Error: Error refreshing state: 4 error(s) occurred:
* google_sql_user.proxyuser_main: 1 error(s) occurred:
`

const planFailureResult0_15 = `
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
| Error: Error refreshing state: 4 error(s) occurred:
|
| * google_sql_database.main: 1 error(s) occurred:
|
| * 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:
`

const planNoChanges = `
google_bigquery_dataset.tfnotify_echo: Refreshing state...
google_project.team: Refreshing state...
Expand Down Expand Up @@ -300,7 +313,7 @@ google_dns_record_set.dev_tfnotifyapps_com: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
`

const applyFailureResult = `
const applyFailureResult0_12 = `
data.terraform_remote_state.teams_platform_development: Refreshing state...
google_project.tfnotify_jp_tfnotify_prod: Refreshing state...
google_project_services.tfnotify_jp_tfnotify_prod: Refreshing state...
Expand Down Expand Up @@ -331,6 +344,37 @@ Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for reques
`

const applyFailureResult0_15 = `
data.terraform_remote_state.teams_platform_development: Refreshing state...
google_project.tfnotify_jp_tfnotify_prod: Refreshing state...
google_project_services.tfnotify_jp_tfnotify_prod: Refreshing state...
google_bigquery_dataset.gateway_access_log: Refreshing state...
google_compute_global_address.reviews_web_tfnotify_in: Refreshing state...
google_compute_global_address.chartmuseum_tfnotifyapps_com: Refreshing state...
google_storage_bucket.chartmuseum: Refreshing state...
google_storage_bucket.ark_tfnotify_prod: Refreshing state...
google_compute_global_address.reviews_api_tfnotify_in: Refreshing state...
google_logging_project_sink.gateway_access_log_bigquery_sink: Refreshing state...
google_project_iam_member.gateway_access_log_bigquery_sink_writer_is_bigquery_data_editor: Refreshing state...
aws_s3_bucket.terraform_backend: Refreshing state...
aws_s3_bucket.teams_terraform_private_modules: Refreshing state...
aws_iam_policy.datadog_aws_integration: Refreshing state...
aws_iam_role.datadog_aws_integration: Refreshing state...
aws_iam_user.teams_terraform: Refreshing state...
aws_iam_user_policy.teams_terraform: Refreshing state...
aws_iam_role_policy_attachment.datadog_aws_integration: Refreshing state...
google_dns_managed_zone.tfnotifyapps_com: Refreshing state...
google_dns_record_set.dev_tfnotifyapps_com: Refreshing state...
| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden
|
| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service":
| 6: resource "google_project_service" "gcp_api_service" {
|
|
`

func TestDefaultParserParse(t *testing.T) {
testCases := []struct {
body string
Expand Down Expand Up @@ -428,8 +472,8 @@ func TestPlanParserParse(t *testing.T) {
},
},
{
name: "plan ng pattern",
body: planFailureResult,
name: "plan ng pattern 0.12",
body: planFailureResult0_12,
result: ParseResult{
Result: `Error: Error refreshing state: 4 error(s) occurred:
Expand All @@ -445,6 +489,24 @@ func TestPlanParserParse(t *testing.T) {
Error: nil,
},
},
{
name: "plan ng pattern 0.15",
body: planFailureResult0_15,
result: ParseResult{
Result: `| Error: Error refreshing state: 4 error(s) occurred:
|
| * google_sql_database.main: 1 error(s) occurred:
|
| * 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:`,
HasAddOrUpdateOnly: false,
HasDestroy: false,
HasNoChanges: false,
HasPlanError: true,
ExitCode: 1,
Error: nil,
},
},
{
name: "plan no changes",
body: planNoChanges,
Expand Down Expand Up @@ -531,8 +593,8 @@ func TestApplyParserParse(t *testing.T) {
},
},
{
name: "apply ng pattern",
body: applyFailureResult,
name: "apply ng pattern 0.12",
body: applyFailureResult0_12,
result: ParseResult{
Result: `Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden
Expand All @@ -544,6 +606,20 @@ func TestApplyParserParse(t *testing.T) {
Error: nil,
},
},
{
name: "apply ng pattern 0.15",
body: applyFailureResult0_15,
result: ParseResult{
Result: `| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden
|
| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service":
| 6: resource "google_project_service" "gcp_api_service" {
|
|`,
ExitCode: 1,
Error: nil,
},
},
}
for _, testCase := range testCases {
result := NewApplyParser().Parse(testCase.body)
Expand Down

0 comments on commit d912440

Please sign in to comment.