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 notifications #3

Merged
merged 6 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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 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,
},

"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),
},
},

"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)

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

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 := d.Get("bucket").(string)
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", strings.SplitAfter(res.Topic, "//pubsub.googleapis.com/")[1])
d.Set("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 := d.Get("bucket").(string)

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) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 3 || parts[1] != "notificationConfigs" {
return nil, fmt.Errorf("Invalid notification configuration specifier. Expecting {bucket}/notificationConfigs/{id}")
}

d.Set("bucket", parts[0])

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

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

func resourceStorageNotificationParseID(id string) string {
//NotificationID
parts := strings.Split(id, "/")
return parts[2]
}
Loading