From 75fa811a10f1475471e7e53a0ccbe53b7bad918d Mon Sep 17 00:00:00 2001 From: Bohdan Yurov Date: Sat, 20 Jul 2019 00:37:21 +0300 Subject: [PATCH 1/2] Fixes #9: Add support for Terraform 0.12 https://github.com/terraform-google-modules/terraform-google-scheduled-function/issues/9 Fixed helper scripts, tests. Cleanup --- .gitignore | 2 + Makefile | 46 +- README.md | 7 +- examples/pubsub_scheduled/README.md | 2 +- examples/pubsub_scheduled/function_source.zip | Bin 869 -> 0 bytes helpers/combine_docfiles.py | 59 -- helpers/terraform_docs | 694 ++++++++++++++++++ helpers/terraform_validate | 23 + test/ci_integration.sh | 7 + test/fixtures/shared/terraform.tfvars | 2 + test/fixtures/shared/terraform.tfvars.sample | 2 - test/integration/pubsub_scheduled/inspec.yml | 4 +- test/make.sh | 48 +- 13 files changed, 797 insertions(+), 99 deletions(-) delete mode 100644 examples/pubsub_scheduled/function_source.zip delete mode 100755 helpers/combine_docfiles.py create mode 100755 helpers/terraform_docs create mode 100755 helpers/terraform_validate create mode 100644 test/fixtures/shared/terraform.tfvars delete mode 100644 test/fixtures/shared/terraform.tfvars.sample diff --git a/.gitignore b/.gitignore index 8b1312e..3c63848 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ terraform.tfstate credentials.json *.iml .idea +*.pyc +.kitchen examples/**/*.zip examples/**/terraform.tfvars diff --git a/Makefile b/Makefile index c8ac199..2176964 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,11 @@ DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 0.11.11_235.0.0_1.19.1_0.1.10 DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM} # All is the first target in the file so it will get picked up when you just run 'make' on its own -all: check_shell check_python check_golang check_terraform check_docker check_base_files test_check_headers check_headers check_trailing_whitespace generate_docs +all: check generate_docs + +.PHONY: check +check: check_shell check_python check_golang check_terraform check_docker check_base_files test_check_headers check_headers check_trailing_whitespace + # The .PHONY directive tells make that this isn't a real target and so # the presence of a file named 'check_shell' won't cause this target to stop @@ -82,53 +86,59 @@ version: .PHONY: docker_run docker_run: docker run --rm -it \ - -e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \ + -e PROJECT_ID \ + -e SERVICE_ACCOUNT_JSON \ -e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \ - -e TF_VAR_project_id \ -v $(CURDIR):/cft/workdir \ ${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \ - /bin/bash + /bin/bash -c "source test/ci_integration.sh && setup_environment && exec /bin/bash" .PHONY: docker_create docker_create: docker run --rm -it \ - -e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \ + -e PROJECT_ID \ + -e SERVICE_ACCOUNT_JSON \ -e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \ - -e TF_VAR_project_id \ -v $(CURDIR):/cft/workdir \ ${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \ - /bin/bash -c "kitchen create" + /bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen create" .PHONY: docker_converge docker_converge: docker run --rm -it \ - -e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \ + -e PROJECT_ID \ + -e SERVICE_ACCOUNT_JSON \ -e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \ - -e TF_VAR_project_id \ -v $(CURDIR):/cft/workdir \ ${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \ - /bin/bash -c "kitchen converge && kitchen converge" + /bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen converge" .PHONY: docker_verify docker_verify: docker run --rm -it \ - -e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \ + -e PROJECT_ID \ + -e SERVICE_ACCOUNT_JSON \ -e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \ - -e TF_VAR_project_id \ -v $(CURDIR):/cft/workdir \ ${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \ - /bin/bash -c "kitchen verify" + /bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen verify" .PHONY: docker_destroy docker_destroy: docker run --rm -it \ - -e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \ + -e PROJECT_ID \ + -e SERVICE_ACCOUNT_JSON \ -e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \ - -e TF_VAR_project_id \ -v $(CURDIR):/cft/workdir \ ${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \ - /bin/bash -c "kitchen destroy" + /bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen destroy" .PHONY: test_integration_docker -test_integration_docker: docker_create docker_converge docker_verify docker_destroy - @echo "Running test-kitchen tests in docker" +test_integration_docker: + docker run --rm -it \ + -e PROJECT_ID \ + -e SERVICE_ACCOUNT_JSON \ + -e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \ + -v $(CURDIR):/cft/workdir \ + ${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \ + make test_integration diff --git a/README.md b/README.md index 20af64e..fafd24a 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,12 @@ Then perform the following commands on the root folder: - [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v2.1 ### App Engine -Note that this module requires App Engine being configured in the specified project/region. +Note that this module requires App Engine being configured in the specified project/region. This is because Google Cloud Scheduler is dependent on the project being configured with App Engine. -Refer to the [Google Cloud Scheduler documentation](https://cloud.google.com/scheduler/docs/) for more information on the App Engine dependency. +Refer to the [Google Cloud Scheduler documentation](https://cloud.google.com/scheduler/docs/) for more +information on the App Engine dependency. -The recommended way to create projects with App Engine enabled is via the [Project Factory module](https://github.com/terraform-google-modules/terraform-google-project-factory). +The recommended way to create projects with App Engine enabled is via the [Project Factory module](https://github.com/terraform-google-modules/terraform-google-project-factory). There is an example of how to create the project [within that module](https://github.com/terraform-google-modules/terraform-google-project-factory/tree/master/examples/app_engine) ### Configure a Service Account diff --git a/examples/pubsub_scheduled/README.md b/examples/pubsub_scheduled/README.md index 1059310..14d05ac 100644 --- a/examples/pubsub_scheduled/README.md +++ b/examples/pubsub_scheduled/README.md @@ -2,7 +2,7 @@ This example module schedules a job to publish a message to a Pub/Sub topic every 5 minutes, which will trigger a CloudFunctions function. -Running this module requires an App Engine app in the specified project/region, which is not handled by this example. +Running this module requires an App Engine app in the specified project/region, which is not handled by this example. More information is in the [root readme](../../README.md#app-engine). [^]: (autogen_docs_start) diff --git a/examples/pubsub_scheduled/function_source.zip b/examples/pubsub_scheduled/function_source.zip deleted file mode 100644 index d8f6a466be0f47666cc0cf06d16ad2efe1c9f473..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 869 zcmWIWW@Zs#-~d7f2E{HQ0S6$F%)FG;3cal2lt~x!FB=FPyZ<@7=1b;mFRO2Cmj#x- zR!GVUylnXPV&S@zjgO7DZC*F)+kWNcvWt}XCqDiCZukAU`9)>Z9`YC$TA%loE-d`_ z&URmiNn1d~+;j<+?Q2f5t-5%7mQ9lP46CKlRlPkQn`;s_?l^J2a1zU6=Zuo7nL1^c z76)5LbiQC#+$OUj(RqQouU?bxmXdQDHqF}b^afukpI#>mKRf@*MM>POx_Rt3|7f># zvA_7T)o>U8mIK2P~}T93l)9ij6k`JPp*TKB8MWOKL+pF7jW0I|iHor`21 ze%_*Us?cbs{pm^M z@c&<@a`|r3vCg@AtN3z&)T3kG?5%xH9-=|}XGp%xu6e)O^JaL*qaW|L)fSyv&38V3 z+ExEcA6dV6m?hc$nR`nzu$0NkJ@Bg5sy`a9mu(1xPlZEA-D;!&1 >/dev/null || ( echo "awk is required for terraform-docs hack to work with Terraform 0.12"; exit 1) + + tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") + terraform_docs_awk "$tmp_file_awk" + terraform_docs "$tmp_file_awk" "$args" "$files" + rm -f "$tmp_file_awk" + else + terraform_docs "0" "$args" "$files" + fi + +} + +terraform_docs() { + readonly terraform_docs_awk_file="$1" + readonly args="$2" + readonly files="$3" + + declare -a paths + declare -a tfvars_files + + index=0 + + for file_with_path in $files; do + file_with_path="${file_with_path// /__REPLACED__SPACE__}" + + paths[index]=$(dirname "$file_with_path") + + if [[ "$file_with_path" == *".tfvars" ]]; then + tfvars_files+=("$file_with_path") + fi + + ((index+=1)) + done + + readonly tmp_file=$(mktemp) + readonly text_file="README.md" + + for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do + path_uniq="${path_uniq//__REPLACED__SPACE__/ }" + + pushd "$path_uniq" > /dev/null + + if [[ ! -f "$text_file" ]]; then + popd > /dev/null + continue + fi + + if [[ "$terraform_docs_awk_file" == "0" ]]; then + terraform-docs $args md ./ > "$tmp_file" + else + # Can't append extension for mktemp, so renaming instead + tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") + mv "$tmp_file_docs" "$tmp_file_docs.tf" + tmp_file_docs_tf="$tmp_file_docs.tf" + + awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf" + terraform-docs $args md "$tmp_file_docs_tf" > "$tmp_file" + rm -f "$tmp_file_docs_tf" + fi + + # Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834 + perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file" + + # Replace placeholder with the content of the file + perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", ; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file" + + rm -f "$tmp_file" + + popd > /dev/null + done +} + +terraform_docs_awk() { + readonly output_file=$1 + + cat <<"EOF" > $output_file +# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs` +# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it. +# https://github.com/segmentio/terraform-docs/ +# https://github.com/segmentio/terraform-docs/issues/62 + +# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk + +{ + if ( $0 ~ /\{/ ) { + braceCnt++ + } + + if ( $0 ~ /\}/ ) { + braceCnt-- + } + + # [START] variable or output block started + if ($0 ~ /^[[:space:]]*(variable|output)[[:space:]][[:space:]]*"(.*?)"/) { + # Normalize the braceCnt (should be 1 now) + braceCnt = 1 + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + blockCnt++ + print $0 + } + + # [START] multiline default statement started + if (blockCnt > 0) { + if ($0 ~ /^[[:space:]][[:space:]]*(default)[[:space:]][[:space:]]*=/) { + if ($3 ~ "null") { + print " default = \"null\"" + } else { + print $0 + blockDefCnt++ + blockDefStart=1 + } + } + } + + # [PRINT] single line "description" + if (blockCnt > 0) { + if (blockDefCnt == 0) { + if ($0 ~ /^[[:space:]][[:space:]]*description[[:space:]][[:space:]]*=/) { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + print $0 + } + } + } + + # [PRINT] single line "type" + if (blockCnt > 0) { + if ($0 ~ /^[[:space:]][[:space:]]*type[[:space:]][[:space:]]*=/ ) { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + type=$3 + if (type ~ "object") { + print " type = \"object\"" + } else { + # legacy quoted types: "string", "list", and "map" + if ($3 ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) { + print " type = " $3 + } else { + print " type = \"" $3 "\"" + } + } + } + } + + # [CLOSE] variable/output block + if (blockCnt > 0) { + if (braceCnt == 0 && blockCnt > 0) { + blockCnt-- + print $0 + } + } + + # [PRINT] Multiline "default" statement + if (blockCnt > 0 && blockDefCnt > 0) { + if (blockDefStart == 1) { + blockDefStart = 0 + } else { + print $0 + } + } +} +EOF + +} + +getopt() { + # pure-getopt, a drop-in replacement for GNU getopt in pure Bash. + # version 1.4.3 + # + # Copyright 2012-2018 Aron Griffis + # + # Permission is hereby granted, free of charge, to any person obtaining + # a copy of this software and associated documentation files (the + # "Software"), to deal in the Software without restriction, including + # without limitation the rights to use, copy, modify, merge, publish, + # distribute, sublicense, and/or sell copies of the Software, and to + # permit persons to whom the Software is furnished to do so, subject to + # the following conditions: + # + # The above copyright notice and this permission notice shall be included + # in all copies or substantial portions of the Software. + # + # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + _getopt_main() { + # Returns one of the following statuses: + # 0 success + # 1 error parsing parameters + # 2 error in getopt invocation + # 3 internal error + # 4 reserved for -T + # + # For statuses 0 and 1, generates normalized and shell-quoted + # "options -- parameters" on stdout. + + declare parsed status + declare short long name flags + declare have_short=false + + # Synopsis from getopt man-page: + # + # getopt optstring parameters + # getopt [options] [--] optstring parameters + # getopt [options] -o|--options optstring [options] [--] parameters + # + # The first form can be normalized to the third form which + # _getopt_parse() understands. The second form can be recognized after + # first parse when $short hasn't been set. + + if [[ -n ${GETOPT_COMPATIBLE+isset} || $1 == [^-]* ]]; then + # Enable compatibility mode + flags=c$flags + # Normalize first to third synopsis form + set -- -o "$1" -- "${@:2}" + fi + + # First parse always uses flags=p since getopt always parses its own + # arguments effectively in this mode. + parsed=$(_getopt_parse getopt ahl:n:o:qQs:TuV \ + alternative,help,longoptions:,name:,options:,quiet,quiet-output,shell:,test,version \ + p "$@") + status=$? + if [[ $status != 0 ]]; then + if [[ $status == 1 ]]; then + echo "Try \`getopt --help' for more information." >&2 + # Since this is the first parse, convert status 1 to 2 + status=2 + fi + return $status + fi + eval "set -- $parsed" + + while [[ $# -gt 0 ]]; do + case $1 in + (-a|--alternative) + flags=a$flags ;; + + (-h|--help) + _getopt_help + return 2 # as does GNU getopt + ;; + + (-l|--longoptions) + long="$long${long:+,}$2" + shift ;; + + (-n|--name) + name=$2 + shift ;; + + (-o|--options) + short=$2 + have_short=true + shift ;; + + (-q|--quiet) + flags=q$flags ;; + + (-Q|--quiet-output) + flags=Q$flags ;; + + (-s|--shell) + case $2 in + (sh|bash) + flags=${flags//t/} ;; + (csh|tcsh) + flags=t$flags ;; + (*) + echo 'getopt: unknown shell after -s or --shell argument' >&2 + echo "Try \`getopt --help' for more information." >&2 + return 2 ;; + esac + shift ;; + + (-u|--unquoted) + flags=u$flags ;; + + (-T|--test) + return 4 ;; + + (-V|--version) + echo "pure-getopt 1.4.3" + return 0 ;; + + (--) + shift + break ;; + esac + + shift + done + + if ! $have_short; then + # $short was declared but never set, not even to an empty string. + # This implies the second form in the synopsis. + if [[ $# == 0 ]]; then + echo 'getopt: missing optstring argument' >&2 + echo "Try \`getopt --help' for more information." >&2 + return 2 + fi + short=$1 + have_short=true + shift + fi + + if [[ $short == -* ]]; then + # Leading dash means generate output in place rather than reordering, + # unless we're already in compatibility mode. + [[ $flags == *c* ]] || flags=i$flags + short=${short#?} + elif [[ $short == +* ]]; then + # Leading plus means POSIXLY_CORRECT, unless we're already in + # compatibility mode. + [[ $flags == *c* ]] || flags=p$flags + short=${short#?} + fi + + # This should fire if POSIXLY_CORRECT is in the environment, even if + # it's an empty string. That's the difference between :+ and + + flags=${POSIXLY_CORRECT+p}$flags + + _getopt_parse "${name:-getopt}" "$short" "$long" "$flags" "$@" + } + + _getopt_parse() { + # Inner getopt parser, used for both first parse and second parse. + # Returns 0 for success, 1 for error parsing, 3 for internal error. + # In the case of status 1, still generates stdout with whatever could + # be parsed. + # + # $flags is a string of characters with the following meanings: + # a - alternative parsing mode + # c - GETOPT_COMPATIBLE + # i - generate output in place rather than reordering + # p - POSIXLY_CORRECT + # q - disable error reporting + # Q - disable normal output + # t - quote for csh/tcsh + # u - unquoted output + + declare name="$1" short="$2" long="$3" flags="$4" + shift 4 + + # Split $long on commas, prepend double-dashes, strip colons; + # for use with _getopt_resolve_abbrev + declare -a longarr + _getopt_split longarr "$long" + longarr=( "${longarr[@]/#/--}" ) + longarr=( "${longarr[@]%:}" ) + longarr=( "${longarr[@]%:}" ) + + # Parse and collect options and parameters + declare -a opts params + declare o alt_recycled=false error=0 + + while [[ $# -gt 0 ]]; do + case $1 in + (--) + params=( "${params[@]}" "${@:2}" ) + break ;; + + (--*=*) + o=${1%%=*} + if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then + error=1 + elif [[ ,"$long", == *,"${o#--}"::,* ]]; then + opts=( "${opts[@]}" "$o" "${1#*=}" ) + elif [[ ,"$long", == *,"${o#--}":,* ]]; then + opts=( "${opts[@]}" "$o" "${1#*=}" ) + elif [[ ,"$long", == *,"${o#--}",* ]]; then + if $alt_recycled; then o=${o#-}; fi + _getopt_err "$name: option '$o' doesn't allow an argument" + error=1 + else + echo "getopt: assertion failed (1)" >&2 + return 3 + fi + alt_recycled=false + ;; + + (--?*) + o=$1 + if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then + error=1 + elif [[ ,"$long", == *,"${o#--}",* ]]; then + opts=( "${opts[@]}" "$o" ) + elif [[ ,"$long", == *,"${o#--}::",* ]]; then + opts=( "${opts[@]}" "$o" '' ) + elif [[ ,"$long", == *,"${o#--}:",* ]]; then + if [[ $# -ge 2 ]]; then + shift + opts=( "${opts[@]}" "$o" "$1" ) + else + if $alt_recycled; then o=${o#-}; fi + _getopt_err "$name: option '$o' requires an argument" + error=1 + fi + else + echo "getopt: assertion failed (2)" >&2 + return 3 + fi + alt_recycled=false + ;; + + (-*) + if [[ $flags == *a* ]]; then + # Alternative parsing mode! + # Try to handle as a long option if any of the following apply: + # 1. There's an equals sign in the mix -x=3 or -xy=3 + # 2. There's 2+ letters and an abbreviated long match -xy + # 3. There's a single letter and an exact long match + # 4. There's a single letter and no short match + o=${1::2} # temp for testing #4 + if [[ $1 == *=* || $1 == -?? || \ + ,$long, == *,"${1#-}"[:,]* || \ + ,$short, != *,"${o#-}"[:,]* ]]; then + o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2>/dev/null) + case $? in + (0) + # Unambiguous match. Let the long options parser handle + # it, with a flag to get the right error message. + set -- "-$1" "${@:2}" + alt_recycled=true + continue ;; + (1) + # Ambiguous match, generate error and continue. + _getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" >/dev/null + error=1 + shift + continue ;; + (2) + # No match, fall through to single-character check. + true ;; + (*) + echo "getopt: assertion failed (3)" >&2 + return 3 ;; + esac + fi + fi + + o=${1::2} + if [[ "$short" == *"${o#-}"::* ]]; then + if [[ ${#1} -gt 2 ]]; then + opts=( "${opts[@]}" "$o" "${1:2}" ) + else + opts=( "${opts[@]}" "$o" '' ) + fi + elif [[ "$short" == *"${o#-}":* ]]; then + if [[ ${#1} -gt 2 ]]; then + opts=( "${opts[@]}" "$o" "${1:2}" ) + elif [[ $# -ge 2 ]]; then + shift + opts=( "${opts[@]}" "$o" "$1" ) + else + _getopt_err "$name: option requires an argument -- '${o#-}'" + error=1 + fi + elif [[ "$short" == *"${o#-}"* ]]; then + opts=( "${opts[@]}" "$o" ) + if [[ ${#1} -gt 2 ]]; then + set -- "$o" "-${1:2}" "${@:2}" + fi + else + if [[ $flags == *a* ]]; then + # Alternative parsing mode! Report on the entire failed + # option. GNU includes =value but we omit it for sanity with + # very long values. + _getopt_err "$name: unrecognized option '${1%%=*}'" + else + _getopt_err "$name: invalid option -- '${o#-}'" + if [[ ${#1} -gt 2 ]]; then + set -- "$o" "-${1:2}" "${@:2}" + fi + fi + error=1 + fi ;; + + (*) + # GNU getopt in-place mode (leading dash on short options) + # overrides POSIXLY_CORRECT + if [[ $flags == *i* ]]; then + opts=( "${opts[@]}" "$1" ) + elif [[ $flags == *p* ]]; then + params=( "${params[@]}" "$@" ) + break + else + params=( "${params[@]}" "$1" ) + fi + esac + + shift + done + + if [[ $flags == *Q* ]]; then + true # generate no output + else + echo -n ' ' + if [[ $flags == *[cu]* ]]; then + printf '%s -- %s' "${opts[*]}" "${params[*]}" + else + if [[ $flags == *t* ]]; then + _getopt_quote_csh "${opts[@]}" -- "${params[@]}" + else + _getopt_quote "${opts[@]}" -- "${params[@]}" + fi + fi + echo + fi + + return $error + } + + _getopt_err() { + if [[ $flags != *q* ]]; then + printf '%s\n' "$1" >&2 + fi + } + + _getopt_resolve_abbrev() { + # Resolves an abbrevation from a list of possibilities. + # If the abbreviation is unambiguous, echoes the expansion on stdout + # and returns 0. If the abbreviation is ambiguous, prints a message on + # stderr and returns 1. (For first parse this should convert to exit + # status 2.) If there is no match at all, prints a message on stderr + # and returns 2. + declare a q="$1" + declare -a matches + shift + for a; do + if [[ $q == "$a" ]]; then + # Exact match. Squash any other partial matches. + matches=( "$a" ) + break + elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q" ]]; then + # Exact alternative match. Squash any other partial matches. + matches=( "$a" ) + break + elif [[ $a == "$q"* ]]; then + # Abbreviated match. + matches=( "${matches[@]}" "$a" ) + elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q"* ]]; then + # Abbreviated alternative match. + matches=( "${matches[@]}" "${a#-}" ) + fi + done + case ${#matches[@]} in + (0) + [[ $flags == *q* ]] || \ + printf "$name: unrecognized option %s\\n" >&2 \ + "$(_getopt_quote "$q")" + return 2 ;; + (1) + printf '%s' "${matches[0]}"; return 0 ;; + (*) + [[ $flags == *q* ]] || \ + printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \ + "$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" + return 1 ;; + esac + } + + _getopt_split() { + # Splits $2 at commas to build array specified by $1 + declare IFS=, + eval "$1=( \$2 )" + } + + _getopt_quote() { + # Quotes arguments with single quotes, escaping inner single quotes + declare s space q=\' + for s; do + printf "$space'%s'" "${s//$q/$q\\$q$q}" + space=' ' + done + } + + _getopt_quote_csh() { + # Quotes arguments with single quotes, escaping inner single quotes, + # bangs, backslashes and newlines + declare s i c space + for s; do + echo -n "$space'" + for ((i=0; i<${#s}; i++)); do + c=${s:i:1} + case $c in + (\\|\'|!) + echo -n "'\\$c'" ;; + ($'\n') + echo -n "\\$c" ;; + (*) + echo -n "$c" ;; + esac + done + echo -n \' + space=' ' + done + } + + _getopt_help() { + cat <<-EOT >&2 + + Usage: + getopt + getopt [options] [--] + getopt [options] -o|--options [options] [--] + + Parse command options. + + Options: + -a, --alternative allow long options starting with single - + -l, --longoptions the long options to be recognized + -n, --name the name under which errors are reported + -o, --options the short options to be recognized + -q, --quiet disable error reporting by getopt(3) + -Q, --quiet-output no normal output + -s, --shell set quoting conventions to those of + -T, --test test for getopt(1) version + -u, --unquoted do not quote the output + + -h, --help display this help and exit + -V, --version output version information and exit + + For more details see getopt(1). + EOT + } + + _getopt_version_check() { + if [[ -z $BASH_VERSION ]]; then + echo "getopt: unknown version of bash might not be compatible" >&2 + return 1 + fi + + # This is a lexical comparison that should be sufficient forever. + if [[ $BASH_VERSION < 2.05b ]]; then + echo "getopt: bash $BASH_VERSION might not be compatible" >&2 + return 1 + fi + + return 0 + } + + _getopt_version_check + _getopt_main "$@" + declare status=$? + unset -f _getopt_main _getopt_err _getopt_parse _getopt_quote \ + _getopt_quote_csh _getopt_resolve_abbrev _getopt_split _getopt_help \ + _getopt_version_check + return $status +} + +[[ $BASH_SOURCE != "$0" ]] || main "$@" diff --git a/helpers/terraform_validate b/helpers/terraform_validate new file mode 100755 index 0000000..7f60982 --- /dev/null +++ b/helpers/terraform_validate @@ -0,0 +1,23 @@ +#! /bin/bash +# +# Copyright 2019 Google LLC. This software is provided as-is, without warranty +# or representation for any use or purpose. Your use of it is subject to your +# agreement with Google. +# +# This script initializes modules so that terraform validate as of 0.12 behaves +# as expected and does not issue errors such as: +# +# Error: Module not installed +# +# on test/fixtures/shared_vpc_no_subnets/main.tf line 37: +# 37: module "project-factory" { +# +# This module is not yet installed. Run "terraform init" to install all modules +# required by this configuration. + +# The first and only argument to this script is the directory containing *.tf +# files to validate. This directory is assumed to be a root module. + +cd "$1" +terraform init -backend=false +terraform validate diff --git a/test/ci_integration.sh b/test/ci_integration.sh index f334908..51d961b 100755 --- a/test/ci_integration.sh +++ b/test/ci_integration.sh @@ -44,6 +44,13 @@ setup_environment() { # Terraform input variables export TF_VAR_project_id="${PROJECT_ID}" + export TF_VAR_region="${REGION:-us-central1}" + + # Stubs for module/project_cleanup + export TF_VAR_job_name="" + export TF_VAR_function_entry_point="" + export TF_VAR_function_source_directory="" + export TF_VAR_function_name="test" } main() { diff --git a/test/fixtures/shared/terraform.tfvars b/test/fixtures/shared/terraform.tfvars new file mode 100644 index 0000000..577d6b4 --- /dev/null +++ b/test/fixtures/shared/terraform.tfvars @@ -0,0 +1,2 @@ +project_id = "" +region = "us-central1" diff --git a/test/fixtures/shared/terraform.tfvars.sample b/test/fixtures/shared/terraform.tfvars.sample deleted file mode 100644 index 41f554b..0000000 --- a/test/fixtures/shared/terraform.tfvars.sample +++ /dev/null @@ -1,2 +0,0 @@ -project_id="" -region="us-central1" \ No newline at end of file diff --git a/test/integration/pubsub_scheduled/inspec.yml b/test/integration/pubsub_scheduled/inspec.yml index 7b02274..0fd8380 100644 --- a/test/integration/pubsub_scheduled/inspec.yml +++ b/test/integration/pubsub_scheduled/inspec.yml @@ -2,11 +2,11 @@ name: pubsub_scheduled depends: - name: inspec-gcp git: https://github.com/inspec/inspec-gcp.git - version: ~> 0.9.0 + version: ~> 0.11.0 attributes: - name: project_id required: true type: string - name: name required: true - type: string \ No newline at end of file + type: string diff --git a/test/make.sh b/test/make.sh index 5a9cd37..6c31950 100755 --- a/test/make.sh +++ b/test/make.sh @@ -25,7 +25,8 @@ finish() { trap finish EXIT # Create a temporary file in the auto-cleaned up directory while avoiding # overwriting TMPDIR for other processes. -# shellcheck disable=SC2120 # (Arguments may be passed, e.g. maketemp -d) +# shellcheck disable=SC2120 +# (Arguments may be passed, e.g. maketemp -d) maketemp() { TMPDIR="${DELETE_AT_EXIT}" mktemp "$@" } @@ -35,7 +36,11 @@ maketemp() { find_files() { local pth="$1" shift - find "${pth}" '(' -path '*/.git' -o -path '*/.terraform' ')' \ + find "${pth}" '(' \ + -path '*/.git' \ + -o -path '*/.terraform' \ + -o -path '*/.kitchen' \ + ')' \ -prune -o -type f "$@" } @@ -73,12 +78,25 @@ function docker() { # This function runs 'terraform validate' against all # directory paths which contain *.tf files. function check_terraform() { - echo "Running terraform validate" + local rval=125 + # fmt is before validate for faster feedback, validate requires terraform + # init which takes time. + echo "Running terraform fmt" find_files . -name "*.tf" -print0 \ | compat_xargs -0 -n1 dirname \ | sort -u \ - | grep -xv './test/fixtures/shared' \ - | compat_xargs -t -n1 terraform validate --check-variables=false + | compat_xargs -t -n1 terraform fmt -diff -check=true -write=false + rval="$?" + if [[ "${rval}" -gt 0 ]]; then + echo "Error: terraform fmt failed with exit code ${rval}" >&2 + echo "Check the output for diffs and correct using terraform fmt " >&2 + return "${rval}" + fi + echo "Running terraform validate" + find_files . -not -path "./test/fixtures/shared/*" -name "*.tf" -print0 \ + | compat_xargs -0 -n1 dirname \ + | sort -u \ + | compat_xargs -t -n1 helpers/terraform_validate } # This function runs 'go fmt' and 'go vet' on every file @@ -121,16 +139,18 @@ function check_trailing_whitespace() { function generate_docs() { echo "Generating markdown docs with terraform-docs" - local path tmpfile - while read -r path; do - if [[ -e "${path}/README.md" ]]; then - # shellcheck disable=SC2119 - tmpfile="$(maketemp)" - echo "terraform-docs markdown ${path}" - terraform-docs markdown "${path}" > "${tmpfile}" - helpers/combine_docfiles.py "${path}"/README.md "${tmpfile}" + local pth helper_dir rval + helper_dir="$(pwd)/helpers" + while read -r pth; do + if [[ -e "${pth}/README.md" ]]; then + (cd "${pth}" || return 3; "${helper_dir}"/terraform_docs .;) + rval="$?" + if [[ "${rval}" -gt 0 ]]; then + echo "Error: terraform_docs in ${pth} exit code: ${rval}" >&2 + return "${rval}" + fi else - echo "Skipping ${path} because README.md does not exist." + echo "Skipping ${pth} because README.md does not exist." fi done < <(find_files . -name '*.tf' -print0 \ | compat_xargs -0 -n1 dirname \ From 06c2e67017311020abe458207f4fb984a1f8ed96 Mon Sep 17 00:00:00 2001 From: Bohdan Yurov Date: Sat, 20 Jul 2019 19:07:27 +0300 Subject: [PATCH 2/2] Fixes #9: Add support for Terraform 0.12 https://github.com/terraform-google-modules/terraform-google-event-function/issues/20 - Migrated to the new TF 0.12 syntax - Added type for variables - Removed instances of unnecessary string interpolation from the code base - Removed unnecessary "element" calls - Run tests locally - Switched to docker image for terraform 0.12 (version 2.0.0) - Updated CHANGELOG.md - Updated README.md, add latest 0.11 release --- CHANGELOG.md | 8 ++- Makefile | 2 +- README.md | 12 +++- examples/pubsub_scheduled/main.tf | 16 +++-- examples/pubsub_scheduled/outputs.tf | 4 +- examples/pubsub_scheduled/variables.tf | 2 + main.tf | 72 +++++++++++--------- modules/project_cleanup/main.tf | 16 ++--- modules/project_cleanup/variables.tf | 8 ++- outputs.tf | 2 +- test/ci_integration.sh | 2 +- test/fixtures/pubsub_scheduled/main.tf | 4 +- test/integration/pubsub_scheduled/inspec.yml | 2 +- test/make.sh | 1 + variables.tf | 47 ++++++------- versions.tf | 19 ++++++ 16 files changed, 134 insertions(+), 83 deletions(-) create mode 100644 versions.tf diff --git a/CHANGELOG.md b/CHANGELOG.md index afabde5..e9cf681 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog][keepachangelog-site], and this project adheres to [Semantic Versioning][semver-site]. - ## [Unreleased] +## [1.0.0] - 2019-YY-ZZ + +### Changed + +- Supported version of Terraform is 0.12. [#11] + ## [0.4.1] - 2019-07-03 ### Fixed @@ -45,6 +50,7 @@ and this project adheres to [Semantic Versioning][semver-site]. [0.2.0]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/compare/v0.1.0...v0.2.0 [0.1.0]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/releases/tag/v0.1.0 +[#11]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/pull/11 [#8]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/pull/8 [#5]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/pull/5 diff --git a/Makefile b/Makefile index 2176964..b277f12 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ SHELL := /usr/bin/env bash # Docker build config variables CREDENTIALS_PATH ?= /cft/workdir/credentials.json DOCKER_ORG := gcr.io/cloud-foundation-cicd -DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 0.11.11_235.0.0_1.19.1_0.1.10 +DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 2.3.0 DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM} # All is the first target in the file so it will get picked up when you just run 'make' on its own diff --git a/README.md b/README.md index fafd24a..b9b00d4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # Terraform Google Scheduled Functions Module + This modules makes it easy to set up a scheduled job to trigger events/run functions. +## Compatibility + +This module is meant for use with Terraform 0.12. If you haven't +[upgraded](https://www.terraform.io/upgrade-guides/0-12.html) and need a Terraform 0.11.x-compatible +version of this module, the last released version intended for Terraform 0.11.x +is [v0.4.1](https://registry.terraform.io/modules/terraform-google-modules/scheduled-function/google/0.4.1). + ## Usage You can go to the examples folder, however the usage of the module could be like this in your own main.tf file: @@ -62,7 +70,7 @@ Then perform the following commands on the root folder: ## Requirements ### Terraform plugins -- [Terraform](https://www.terraform.io/downloads.html) 0.11.x +- [Terraform](https://www.terraform.io/downloads.html) 0.12.x - [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v2.1 ### App Engine @@ -94,7 +102,7 @@ In order to operate with the Service Account you must activate the following API ## Install ### Terraform -Be sure you have the correct Terraform version (0.11.x), you can choose the binary here: +Be sure you have the correct Terraform version (0.12.x), you can choose the binary here: - https://releases.hashicorp.com/terraform/ ## Testing and documentation generation diff --git a/examples/pubsub_scheduled/main.tf b/examples/pubsub_scheduled/main.tf index b7e9b20..0b53a3d 100644 --- a/examples/pubsub_scheduled/main.tf +++ b/examples/pubsub_scheduled/main.tf @@ -14,24 +14,28 @@ * limitations under the License. */ +terraform { + required_version = ">= 0.12" +} + provider "google-beta" { - version = "~> 2.1" - project = "${var.project_id}" - region = "${var.region}" + version = "~> 2.5" + project = var.project_id + region = var.region } module "pubsub_scheduled_example" { providers = { - google = "google-beta" + google = google-beta } source = "../../" - project_id = "${var.project_id}" + project_id = var.project_id job_name = "pubsub-example" job_schedule = "*/5 * * * *" function_entry_point = "doSomething" function_source_directory = "${path.module}/function_source" function_name = "testfunction-foo" - region = "${var.region}" + region = var.region topic_name = "pubsub_example_topic" } diff --git a/examples/pubsub_scheduled/outputs.tf b/examples/pubsub_scheduled/outputs.tf index e1ed80e..28cf813 100644 --- a/examples/pubsub_scheduled/outputs.tf +++ b/examples/pubsub_scheduled/outputs.tf @@ -15,11 +15,11 @@ */ output "name" { - value = "${module.pubsub_scheduled_example.name}" + value = module.pubsub_scheduled_example.name description = "The name of the job created" } output "project_id" { - value = "${var.project_id}" + value = var.project_id description = "The project ID" } diff --git a/examples/pubsub_scheduled/variables.tf b/examples/pubsub_scheduled/variables.tf index 68d4ab5..5fb43b4 100644 --- a/examples/pubsub_scheduled/variables.tf +++ b/examples/pubsub_scheduled/variables.tf @@ -15,10 +15,12 @@ */ variable "project_id" { + type = string description = "The project ID to host the network in" } variable "region" { + type = string description = "The region the project is in (App Engine specific)" default = "us-central1" } diff --git a/main.tf b/main.tf index 4826ffe..0ffe651 100644 --- a/main.tf +++ b/main.tf @@ -19,16 +19,16 @@ *****************************************/ resource "google_cloud_scheduler_job" "job" { - name = "${var.job_name}" - project = "${var.project_id}" - region = "${var.region}" - description = "${var.job_description}" - schedule = "${var.job_schedule}" - time_zone = "${var.time_zone}" + name = var.job_name + project = var.project_id + region = var.region + description = var.job_description + schedule = var.job_schedule + time_zone = var.time_zone - pubsub_target = { + pubsub_target { topic_name = "projects/${var.project_id}/topics/${module.pubsub_topic.topic}" - data = "${var.message_data}" + data = var.message_data } } @@ -37,9 +37,10 @@ resource "google_cloud_scheduler_job" "job" { *****************************************/ module "pubsub_topic" { - source = "github.com/terraform-google-modules/terraform-google-pubsub?ref=v0.1.0" - topic = "${var.topic_name}" - project_id = "${var.project_id}" + source = "terraform-google-modules/pubsub/google" + version = "~> 1.0" + topic = var.topic_name + project_id = var.project_id } /****************************************** @@ -47,35 +48,35 @@ module "pubsub_topic" { *****************************************/ resource "google_cloudfunctions_function" "main" { - name = "${var.function_name}" - source_archive_bucket = "${google_storage_bucket.main.name}" - source_archive_object = "${google_storage_bucket_object.main.name}" - description = "${var.function_description}" - available_memory_mb = "${var.function_available_memory_mb}" - timeout = "${var.function_timeout_s}" - entry_point = "${var.function_entry_point}" + name = var.function_name + source_archive_bucket = google_storage_bucket.main.name + source_archive_object = google_storage_bucket_object.main.name + description = var.function_description + available_memory_mb = var.function_available_memory_mb + timeout = var.function_timeout_s + entry_point = var.function_entry_point event_trigger { event_type = "google.pubsub.topic.publish" - resource = "${module.pubsub_topic.topic}" + resource = module.pubsub_topic.topic failure_policy { - retry = "${var.function_event_trigger_failure_policy_retry}" + retry = var.function_event_trigger_failure_policy_retry } } - labels = "${var.function_labels}" - runtime = "${var.function_runtime}" - environment_variables = "${var.function_environment_variables}" - project = "${var.project_id}" - region = "${var.region}" - service_account_email = "${var.function_service_account_email}" + labels = var.function_labels + runtime = var.function_runtime + environment_variables = var.function_environment_variables + project = var.project_id + region = var.region + service_account_email = var.function_service_account_email } data "archive_file" "main" { type = "zip" - output_path = "${pathexpand("${var.function_source_directory}.zip")}" - source_dir = "${pathexpand("${var.function_source_directory}")}" + output_path = pathexpand("${var.function_source_directory}.zip") + source_dir = pathexpand(var.function_source_directory) } resource "random_string" "random_suffix" { @@ -85,18 +86,21 @@ resource "random_string" "random_suffix" { } resource "google_storage_bucket" "main" { - name = "${coalesce(var.bucket_name, "${var.project_id}-scheduled-function-${random_string.random_suffix.result}")}" + name = coalesce( + var.bucket_name, + "${var.project_id}-scheduled-function-${random_string.random_suffix.result}", + ) force_destroy = "true" - location = "${var.region}" - project = "${var.project_id}" + location = var.region + project = var.project_id storage_class = "REGIONAL" - labels = "${var.function_source_archive_bucket_labels}" + labels = var.function_source_archive_bucket_labels } resource "google_storage_bucket_object" "main" { name = "event_function-${random_string.random_suffix.result}.zip" - bucket = "${google_storage_bucket.main.name}" - source = "${data.archive_file.main.output_path}" + bucket = google_storage_bucket.main.name + source = data.archive_file.main.output_path content_disposition = "attachment" content_encoding = "gzip" content_type = "application/zip" diff --git a/modules/project_cleanup/main.tf b/modules/project_cleanup/main.tf index 53f6e25..040928d 100644 --- a/modules/project_cleanup/main.tf +++ b/modules/project_cleanup/main.tf @@ -15,35 +15,35 @@ */ resource "google_service_account" "project_cleaner_function" { - project = "${var.project_id}" + project = var.project_id account_id = "project-cleaner-function" display_name = "Project Cleaner Function" } resource "google_organization_iam_member" "project_owner" { - org_id = "${var.organization_id}" + org_id = var.organization_id role = "roles/owner" member = "serviceAccount:${google_service_account.project_cleaner_function.email}" } module "scheduled_project_cleaner" { source = "../../" - project_id = "${var.project_id}" + project_id = var.project_id job_name = "project-cleaner" job_schedule = "*/5 * * * *" function_entry_point = "CleanUpProjects" function_source_directory = "${path.module}/function_source" function_name = "old-project-cleaner" - region = "${var.region}" + region = var.region topic_name = "pubsub_scheduled_project_cleaner" - function_available_memory_mb = "128" + function_available_memory_mb = 128 function_description = "Clean up GCP projects older than ${var.max_project_age_in_hours} hours matching particular tags" function_runtime = "go111" function_service_account_email = "${google_service_account.project_cleaner_function.email}" function_environment_variables = { - TARGET_TAG_NAME = "${var.target_tag_name}" - TARGET_TAG_VALUE = "${var.target_tag_value}" - MAX_PROJECT_AGE_HOURS = "${var.max_project_age_in_hours}" + TARGET_TAG_NAME = var.target_tag_name + TARGET_TAG_VALUE = var.target_tag_value + MAX_PROJECT_AGE_HOURS = var.max_project_age_in_hours } } diff --git a/modules/project_cleanup/variables.tf b/modules/project_cleanup/variables.tf index 6ef3273..7841d1d 100644 --- a/modules/project_cleanup/variables.tf +++ b/modules/project_cleanup/variables.tf @@ -15,28 +15,34 @@ */ variable "organization_id" { + type = string description = "The organization ID whose projects to clean up" } variable "project_id" { + type = string description = "The project ID to host the scheduled function in" } variable "region" { + type = string description = "The region the project is in (App Engine specific)" } variable "target_tag_name" { + type = string description = "The name of a tag to filter GCP projects on for consideration by the cleanup utility" default = "cft-ephemeral" } variable "target_tag_value" { + type = string description = "The value of a tag to filter GCP projects on for consideration by the cleanup utility" default = "true" } variable "max_project_age_in_hours" { + type = number description = "The maximum number of hours that a GCP project, selected by `target_tag_name` and `target_tag_value`, can exist" - default = "6" + default = 6 } diff --git a/outputs.tf b/outputs.tf index 200972e..f77f98b 100644 --- a/outputs.tf +++ b/outputs.tf @@ -15,6 +15,6 @@ */ output "name" { - value = "${google_cloud_scheduler_job.job.name}" + value = google_cloud_scheduler_job.job.name description = "The name of the job created" } diff --git a/test/ci_integration.sh b/test/ci_integration.sh index 51d961b..1fc11ba 100755 --- a/test/ci_integration.sh +++ b/test/ci_integration.sh @@ -46,7 +46,7 @@ setup_environment() { export TF_VAR_project_id="${PROJECT_ID}" export TF_VAR_region="${REGION:-us-central1}" - # Stubs for module/project_cleanup + # Stubs for module/project_cleanup (for linters to pass) export TF_VAR_job_name="" export TF_VAR_function_entry_point="" export TF_VAR_function_source_directory="" diff --git a/test/fixtures/pubsub_scheduled/main.tf b/test/fixtures/pubsub_scheduled/main.tf index 450b39b..63b41d7 100644 --- a/test/fixtures/pubsub_scheduled/main.tf +++ b/test/fixtures/pubsub_scheduled/main.tf @@ -16,6 +16,6 @@ module "pubsub_scheduled_example" { source = "../../../examples/pubsub_scheduled" - project_id = "${var.project_id}" - region = "${var.region}" + project_id = var.project_id + region = var.region } diff --git a/test/integration/pubsub_scheduled/inspec.yml b/test/integration/pubsub_scheduled/inspec.yml index 0fd8380..81072eb 100644 --- a/test/integration/pubsub_scheduled/inspec.yml +++ b/test/integration/pubsub_scheduled/inspec.yml @@ -2,7 +2,7 @@ name: pubsub_scheduled depends: - name: inspec-gcp git: https://github.com/inspec/inspec-gcp.git - version: ~> 0.11.0 + tag: v0.10.0 attributes: - name: project_id required: true diff --git a/test/make.sh b/test/make.sh index 6c31950..73291ec 100755 --- a/test/make.sh +++ b/test/make.sh @@ -40,6 +40,7 @@ find_files() { -path '*/.git' \ -o -path '*/.terraform' \ -o -path '*/.kitchen' \ + -o -path '*.zip' \ ')' \ -prune -o -type f "$@" } diff --git a/variables.tf b/variables.tf index 7ea79cb..aba8c8f 100644 --- a/variables.tf +++ b/variables.tf @@ -15,120 +15,121 @@ */ variable "project_id" { + type = string description = "The ID of the project where the resources will be created" } variable "job_name" { - type = "string" + type = string description = "The name of the scheduled job to run" } variable "job_description" { - type = "string" + type = string description = "Addition text to describet the job" default = "" } variable "job_schedule" { - type = "string" + type = string description = "The job frequency, in cron syntax" default = "*/2 * * * *" } variable "function_available_memory_mb" { - type = "string" - default = "256" + type = number + default = 256 description = "The amount of memory in megabytes allotted for the function to use." } variable "function_description" { - type = "string" + type = string default = "Processes log export events provided through a Pub/Sub topic subscription." description = "The description of the function." } variable "function_entry_point" { - type = "string" + type = string description = "The name of a method in the function source which will be invoked when the function is executed." } variable "function_environment_variables" { - type = "map" + type = map(string) default = {} description = "A set of key/value environment variable pairs to assign to the function." } variable "function_event_trigger_failure_policy_retry" { - type = "string" - default = "false" + type = bool + default = false description = "A toggle to determine if the function should be retried on failure." } variable "function_labels" { - type = "map" + type = map(string) default = {} description = "A set of key/value label pairs to assign to the function." } variable "function_runtime" { - type = "string" + type = string default = "nodejs6" description = "The runtime in which the function will be executed." } variable "function_source_archive_bucket_labels" { - type = "map" + type = map(string) default = {} description = "A set of key/value label pairs to assign to the function source archive bucket." } variable "function_source_directory" { - type = "string" + type = string description = "The contents of this directory will be archived and used as the function source." } variable "function_timeout_s" { - type = "string" - default = "60" + type = number + default = 60 description = "The amount of time in seconds allotted for the execution of the function." } variable "function_service_account_email" { - type = "string" + type = string default = "" description = "The service account to run the function as." } variable "bucket_name" { - type = "string" + type = string default = "" description = "The name to apply to the bucket. Will default to a string of -scheduled-function-XXXX> with XXXX being random characters." } variable "function_name" { - type = "string" + type = string description = "The name to apply to the function" } variable "region" { - type = "string" + type = string description = "The region in which resources will be applied." } variable "topic_name" { - type = "string" + type = string description = "Name of pubsub topic connecting the scheduled job and the function" default = "test-topic" } variable "message_data" { - type = "string" + type = string description = "The data to send in the topic message." default = "dGVzdA==" } variable "time_zone" { - type = "string" + type = string description = "The timezone to use in scheduler" default = "Etc/UTC" } diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..2970427 --- /dev/null +++ b/versions.tf @@ -0,0 +1,19 @@ +/** + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +terraform { + required_version = ">= 0.12" +}