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