From 35e0356188b64a4c5af9a4e7200d936e514cba71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szczepaniak?= Date: Mon, 17 Jun 2019 13:09:31 +0200 Subject: [PATCH] Upgraded to work with Terraform >= 0.12 (#44) --- .pre-commit-hooks.yaml | 16 +++--------- README.md | 5 +--- ...with_variables.sh => terraform_validate.sh | 4 +-- terraform_validate_no_variables.sh | 26 ------------------- 4 files changed, 6 insertions(+), 45 deletions(-) rename terraform_validate_with_variables.sh => terraform_validate.sh (84%) delete mode 100755 terraform_validate_no_variables.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index afb5d6066..ee10f8228 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -27,25 +27,17 @@ - id: terraform_docs_replace name: Terraform docs (overwrite README.md) - description: Overwrite content of README.md with terraform-docs + description: Overwrite content of README.md with terraform-docs. require_serial: true entry: terraform_docs_replace language: python files: (\.tf)$ exclude: \.terraform\/.*$ -- id: terraform_validate_no_variables +- id: terraform_validate name: Terraform validate without variables - description: Validates all Terraform configuration files without checking whether all required variables were set (basic check). - entry: terraform_validate_no_variables.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ - -- id: terraform_validate_with_variables - name: Terraform validate with variables - description: Validates all Terraform configuration files and checks whether all required variables were specified. - entry: terraform_validate_with_variables.sh + description: Validates all Terraform configuration files. + entry: terraform_validate.sh language: script files: (\.tf|\.tfvars)$ exclude: \.terraform\/.*$ diff --git a/README.md b/README.md index b39066b79..370ac9eff 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,7 @@ pre-commit run -a There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) in a good shape: * `terraform_fmt` - Rewrites all Terraform configuration files to a canonical format. -* `terraform_validate_no_variables` - Validates all Terraform configuration files without checking whether all required variables were set. -* `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. +* `terraform_validate` - Validates all Terraform configuration files. * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. * `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md @@ -58,8 +57,6 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo ## Notes about hooks -1. `terraform_validate_no_variables` and `terraform_validate_with_variables` will not work if variables are being set dynamically (eg, when using [Terragrunt](https://github.com/gruntwork-io/terragrunt)). Use `terragrunt validate` command instead. - 1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. 1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets you change the name of the file that gets created/modified. diff --git a/terraform_validate_with_variables.sh b/terraform_validate.sh similarity index 84% rename from terraform_validate_with_variables.sh rename to terraform_validate.sh index 355a91372..6f7eccac2 100755 --- a/terraform_validate_with_variables.sh +++ b/terraform_validate.sh @@ -15,16 +15,14 @@ done for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do path_uniq="${path_uniq//__REPLACED__SPACE__/ }" - pushd "$path_uniq" > /dev/null if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]] ; then - if ! terraform validate -check-variables=true ; then + if ! terraform validate $path_uniq; then error=1 echo echo "Failed path: $path_uniq" echo "================================" fi fi - popd > /dev/null done if [[ "${error}" -ne 0 ]] ; then diff --git a/terraform_validate_no_variables.sh b/terraform_validate_no_variables.sh deleted file mode 100755 index 2e190448a..000000000 --- a/terraform_validate_no_variables.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash -set -e - -declare -a paths -index=0 - -for file_with_path in "$@"; do - file_with_path="${file_with_path// /__REPLACED__SPACE__}" - - paths[index]=$(dirname "$file_with_path") - (( "index+=1" )) -done - -for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do - path_uniq="${path_uniq//__REPLACED__SPACE__/ }" - - pushd "$path_uniq" > /dev/null - if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]] ; then - if ! terraform validate -check-variables=false ; then - echo - echo "Failed path: $path_uniq" - echo "================================" - fi - fi - popd > /dev/null -done