Skip to content

Commit

Permalink
Fixes terraform-google-modules#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 20, 2019
1 parent 75fa811 commit 9cb4889
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 66 deletions.
7 changes: 6 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]

## [2.0.0] - 2019-YY-ZZ

### Changed

- Supported version of Terraform is 0.12. [#3]

## [0.4.1] - 2019-07-03

### Fixed
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
8 changes: 8 additions & 0 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][terraform-0.12-upgrade] and need a Terraform 0.11.x-compatible
version of this module, the last released version intended for Terraform 0.11.x
is [0.4.1][v0.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
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"
}
69 changes: 36 additions & 33 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 @@ -38,44 +38,44 @@ 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}"
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 +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"
Expand Down
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"
}
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
}
40 changes: 20 additions & 20 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,116 +19,116 @@ variable "project_id" {
}

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"
type = string
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"
type = string
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"
type = string
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 <project-id>-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"
}
19 changes: 19 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 9cb4889

Please sign in to comment.