Review fixes.

* Refactor trigger_config and trigger_config_v2 into one structure
* bump default python version to 3.10
* typo fixes
This commit is contained in:
Wiktor Niesiobędzki 2022-11-12 12:34:09 +01:00
parent 303b51a0a9
commit 9e37a915c8
9 changed files with 100 additions and 96 deletions

View File

@ -80,18 +80,17 @@ module "cf" {
bucket_name = "${var.name}-${random_pet.random.id}"
bucket_config = {
location = var.region
lifecycle_delete_age = null
}
bundle_config = {
source_dir = "cf"
output_path = var.bundle_path
excludes = null
}
service_account = module.service-account.email
trigger_config = {
v1 = {
event = "google.pubsub.topic.publish"
resource = module.pubsub.topic.id
retry = null
}
}
}

View File

@ -138,14 +138,12 @@ module "cloud-function" {
bucket_name = "${local.monitoring_project}-network-dashboard-bucket"
bucket_config = {
location = var.region
lifecycle_delete_age = null
}
region = var.region
bundle_config = {
source_dir = "cloud-function"
output_path = "cloud-function.zip"
excludes = null
}
function_config = {
@ -169,10 +167,17 @@ module "cloud-function" {
# Internal only doesn't seem to work with CFv2:
ingress_settings = var.cf_version == "V2" ? "ALLOW_ALL" : "ALLOW_INTERNAL_ONLY"
trigger_config = {
trigger_config = var.cf_version == "V2" ? {
v2 = {
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = module.pubsub.topic.id
# TODO: service_account_email
}
} : {
v1 = {
event = "google.pubsub.topic.publish"
resource = module.pubsub.topic.id
retry = null
}
}
}

View File

@ -53,12 +53,10 @@ module "cf" {
bucket_name = "${var.name}-${random_pet.random.id}"
bucket_config = {
location = var.region
lifecycle_delete_age = null
}
bundle_config = {
source_dir = "cf"
output_path = var.bundle_path
excludes = null
}
# https://github.com/hashicorp/terraform-provider-archive/issues/40
# https://issuetracker.google.com/issues/155215191
@ -68,9 +66,10 @@ module "cf" {
}
service_account_create = true
trigger_config = {
v1 = {
event = "google.pubsub.topic.publish"
resource = module.pubsub.topic.id
retry = null
}
}
}

View File

@ -92,18 +92,17 @@ module "cf" {
bucket_name = "${var.name}-${random_pet.random.id}"
bucket_config = {
location = var.region
lifecycle_delete_age = null
}
bundle_config = {
source_dir = "cf"
output_path = var.bundle_path
excludes = null
}
service_account = module.service-account.email
trigger_config = {
v1 = {
event = "google.pubsub.topic.publish"
resource = module.pubsub.topic.id
retry = null
}
}
}
@ -125,10 +124,12 @@ module "cffile" {
}
service_account = module.service-account.email
trigger_config = {
v1 = {
event = "google.pubsub.topic.publish"
resource = module.pubsub_file.topic.id
retry = null
}
}
}
resource "random_pet" "random" {

View File

@ -115,12 +115,10 @@ module "cf-restarter" {
bucket_name = "cf-bundle-bucket-${random_pet.random.id}"
bucket_config = {
location = var.region
lifecycle_delete_age = null
}
bundle_config = {
source_dir = "${path.module}/function/restarter"
output_path = "restarter.zip"
excludes = []
}
service_account = module.service-account-restarter.email
@ -134,9 +132,10 @@ module "cf-restarter" {
}
trigger_config = {
v1 = {
event = "google.pubsub.topic.publish"
resource = module.pubsub.topic.id
retry = null
}
}
}
@ -151,7 +150,6 @@ module "cf-healthchecker" {
bundle_config = {
source_dir = "${path.module}/function/healthchecker"
output_path = "healthchecker.zip"
excludes = []
}
service_account = module.service-account-healthchecker.email

View File

@ -195,11 +195,9 @@ module "function-hello" {
bundle_config = {
source_dir = "${path.module}/assets"
output_path = "bundle.zip"
excludes = null
}
bucket_config = {
location = var.region
lifecycle_delete_age = null
}
iam = {
"roles/cloudfunctions.invoker" = ["allUsers"]

View File

@ -59,14 +59,16 @@ module "cf-http" {
output_path = "bundle.zip"
}
trigger_config = {
v1 = {
event = "google.pubsub.topic.publish"
resource = "local.my-topic"
}
}
}
# tftest modules=1 resources=2
```
Cloud Functions 2nd gen support only [Eventarc](https://cloud.google.com/eventarc/docs) and user separate structure
Cloud Functions 2nd gen support only [Eventarc](https://cloud.google.com/eventarc/docs) and uses separate structure
to configure:
```hcl
module "trigger-service-account" {
@ -90,11 +92,13 @@ module "cf-http" {
source_dir = "fabric/assets/"
output_path = "bundle.zip"
}
trigger_config_v2 = {
trigger_config = {
v2 = {
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = "local.my-topic"
service_account_email = module.trigger-service-account.email
}
}
}
# tftest modules=2 resources=4
```
@ -230,20 +234,19 @@ module "cf-http" {
| [build_worker_pool](variables.tf#L31) | Build worker pool, in projects/<PROJECT-ID>/locations/<REGION>/workerPools/<POOL_NAME> format | <code>string</code> | | <code>null</code> |
| [description](variables.tf#L46) | Optional description. | <code>string</code> | | <code>&#34;Terraform managed.&#34;</code> |
| [environment_variables](variables.tf#L52) | Cloud function environment variables. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
| [function_config](variables.tf#L58) | Cloud function configuration. Defaults to using main as entrypoint, 1 instance with 256MiB of memory, and 180 second timeout | <code title="object&#40;&#123;&#10; entry_point &#61; optional&#40;string, &#34;main&#34;&#41;&#10; instances &#61; optional&#40;number, 1&#41;&#10; memory &#61; optional&#40;number, 256&#41; &#35; Memory in MB&#10; runtime &#61; optional&#40;string, &#34;python37&#34;&#41;&#10; timeout &#61; optional&#40;number, 180&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; entry_point &#61; &#34;main&#34;&#10; instances &#61; 1&#10; memory &#61; 256&#10; runtime &#61; &#34;python37&#34;&#10; timeout &#61; 180&#10;&#125;">&#123;&#8230;&#125;</code> |
| [function_config](variables.tf#L58) | Cloud function configuration. Defaults to using main as entrypoint, 1 instance with 256MiB of memory, and 180 second timeout | <code title="object&#40;&#123;&#10; entry_point &#61; optional&#40;string, &#34;main&#34;&#41;&#10; instances &#61; optional&#40;number, 1&#41;&#10; memory &#61; optional&#40;number, 256&#41; &#35; Memory in MB&#10; runtime &#61; optional&#40;string, &#34;python310&#34;&#41;&#10; timeout &#61; optional&#40;number, 180&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; entry_point &#61; &#34;main&#34;&#10; instances &#61; 1&#10; memory &#61; 256&#10; runtime &#61; &#34;python37&#34;&#10; timeout &#61; 180&#10;&#125;">&#123;&#8230;&#125;</code> |
| [iam](variables.tf#L76) | IAM bindings for topic in {ROLE => [MEMBERS]} format. | <code>map&#40;list&#40;string&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [ingress_settings](variables.tf#L82) | Control traffic that reaches the cloud function. Allowed values are ALLOW_ALL, ALLOW_INTERNAL_AND_GCLB and ALLOW_INTERNAL_ONLY . | <code>string</code> | | <code>null</code> |
| [labels](variables.tf#L88) | Resource labels. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
| [prefix](variables.tf#L99) | Optional prefix used for resource names. | <code>string</code> | | <code>null</code> |
| [region](variables.tf#L114) | Region used for all resources. | <code>string</code> | | <code>&#34;europe-west1&#34;</code> |
| [secrets](variables.tf#L120) | Secret Manager secrets. Key is the variable name or mountpoint, volume versions are in version:path format. | <code title="map&#40;object&#40;&#123;&#10; is_volume &#61; bool&#10; project_id &#61; number&#10; secret &#61; string&#10; versions &#61; list&#40;string&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [service_account](variables.tf#L132) | Service account email.service_account Unused if service account is auto-created. | <code>string</code> | | <code>null</code> |
| [service_account](variables.tf#L132) | Service account email. Unused if service account is auto-created. | <code>string</code> | | <code>null</code> |
| [service_account_create](variables.tf#L138) | Auto-create service account. | <code>bool</code> | | <code>false</code> |
| [trigger_config](variables.tf#L144) | Function trigger configuration. Leave null for HTTP trigger. | <code title="object&#40;&#123;&#10; event &#61; string&#10; resource &#61; string&#10; retry &#61; optional&#40;bool&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [trigger_config_v2](variables.tf#L154) | Function trigger configuration. Leave null for HTTP trigger. | <code title="object&#40;&#123;&#10; region &#61; optional&#40;string&#41;&#10; event_type &#61; optional&#40;string&#41;&#10; pubsub_topic &#61; optional&#40;string&#41;&#10; event_filters &#61; optional&#40;list&#40;object&#40;&#123;&#10; attribute &#61; string&#10; value &#61; string&#10; operator &#61; string&#10; &#125;&#41;&#41;, &#91;&#93;&#41;&#10; service_account_email &#61; optional&#40;string&#41;&#10; retry_policy &#61; optional&#40;string&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [v2](variables.tf#L190) | Whether to use Cloud Function version 2nd Gen or 1st Gen. | <code>bool</code> | | <code>false</code> |
| [vpc_connector](variables.tf#L171) | VPC connector configuration. Set create to 'true' if a new connector needs to be created. | <code title="object&#40;&#123;&#10; create &#61; bool&#10; name &#61; string&#10; egress_settings &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [vpc_connector_config](variables.tf#L181) | VPC connector network configuration. Must be provided if new VPC connector is being created. | <code title="object&#40;&#123;&#10; ip_cidr_range &#61; string&#10; network &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [trigger_config](variables.tf#L144) | Function trigger configuration. Leave null for HTTP trigger. | <code title="object&#40;&#123;&#10; v1 &#61; optional&#40;object&#40;&#123;&#10; event &#61; string&#10; resource &#61; string&#10; retry &#61; optional&#40;bool&#41;&#10; &#125;&#41;&#41;,&#10; v2 &#61; optional&#40;object&#40;&#123;&#10; region &#61; optional&#40;string&#41;&#10; event_type &#61; optional&#40;string&#41;&#10; pubsub_topic &#61; optional&#40;string&#41;&#10; event_filters &#61; optional&#40;list&#40;object&#40;&#123;&#10; attribute &#61; string&#10; value &#61; string&#10; operator &#61; string&#10; &#125;&#41;&#41;&#41;&#10; service_account_email &#61; optional&#40;string&#41;&#10; retry_policy &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [v2](variables.tf#L191) | Whether to use Cloud Function version 2nd Gen or 1st Gen. | <code>bool</code> | | <code>false</code> |
| [vpc_connector](variables.tf#L172) | VPC connector configuration. Set create to 'true' if a new connector needs to be created. | <code title="object&#40;&#123;&#10; create &#61; bool&#10; name &#61; string&#10; egress_settings &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [vpc_connector_config](variables.tf#L182) | VPC connector network configuration. Must be provided if new VPC connector is being created. | <code title="object&#40;&#123;&#10; ip_cidr_range &#61; string&#10; network &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
## Outputs

View File

@ -75,7 +75,7 @@ resource "google_cloudfunctions_function" "function" {
source_archive_bucket = local.bucket
source_archive_object = google_storage_bucket_object.bundle.name
labels = var.labels
trigger_http = var.trigger_config == null ? true : null
trigger_http = try(var.trigger_config.v1 == null, true) ? true : null
ingress_settings = var.ingress_settings
build_worker_pool = var.build_worker_pool
@ -85,14 +85,14 @@ resource "google_cloudfunctions_function" "function" {
)
dynamic "event_trigger" {
for_each = var.trigger_config == null ? [] : [""]
for_each = try(var.trigger_config.v1 == null, true) ? [] : [""]
content {
event_type = var.trigger_config.event
resource = var.trigger_config.resource
event_type = var.trigger_config.v1.event
resource = var.trigger_config.v1.resource
dynamic "failure_policy" {
for_each = var.trigger_config.retry == null ? [] : [""]
for_each = var.trigger_config.v1.retry == null ? [] : [""]
content {
retry = var.trigger_config.retry
retry = var.trigger_config.v1.retry
}
}
}
@ -148,13 +148,13 @@ resource "google_cloudfunctions2_function" "function" {
}
}
dynamic "event_trigger" {
for_each = var.trigger_config_v2 == null ? [] : [""]
for_each = try(var.trigger_config.v2 == null, true) ? [] : [""]
content {
trigger_region = var.trigger_config_v2.region
event_type = var.trigger_config_v2.event_type
pubsub_topic = var.trigger_config_v2.pubsub_topic
trigger_region = var.trigger_config.v2.region
event_type = var.trigger_config.v2.event_type
pubsub_topic = var.trigger_config.v2.pubsub_topic
dynamic "event_filters" {
for_each = var.trigger_config_v2.event_filters
for_each = var.trigger_config.v2.event_filters == null ? [] : var.trigger_config.v2.event_filters
iterator = event_filter
content {
attribute = event_filter.attribute
@ -162,8 +162,8 @@ resource "google_cloudfunctions2_function" "function" {
operator = event_filter.operator
}
}
service_account_email = var.trigger_config_v2.service_account_email
retry_policy = var.trigger_config_v2.retry_policy
service_account_email = var.trigger_config.v2.service_account_email
retry_policy = var.trigger_config.v2.retry_policy
}
}
service_config {

View File

@ -61,7 +61,7 @@ variable "function_config" {
entry_point = optional(string, "main")
instances = optional(number, 1)
memory = optional(number, 256) # Memory in MB
runtime = optional(string, "python37")
runtime = optional(string, "python310")
timeout = optional(number, 180)
})
default = {
@ -130,7 +130,7 @@ variable "secrets" {
}
variable "service_account" {
description = "Service account email.service_account Unused if service account is auto-created."
description = "Service account email. Unused if service account is auto-created."
type = string
default = null
}
@ -144,16 +144,12 @@ variable "service_account_create" {
variable "trigger_config" {
description = "Function trigger configuration. Leave null for HTTP trigger."
type = object({
v1 = optional(object({
event = string
resource = string
retry = optional(bool)
})
default = null
}
variable "trigger_config_v2" {
description = "Function trigger configuration. Leave null for HTTP trigger."
type = object({
})),
v2 = optional(object({
region = optional(string)
event_type = optional(string)
pubsub_topic = optional(string)
@ -161,11 +157,16 @@ variable "trigger_config_v2" {
attribute = string
value = string
operator = string
})), [])
})))
service_account_email = optional(string)
retry_policy = optional(string)
}))
})
default = null
validation {
condition = try(((var.trigger_config.v1 == null) != (var.trigger_config.v2 == null)), var.trigger_config == null)
error_message = "Provide configuration for only one generation - either v1 or v2"
}
}
variable "vpc_connector" {