dineshba / tf-summarize

A command-line utility to print the summary of the terraform plan
MIT License
534 stars 38 forks source link

Output format tree shows all unchanged resources since 0.3.8 #71

Closed Daniel-I-Am closed 8 months ago

Daniel-I-Am commented 8 months ago

This morning I updated our version of tf-summarize from 0.3.7 to 0.3.8 and the output has become very verbose in the tree format option.

Example

I partially applied the following terraform code:

resource "random_id" "a" {
  byte_length = 5
}

resource "random_id" "b" {
  byte_length = 10
}

resource "random_id" "c" {
  byte_length = 10
}

Real output

$ terraform plan -out=tfplan
random_id.b: Refreshing state... [id=NFtC1Xo]
random_id.a: Refreshing state... [id=RJrHXAs]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # random_id.b must be replaced
-/+ resource "random_id" "b" {
      ~ b64_std     = "NFtC1Xo=" -> (known after apply)
      ~ b64_url     = "NFtC1Xo" -> (known after apply)
      ~ byte_length = 5 -> 10 # forces replacement
      ~ dec         = "224869406074" -> (known after apply)
      ~ hex         = "345b42d57a" -> (known after apply)
      ~ id          = "NFtC1Xo" -> (known after apply)
    }

  # random_id.c will be created
  + resource "random_id" "c" {
      + b64_std     = (known after apply)
      + b64_url     = (known after apply)
      + byte_length = 10
      + dec         = (known after apply)
      + hex         = (known after apply)
      + id          = (known after apply)
    }

Plan: 2 to add, 0 to change, 1 to destroy.

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Saved the plan to: tfplan

To perform exactly these actions, run the following command to apply:
    terraform apply "tfplan"
$ ~/bin/tf-summarize tfplan
+----------+-------------+
|  CHANGE  |  RESOURCE   |
+----------+-------------+
| add      | random_id.c |
+----------+-------------+
| recreate | random_id.b |
+----------+-------------+
$ ~/bin/tf-summarize -tree tfplan
|---random_id
|   |---a
|   |---b(-/+)
|   |---c(+)

Expected output

The normal table view is correct, only showing changes to random_id.b and random_id.c. However, in tree view, the random_id.a is also (incorrectly) shown. I'd expect that output to omit random_id.a.

Downgrading to 0.3.7 and running the same shows the expected output:

$ ~/bin/tf-summarize-0.3.7 -v
Version: 0.3.7
$ ~/bin/tf-summarize-0.3.7 -tree tfplan
|---random_id
|   |---b(-/+)
|   |---c(+)

Environment info

$ terraform version
Terraform v1.7.4
on linux_amd64
+ provider registry.terraform.io/hashicorp/random v3.6.0
$ ~/bin/tf-summarize -v
Version: 0.3.8

Introduction

I ran a git bisect between 0.3.7 and 0.3.8:

$ cat test.sh
#!/usr/bin/env bash

set -e

go build || exit 125

(cd ../ && terraform show -json tfplan) | ./tf-summarize -tree | ( ! grep -Fq -- '---a')
$ git bisect start v0.3.8 v0.3.7
Already on 'main'
Your branch is up to date with 'origin/main'.
Bisecting: 2 revisions left to test after this (roughly 1 step)
[908b8f01702cca83cdc5e5916ab714559c86f6aa] Add nil check for IsImport (#66)
$ git bisect run ./test.sh
running './test.sh'
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[b74b13da17e8c3390a56ae9e2efbcbad758b851a] [WIP] Feat: use terraform-json package (#60)
running './test.sh'
b74b13da17e8c3390a56ae9e2efbcbad758b851a is the first bad commit

Which points at this being introduced in #60

QuintenBruynseraede commented 8 months ago

Hey @Daniel-I-Am, thanks for the detailed report. I suspect this was introduced on this line: https://github.com/dineshba/tf-summarize/pull/60/files#diff-071d425dbd9ec6e84286a86dd0ac98090fc70d706487e97f0e8e7b3cec91ec3eR95

The check for being a no-op requires r.Change.Importing to be present, which is probably not the case for most resources. I'll try to reproduce and fix later this week :)

dineshba commented 8 months ago

Fixed with #74 and released as part of v0.3.10