@@ -119,6 +119,22 @@ func createAlertV2Schema(original map[string]*schema.Schema) map[string]*schema.
119119 Type : schema .TypeString ,
120120 Optional : true ,
121121 },
122+ "additional_field" : {
123+ Type : schema .TypeSet ,
124+ Optional : true ,
125+ Elem : & schema.Resource {
126+ Schema : map [string ]* schema.Schema {
127+ "name" : {
128+ Type : schema .TypeString ,
129+ Required : true ,
130+ },
131+ "value" : {
132+ Type : schema .TypeString ,
133+ Required : true ,
134+ },
135+ },
136+ },
137+ },
122138 },
123139 },
124140 },
@@ -271,12 +287,22 @@ func buildAlertV2CommonStruct(d *schema.ResourceData) *v2.AlertV2Common {
271287
272288 customNotification := v2.CustomNotificationTemplateV2 {}
273289 if attr , ok := d .GetOk ("custom_notification" ); ok && attr != nil {
274- if len (attr .([]interface {})) > 0 {
290+ if len (attr .([]interface {})) > 0 && attr .([] interface {})[ 0 ] != nil {
275291 m := attr .([]interface {})[0 ].(map [string ]interface {})
276292
277293 customNotification .Subject = m ["subject" ].(string )
278294 customNotification .AppendText = m ["append" ].(string )
279295 customNotification .PrependText = m ["prepend" ].(string )
296+ customNotification .AdditionalNotificationFields = []v2.CustomNotificationAdditionalField {}
297+ if m ["additional_field" ] != nil {
298+ for _ , field := range m ["additional_field" ].(* schema.Set ).List () {
299+ fieldMap := field .(map [string ]interface {})
300+ customNotification .AdditionalNotificationFields = append (customNotification .AdditionalNotificationFields , v2.CustomNotificationAdditionalField {
301+ Name : fieldMap ["name" ].(string ),
302+ Value : fieldMap ["value" ].(string ),
303+ })
304+ }
305+ }
280306 }
281307 }
282308 alert .CustomNotificationTemplate = & customNotification
@@ -366,15 +392,35 @@ func updateAlertV2CommonState(d *schema.ResourceData, alert *v2.AlertV2Common) (
366392 }
367393 _ = d .Set ("notification_channels" , notificationChannels )
368394
369- if alert .CustomNotificationTemplate != nil && ! (alert .CustomNotificationTemplate .Subject == "" &&
370- alert .CustomNotificationTemplate .AppendText == "" &&
371- alert .CustomNotificationTemplate .PrependText == "" ) {
395+ if alert .CustomNotificationTemplate != nil &&
396+ ! (alert .CustomNotificationTemplate .Subject == "" &&
397+ alert .CustomNotificationTemplate .AppendText == "" &&
398+ alert .CustomNotificationTemplate .PrependText == "" &&
399+ len (alert .CustomNotificationTemplate .AdditionalNotificationFields ) == 0 ) {
372400 customNotification := map [string ]interface {}{}
373401 customNotification ["subject" ] = alert .CustomNotificationTemplate .Subject
374402 customNotification ["append" ] = alert .CustomNotificationTemplate .AppendText
375403 customNotification ["prepend" ] = alert .CustomNotificationTemplate .PrependText
376-
404+ additionalFields := []interface {}{}
405+ for _ , field := range alert .CustomNotificationTemplate .AdditionalNotificationFields {
406+ additionalFields = append (additionalFields , map [string ]interface {}{
407+ "name" : field .Name ,
408+ "value" : field .Value ,
409+ })
410+ }
411+ customNotification ["additional_field" ] = additionalFields
377412 _ = d .Set ("custom_notification" , []interface {}{customNotification })
413+ } else {
414+ // if the custom notification template has all empty fields, we don't set it in the state
415+ // this because, even if the alert was created without custom notification template, the api returs:
416+ // ```
417+ // "customNotificationTemplate" : {
418+ // "subject" : ""
419+ // }
420+ // ```
421+ // and it would triggert a diff compared to the empty state defined in the schema.
422+ // (an empty subject creates a notification with default title anyway, so it is equal to no subject definition)
423+ _ = d .Set ("custom_notification" , []interface {}{})
378424 }
379425
380426 if alert .CaptureConfig != nil {
0 commit comments