Skip to content

Commit

Permalink
Fixes #9: Add support for Terraform 0.12
Browse files Browse the repository at this point in the history
terraform-google-modules/terraform-google-event-function#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
  • Loading branch information
nick4fake committed Jul 30, 2019
1 parent 75fa811 commit 06c2e67
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 83 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions examples/pubsub_scheduled/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions examples/pubsub_scheduled/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 2 additions & 0 deletions examples/pubsub_scheduled/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
72 changes: 38 additions & 34 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -37,45 +37,46 @@ 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
}

/******************************************
Cloud Function Resource Definitions
*****************************************/

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" {
Expand All @@ -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"
Expand Down
16 changes: 8 additions & 8 deletions modules/project_cleanup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
8 changes: 7 additions & 1 deletion modules/project_cleanup/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion test/ci_integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/pubsub_scheduled/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion test/integration/pubsub_scheduled/inspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ find_files() {
-path '*/.git' \
-o -path '*/.terraform' \
-o -path '*/.kitchen' \
-o -path '*.zip' \
')' \
-prune -o -type f "$@"
}
Expand Down
Loading

0 comments on commit 06c2e67

Please sign in to comment.