Skip to content

Commit 5bbc1b4

Browse files
Divya-Singh1693hkantare
authored andcommitted
EN template update bug fix, support spf and dkim verification method for custom email destination
1 parent 9a3677b commit 5bbc1b4

8 files changed

+247
-38
lines changed

ibm/service/eventnotification/data_source_ibm_en_destination_custom_email.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,54 @@ func DataSourceIBMEnCustomEmailDestination() *schema.Resource {
6767
Computed: true,
6868
Description: "The custom doamin",
6969
},
70+
"dkim": &schema.Schema{
71+
Type: schema.TypeList,
72+
Computed: true,
73+
Description: "The DKIM attributes.",
74+
Elem: &schema.Resource{
75+
Schema: map[string]*schema.Schema{
76+
"public_key": &schema.Schema{
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "dkim public key.",
80+
},
81+
"selector": &schema.Schema{
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "dkim selector.",
85+
},
86+
"verification": &schema.Schema{
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "dkim verification.",
90+
},
91+
},
92+
},
93+
},
94+
"spf": &schema.Schema{
95+
Type: schema.TypeList,
96+
Computed: true,
97+
Description: "The SPF attributes.",
98+
Elem: &schema.Resource{
99+
Schema: map[string]*schema.Schema{
100+
"txt_name": &schema.Schema{
101+
Type: schema.TypeString,
102+
Computed: true,
103+
Description: "spf text name.",
104+
},
105+
"txt_value": &schema.Schema{
106+
Type: schema.TypeString,
107+
Computed: true,
108+
Description: "spf text value.",
109+
},
110+
"verification": &schema.Schema{
111+
Type: schema.TypeString,
112+
Computed: true,
113+
Description: "spf verification.",
114+
},
115+
},
116+
},
117+
},
70118
},
71119
},
72120
},
@@ -140,7 +188,7 @@ func dataSourceIBMEnCustomEmailDestinationRead(context context.Context, d *schem
140188
}
141189

142190
if result.Config != nil {
143-
err = d.Set("config", enSlackDestinationFlattenConfig(*result.Config))
191+
err = d.Set("config", enCustomEmailDestinationFlattenConfig(*result.Config))
144192
if err != nil {
145193
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting config: %s", err), "(Data) ibm_en_destination_custom_email", "read")
146194
return tfErr.GetDiag()
@@ -194,9 +242,52 @@ func enCustomEmailDestinationConfigParamsToMap(paramsItem en.DestinationConfigOn
194242

195243
params := paramsItem.(*en.DestinationConfigOneOf)
196244

197-
if params.URL != nil {
245+
if params.Domain != nil {
198246
paramsMap["domain"] = params.Domain
199247
}
200248

249+
if params.Dkim != nil {
250+
dkimMap, err := dataSourceIBMEnDestinationDkimAttributesToMap(params.Dkim)
251+
if err != nil {
252+
return paramsMap
253+
}
254+
paramsMap["dkim"] = []map[string]interface{}{dkimMap}
255+
}
256+
if params.Spf != nil {
257+
spfMap, err := dataSourceIBMEnDestinationSpfAttributesToMap(params.Spf)
258+
if err != nil {
259+
return paramsMap
260+
}
261+
paramsMap["spf"] = []map[string]interface{}{spfMap}
262+
}
263+
201264
return paramsMap
202265
}
266+
267+
func dataSourceIBMEnDestinationDkimAttributesToMap(model *en.DkimAttributes) (map[string]interface{}, error) {
268+
modelMap := make(map[string]interface{})
269+
if model.PublicKey != nil {
270+
modelMap["public_key"] = model.PublicKey
271+
}
272+
if model.Selector != nil {
273+
modelMap["selector"] = model.Selector
274+
}
275+
if model.Verification != nil {
276+
modelMap["verification"] = model.Verification
277+
}
278+
return modelMap, nil
279+
}
280+
281+
func dataSourceIBMEnDestinationSpfAttributesToMap(model *en.SpfAttributes) (map[string]interface{}, error) {
282+
modelMap := make(map[string]interface{})
283+
if model.TxtName != nil {
284+
modelMap["txt_name"] = model.TxtName
285+
}
286+
if model.TxtValue != nil {
287+
modelMap["txt_value"] = model.TxtValue
288+
}
289+
if model.Verification != nil {
290+
modelMap["verification"] = model.Verification
291+
}
292+
return modelMap, nil
293+
}

ibm/service/eventnotification/resource_ibm_en_destination_custom_email.go

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func ResourceIBMEnCustomEmailDestination() *schema.Resource {
5252
Optional: true,
5353
Description: "Whether to collect the failed event in Cloud Object Storage bucket",
5454
},
55+
"verification_type": &schema.Schema{
56+
Type: schema.TypeString,
57+
Optional: true,
58+
Description: "Verification Method spf/dkim.",
59+
},
5560
"config": {
5661
Type: schema.TypeList,
5762
MaxItems: 1,
@@ -61,7 +66,6 @@ func ResourceIBMEnCustomEmailDestination() *schema.Resource {
6166
Schema: map[string]*schema.Schema{
6267
"params": {
6368
Type: schema.TypeList,
64-
MaxItems: 1,
6569
Optional: true,
6670
Elem: &schema.Resource{
6771
Schema: map[string]*schema.Schema{
@@ -70,6 +74,54 @@ func ResourceIBMEnCustomEmailDestination() *schema.Resource {
7074
Required: true,
7175
Description: "Domain for the Custom Domain Email Destination",
7276
},
77+
"dkim": &schema.Schema{
78+
Type: schema.TypeList,
79+
Computed: true,
80+
Description: "The DKIM attributes.",
81+
Elem: &schema.Resource{
82+
Schema: map[string]*schema.Schema{
83+
"public_key": &schema.Schema{
84+
Type: schema.TypeString,
85+
Computed: true,
86+
Description: "dkim public key.",
87+
},
88+
"selector": &schema.Schema{
89+
Type: schema.TypeString,
90+
Computed: true,
91+
Description: "dkim selector.",
92+
},
93+
"verification": &schema.Schema{
94+
Type: schema.TypeString,
95+
Computed: true,
96+
Description: "dkim verification.",
97+
},
98+
},
99+
},
100+
},
101+
"spf": &schema.Schema{
102+
Type: schema.TypeList,
103+
Computed: true,
104+
Description: "The SPF attributes.",
105+
Elem: &schema.Resource{
106+
Schema: map[string]*schema.Schema{
107+
"txt_name": &schema.Schema{
108+
Type: schema.TypeString,
109+
Computed: true,
110+
Description: "spf text name.",
111+
},
112+
"txt_value": &schema.Schema{
113+
Type: schema.TypeString,
114+
Computed: true,
115+
Description: "spf text value.",
116+
},
117+
"verification": &schema.Schema{
118+
Type: schema.TypeString,
119+
Computed: true,
120+
Description: "spf verification.",
121+
},
122+
},
123+
},
124+
},
73125
},
74126
},
75127
},
@@ -222,6 +274,7 @@ func resourceIBMEnCustomEmailDestinationUpdate(context context.Context, d *schem
222274
}
223275

224276
options := &en.UpdateDestinationOptions{}
277+
verifyCustomEmailDestinationConfiguration := &en.UpdateVerifyDestinationOptions{}
225278

226279
parts, err := flex.SepIdParts(d.Id(), "/")
227280
if err != nil {
@@ -231,6 +284,12 @@ func resourceIBMEnCustomEmailDestinationUpdate(context context.Context, d *schem
231284

232285
options.SetInstanceID(parts[0])
233286
options.SetID(parts[1])
287+
verifyCustomEmailDestinationConfiguration.SetType(d.Get("verification_type").(string))
288+
verifyCustomEmailDestinationConfiguration.SetInstanceID(parts[0])
289+
verifyCustomEmailDestinationConfiguration.SetID(parts[1])
290+
hasChangeverification := false
291+
292+
verifyCustomEmailDestinationConfiguration.SetType(d.Get("verification_type").(string))
234293

235294
if ok := d.HasChanges("name", "description", "collect_failed_events", "config"); ok {
236295
options.SetName(d.Get("name").(string))
@@ -244,6 +303,11 @@ func resourceIBMEnCustomEmailDestinationUpdate(context context.Context, d *schem
244303
}
245304

246305
destinationtype := d.Get("type").(string)
306+
307+
if d.HasChange("verification_type") {
308+
verifyCustomEmailDestinationConfiguration.SetType(d.Get("verification_type").(string))
309+
hasChangeverification = true
310+
}
247311
if _, ok := d.GetOk("config"); ok {
248312
config := CustomEmaildestinationConfigMapToDestinationConfig(d.Get("config.0.params.0").(map[string]interface{}), destinationtype)
249313
options.SetConfig(&config)
@@ -255,6 +319,13 @@ func resourceIBMEnCustomEmailDestinationUpdate(context context.Context, d *schem
255319
return tfErr.GetDiag()
256320
}
257321

322+
if hasChangeverification {
323+
_, _, err = enClient.UpdateVerifyDestinationWithContext(context, verifyCustomEmailDestinationConfiguration)
324+
if err != nil {
325+
return diag.FromErr(err)
326+
}
327+
}
328+
258329
return resourceIBMEnCustomEmailDestinationRead(context, d, meta)
259330
}
260331

@@ -302,7 +373,44 @@ func CustomEmaildestinationConfigMapToDestinationConfig(configParams map[string]
302373
params.Domain = core.StringPtr(configParams["domain"].(string))
303374
}
304375

376+
if configParams["dkim"] != nil && len(configParams["dkim"].([]interface{})) > 0 {
377+
DkimModel, _ := resourceIBMEnDestinationMapToDkimAttributes(configParams["dkim"].([]interface{})[0].(map[string]interface{}))
378+
params.Dkim = &DkimModel
379+
}
380+
if configParams["spf"] != nil && len(configParams["spf"].([]interface{})) > 0 {
381+
SpfModel, _ := resourceIBMEnDestinationMapToSpfAttributes(configParams["spf"].([]interface{})[0].(map[string]interface{}))
382+
params.Spf = &SpfModel
383+
}
384+
305385
destinationConfig := new(en.DestinationConfig)
306386
destinationConfig.Params = params
307387
return *destinationConfig
308388
}
389+
390+
func resourceIBMEnDestinationMapToDkimAttributes(modelMap map[string]interface{}) (en.DkimAttributes, error) {
391+
model := new(en.DkimAttributes)
392+
if modelMap["public_key"] != nil && modelMap["public_key"].(string) != "" {
393+
model.PublicKey = core.StringPtr(modelMap["public_key"].(string))
394+
}
395+
if modelMap["selector"] != nil && modelMap["selector"].(string) != "" {
396+
model.Selector = core.StringPtr(modelMap["selector"].(string))
397+
}
398+
if modelMap["verification"] != nil && modelMap["verification"].(string) != "" {
399+
model.Verification = core.StringPtr(modelMap["verification"].(string))
400+
}
401+
return *model, nil
402+
}
403+
404+
func resourceIBMEnDestinationMapToSpfAttributes(modelMap map[string]interface{}) (en.SpfAttributes, error) {
405+
model := new(en.SpfAttributes)
406+
if modelMap["txt_name"] != nil && modelMap["txt_name"].(string) != "" {
407+
model.TxtName = core.StringPtr(modelMap["txt_name"].(string))
408+
}
409+
if modelMap["txt_value"] != nil && modelMap["txt_value"].(string) != "" {
410+
model.TxtValue = core.StringPtr(modelMap["txt_value"].(string))
411+
}
412+
if modelMap["verification"] != nil && modelMap["verification"].(string) != "" {
413+
model.Verification = core.StringPtr(modelMap["verification"].(string))
414+
}
415+
return *model, nil
416+
}

ibm/service/eventnotification/resource_ibm_en_email_template.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func resourceIBMEnEmailTemplateCreate(context context.Context, d *schema.Resourc
9898
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_en_email_template", "create")
9999
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
100100
return tfErr.GetDiag()
101-
// return diag.FromErr(err)
102101
}
103102

104103
options := &en.CreateTemplateOptions{}
@@ -120,7 +119,6 @@ func resourceIBMEnEmailTemplateCreate(context context.Context, d *schema.Resourc
120119
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("CreateTemplateWithContext failed: %s", err.Error()), "ibm_en_email_template", "create")
121120
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
122121
return tfErr.GetDiag()
123-
// return diag.FromErr(fmt.Errorf("CreateTemplateWithContext failed %s\n%s", err, response))
124122
}
125123

126124
d.SetId(fmt.Sprintf("%s/%s", *options.InstanceID, *result.ID))
@@ -134,7 +132,6 @@ func resourceIBMEnEmailTemplateRead(context context.Context, d *schema.ResourceD
134132
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_en_email_template", "read")
135133
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
136134
return tfErr.GetDiag()
137-
// return diag.FromErr(err)
138135
}
139136

140137
options := &en.GetTemplateOptions{}
@@ -143,7 +140,6 @@ func resourceIBMEnEmailTemplateRead(context context.Context, d *schema.ResourceD
143140
if err != nil {
144141
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_en_email_template", "read")
145142
return tfErr.GetDiag()
146-
// return diag.FromErr(err)
147143
}
148144

149145
options.SetInstanceID(parts[0])
@@ -158,7 +154,6 @@ func resourceIBMEnEmailTemplateRead(context context.Context, d *schema.ResourceD
158154
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetTemplateWithContext failed: %s", err.Error()), "ibm_en_email_template", "read")
159155
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
160156
return tfErr.GetDiag()
161-
// return diag.FromErr(fmt.Errorf("GetTemplateWithContext failed %s\n%s", err, response))
162157
}
163158

164159
if err = d.Set("instance_guid", options.InstanceID); err != nil {
@@ -203,7 +198,6 @@ func resourceIBMEnEmailTemplateUpdate(context context.Context, d *schema.Resourc
203198
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_en_email_template", "update")
204199
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
205200
return tfErr.GetDiag()
206-
// return diag.FromErr(err)
207201
}
208202

209203
options := &en.ReplaceTemplateOptions{}
@@ -212,11 +206,11 @@ func resourceIBMEnEmailTemplateUpdate(context context.Context, d *schema.Resourc
212206
if err != nil {
213207
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_en_email_template", "update")
214208
return tfErr.GetDiag()
215-
// return diag.FromErr(err)
216209
}
217210

218211
options.SetInstanceID(parts[0])
219212
options.SetID(parts[1])
213+
options.SetType(d.Get("type").(string))
220214

221215
if ok := d.HasChanges("name", "description", "params"); ok {
222216
options.SetName(d.Get("name").(string))
@@ -233,7 +227,6 @@ func resourceIBMEnEmailTemplateUpdate(context context.Context, d *schema.Resourc
233227
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ReplaceTemplateWithContext failed: %s", err.Error()), "ibm_en_email_template", "update")
234228
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
235229
return tfErr.GetDiag()
236-
// return diag.FromErr(fmt.Errorf("ReplaceTemplateWithContext failed %s\n%s", err, response))
237230
}
238231

239232
return resourceIBMEnEmailTemplateRead(context, d, meta)
@@ -248,7 +241,6 @@ func resourceIBMEnEmailTemplateDelete(context context.Context, d *schema.Resourc
248241
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_en_email_template", "delete")
249242
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
250243
return tfErr.GetDiag()
251-
// return diag.FromErr(err)
252244
}
253245

254246
options := &en.DeleteTemplateOptions{}
@@ -257,7 +249,6 @@ func resourceIBMEnEmailTemplateDelete(context context.Context, d *schema.Resourc
257249
if err != nil {
258250
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_en_email_template", "delete")
259251
return tfErr.GetDiag()
260-
// return diag.FromErr(err)
261252
}
262253

263254
options.SetInstanceID(parts[0])
@@ -272,7 +263,6 @@ func resourceIBMEnEmailTemplateDelete(context context.Context, d *schema.Resourc
272263
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("DeleteTemplateWithContext: %s", err.Error()), "ibm_en_email_template", "delete")
273264
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
274265
return tfErr.GetDiag()
275-
// return diag.FromErr(fmt.Errorf("DeleteTemplateWithContext failed %s\n%s", err, response))
276266
}
277267

278268
d.SetId("")

0 commit comments

Comments
 (0)