Add schemas to Pubsub topic module.
Pubsub topics can now have schemas (https://cloud.google.com/pubsub/docs/admin#schemas). This PR adds an option to set the schema settings and create a new optional resource of type `google_pubsub_schema` attached to the `google_pubsub_topic`.
This commit is contained in:
parent
f4960dd8fe
commit
54e9738c39
|
@ -1,6 +1,6 @@
|
||||||
# Google Cloud Pub/Sub Module
|
# Google Cloud Pub/Sub Module
|
||||||
|
|
||||||
This module allows managing a single Pub/Sub topic, including multiple subscriptions and IAM bindings at the topic and subscriptions levels.
|
This module allows managing a single Pub/Sub topic, including multiple subscriptions and IAM bindings at the topic and subscriptions levels, as well as schemas.
|
||||||
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
@ -20,6 +20,38 @@ module "pubsub" {
|
||||||
# tftest modules=1 resources=3
|
# tftest modules=1 resources=3
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Topic with schema
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
module "topic_with_schema" {
|
||||||
|
source = "./fabric/modules/pubsub"
|
||||||
|
project_id = "my-project"
|
||||||
|
name = "my-topic"
|
||||||
|
schema = {
|
||||||
|
msg_encoding = "JSON"
|
||||||
|
schema_type = "AVRO"
|
||||||
|
definition = jsonencode({
|
||||||
|
"type" = "record",
|
||||||
|
"name" = "Avro",
|
||||||
|
"fields" : [{
|
||||||
|
"name" = "StringField",
|
||||||
|
"type" = "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" = "FloatField",
|
||||||
|
"type" = "float"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" = "BooleanField",
|
||||||
|
"type" = "boolean"
|
||||||
|
},
|
||||||
|
]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# tftest modules=1 resources=3
|
||||||
|
```
|
||||||
|
|
||||||
### Subscriptions
|
### Subscriptions
|
||||||
|
|
||||||
Subscriptions are defined with the `subscriptions` variable, allowing optional configuration of per-subscription defaults. Push subscriptions need extra configuration, shown in the following example.
|
Subscriptions are defined with the `subscriptions` variable, allowing optional configuration of per-subscription defaults. Push subscriptions need extra configuration, shown in the following example.
|
||||||
|
@ -104,6 +136,7 @@ module "pubsub" {
|
||||||
| [message_retention_duration](variables.tf#L62) | Minimum duration to retain a message after it is published to the topic. | <code>string</code> | | <code>null</code> |
|
| [message_retention_duration](variables.tf#L62) | Minimum duration to retain a message after it is published to the topic. | <code>string</code> | | <code>null</code> |
|
||||||
| [push_configs](variables.tf#L78) | Push subscription configurations. | <code title="map(object({ attributes = map(string) endpoint = string oidc_token = object({ audience = string service_account_email = string }) }))">map(object({…}))</code> | | <code>{}</code> |
|
| [push_configs](variables.tf#L78) | Push subscription configurations. | <code title="map(object({ attributes = map(string) endpoint = string oidc_token = object({ audience = string service_account_email = string }) }))">map(object({…}))</code> | | <code>{}</code> |
|
||||||
| [regions](variables.tf#L91) | List of regions used to set persistence policy. | <code>list(string)</code> | | <code>[]</code> |
|
| [regions](variables.tf#L91) | List of regions used to set persistence policy. | <code>list(string)</code> | | <code>[]</code> |
|
||||||
|
| [schema](variables.tf#L118) | Topic schema. If set, all messages in this topic should follow this schema. | <code title="object({ schema_type = string definition = string msg_encoding = optional(string, "ENCODING_UNSPECIFIED") })">object({…})</code> | | <code>null</code> |
|
||||||
| [subscription_iam](variables.tf#L97) | IAM bindings for subscriptions in {SUBSCRIPTION => {ROLE => [MEMBERS]}} format. | <code>map(map(list(string)))</code> | | <code>{}</code> |
|
| [subscription_iam](variables.tf#L97) | IAM bindings for subscriptions in {SUBSCRIPTION => {ROLE => [MEMBERS]}} format. | <code>map(map(list(string)))</code> | | <code>{}</code> |
|
||||||
| [subscriptions](variables.tf#L103) | Topic subscriptions. Also define push configs for push subscriptions. If options is set to null subscription defaults will be used. Labels default to topic labels if set to null. | <code title="map(object({ labels = map(string) options = object({ ack_deadline_seconds = number message_retention_duration = string retain_acked_messages = bool expiration_policy_ttl = string filter = string }) }))">map(object({…}))</code> | | <code>{}</code> |
|
| [subscriptions](variables.tf#L103) | Topic subscriptions. Also define push configs for push subscriptions. If options is set to null subscription defaults will be used. Labels default to topic labels if set to null. | <code title="map(object({ labels = map(string) options = object({ ack_deadline_seconds = number message_retention_duration = string retain_acked_messages = bool expiration_policy_ttl = string filter = string }) }))">map(object({…}))</code> | | <code>{}</code> |
|
||||||
|
|
||||||
|
@ -112,6 +145,8 @@ module "pubsub" {
|
||||||
| name | description | sensitive |
|
| name | description | sensitive |
|
||||||
|---|---|:---:|
|
|---|---|:---:|
|
||||||
| [id](outputs.tf#L17) | Topic id. | |
|
| [id](outputs.tf#L17) | Topic id. | |
|
||||||
|
| [schema](outputs.tf#L59) | Schema resource. | |
|
||||||
|
| [schema_id](outputs.tf#L51) | Schema resource id. | |
|
||||||
| [subscription_id](outputs.tf#L25) | Subscription ids. | |
|
| [subscription_id](outputs.tf#L25) | Subscription ids. | |
|
||||||
| [subscriptions](outputs.tf#L35) | Subscription resources. | |
|
| [subscriptions](outputs.tf#L35) | Subscription resources. | |
|
||||||
| [topic](outputs.tf#L43) | Topic resource. | |
|
| [topic](outputs.tf#L43) | Topic resource. | |
|
||||||
|
|
|
@ -35,6 +35,14 @@ locals {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "google_pubsub_schema" "default" {
|
||||||
|
count = var.schema == null ? 0 : 1
|
||||||
|
name = format("%s-%s", var.name, "schema")
|
||||||
|
type = var.schema.schema_type
|
||||||
|
definition = var.schema.definition
|
||||||
|
project = var.project_id
|
||||||
|
}
|
||||||
|
|
||||||
resource "google_pubsub_topic" "default" {
|
resource "google_pubsub_topic" "default" {
|
||||||
project = var.project_id
|
project = var.project_id
|
||||||
name = var.name
|
name = var.name
|
||||||
|
@ -48,6 +56,15 @@ resource "google_pubsub_topic" "default" {
|
||||||
allowed_persistence_regions = var.regions
|
allowed_persistence_regions = var.regions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
depends_on = [google_pubsub_schema.default]
|
||||||
|
dynamic "schema_settings" {
|
||||||
|
for_each = var.schema == null ? [] : [""]
|
||||||
|
content {
|
||||||
|
schema = google_pubsub_schema.default[0].id
|
||||||
|
encoding = var.schema.msg_encoding
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "google_pubsub_topic_iam_binding" "default" {
|
resource "google_pubsub_topic_iam_binding" "default" {
|
||||||
|
|
|
@ -47,3 +47,19 @@ output "topic" {
|
||||||
google_pubsub_topic_iam_binding.default
|
google_pubsub_topic_iam_binding.default
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output "schema_id" {
|
||||||
|
description = "Schema resource id."
|
||||||
|
value = google_pubsub_schema.default[0].id
|
||||||
|
depends_on = [
|
||||||
|
google_pubsub_schema.default
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "schema" {
|
||||||
|
description = "Schema resource."
|
||||||
|
value = google_pubsub_schema.default[0]
|
||||||
|
depends_on = [
|
||||||
|
google_pubsub_schema.default
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
|
@ -114,3 +114,13 @@ variable "subscriptions" {
|
||||||
}))
|
}))
|
||||||
default = {}
|
default = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "schema" {
|
||||||
|
description = "Topic schema. If set, all messages in this topic should follow this schema."
|
||||||
|
type = object({
|
||||||
|
schema_type = string
|
||||||
|
definition = string
|
||||||
|
msg_encoding = optional(string, "ENCODING_UNSPECIFIED")
|
||||||
|
})
|
||||||
|
default = null
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue