Skip to content

Commit 06c2e67

Browse files
committed
Fixes #9: Add support for Terraform 0.12
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
1 parent 75fa811 commit 06c2e67

File tree

16 files changed

+134
-83
lines changed

16 files changed

+134
-83
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog][keepachangelog-site],
66
and this project adheres to [Semantic Versioning][semver-site].
77

8-
98
## [Unreleased]
109

10+
## [1.0.0] - 2019-YY-ZZ
11+
12+
### Changed
13+
14+
- Supported version of Terraform is 0.12. [#11]
15+
1116
## [0.4.1] - 2019-07-03
1217

1318
### Fixed
@@ -45,6 +50,7 @@ and this project adheres to [Semantic Versioning][semver-site].
4550
[0.2.0]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/compare/v0.1.0...v0.2.0
4651
[0.1.0]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/releases/tag/v0.1.0
4752

53+
[#11]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/pull/11
4854
[#8]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/pull/8
4955
[#5]: https://github.com/terraform-google-modules/terraform-google-scheduled-function/pull/5
5056

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SHELL := /usr/bin/env bash
1818
# Docker build config variables
1919
CREDENTIALS_PATH ?= /cft/workdir/credentials.json
2020
DOCKER_ORG := gcr.io/cloud-foundation-cicd
21-
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 0.11.11_235.0.0_1.19.1_0.1.10
21+
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 2.3.0
2222
DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
2323

2424
# All is the first target in the file so it will get picked up when you just run 'make' on its own

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Terraform Google Scheduled Functions Module
2+
23
This modules makes it easy to set up a scheduled job to trigger events/run functions.
34

5+
## Compatibility
6+
7+
This module is meant for use with Terraform 0.12. If you haven't
8+
[upgraded](https://www.terraform.io/upgrade-guides/0-12.html) and need a Terraform 0.11.x-compatible
9+
version of this module, the last released version intended for Terraform 0.11.x
10+
is [v0.4.1](https://registry.terraform.io/modules/terraform-google-modules/scheduled-function/google/0.4.1).
11+
412
## Usage
513
You can go to the examples folder, however the usage of the module could be like this in your own main.tf file:
614

@@ -62,7 +70,7 @@ Then perform the following commands on the root folder:
6270

6371
## Requirements
6472
### Terraform plugins
65-
- [Terraform](https://www.terraform.io/downloads.html) 0.11.x
73+
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
6674
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v2.1
6775

6876
### App Engine
@@ -94,7 +102,7 @@ In order to operate with the Service Account you must activate the following API
94102
## Install
95103

96104
### Terraform
97-
Be sure you have the correct Terraform version (0.11.x), you can choose the binary here:
105+
Be sure you have the correct Terraform version (0.12.x), you can choose the binary here:
98106
- https://releases.hashicorp.com/terraform/
99107

100108
## Testing and documentation generation

examples/pubsub_scheduled/main.tf

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@
1414
* limitations under the License.
1515
*/
1616

17+
terraform {
18+
required_version = ">= 0.12"
19+
}
20+
1721
provider "google-beta" {
18-
version = "~> 2.1"
19-
project = "${var.project_id}"
20-
region = "${var.region}"
22+
version = "~> 2.5"
23+
project = var.project_id
24+
region = var.region
2125
}
2226

2327
module "pubsub_scheduled_example" {
2428
providers = {
25-
google = "google-beta"
29+
google = google-beta
2630
}
2731

2832
source = "../../"
29-
project_id = "${var.project_id}"
33+
project_id = var.project_id
3034
job_name = "pubsub-example"
3135
job_schedule = "*/5 * * * *"
3236
function_entry_point = "doSomething"
3337
function_source_directory = "${path.module}/function_source"
3438
function_name = "testfunction-foo"
35-
region = "${var.region}"
39+
region = var.region
3640
topic_name = "pubsub_example_topic"
3741
}

examples/pubsub_scheduled/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616

1717
output "name" {
18-
value = "${module.pubsub_scheduled_example.name}"
18+
value = module.pubsub_scheduled_example.name
1919
description = "The name of the job created"
2020
}
2121

2222
output "project_id" {
23-
value = "${var.project_id}"
23+
value = var.project_id
2424
description = "The project ID"
2525
}

examples/pubsub_scheduled/variables.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616

1717
variable "project_id" {
18+
type = string
1819
description = "The project ID to host the network in"
1920
}
2021

2122
variable "region" {
23+
type = string
2224
description = "The region the project is in (App Engine specific)"
2325
default = "us-central1"
2426
}

main.tf

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
*****************************************/
2020

2121
resource "google_cloud_scheduler_job" "job" {
22-
name = "${var.job_name}"
23-
project = "${var.project_id}"
24-
region = "${var.region}"
25-
description = "${var.job_description}"
26-
schedule = "${var.job_schedule}"
27-
time_zone = "${var.time_zone}"
22+
name = var.job_name
23+
project = var.project_id
24+
region = var.region
25+
description = var.job_description
26+
schedule = var.job_schedule
27+
time_zone = var.time_zone
2828

29-
pubsub_target = {
29+
pubsub_target {
3030
topic_name = "projects/${var.project_id}/topics/${module.pubsub_topic.topic}"
31-
data = "${var.message_data}"
31+
data = var.message_data
3232
}
3333
}
3434

@@ -37,45 +37,46 @@ resource "google_cloud_scheduler_job" "job" {
3737
*****************************************/
3838

3939
module "pubsub_topic" {
40-
source = "github.com/terraform-google-modules/terraform-google-pubsub?ref=v0.1.0"
41-
topic = "${var.topic_name}"
42-
project_id = "${var.project_id}"
40+
source = "terraform-google-modules/pubsub/google"
41+
version = "~> 1.0"
42+
topic = var.topic_name
43+
project_id = var.project_id
4344
}
4445

4546
/******************************************
4647
Cloud Function Resource Definitions
4748
*****************************************/
4849

4950
resource "google_cloudfunctions_function" "main" {
50-
name = "${var.function_name}"
51-
source_archive_bucket = "${google_storage_bucket.main.name}"
52-
source_archive_object = "${google_storage_bucket_object.main.name}"
53-
description = "${var.function_description}"
54-
available_memory_mb = "${var.function_available_memory_mb}"
55-
timeout = "${var.function_timeout_s}"
56-
entry_point = "${var.function_entry_point}"
51+
name = var.function_name
52+
source_archive_bucket = google_storage_bucket.main.name
53+
source_archive_object = google_storage_bucket_object.main.name
54+
description = var.function_description
55+
available_memory_mb = var.function_available_memory_mb
56+
timeout = var.function_timeout_s
57+
entry_point = var.function_entry_point
5758

5859
event_trigger {
5960
event_type = "google.pubsub.topic.publish"
60-
resource = "${module.pubsub_topic.topic}"
61+
resource = module.pubsub_topic.topic
6162

6263
failure_policy {
63-
retry = "${var.function_event_trigger_failure_policy_retry}"
64+
retry = var.function_event_trigger_failure_policy_retry
6465
}
6566
}
6667

67-
labels = "${var.function_labels}"
68-
runtime = "${var.function_runtime}"
69-
environment_variables = "${var.function_environment_variables}"
70-
project = "${var.project_id}"
71-
region = "${var.region}"
72-
service_account_email = "${var.function_service_account_email}"
68+
labels = var.function_labels
69+
runtime = var.function_runtime
70+
environment_variables = var.function_environment_variables
71+
project = var.project_id
72+
region = var.region
73+
service_account_email = var.function_service_account_email
7374
}
7475

7576
data "archive_file" "main" {
7677
type = "zip"
77-
output_path = "${pathexpand("${var.function_source_directory}.zip")}"
78-
source_dir = "${pathexpand("${var.function_source_directory}")}"
78+
output_path = pathexpand("${var.function_source_directory}.zip")
79+
source_dir = pathexpand(var.function_source_directory)
7980
}
8081

8182
resource "random_string" "random_suffix" {
@@ -85,18 +86,21 @@ resource "random_string" "random_suffix" {
8586
}
8687

8788
resource "google_storage_bucket" "main" {
88-
name = "${coalesce(var.bucket_name, "${var.project_id}-scheduled-function-${random_string.random_suffix.result}")}"
89+
name = coalesce(
90+
var.bucket_name,
91+
"${var.project_id}-scheduled-function-${random_string.random_suffix.result}",
92+
)
8993
force_destroy = "true"
90-
location = "${var.region}"
91-
project = "${var.project_id}"
94+
location = var.region
95+
project = var.project_id
9296
storage_class = "REGIONAL"
93-
labels = "${var.function_source_archive_bucket_labels}"
97+
labels = var.function_source_archive_bucket_labels
9498
}
9599

96100
resource "google_storage_bucket_object" "main" {
97101
name = "event_function-${random_string.random_suffix.result}.zip"
98-
bucket = "${google_storage_bucket.main.name}"
99-
source = "${data.archive_file.main.output_path}"
102+
bucket = google_storage_bucket.main.name
103+
source = data.archive_file.main.output_path
100104
content_disposition = "attachment"
101105
content_encoding = "gzip"
102106
content_type = "application/zip"

modules/project_cleanup/main.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,35 @@
1515
*/
1616

1717
resource "google_service_account" "project_cleaner_function" {
18-
project = "${var.project_id}"
18+
project = var.project_id
1919
account_id = "project-cleaner-function"
2020
display_name = "Project Cleaner Function"
2121
}
2222

2323
resource "google_organization_iam_member" "project_owner" {
24-
org_id = "${var.organization_id}"
24+
org_id = var.organization_id
2525
role = "roles/owner"
2626
member = "serviceAccount:${google_service_account.project_cleaner_function.email}"
2727
}
2828

2929
module "scheduled_project_cleaner" {
3030
source = "../../"
31-
project_id = "${var.project_id}"
31+
project_id = var.project_id
3232
job_name = "project-cleaner"
3333
job_schedule = "*/5 * * * *"
3434
function_entry_point = "CleanUpProjects"
3535
function_source_directory = "${path.module}/function_source"
3636
function_name = "old-project-cleaner"
37-
region = "${var.region}"
37+
region = var.region
3838
topic_name = "pubsub_scheduled_project_cleaner"
39-
function_available_memory_mb = "128"
39+
function_available_memory_mb = 128
4040
function_description = "Clean up GCP projects older than ${var.max_project_age_in_hours} hours matching particular tags"
4141
function_runtime = "go111"
4242
function_service_account_email = "${google_service_account.project_cleaner_function.email}"
4343

4444
function_environment_variables = {
45-
TARGET_TAG_NAME = "${var.target_tag_name}"
46-
TARGET_TAG_VALUE = "${var.target_tag_value}"
47-
MAX_PROJECT_AGE_HOURS = "${var.max_project_age_in_hours}"
45+
TARGET_TAG_NAME = var.target_tag_name
46+
TARGET_TAG_VALUE = var.target_tag_value
47+
MAX_PROJECT_AGE_HOURS = var.max_project_age_in_hours
4848
}
4949
}

modules/project_cleanup/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,34 @@
1515
*/
1616

1717
variable "organization_id" {
18+
type = string
1819
description = "The organization ID whose projects to clean up"
1920
}
2021

2122
variable "project_id" {
23+
type = string
2224
description = "The project ID to host the scheduled function in"
2325
}
2426

2527
variable "region" {
28+
type = string
2629
description = "The region the project is in (App Engine specific)"
2730
}
2831

2932
variable "target_tag_name" {
33+
type = string
3034
description = "The name of a tag to filter GCP projects on for consideration by the cleanup utility"
3135
default = "cft-ephemeral"
3236
}
3337

3438
variable "target_tag_value" {
39+
type = string
3540
description = "The value of a tag to filter GCP projects on for consideration by the cleanup utility"
3641
default = "true"
3742
}
3843

3944
variable "max_project_age_in_hours" {
45+
type = number
4046
description = "The maximum number of hours that a GCP project, selected by `target_tag_name` and `target_tag_value`, can exist"
41-
default = "6"
47+
default = 6
4248
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
output "name" {
18-
value = "${google_cloud_scheduler_job.job.name}"
18+
value = google_cloud_scheduler_job.job.name
1919
description = "The name of the job created"
2020
}

0 commit comments

Comments
 (0)