From 4c9e224d4922abc77e01f95f7e9332503984a212 Mon Sep 17 00:00:00 2001 From: Eugene Miretsky Date: Wed, 9 Feb 2022 15:40:51 -0500 Subject: [PATCH] feat: add schema support (#83) --- README.md | 1 + examples/simple/main.tf | 6 ++++++ main.tf | 19 +++++++++++++++++++ test/integration/pubsub/controls/pubsub.rb | 13 +++++++++++-- variables.tf | 11 +++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c776740..bf6faef 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ module "pubsub" { | project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | | pull\_subscriptions | The list of the pull subscriptions | `list(map(string))` | `[]` | no | | push\_subscriptions | The list of the push subscriptions | `list(map(string))` | `[]` | no | +| schema | Schema for the topic |
object({
name = string
type = string
definition = string
encoding = string
})
| `null` | no | | subscription\_labels | A map of labels to assign to every Pub/Sub subscription | `map(string)` | `{}` | no | | topic | The Pub/Sub topic name | `string` | n/a | yes | | topic\_kms\_key\_name | The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. | `string` | `null` | no | diff --git a/examples/simple/main.tf b/examples/simple/main.tf index 1a738f6..e54d163 100644 --- a/examples/simple/main.tf +++ b/examples/simple/main.tf @@ -42,4 +42,10 @@ module "pubsub" { }, ] + schema = { + name = "example" + type = "AVRO" + encoding = "JSON" + definition = "{\n \"type\" : \"record\",\n \"name\" : \"Avro\",\n \"fields\" : [\n {\n \"name\" : \"StringField\",\n \"type\" : \"string\"\n },\n {\n \"name\" : \"IntField\",\n \"type\" : \"int\"\n }\n ]\n}\n" + } } diff --git a/main.tf b/main.tf index dfcca2c..12156b0 100644 --- a/main.tf +++ b/main.tf @@ -23,6 +23,15 @@ locals { pubsub_svc_account_email = "service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com" } +resource "google_pubsub_schema" "schema" { + count = var.schema != null ? 1 : 0 + project = var.project_id + name = var.schema.name + type = var.schema.type + definition = var.schema.definition +} + + resource "google_project_iam_member" "token_creator_binding" { count = var.grant_token_creator ? 1 : 0 project = var.project_id @@ -94,6 +103,15 @@ resource "google_pubsub_topic" "topic" { allowed_persistence_regions = message_storage_policy.key == "allowed_persistence_regions" ? message_storage_policy.value : null } } + + dynamic "schema_settings" { + for_each = var.schema != null ? [var.schema] : [] + content { + schema = google_pubsub_schema.schema[0].id + encoding = lookup(schema_settings.value, "encoding", null) + } + } + depends_on = [google_pubsub_schema.schema] } resource "google_pubsub_subscription" "push_subscriptions" { @@ -258,3 +276,4 @@ resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_v google_pubsub_subscription.pull_subscriptions, ] } + diff --git a/test/integration/pubsub/controls/pubsub.rb b/test/integration/pubsub/controls/pubsub.rb index 8fe3718..71f0d75 100644 --- a/test/integration/pubsub/controls/pubsub.rb +++ b/test/integration/pubsub/controls/pubsub.rb @@ -12,8 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -project_id = attribute('project_id') -topic = attribute('topic_name') +project_id = attribute('project_id') +topic = attribute('topic_name') +schema_name = 'example' + describe command("gcloud --project='#{project_id}' pubsub topics describe #{topic}") do its(:exit_status) { should be_zero } @@ -37,3 +39,10 @@ it { expect(stdout).to include(pushConfig: { pushEndpoint: "https://#{project_id}.appspot.com/" }) } it { expect(stdout).to include(ackDeadlineSeconds: 20) } end + +describe command("gcloud --project='#{project_id}' pubsub schemas describe #{schema_name}") do + its(:exit_status) { should be_zero } + it { expect(subject.stdout).to match(%r{name: projects/#{project_id}/schemas/example}) } +end + + diff --git a/variables.tf b/variables.tf index db8e933..7108c33 100644 --- a/variables.tf +++ b/variables.tf @@ -76,3 +76,14 @@ variable "grant_token_creator" { description = "Specify true if you want to add token creator role to the default Pub/Sub SA" default = true } + +variable "schema" { + type = object({ + name = string + type = string + definition = string + encoding = string + }) + description = "Schema for the topic" + default = null +}