-
Notifications
You must be signed in to change notification settings - Fork 645
/
resource_ibm_en_subscription_email.go
399 lines (335 loc) · 11.9 KB
/
resource_ibm_en_subscription_email.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package eventnotification
import (
"context"
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
en "github.com/IBM/event-notifications-go-admin-sdk/eventnotificationsv1"
"github.com/IBM/go-sdk-core/v5/core"
)
func ResourceIBMEnEmailSubscription() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMEnEmailSubscriptionCreate,
ReadContext: resourceIBMEnEmailSubscriptionRead,
UpdateContext: resourceIBMEnEmailSubscriptionUpdate,
DeleteContext: resourceIBMEnEmailSubscriptionDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"instance_guid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Unique identifier for IBM Cloud Event Notifications instance.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Subscription name.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Subscription description.",
},
"destination_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Destination ID.",
},
"topic_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Topic ID.",
},
"attributes": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"to": {
Type: schema.TypeList,
Optional: true,
Description: "The email id in case of smtp_ibm destination type.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"add_notification_payload": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to add the notification payload to the email.",
},
"reply_to_mail": {
Type: schema.TypeString,
Optional: true,
Description: "The email address to reply to.",
},
"reply_to_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the email address user to reply to.",
},
"from_name": {
Type: schema.TypeString,
Optional: true,
Description: "The email address from which email is sourced.",
},
"invited": {
Type: schema.TypeList,
Optional: true,
Description: "The Email address send the invite to in case of smtp_ibm.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"unsubscribed": {
Type: schema.TypeList,
Optional: true,
Description: "The Email address which should be unsubscribed from smtp_ibm.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"add": {
Type: schema.TypeList,
Optional: true,
Description: "The Email address which should be added to smtp_ibm.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"subscription_id": {
Type: schema.TypeString,
Computed: true,
Description: "Subscription ID.",
},
"destination_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of Destination.",
},
"destination_name": {
Type: schema.TypeString,
Computed: true,
Description: "The Destintion name.",
},
"topic_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the topic.",
},
"from": {
Type: schema.TypeString,
Computed: true,
Description: "From Email ID (it will be displayed only in case of smtp_ibm destination type).",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Last updated time.",
},
},
}
}
func resourceIBMEnEmailSubscriptionCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
enClient, err := meta.(conns.ClientSession).EventNotificationsApiV1()
if err != nil {
return diag.FromErr(err)
}
options := &en.CreateSubscriptionOptions{}
options.SetInstanceID(d.Get("instance_guid").(string))
options.SetName(d.Get("name").(string))
options.SetTopicID(d.Get("topic_id").(string))
options.SetDestinationID(d.Get("destination_id").(string))
if _, ok := d.GetOk("description"); ok {
options.SetDescription(d.Get("description").(string))
}
attributes := EmailattributesMapToAttributes(d.Get("attributes.0").(map[string]interface{}))
options.SetAttributes(&attributes)
result, response, err := enClient.CreateSubscriptionWithContext(context, options)
if err != nil {
return diag.FromErr(fmt.Errorf("CreateSubscriptionWithContext failed %s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s/%s", *options.InstanceID, *result.ID))
return resourceIBMEnEmailSubscriptionRead(context, d, meta)
}
func resourceIBMEnEmailSubscriptionRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
enClient, err := meta.(conns.ClientSession).EventNotificationsApiV1()
if err != nil {
return diag.FromErr(err)
}
options := &en.GetSubscriptionOptions{}
parts, err := flex.SepIdParts(d.Id(), "/")
if err != nil {
return diag.FromErr(err)
}
options.SetInstanceID(parts[0])
options.SetID(parts[1])
result, response, err := enClient.GetSubscriptionWithContext(context, options)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("GetSubscriptionWithContext failed %s\n%s", err, response))
}
if err = d.Set("instance_guid", options.InstanceID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instance_guid: %s", err))
}
if err = d.Set("subscription_id", result.ID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instance_guid: %s", err))
}
if err = d.Set("name", result.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if result.Description != nil {
if err = d.Set("description", result.Description); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting description: %s", err))
}
}
if result.From != nil {
if err = d.Set("from", result.From); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting from: %s", err))
}
}
if err = d.Set("destination_id", result.DestinationID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting destination_id: %s", err))
}
if err = d.Set("destination_type", result.DestinationType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting destination_type: %s", err))
}
if result.DestinationName != nil {
if err = d.Set("destination_name", result.DestinationName); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting destination_name: %s", err))
}
}
if err = d.Set("topic_id", result.TopicID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting topic_id: %s", err))
}
if result.TopicName != nil {
if err = d.Set("topic_name", result.TopicName); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting topic_name: %s", err))
}
}
if err = d.Set("updated_at", result.UpdatedAt); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting updated_at: %s", err))
}
return nil
}
func resourceIBMEnEmailSubscriptionUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
enClient, err := meta.(conns.ClientSession).EventNotificationsApiV1()
if err != nil {
return diag.FromErr(err)
}
options := &en.UpdateSubscriptionOptions{}
parts, err := flex.SepIdParts(d.Id(), "/")
if err != nil {
return diag.FromErr(err)
}
options.SetInstanceID(parts[0])
options.SetID(parts[1])
if ok := d.HasChanges("name", "description", "attributes"); ok {
options.SetName(d.Get("name").(string))
if _, ok := d.GetOk("description"); ok {
options.SetDescription(d.Get("description").(string))
}
attributes := EmailattributesupdateMapToAttributes(d.Get("attributes.0").(map[string]interface{}))
options.SetAttributes(&attributes)
_, response, err := enClient.UpdateSubscriptionWithContext(context, options)
if err != nil {
return diag.FromErr(fmt.Errorf("UpdateSubscriptionWithContext failed %s\n%s", err, response))
}
return resourceIBMEnEmailSubscriptionRead(context, d, meta)
}
return nil
}
func resourceIBMEnEmailSubscriptionDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
enClient, err := meta.(conns.ClientSession).EventNotificationsApiV1()
if err != nil {
return diag.FromErr(err)
}
options := &en.DeleteSubscriptionOptions{}
parts, err := flex.SepIdParts(d.Id(), "/")
if err != nil {
return diag.FromErr(err)
}
options.SetInstanceID(parts[0])
options.SetID(parts[1])
response, err := enClient.DeleteSubscriptionWithContext(context, options)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("DeleteSubscriptionWithContext failed %s\n%s", err, response))
}
d.SetId("")
return nil
}
func EmailattributesMapToAttributes(attributeMap map[string]interface{}) en.SubscriptionCreateAttributes {
attributesCreate := en.SubscriptionCreateAttributes{}
if attributeMap["to"] != nil {
to := []string{}
for _, toItem := range attributeMap["to"].([]interface{}) {
to = append(to, toItem.(string))
}
attributesCreate.To = to
}
if attributeMap["add_notification_payload"] != nil {
attributesCreate.AddNotificationPayload = core.BoolPtr(attributeMap["add_notification_payload"].(bool))
}
if attributeMap["reply_to_mail"] != nil {
attributesCreate.ReplyToMail = core.StringPtr(attributeMap["reply_to_mail"].(string))
}
if attributeMap["reply_to_name"] != nil {
attributesCreate.ReplyToName = core.StringPtr(attributeMap["reply_to_name"].(string))
}
if attributeMap["from_name"] != nil {
attributesCreate.FromName = core.StringPtr(attributeMap["from_name"].(string))
}
return attributesCreate
}
func EmailattributesupdateMapToAttributes(attributeMap map[string]interface{}) en.SubscriptionUpdateAttributesEmailUpdateAttributes {
updateattributes := en.SubscriptionUpdateAttributesEmailUpdateAttributes{}
addemail := new(en.EmailUpdateAttributesTo)
if attributeMap["add"] != nil {
to := []string{}
for _, toItem := range attributeMap["add"].([]interface{}) {
to = append(to, toItem.(string))
}
addemail.Add = to
}
updateattributes.To = addemail
if attributeMap["add_notification_payload"] != nil {
updateattributes.AddNotificationPayload = core.BoolPtr(attributeMap["add_notification_payload"].(bool))
}
if attributeMap["reply_to_mail"] != nil {
updateattributes.ReplyToMail = core.StringPtr(attributeMap["reply_to_mail"].(string))
}
if attributeMap["reply_to_name"] != nil {
updateattributes.ReplyToName = core.StringPtr(attributeMap["reply_to_name"].(string))
}
if attributeMap["from_name"] != nil {
updateattributes.FromName = core.StringPtr(attributeMap["from_name"].(string))
}
if attributeMap["invited"] != nil {
invited := []string{}
for _, invitedItem := range attributeMap["invited"].([]interface{}) {
invited = append(invited, invitedItem.(string))
}
updateattributes.Invited = invited
}
unsubscribed := new(en.EmailUpdateAttributesUnsubscribed)
if attributeMap["unsubscribed"] != nil {
unsubscribe := []string{}
for _, unsubscribeItem := range attributeMap["unsubscribed"].([]interface{}) {
unsubscribe = append(unsubscribe, unsubscribeItem.(string))
}
addemail.Remove = unsubscribe
unsubscribed.Remove = unsubscribe
}
updateattributes.Unsubscribed = unsubscribed
return updateattributes
}