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