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

Storage Notification Resource #1033

Merged
merged 24 commits into from
Feb 5, 2018
Merged

Conversation

ishashchuk
Copy link
Contributor

Support for GSC Notifications issue 971

API reference here

The resource closely mimics the API functionality. if no event_types are passed, ALL events will be publishing to pubsub topic. If event_types are passed, only specific events will be published

There is no update functionality; updating the notification will always result in new notification config

@emilymye emilymye requested a review from danawillow February 1, 2018 18:28
Copy link
Contributor

@emilymye emilymye left a comment

Choose a reason for hiding this comment

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

Thanks for adding this!

},
},

"prefix": &schema.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

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

We tend to prefer having this match the resource exactly. Would it be possible to rename this as "object_name_prefix"?

}

res, err := config.clientStorage.Notifications.Insert(bucket, storageNotification).Do()

Copy link
Contributor

Choose a reason for hiding this comment

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

nit - can you remove the newline here? this is more personal preference but generally we don't have an empty line between _, err := ... and if err != nil {} blocks

notificationID := resourceStorageNotificationParseID(d.Id())

res, err := config.clientStorage.Notifications.Get(bucket, notificationID).Do()

Copy link
Contributor

Choose a reason for hiding this comment

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

nit - newline

Type: schema.TypeString,
Required: true,
ForceNew: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a DiffSuppressFunc here? Example in PubSub topic resource

EventTypes: convertStringSet(d.Get("event_types").(*schema.Set)),
ObjectNamePrefix: d.Get("prefix").(string),
PayloadFormat: d.Get("payload_format").(string),
Topic: d.Get("topic").(string),
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be best to do some regex check here and reformat if needed. It's likely someone would pass in a topic self-link or relative name ("projects/%s/topics/%s) here.

}

d.Set("payload_format", res.PayloadFormat)
d.Set("topic", strings.SplitAfter(res.Topic, "//pubsub.googleapis.com/")[1])
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this will happen anytime soon, but if the topic doesn't have this value anymore, SplitAfter will return only one elem here and this will panic. You can also use the getRelativePath self-link helper func.

notificationID := resourceStorageNotificationParseID(d.Id())

err := config.clientStorage.Notifications.Delete(bucket, notificationID).Do()

Copy link
Contributor

Choose a reason for hiding this comment

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

nit - newline

}

func resourceStorageNotificationImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be nicer if we just have this in resourceStorageNotificationParseID, and have resourceStorageNotificationParseID return both bucket and notificationConfig id.


- - -

* `custom_attrubutes` - (Optional) A set of key/value attribute pairs to attach to each Cloud PubSub message published for this notification subscription
Copy link
Contributor

Choose a reason for hiding this comment

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

s/custom_attrubutes/custom_attributes

func resourceStorageNotificationRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

bucket := d.Get("bucket").(string)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the bucket is already part of the id, you could avoid this d.Get here by using the return value from the parse function, which would then allow you to use an ImportStatePassthrough

return handleNotFoundError(err, d, fmt.Sprintf("Notification configuration %s for bucket %s", notificationID, bucket))
}

topic, _ := getRelativePath(res.Topic)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we have the DiffSuppressFunc for self link or resource name, we should be fine just storing this the way it's returned from the API (either way is really fine, but since that's the case may as well go with the simpler solution)


func testGoogleStorageNotificationBasic(bucketName, topicName, topic string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we try to have these all the way on the left (mostly a personal preference thing, it just makes it a bit easier to copy/paste into our own configs later, feel free to ignore)

// Otherwise notification configuration won't work
resource "google_pubsub_topic_iam_binding" "binding" {
topic = "${google_pubsub_topic.topic.name}"
role = "roles/pubsub.publisher"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: align =s (here and elsewhere)

)

var (
gscServiceAccount = fmt.Sprintf("serviceAccount:%[email protected]", os.Getenv("GOOGLE_PROJECT"))
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be gcs? or does gsc stand for something?


bucket := rs.Primary.Attributes["bucket"]

_, notificationID := resourceStorageNotificationParseID(rs.Primary.ID)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the bucket is in the id also, you could just do bucket, notificationID here


func testAccCheckStorageNotificationCheckEventType(notification *storage.Notification, eventTypes []string) resource.TestCheckFunc {
return func(s *terraform.State) error {

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: extra newline

name = "default_topic"
}

//In order to enable notifications,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can you add a space after each //?

```
$ terraform import google_storage_notification.notification default_bucket/notificationConfigs/102
```

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: there are a few extra lines here that can be deleted


## Import

Storage notifications can be imported using the notification `id` in the format `<bucket_name>/notificationConfigs/<id>` e.g.
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder whether it makes more sense to just have it be <bucket_name>/<id> that way people don't have to bother typing out "/notificationConfigs/" (unless there's somewhere where that string tends to be copy/pasted from). What do you think?

Also, do you think most people will generally know the id of the notification? If there can only be one notification per topic per bucket then you could use the topic name as something more easily identifiable. If not though then it probably has to be the id.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was debating on the way to id them too.. The reason I decided to go with the <bucket_name>/notificationConfigs/ is because that's how they are being returned in CLI util:

gsutil  notifications list gs://test_bucket/
projects/_/buckets/test_bucket/notificationConfigs/89
        Cloud Pub/Sub topic: projects/sandbox/topics/test_topic

It seems to me that this would be one of the quickest and user friendly ways to get the list of notifications on the bucket, in case they need to be imported and the id can be just copy/pasted. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, that's a great reason. Thanks for the explanation!

return nil
}

func resourceStorageNotificationImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function can be removed now (you can just set the importer to schema.ImportStatePassthrough, see other resources for examples), and then you can do the d.Set call inside the read fn


## Import

Storage notifications can be imported using the notification `id` in the format `<bucket_name>/notificationConfigs/<id>` e.g.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, that's a great reason. Thanks for the explanation!

@emilymye
Copy link
Contributor

emilymye commented Feb 5, 2018

I'm going to approve, but I'd wait for Dana's approval before actually merging.

Copy link
Contributor

@danawillow danawillow left a comment

Choose a reason for hiding this comment

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

Thanks @ishashchuk, looks good!

(I'll go ahead and merge this @emilymye since I'm already here)

@danawillow danawillow merged commit 7257b9a into hashicorp:master Feb 5, 2018
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
* Storage Default Object ACL resource

* Fixed the doc

* Renamed the resource id. Log change

* Complying with go vet

* Changes for review

* link to default object acl docs in sidebar

* Support for GCS notifications

* docs for storage notification

* docs for storage notification

* Clarified the doc

* Doc modifications

* Addressing requested changes from review

* Addressing requested changes from review

* Using ImportStatePassthrough
modular-magician added a commit to modular-magician/terraform-provider-google that referenced this pull request Sep 27, 2019
@ghost
Copy link

ghost commented Mar 29, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants