MON-494 use terraform-config-inspect to parse terraform modules
This commit is contained in:
parent
d42fe60fcf
commit
036ce643b9
@ -36,7 +36,7 @@ function check_version() {
|
||||
req_ver="0.6.0"
|
||||
cur_ver=$(terraform-docs --version)
|
||||
else
|
||||
return
|
||||
return 0
|
||||
fi
|
||||
if ! verlte $req_ver $cur_ver; then
|
||||
echo "This requires at least version ${req_ver} of $1, please upgrade (current version is ${cur_ver})"
|
||||
@ -44,7 +44,7 @@ function check_version() {
|
||||
fi
|
||||
}
|
||||
|
||||
for cmd in terraform terraform-docs; do
|
||||
for cmd in terraform terraform-docs terraform-config-inspect jq; do
|
||||
echo -e "\t- Check command \"$cmd\" exists and in right version"
|
||||
check_command $cmd
|
||||
check_version $cmd
|
||||
|
||||
@ -4,14 +4,16 @@ source "$(dirname $0)/utils.sh"
|
||||
init
|
||||
echo "Generate terraform outputs.tf files for every monitors modules"
|
||||
|
||||
# loop over every monitors set
|
||||
# loop over every modules
|
||||
for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
|
||||
echo -e "\t- Generate outputs.tf for module: ${module}"
|
||||
cd ${module}
|
||||
# empty outputs
|
||||
> outputs.tf
|
||||
# loop over monitors for each set
|
||||
for monitor in $(cat monitors-*.tf | grep 'resource "datadog_monitor"' | awk '{print $3}' | tr -d '"' ); do
|
||||
# gather a information line splitted with "|" for every monitor
|
||||
for row in $(terraform-config-inspect --json | jq -c -r '.managed_resources | map([.name] | join("|")) | join("\n")'); do
|
||||
# split line for each info one variable
|
||||
IFS='|' read monitor type < <(echo $row)
|
||||
# create output block for current monitor
|
||||
cat >> outputs.tf <<EOF
|
||||
output "${monitor}_id" {
|
||||
|
||||
@ -8,7 +8,7 @@ echo "Update global README.md"
|
||||
sed -i '/### Monitors summary ###/q' README.md
|
||||
# add a newline after listing section
|
||||
echo >> README.md
|
||||
# loop over all ready monitors sets on the repo
|
||||
# loop over path of modules tree
|
||||
for path in $(find -mindepth 1 -type d ! -path '*/.*' ! -path './scripts*' -print | sort -fdbi); do
|
||||
# split path in directories
|
||||
directories=($(list_dirs $path))
|
||||
|
||||
@ -10,9 +10,9 @@ curl -Lso ${TERRAFORM_AWK} "https://raw.githubusercontent.com/cloudposse/build-h
|
||||
# this is the pattern from where custom information is saved to be restored
|
||||
PATTERN_DOC="Related documentation"
|
||||
|
||||
# loop over every monitors set readme
|
||||
# loop over every modules
|
||||
for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
|
||||
echo -e "\t- Generate outputs.tf for module: ${module}"
|
||||
echo -e "\t- Generate README.md for module: ${module}"
|
||||
cd ${module}
|
||||
EXIST=0
|
||||
if [ -f README.md ]; then
|
||||
@ -59,27 +59,30 @@ EOF
|
||||
Creates DataDog monitors with the following checks:
|
||||
|
||||
EOF
|
||||
SAVEIFS=$IFS
|
||||
# allow looping over strings which contains spaces
|
||||
IFS=$(echo -en "\n\b")
|
||||
# loop over each monitor in the set
|
||||
for match in $(cat monitors-*.tf | grep -E ^[[:space:]]+name[[:space:]]+= | sort -fdbi); do
|
||||
## TODO rewrite this (and other things) using:
|
||||
## terraform-config-inspect --json| jq -C
|
||||
## awk '1;/^\}/{exit}' monitors-ingress.tf # with line numer of each resource
|
||||
list=""
|
||||
# gather a information line splitted with "|" for every monitor
|
||||
for row in $(terraform-config-inspect --json | jq -c -r '.managed_resources | map([.pos.filename, .pos.line] | join("|")) | join("\n")' | sort -fdbi); do
|
||||
# split line for each info one variable
|
||||
IFS='|' read filename line < <(echo $row)
|
||||
# gather all config HCL code for current monitor
|
||||
config=$(tail -n +${line} ${filename} | sed '/^}/q')
|
||||
# parse monitor's name
|
||||
name=$(get_name "${match}")
|
||||
name=$(get_name "$(echo "${config}" | grep 'name[[:space:]]*=')")
|
||||
# search if monitor is enabled
|
||||
[[ "$(cat monitors-*.tf | grep -B1 "$name" | grep -q enabled)" =~ ^[[:space:]]*count[[:space:]]*=[[:space:]]*var\.([a-z0-9_]*_enabled) ]] &&
|
||||
[[ "$(echo "${config}" | grep 'count[[:space:]]*=')" =~ ^[[:space:]]*count[[:space:]]*=[[:space:]]*var\.([a-z0-9_]*_enabled) ]] &&
|
||||
# add "disabled by default" mention if not enabled
|
||||
if ! grep -A4 "${BASH_REMATCH[1]}" inputs.tf | grep default.*true; then
|
||||
if ! grep -A4 "${BASH_REMATCH[1]}" inputs.tf | grep -q default.*true; then
|
||||
name="${name} (disabled by default)"
|
||||
fi
|
||||
# monitor name element to the list and replace "could reach" pattern to "forecast" for better naming
|
||||
echo "- ${name/could reach/forecast}" >> README.md
|
||||
# append new line to list if not empty
|
||||
if ! [ -z "${list}" ]; then
|
||||
list="${list}\n"
|
||||
fi
|
||||
# append name to list and improve forecast naming
|
||||
list="${list}- ${name/could reach/forecast}"
|
||||
done
|
||||
IFS=$SAVEIFS
|
||||
echo >> README.md
|
||||
# write sorted list to readme appending newline to end
|
||||
echo -e "$(echo -e "${list}" | sort -fdbi)\n" >> README.md
|
||||
# hack for terraform-docs with terraform 0.12 / HCL2 support
|
||||
tmp_tf=$(mktemp -d)
|
||||
awk -f ${TERRAFORM_AWK} ./*.tf > ${tmp_tf}/main.tf
|
||||
|
||||
@ -5,7 +5,7 @@ init
|
||||
echo "Generate outputs.tf files when does not exist for every monitors modules"
|
||||
root=$(basename ${PWD})
|
||||
|
||||
# loop over every monitors set
|
||||
# loop over every modules
|
||||
for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
|
||||
cd ${module}
|
||||
# get name of the monitors set directory
|
||||
|
||||
@ -5,7 +5,7 @@ init
|
||||
echo "Check best practices respect"
|
||||
|
||||
echo -e "\t- Check only one notify_no_data set to true per module"
|
||||
# loop over every monitors set
|
||||
# loop over every modules
|
||||
for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
|
||||
# check if there is more than 1 notify_no_data parameter set to true per set of monitors
|
||||
if [[ $(cat ${module}/monitors-*.tf | grep -c notify_no_data.*true) -gt 1 ]]; then
|
||||
|
||||
@ -12,6 +12,7 @@ trap 'err $LINENO' ERR TERM EXIT INT
|
||||
|
||||
provider_version=$(grep ^[[:space:]]*version[[:space:]]= README.md | awk '{print $3}')
|
||||
|
||||
# loop over every modules
|
||||
for module in $(browse_modules "$(get_scope ${1:-})" 'inputs.tf'); do
|
||||
echo -e "\t- Terraform validate on module: ${module}"
|
||||
cat <<EOF > ${module}/tmp.tf
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user