Skip to content

Commit

Permalink
Fix output only plan from causing parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
skluck committed Jan 28, 2022
1 parent f89c873 commit 5406061
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 10 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 27 additions & 10 deletions src/Terraform12OutputParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @copyright (c) 2019 Steve Kluck
* @copyright (c) 2022 Steve Kluck
*
* For full license information, please view the LICENSE distributed with this source code.
*/
Expand All @@ -19,6 +19,7 @@ class Terraform12OutputParser
const NO_CHANGES_STRING = "\nNo changes. Infrastructure is up-to-date.\n";
# New in Terraform 0.13+
const NO_CHANGES_STRING_ALT = "\nNo changes. Your infrastructure matches the configuration.\n";
const NO_CHANGES_OUTPUT_ONLY = "\nYou can apply this plan to save these new output values to the Terraform\nstate, without changing any real infrastructure.\n";

const CONTENT_START_STRING = "\nTerraform will perform the following actions:\n";
const CONTENT_END_STRING = "\nPlan:";
Expand Down Expand Up @@ -85,15 +86,7 @@ public function parse(string $input): array
$input = $this->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' => [],
Expand Down Expand Up @@ -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
*
Expand Down
5 changes: 5 additions & 0 deletions tests/.fixtures-1.0/86-output-only-changes.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errors": [],
"changedResources": [],
"modules": []
}
49 changes: 49 additions & 0 deletions tests/.fixtures-1.0/86-output-only-changes.stdout.txt
Original file line number Diff line number Diff line change
@@ -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.
╵
1 change: 1 addition & 0 deletions tests/Terraform10OutputParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5406061

Please sign in to comment.