From 540606162cb484883b2d37b67ed42e0319aa7070 Mon Sep 17 00:00:00 2001 From: Steve Kluck <3161125+skluck@users.noreply.github.com> Date: Fri, 28 Jan 2022 12:23:17 -0500 Subject: [PATCH] Fix output only plan from causing parse error --- CHANGELOG.md | 21 ++++++++ src/Terraform12OutputParser.php | 37 ++++++++++---- .../86-output-only-changes.expected.json | 5 ++ .../86-output-only-changes.stdout.txt | 49 +++++++++++++++++++ tests/Terraform10OutputParserTest.php | 1 + 5 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 tests/.fixtures-1.0/86-output-only-changes.expected.json create mode 100644 tests/.fixtures-1.0/86-output-only-changes.stdout.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index b84244e..75a3bce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,27 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/). > Sections: (`Added`, `Changed`, `Deprecated`, `Removed`, `Fixed`, `Security`) +## [1.3.6] - 2022-01-28 + +### Fixed +- Prevent parsing error when an empty plan is generated for Terraform 0.13+ where there is an output-only change. + > Example output of a plan: + > ``` + > # aws_lambda_function.default has been changed + > ~ resource "aws_lambda_function" "default" { + > # (2 unchanged blocks hidden) + > } + > + > Unless you have made equivalent changes to your configuration, or ignored the + > relevant attributes using ignore_changes, the following plan may include + > actions to undo or respond to these changes. + > + > ───────────────────────────────────────────────────────────────────────────── + > + > Changes to Outputs: + > ~ lambda_source_code_size = 100 -> 101 + > ``` + ## [1.3.4] - 2022-01-13 ### Fixed diff --git a/src/Terraform12OutputParser.php b/src/Terraform12OutputParser.php index e67d353..c36b8e4 100755 --- a/src/Terraform12OutputParser.php +++ b/src/Terraform12OutputParser.php @@ -1,6 +1,6 @@ stripANSICodes($input); $input = $this->sanitizeWindowsLineEndings($input); - if (strpos($input, self::NO_CHANGES_STRING) !== false) { - return [ - 'errors' => $this->errors(), - 'changedResources' => [], - 'modules' => [], - ]; - } - - if (strpos($input, self::NO_CHANGES_STRING_ALT) !== false) { + if ($this->isNoChangesPlan($input)) { return [ 'errors' => $this->errors(), 'changedResources' => [], @@ -373,6 +366,30 @@ private function parseUsedModules($input) return $modules; } + /** + * @param string $input + * + * @return bool + */ + private function isNoChangesPlan($input) + { + if (strpos($input, self::NO_CHANGES_STRING) !== false) { + return true; + } + + // Terraform 0.13+ where absolutely no changes (not even to outputs) + if (strpos($input, self::NO_CHANGES_STRING_ALT) !== false) { + return true; + } + + // Terraform 0.13+ where output only changes (may have "changes outside of terraform") + if (strpos($input, self::NO_CHANGES_OUTPUT_ONLY) !== false) { + return true; + } + + return false; + } + /** * @param string $input * diff --git a/tests/.fixtures-1.0/86-output-only-changes.expected.json b/tests/.fixtures-1.0/86-output-only-changes.expected.json new file mode 100644 index 0000000..37be1f4 --- /dev/null +++ b/tests/.fixtures-1.0/86-output-only-changes.expected.json @@ -0,0 +1,5 @@ +{ + "errors": [], + "changedResources": [], + "modules": [] +} diff --git a/tests/.fixtures-1.0/86-output-only-changes.stdout.txt b/tests/.fixtures-1.0/86-output-only-changes.stdout.txt new file mode 100644 index 0000000..f90836c --- /dev/null +++ b/tests/.fixtures-1.0/86-output-only-changes.stdout.txt @@ -0,0 +1,49 @@ +Note: Objects have changed outside of Terraform + +Terraform detected the following changes made outside of Terraform since the +last "terraform apply": + + # aws_lambda_function.default has been changed + ~ resource "aws_lambda_function" "default" { + id = "prod-123456-myservice" + ~ last_modified = "2022-01-26T01:40:57.000+0000" -> "2022-01-26T14:31:40.000+0000" + ~ source_code_size = 2830 -> 2397 + tags = { + "app-id" = "123456" + "environment" = "prod" + "iac" = "terraform" + } + # (17 unchanged attributes hidden) + + + environment { + + variables = { + + "ExampleVar" = "myenvvar" + } + } + + + # (2 unchanged blocks hidden) + } + +Unless you have made equivalent changes to your configuration, or ignored the +relevant attributes using ignore_changes, the following plan may include +actions to undo or respond to these changes. + +───────────────────────────────────────────────────────────────────────────── + +Changes to Outputs: + ~ lambda_source_code_size = 2830 -> 2397 + +You can apply this plan to save these new output values to the Terraform +state, without changing any real infrastructure. +╷ +│ Warning: Version constraints inside provider configuration blocks are deprecated +│  +│  on .terraform/modules/lambda_iam_module/modules/lambda-iam/main.tf line 3, in provider "aws": +│  3: version = "~> 3.20" +│  +│ Terraform 0.13 and earlier allowed provider version constraints inside the +│ provider configuration block, but that is now deprecated and will be +│ removed in a future version of Terraform. To silence this warning, move the +│ provider version constraint into the required_providers block. +╵ diff --git a/tests/Terraform10OutputParserTest.php b/tests/Terraform10OutputParserTest.php index 7c3130b..7f02ca7 100755 --- a/tests/Terraform10OutputParserTest.php +++ b/tests/Terraform10OutputParserTest.php @@ -30,6 +30,7 @@ public function providerTestCases() 'nochanges' => '83-nochanges', 'resource-comment' => '84-resource-comment', 'debug-output' => '85-debug-output', + 'output-only-changes' => '86-output-only-changes', ]; return array_map(function ($case) use ($fixturesDir) {