Skip to content

Commit

Permalink
feat: add schema support (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenemiretsky authored Feb 9, 2022
1 parent b2c9ed1 commit 4c9e224
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | <pre>object({<br> name = string<br> type = string<br> definition = string<br> encoding = string<br> })</pre> | `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 |
Expand Down
6 changes: 6 additions & 0 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
19 changes: 19 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -258,3 +276,4 @@ resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_v
google_pubsub_subscription.pull_subscriptions,
]
}

13 changes: 11 additions & 2 deletions test/integration/pubsub/controls/pubsub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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


11 changes: 11 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4c9e224

Please sign in to comment.