Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for configuring pubsub object notifications #48

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Functional examples are included in the
| names | Bucket name suffixes. | list(string) | n/a | yes |
| prefix | Prefix used to generate the bucket name. | string | n/a | yes |
| project\_id | Bucket project id. | string | n/a | yes |
| pubsub\_topics | Map of bucket=>topic to configure pubsub notifications. | map(string) | `<map>` | no |
| set\_admin\_roles | Grant roles/storage.objectAdmin role to admins and bucket_admins. | bool | `"false"` | no |
| set\_creator\_roles | Grant roles/storage.objectCreator role to creators and bucket_creators. | bool | `"false"` | no |
| set\_viewer\_roles | Grant roles/storage.objectViewer role to viewers and bucket_viewers. | bool | `"false"` | no |
Expand Down
18 changes: 18 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,21 @@ resource "google_storage_bucket_iam_binding" "viewers" {
),
)
}

locals {
bucket_names = zipmap(var.names, slice(google_storage_bucket.buckets[*].name, 0, length(var.names)))
notification_configs = {
for name, topic in var.pubsub_topics :
name => {
bucket = local.bucket_names[name]
topic = topic
}
}
}

module "notifications" {
source = "./modules/notifications"

project_id = var.project_id
bucket_configs = local.notification_configs
}
55 changes: 55 additions & 0 deletions modules/notifications/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

locals {
# [for o in var.list : o.interfaces[0].name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you meant to remove this?

topics = [for o in var.bucket_configs : o.topic]
}

# Create the notifications
resource "google_storage_notification" "notifications" {
for_each = var.bucket_configs

# project_id = var.project_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you meant to remove this?

bucket = lookup(each.value, "bucket")
payload_format = "JSON_API_V1"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the google_storage_notification resource docs I think object_name_prefix and custom_attributes seem useful to be user configurable.

topic = lookup(each.value, "topic")
event_types = var.event_types

depends_on = [module.pubsub-iam-bindings.members]
}

# Enable IAM
data "google_storage_project_service_account" "gcs_account" {
project = var.project_id
}

module "pubsub-iam-bindings" {
source = "terraform-google-modules/iam/google//modules/pubsub_topics_iam"
version = "~> 5.0"

project = var.project_id

pubsub_topics = local.topics
mode = "additive"

bindings = {
"roles/pubsub.publisher" = [
"serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"
]
}
}
35 changes: 35 additions & 0 deletions modules/notifications/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "Bucket project id."
type = string
}

variable "bucket_configs" {
type = map(object({
bucket = string
topic = string
}))
description = "Map of buckets to the notification configs they should use."
default = {}
}

variable "event_types" {
type = list(string)
description = "Event types to configure notifications for. Default all."
default = null
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ output "buckets" {

output "names" {
description = "Bucket names."
value = zipmap(var.names, slice(google_storage_bucket.buckets[*].name, 0, length(var.names)))
value = local.bucket_names
}

output "urls" {
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ variable "lifecycle_rules" {
description = "List of lifecycle rules to configure. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket.html#lifecycle_rule except condition.matches_storage_class should be a comma delimited string."
default = []
}

variable "pubsub_topics" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
variable "pubsub_topics" {
variable "pubsub_notification_topics" {

type = map(string)
description = "Map of bucket=>topic to configure pubsub notifications."
default = {}
}