forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_storage_notification.go
192 lines (163 loc) · 6.27 KB
/
resource_storage_notification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package google
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/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: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of the bucket.`,
},
"payload_format": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"JSON_API_V1", "NONE"}, false),
Description: `The desired content of the Payload. One of "JSON_API_V1" or "NONE".`,
},
"topic": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `The Cloud Pub/Sub topic to which this subscription publishes. Expects either the topic name, assumed to belong to the default GCP provider project, or the project-level name, i.e. projects/my-gcp-project/topics/my-topic or my-topic. If the project is not set in the provider, you will need to use the project-level name.`,
},
"custom_attributes": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: ` A set of key/value attribute pairs to attach to each Cloud Pub/Sub message published for this notification subscription`,
},
"event_types": {
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),
},
Description: `List of event type filters for this notification config. If not specified, Cloud Storage will send notifications for all event types. The valid types are: "OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE", "OBJECT_DELETE", "OBJECT_ARCHIVE"`,
},
"object_name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies a prefix path filter for this notification config. Cloud Storage will only send notifications for objects in this bucket whose names begin with the specified prefix.`,
},
"notification_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the created notification.`,
},
"self_link": {
Type: schema.TypeString,
Computed: true,
Description: `The URI of the created resource.`,
},
},
UseJSONNumber: true,
}
}
func resourceStorageNotificationCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
bucket := d.Get("bucket").(string)
topicName := d.Get("topic").(string)
computedTopicName := getComputedTopicName("", topicName)
if computedTopicName != topicName {
project, err := getProject(d, config)
if err != nil {
return err
}
computedTopicName = getComputedTopicName(project, topicName)
}
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.NewStorageClient(userAgent).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)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
bucket, notificationID := resourceStorageNotificationParseID(d.Id())
res, err := config.NewStorageClient(userAgent).Notifications.Get(bucket, notificationID).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Notification configuration %s for bucket %s", notificationID, bucket))
}
if err := d.Set("bucket", bucket); err != nil {
return fmt.Errorf("Error setting bucket: %s", err)
}
if err := d.Set("payload_format", res.PayloadFormat); err != nil {
return fmt.Errorf("Error setting payload_format: %s", err)
}
if err := d.Set("topic", res.Topic); err != nil {
return fmt.Errorf("Error setting topic: %s", err)
}
if err := d.Set("object_name_prefix", res.ObjectNamePrefix); err != nil {
return fmt.Errorf("Error setting object_name_prefix: %s", err)
}
if err := d.Set("event_types", res.EventTypes); err != nil {
return fmt.Errorf("Error setting event_types: %s", err)
}
if err := d.Set("notification_id", notificationID); err != nil {
return fmt.Errorf("Error setting notification_id: %s", err)
}
if err := d.Set("self_link", res.SelfLink); err != nil {
return fmt.Errorf("Error setting self_link: %s", err)
}
if err := d.Set("custom_attributes", res.CustomAttributes); err != nil {
return fmt.Errorf("Error setting custom_attributes: %s", err)
}
return nil
}
func resourceStorageNotificationDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
bucket, notificationID := resourceStorageNotificationParseID(d.Id())
err = config.NewStorageClient(userAgent).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 resourceStorageNotificationParseID(id string) (string, string) {
//bucket, NotificationID
parts := strings.Split(id, "/")
return parts[0], parts[2]
}