dineshba / tf-summarize

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

Add an option to have an empty output #6

Open xocasdashdash opened 2 years ago

xocasdashdash commented 2 years ago

Loving the tool,

I'm looking to add an option in order to know if the plan is empty and either output a preset message (no changes in current plan), putting out an empty plan or exiting with a specific error code (terraform already does this with the -detailed-exitcode flag).

What do you think?

dineshba commented 2 years ago

Thanks for trying out the tool @xocasdashdash .

Today, we have to run terraform plan -out=tfplan and use the output to tf-summarize. We can use the -detailed-exitcode for terraform plan and not continue if there is no diff exit code.

Thoughts ?

nitrocode commented 2 years ago

Is the command something like this?

terraform plan -detailed-exitcode -out planfile
status=$?
if [[ "$status" == "0" ]]; then
  echo "no changes"
elif [[ "$status" == "1" ]]; then
  echo "terraform threw an error"
elif [[ "$status" == "2" ]]; then
  echo "diff detected, running tf-summarizer"
  status=0
  tf-summarizer planfile
else
  echo "terraform threw an unknown error"
fi
rm -f planfile
exit $status
aquam8 commented 1 year ago

I also have the same query. I use Terragrunt which generates lot of Terraform plans. After the json plans are generated, I iterate through all the json files, and print the path to reference the terraform module/source (see below PATH:) and call tf-summarize for each plan. However when the plan are empty, then tf-summarize will return nothing but I am still display the module path with no changes (example PATH: components/worker/event-bus/). It would be much better if I could test tf-summarize invocation to know if it has detected changes so I can avoid print the path when there are no detected changes.

Example:

TERRAFORM SUMMARY
------------------------

PATH: components/worker/event-bus/

PATH: components/worker/kinesis-to-s3/
|---aws_kinesis_firehose_delivery_stream
|   |---extended_s3_stream(~)

PATH: components/worker/sqs/eventbus-dlq/

PATH: components/worker/sqs/mpgs-transactions/

PATH: components/org-service/sqs/external-merchant-provisioner/

PATH: components/provisioning-api/acm/v2/

PATH: components/provisioning-api/ssm/

PATH: components/shared/rds/groups/postgres13/

PATH: components/shared/rds/groups/postgres15/
|---aws_db_parameter_group
|   |---main(~)

PATH: components/shared/rds/instance/

PATH: components/shared/redis/cluster/

Desired output:

TERRAFORM SUMMARY
------------------------
PATH: components/worker/kinesis-to-s3/
|---aws_kinesis_firehose_delivery_stream
|   |---extended_s3_stream(~)

PATH: components/shared/rds/groups/postgres15/
|---aws_db_parameter_group
|   |---main(~)

Thank you very much for your assistance.