MON-494 support multiple monitors-*.tf files

This commit is contained in:
Quentin Manfroi 2019-08-12 22:29:49 +02:00
parent 57d7874dda
commit 519b571c3f
6 changed files with 23 additions and 25 deletions

View File

@ -5,14 +5,13 @@ 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 monitors set
for path in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
module=$(dirname ${path})
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 # loop over monitors for each set
for monitor in $(grep 'resource "datadog_monitor"' $(basename ${path}) | awk '{print $3}' | tr -d '"' ); do for monitor in $(cat monitors-*.tf | grep 'resource "datadog_monitor"' | awk '{print $3}' | tr -d '"' ); do
# 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" {

View File

@ -11,8 +11,7 @@ curl -Lso ${TERRAFORM_AWK} "https://raw.githubusercontent.com/cloudposse/build-h
PATTERN_DOC="Related documentation" PATTERN_DOC="Related documentation"
# loop over every monitors set readme # loop over every monitors set readme
for path in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
module=$(dirname $path)
echo -e "\t- Generate outputs.tf for module: ${module}" echo -e "\t- Generate outputs.tf for module: ${module}"
cd ${module} cd ${module}
EXIST=0 EXIST=0
@ -21,17 +20,17 @@ for path in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
EXIST=1 EXIST=1
fi fi
# module name from path # module name from path
module=$(list_dirs $(dirname ${path})) module_space=$(list_dirs ${module})
# module name with space as separator # module name with space as separator
module_space=${module^^} module_upper=${module_space^^}
# module name with dash as separator # module name with dash as separator
module_dash=${module//[ ]/-} module_dash=${module_space//[ ]/-}
# module name with slash as separator # module name with slash as separator
module_slash=${module//[ ]/\/} module_slash=${module_space//[ ]/\/}
# (re)generate README from scratch # (re)generate README from scratch
cat <<EOF > README.md cat <<EOF > README.md
# ${module_space} DataDog monitors # ${module_upper} DataDog monitors
## How to use this module ## How to use this module
@ -64,14 +63,14 @@ EOF
# allow looping over strings which contains spaces # allow looping over strings which contains spaces
IFS=$(echo -en "\n\b") IFS=$(echo -en "\n\b")
# loop over each monitor in the set # loop over each monitor in the set
for match in $(grep -E ^[[:space:]]+name[[:space:]]+= $(basename ${path}) | sort -fdbi); do for match in $(cat monitors-*.tf | grep -E ^[[:space:]]+name[[:space:]]+= | sort -fdbi); do
## TODO rewrite this (and other things) using: ## TODO rewrite this (and other things) using:
## terraform-config-inspect --json| jq -C ## terraform-config-inspect --json| jq -C
## awk '1;/^\}/{exit}' monitors-ingress.tf # with line numer of each resource ## 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 "${match}")
# search if monitor is enabled # search if monitor is enabled
[[ "$(grep -B1 "$name" $(basename ${path}) | grep -q enabled)" =~ ^[[:space:]]*count[[:space:]]*=[[:space:]]*var\.([a-z0-9_]*_enabled) ]] && [[ "$(cat monitors-*.tf | grep -B1 "$name" | grep -q enabled)" =~ ^[[: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 default.*true; then
name="${name} (disabled by default)" name="${name} (disabled by default)"

View File

@ -6,11 +6,10 @@ 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 monitors set
for path in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do
module=$(dirname ${path})
cd ${module} cd ${module}
# get name of the monitors set directory # get name of the monitors set directory
resource="$(basename $(dirname $path))" resource="$(basename ${module})"
# if modules.tf does not exist AND if this set respect our tagging convention # if modules.tf does not exist AND if this set respect our tagging convention
if ! [ -f modules.tf ] && grep -q filter_tags_use_defaults inputs.tf; then if ! [ -f modules.tf ] && grep -q filter_tags_use_defaults inputs.tf; then
echo -e "\t- Generate modules.tf for module: ${module}" echo -e "\t- Generate modules.tf for module: ${module}"
@ -32,7 +31,9 @@ module "filter-tags" {
resource = "$resource" resource = "$resource"
filter_tags_use_defaults = var.filter_tags_use_defaults filter_tags_use_defaults = var.filter_tags_use_defaults
filter_tags_custom = var.filter_tags_custom filter_tags_custom = var.filter_tags_custom
filter_tags_custom_excluded = var.filter_tags_custom_excluded
} }
EOF EOF
fi fi
cd - >> /dev/null cd - >> /dev/null

View File

@ -6,10 +6,10 @@ 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 monitors set
for path 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 [[ $(grep -c notify_no_data.*true $path) -gt 1 ]]; then if [[ $(cat ${module}/monitors-*.tf | grep -c notify_no_data.*true) -gt 1 ]]; then
echo "More than one notify_no_data set to true on $path" echo "More than one notify_no_data set to true on $module"
exit 1 exit 1
fi fi
done done

View File

@ -12,8 +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}')
for path in $(browse_modules "$(get_scope ${1:-})" 'inputs.tf'); do for module in $(browse_modules "$(get_scope ${1:-})" 'inputs.tf'); do
module=$(dirname $path)
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
provider "datadog" { provider "datadog" {

View File

@ -53,5 +53,5 @@ function get_name() {
} }
function browse_modules() { function browse_modules() {
find "$1" -name "$2" -print | sort -fdbi find "$1" -name "$2" -exec dirname "{}" \; | sort -fdbiu
} }