1#!/bin/bash 2# 3# Copyright 2022 Google LLC 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# https://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17set -eo pipefail 18 19# Ensure current directory is same as script. 20helperDir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" 21pushd "$helperDir/.." >/dev/null 22 23# If Terraform was previously configured to work with a different project, 24# then remove the previous Terraform state. 25prev_project_id=$(terraform output -raw project_id &>/dev/null) 26if [[ $? -eq 0 ]]; then 27 if [[ "$prev_project_id" != "$GOOGLE_CLOUD_PROJECT" ]]; then 28 if [[ -f terraform.tfstate ]]; then 29 rm terraform.tfstate 30 fi 31 fi 32fi 33 34# Create generated.auto.tfvars which will be used as input values to generated-variables.tf 35touch generated.auto.tfvars 36echo "# Auto-generated by ./.cloud/plan.sh 37project_id = \"$GOOGLE_CLOUD_PROJECT\" 38" >generated.auto.tfvars 39 40# Either use given module list, or get a list of all modules in the parent directory. 41if [ -n "$1" ]; then 42 modules=$1 43else 44 modules=$(listAllModules) 45fi 46echo "Planning around modules $modules" 47IFS=',' 48for module in $modules; do 49 # Only include modules with a .cloud subdirectory in the generated config. 50 if [ -f "../$module/.cloud/preplan.sh" ]; then 51 # shellcheck disable=SC1090 52 source "../$module/.cloud/preplan.sh" generated.auto.tfvars 53 fi 54done 55 56terraform fmt -list=false generated.auto.tfvars 57terraform plan -out generated.tfplan 58terraform show -json generated.tfplan >generated.tfplan.json 59 60popd >/dev/null 61