diff --git a/CHANGELOG.md b/CHANGELOG.md index afabde51..e9cf681a 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 2176964d..b277f126 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 fafd24a8..b9b00d47 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 b7e9b206..0b53a3d7 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 e1ed80e8..28cf8131 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 68d4ab54..5fb43b42 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 4826ffe3..38e1127a 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,9 @@ 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 = "github.com/terraform-google-modules/terraform-google-pubsub?ref=v1.0.0" + topic = var.topic_name + project_id = var.project_id } /****************************************** @@ -47,35 +47,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 +85,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 53f6e250..040928df 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 6ef3273d..7841d1d3 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 200972ea..f77f98bb 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 51d961b2..1fc11ba2 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 450b39b6..63b41d7d 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 0fd8380c..81072ebe 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 6c319507..73291ec9 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 7ea79cb7..aba8c8f6 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 00000000..29704272 --- /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" +}