From f1e0d4a0dd9ad0c47c00a13e0e5aee58fb38ac1f Mon Sep 17 00:00:00 2001 From: Glenn Musa <4622125+glennmusa@users.noreply.github.com> Date: Fri, 4 Feb 2022 15:43:55 +0000 Subject: [PATCH 1/2] refactor validate-terraform.sh and remove build dir --- .../workflows/validate-terraform.sh | 33 +++++++++++----- .github/workflows/validate-terraform.yml | 10 ++--- src/build/delete.sh | 25 ------------ src/build/validate_tf.sh | 39 ------------------- 4 files changed, 27 insertions(+), 80 deletions(-) rename src/build/check_tf_format.sh => .github/workflows/validate-terraform.sh (53%) delete mode 100644 src/build/delete.sh delete mode 100755 src/build/validate_tf.sh diff --git a/src/build/check_tf_format.sh b/.github/workflows/validate-terraform.sh similarity index 53% rename from src/build/check_tf_format.sh rename to .github/workflows/validate-terraform.sh index 906deee2f..214229bb0 100755 --- a/src/build/check_tf_format.sh +++ b/.github/workflows/validate-terraform.sh @@ -3,7 +3,7 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # -# Check Terraform formatting for 1:M directories, exiting if any errors are produced +# Validates and lints Terraform for 1:M directories, exiting if any errors are produced program_log () { echo "${0}: ${1}" @@ -13,17 +13,28 @@ error_log () { echo "Error: ${1}" } -# Check for Terraform +# check for Terraform if ! command -v terraform &> /dev/null; then error_log "Terraform could not be found. This script requires the Terraform CLI." echo "See https://learn.hashicorp.com/tutorials/terraform/install-cli for installation instructions." exit 1 fi -format_tf() { +# validate Terraform with `terraform validate` +validate() { local tf_dir=$1 - cd "$tf_dir" || exit 1 - program_log "checking formatting at $tf_dir..." + cd "${tf_dir}" || exit 1 + program_log "validating at ${tf_dir}..." + terraform init -backend=false >> /dev/null || exit 1 + terraform validate >> /dev/null || exit 1 + program_log "successful validation with 'terraform validate ${tf_dir}!" +} + +# check Terraform formatting with `terraform fmt` +check_formatting() { + local tf_dir=$1 + cd "${tf_dir}" || exit 1 + program_log "checking formatting at ${tf_dir}..." if terraform fmt -check -recursive >> /dev/null; then program_log "successful check with 'terraform fmt -check -recursive ${tf_dir}'" @@ -34,16 +45,20 @@ format_tf() { error_log "'${j}' is not formatted correctly. Format with the command 'terraform fmt ${j}'" done program_log "run 'terraform fmt -recursive' to format all Terraform components in a directory" - exit 1; + exit 1 fi } +# get the starting directory working_dir=$(pwd) +# for every argument, try to validate and check formatting for arg in "$@" do - cd "$working_dir" || exit 1 - format_tf "$(realpath "$arg")" + real_path=$(realpath "${arg}") + validate "${real_path}" + check_formatting "${real_path}" + cd "${working_dir}" || exit 1 done -program_log "done!" \ No newline at end of file +program_log "done!" diff --git a/.github/workflows/validate-terraform.yml b/.github/workflows/validate-terraform.yml index d6e5a0430..c03d09139 100644 --- a/.github/workflows/validate-terraform.yml +++ b/.github/workflows/validate-terraform.yml @@ -4,10 +4,10 @@ # Licensed under the MIT License. name: validate-terraform -on: +on: pull_request: branches: [main] - paths: + paths: - 'src/terraform/**' - '!src/terraform/**.md' workflow_dispatch: @@ -24,8 +24,4 @@ jobs: - shell: bash name: validate and lint terraform run: | - src/build/validate_tf.sh src/terraform/mlz src/terraform/tier3 - - shell: bash - name: check terraform formatting - run: | - src/build/check_tf_format.sh src/terraform + .github/workflows/validate-terraform.sh src/terraform/mlz src/terraform/tier3 diff --git a/src/build/delete.sh b/src/build/delete.sh deleted file mode 100644 index 5584dc6ae..000000000 --- a/src/build/delete.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash -# -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# -# Steps through current logged in az cli subscriptions and deletes resource groups based on first arg, -# as filter as a job by not waiting for them to complete. -# -# Then steps through each diagnostic setting at subscription level with similar filter, -# resets az cli account to a specific subscription to be able to continue to use command line. -# Usage: ./delete.sh "" "" - -for subscription in $(az account list -o tsv); do - az account set --subscription "${subscription}" - for rgname in $(az group list --query "[? contains(name,'$1')][].{name:name}" -o tsv); do - echo Deleting "${rgname}" - az group delete -n "${rgname}" --yes --no-wait - done - for setting in $(az monitor diagnostic-settings subscription list --query "value[? contains(@.name, '$1')].name" -o tsv); do - echo Deleting "${setting}" - az monitor diagnostic-settings delete --name "${setting}" --resource /subscriptions/"${subscription}" - done -done - -az account set --subscription "$2" diff --git a/src/build/validate_tf.sh b/src/build/validate_tf.sh deleted file mode 100755 index ab8815375..000000000 --- a/src/build/validate_tf.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -# -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# -# Validates and lints Terraform for 1:M directories, exiting if any errors are produced - -program_log () { - echo "${0}: ${1}" -} - -error_log () { - echo "Error: ${1}" -} - -# Check for Terraform -if ! command -v terraform &> /dev/null; then - error_log "Terraform could not be found. This script requires the Terraform CLI." - echo "See https://learn.hashicorp.com/tutorials/terraform/install-cli for installation instructions." - exit 1 -fi - -validate_tf() { - local tf_dir=$1 - cd "$tf_dir" || exit 1 - program_log "validating at $tf_dir..." - terraform init -backend=false >> /dev/null || exit 1 - terraform validate >> /dev/null || exit 1 -} - -working_dir=$(pwd) - -for arg in "$@" -do - cd "$working_dir" || exit 1 - validate_tf "$(realpath "$arg")" -done - -program_log "done!" \ No newline at end of file From 5130afe9194e3271a434c187282d8400f92db6c0 Mon Sep 17 00:00:00 2001 From: Glenn Musa <4622125+glennmusa@users.noreply.github.com> Date: Fri, 4 Feb 2022 15:46:49 +0000 Subject: [PATCH 2/2] replace single quotes as they were being escaped in log output --- .github/workflows/validate-terraform.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/validate-terraform.sh b/.github/workflows/validate-terraform.sh index 214229bb0..b3f273f33 100755 --- a/.github/workflows/validate-terraform.sh +++ b/.github/workflows/validate-terraform.sh @@ -27,7 +27,7 @@ validate() { program_log "validating at ${tf_dir}..." terraform init -backend=false >> /dev/null || exit 1 terraform validate >> /dev/null || exit 1 - program_log "successful validation with 'terraform validate ${tf_dir}!" + program_log "successful validation with \"terraform validate ${tf_dir}\"!" } # check Terraform formatting with `terraform fmt` @@ -37,14 +37,14 @@ check_formatting() { program_log "checking formatting at ${tf_dir}..." if terraform fmt -check -recursive >> /dev/null; then - program_log "successful check with 'terraform fmt -check -recursive ${tf_dir}'" + program_log "successful check with \"terraform fmt -check -recursive ${tf_dir}\"" else linting_results=$(terraform fmt -check -recursive) for j in $linting_results do - error_log "'${j}' is not formatted correctly. Format with the command 'terraform fmt ${j}'" + error_log "\"${j}\" is not formatted correctly. Format with the command \"terraform fmt ${j}\"" done - program_log "run 'terraform fmt -recursive' to format all Terraform components in a directory" + program_log "run \"terraform fmt -recursive\" to format all Terraform components in a directory" exit 1 fi }