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
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ba3c60c
Storage Default Object ACL resource
ishashchuk Jan 18, 2018
19912ec
Fixed the doc
ishashchuk Jan 19, 2018
6810e53
Renamed the resource id. Log change
ishashchuk Jan 19, 2018
2c80aee
Merged latest upstream changes into the branch
ishashchuk Jan 22, 2018
58c7e0e
Merge pull request #1 from wayfair/storage_default_acl
amoiseiev Jan 22, 2018
e7cebf7
Complying with go vet
ishashchuk Jan 22, 2018
5e35381
Changes for review
ishashchuk Jan 25, 2018
c702a73
Merge remote-tracking branch 'upstream/master'
ishashchuk Jan 25, 2018
bca2b1b
Merge pull request #2 from wayfair/ishashchuk_storage_defaul_acl
amoiseiev Jan 25, 2018
a89bc5d
link to default object acl docs in sidebar
danawillow Jan 25, 2018
61d2db7
Support for GCS notifications
ishashchuk Jan 30, 2018
bff7fd5
Merge remote-tracking branch 'upstream/master'
ishashchuk Jan 30, 2018
5c1e5b9
Merge branch 'master' into ishashchuk_storage_notifications
ishashchuk Jan 30, 2018
14ebfc9
docs for storage notification
ishashchuk Jan 31, 2018
7f5951c
docs for storage notification
ishashchuk Jan 31, 2018
79a38d9
Clarified the doc
ishashchuk Jan 31, 2018
d224374
Doc modifications
ishashchuk Jan 31, 2018
d7ec385
Merge pull request #3 from wayfair/ishashchuk_storage_notifications
amoiseiev Jan 31, 2018
f153c0f
Addressing requested changes from review
ishashchuk Feb 2, 2018
e5a0d27
Merge pull request #4 from wayfair/ishashchuk_storage_notifications
amoiseiev Feb 2, 2018
0d00aff
Addressing requested changes from review
ishashchuk Feb 5, 2018
93dd896
Merge pull request #6 from wayfair/ishashchuk_notifications
amoiseiev Feb 5, 2018
4794d18
Using ImportStatePassthrough
ishashchuk Feb 5, 2018
c4b6a26
Merge pull request #7 from wayfair/ishashchuk_notifications
amoiseiev Feb 5, 2018
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 google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func Provider() terraform.ResourceProvider {
"google_storage_bucket_object": resourceStorageBucketObject(),
"google_storage_object_acl": resourceStorageObjectAcl(),
"google_storage_default_object_acl": resourceStorageDefaultObjectAcl(),
"google_storage_notification": resourceStorageNotification(),
},

ConfigureFunc: providerConfigure,
Expand Down
157 changes: 157 additions & 0 deletions google/resource_storage_notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"google.golang.org/api/storage/v1"
)

func resourceStorageNotification() *schema.Resource {
return &schema.Resource{
Create: resourceStorageNotificationCreate,
Read: resourceStorageNotificationRead,
Delete: resourceStorageNotificationDelete,
Importer: &schema.ResourceImporter{
State: resourceStorageNotificationImportState,
},

Schema: map[string]*schema.Schema{
"bucket": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"payload_format": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"JSON_API_V1", "NONE"}, false),
},

"topic": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
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


"custom_attributes": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"event_types": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE", "OBJECT_DELETE", "OBJECT_ARCHIVE"},
false),
},
},

"object_name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

bucket := d.Get("bucket").(string)

project, err := getProject(d, config)
if err != nil {
return err
}

computedTopicName := getComputedTopicName(project, d.Get("topic").(string))

storageNotification := &storage.Notification{
CustomAttributes: expandStringMap(d, "custom_attributes"),
EventTypes: convertStringSet(d.Get("event_types").(*schema.Set)),
ObjectNamePrefix: d.Get("object_name_prefix").(string),
PayloadFormat: d.Get("payload_format").(string),
Topic: computedTopicName,
}

res, err := config.clientStorage.Notifications.Insert(bucket, storageNotification).Do()
if err != nil {
return fmt.Errorf("Error creating notification config for bucket %s: %v", bucket, err)
}

d.SetId(fmt.Sprintf("%s/notificationConfigs/%s", bucket, res.Id))

return resourceStorageNotificationRead(d, meta)
}

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

bucket, notificationID := resourceStorageNotificationParseID(d.Id())

res, err := config.clientStorage.Notifications.Get(bucket, notificationID).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Notification configuration %s for bucket %s", notificationID, bucket))
}

d.Set("payload_format", res.PayloadFormat)
d.Set("topic", res.Topic)
d.Set("object_name_prefix", res.ObjectNamePrefix)
d.Set("event_types", res.EventTypes)
d.Set("self_link", res.SelfLink)
d.Set("custom_attributes", res.CustomAttributes)

return nil
}

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

bucket, notificationID := resourceStorageNotificationParseID(d.Id())

err := config.clientStorage.Notifications.Delete(bucket, notificationID).Do()
if err != nil {
return fmt.Errorf("Error deleting notification configuration %s for bucket %s: %v", notificationID, bucket, err)
}

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

bucket, _ := resourceStorageNotificationParseID(d.Id())

d.Set("bucket", bucket)

if err := resourceStorageNotificationRead(d, meta); err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}

func resourceStorageNotificationParseID(id string) (string, string) {
//bucket, NotificationID
parts := strings.Split(id, "/")

return parts[0], parts[2]
}
Loading