11package sumologic
22
33import (
4+ "fmt"
45 "log"
56
67 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -88,7 +89,7 @@ func resourceSumologicMonitorsLibraryMonitor() *schema.Resource {
8889 Schema : map [string ]* schema.Schema {
8990 "trigger_type" : {
9091 Type : schema .TypeString ,
91- Required : true ,
92+ Optional : true ,
9293 },
9394 "threshold" : {
9495 Type : schema .TypeFloat ,
@@ -100,15 +101,15 @@ func resourceSumologicMonitorsLibraryMonitor() *schema.Resource {
100101 },
101102 "time_range" : {
102103 Type : schema .TypeString ,
103- Required : true ,
104+ Optional : true ,
104105 },
105106 "trigger_source" : {
106107 Type : schema .TypeString ,
107- Required : true ,
108+ Optional : true ,
108109 },
109110 "occurrence_type" : {
110111 Type : schema .TypeString ,
111- Required : true ,
112+ Optional : true ,
112113 },
113114 "detection_method" : {
114115 Type : schema .TypeString ,
@@ -133,7 +134,11 @@ func resourceSumologicMonitorsLibraryMonitor() *schema.Resource {
133134 Schema : map [string ]* schema.Schema {
134135 "action_type" : {
135136 Type : schema .TypeString ,
136- Required : true ,
137+ Optional : true ,
138+ },
139+ "connection_type" : {
140+ Type : schema .TypeString ,
141+ Optional : true ,
137142 },
138143 "subject" : {
139144 Type : schema .TypeString ,
@@ -195,6 +200,7 @@ func resourceSumologicMonitorsLibraryMonitor() *schema.Resource {
195200 "is_locked" : {
196201 Type : schema .TypeBool ,
197202 Optional : true ,
203+ Computed : true ,
198204 },
199205 "status" : {
200206 Type : schema .TypeList ,
@@ -291,6 +297,69 @@ func resourceSumologicMonitorsLibraryMonitorRead(d *schema.ResourceData, meta in
291297 d .Set ("is_disabled" , monitor .IsDisabled )
292298 d .Set ("status" , monitor .Status )
293299 d .Set ("group_notifications" , monitor .GroupNotifications )
300+ // set notifications
301+ notifications := make ([]interface {}, len (monitor .Notifications ))
302+ for i , n := range monitor .Notifications {
303+ schemaNotification := make (map [string ]interface {})
304+ // notification in schema should be a list of length exactly 1
305+ schemaInternalNotification := make ([]interface {}, 1 )
306+ internalNotification := make (map [string ]interface {})
307+ internalNotificationDict := n .Notification .(map [string ]interface {})
308+ if internalNotificationDict ["connectionType" ] != nil {
309+ internalNotification ["connection_type" ] = internalNotificationDict ["connectionType" ].(string )
310+ }
311+ internalNotification ["action_type" ] = internalNotificationDict ["actionType" ].(string )
312+ if internalNotification ["action_type" ].(string ) == "EmailAction" ||
313+ internalNotification ["action_type" ].(string ) == "Email" ||
314+ internalNotification ["connection_type" ].(string ) == "EmailAction" ||
315+ internalNotification ["connection_type" ].(string ) == "Email" {
316+ internalNotification ["subject" ] = internalNotificationDict ["subject" ].(string )
317+ internalNotification ["recipients" ] = internalNotificationDict ["recipients" ].([]interface {})
318+ internalNotification ["message_body" ] = internalNotificationDict ["messageBody" ].(string )
319+ internalNotification ["time_zone" ] = internalNotificationDict ["timeZone" ].(string )
320+ } else {
321+ internalNotification ["connection_id" ] = internalNotificationDict ["connectionId" ].(string )
322+ if internalNotificationDict ["payloadOverride" ] != nil {
323+ internalNotification ["payload_override" ] = internalNotificationDict ["payloadOverride" ].(string )
324+ }
325+ }
326+ schemaInternalNotification [0 ] = internalNotification
327+
328+ schemaNotification ["notification" ] = schemaInternalNotification
329+ schemaNotification ["run_for_trigger_types" ] = n .RunForTriggerTypes
330+ notifications [i ] = schemaNotification
331+ }
332+ if err := d .Set ("notifications" , notifications ); err != nil {
333+ return err
334+ }
335+ // set triggers
336+ triggers := make ([]interface {}, len (monitor .Triggers ))
337+ for i , t := range monitor .Triggers {
338+ schemaTrigger := make (map [string ]interface {})
339+ schemaTrigger ["trigger_type" ] = t .TriggerType
340+ schemaTrigger ["threshold" ] = t .Threshold
341+ schemaTrigger ["threshold_type" ] = t .ThresholdType
342+ // we don't read the TimeRange because it overwrites our local timerange and leads to errors
343+ schemaTrigger ["time_range" ] = d .Get (fmt .Sprintf ("triggers.%d.time_range" , i ))
344+ schemaTrigger ["occurrence_type" ] = t .OccurrenceType
345+ schemaTrigger ["trigger_source" ] = t .TriggerSource
346+ schemaTrigger ["detection_method" ] = t .DetectionMethod
347+ triggers [i ] = schemaTrigger
348+ }
349+ if err := d .Set ("triggers" , triggers ); err != nil {
350+ return err
351+ }
352+ // set queries
353+ queries := make ([]interface {}, len (monitor .Queries ))
354+ for i , q := range monitor .Queries {
355+ schemaQuery := make (map [string ]interface {})
356+ schemaQuery ["row_id" ] = q .RowID
357+ schemaQuery ["query" ] = q .Query
358+ queries [i ] = schemaQuery
359+ }
360+ if err := d .Set ("queries" , queries ); err != nil {
361+ return err
362+ }
294363
295364 return nil
296365}
@@ -316,25 +385,26 @@ func resourceSumologicMonitorsLibraryMonitorDelete(d *schema.ResourceData, meta
316385 return nil
317386}
318387
319- func resourceToMonitorsLibraryMonitor (d * schema.ResourceData ) MonitorsLibraryMonitor {
320- // handle notifications
388+ func getNotifications (d * schema.ResourceData ) []MonitorNotification {
321389 rawNotifications := d .Get ("notifications" ).([]interface {})
322390 notifications := make ([]MonitorNotification , len (rawNotifications ))
323391 for i := range rawNotifications {
324392 notificationDict := rawNotifications [i ].(map [string ]interface {})
325393 n := MonitorNotification {}
326394 rawNotificationAction := notificationDict ["notification" ].([]interface {})
327395 notificationActionDict := rawNotificationAction [0 ].(map [string ]interface {})
328- if notificationActionDict ["action_type" ].(string ) == "EmailAction" {
396+ if notificationActionDict ["action_type" ].(string ) == "EmailAction" ||
397+ notificationActionDict ["action_type" ].(string ) == "Email" ||
398+ notificationActionDict ["connection_type" ].(string ) == "EmailAction" ||
399+ notificationActionDict ["connection_type" ].(string ) == "Email" {
329400 notificationAction := EmailNotification {}
330401 notificationAction .ActionType = notificationActionDict ["action_type" ].(string )
331402 notificationAction .Subject = notificationActionDict ["subject" ].(string )
332403 notificationAction .Recipients = notificationActionDict ["recipients" ].([]interface {})
333404 notificationAction .MessageBody = notificationActionDict ["message_body" ].(string )
334405 notificationAction .TimeZone = notificationActionDict ["time_zone" ].(string )
335406 n .Notification = notificationAction
336- }
337- if notificationActionDict ["action_type" ].(string ) == "NamedConnectionAction" {
407+ } else {
338408 notificationAction := WebhookNotificiation {}
339409 notificationAction .ActionType = notificationActionDict ["action_type" ].(string )
340410 notificationAction .ConnectionID = notificationActionDict ["connection_id" ].(string )
@@ -344,7 +414,10 @@ func resourceToMonitorsLibraryMonitor(d *schema.ResourceData) MonitorsLibraryMon
344414 n .RunForTriggerTypes = notificationDict ["run_for_trigger_types" ].([]interface {})
345415 notifications [i ] = n
346416 }
347- // handle triggers
417+ return notifications
418+ }
419+
420+ func getTriggers (d * schema.ResourceData ) []TriggerCondition {
348421 rawTriggers := d .Get ("triggers" ).([]interface {})
349422 triggers := make ([]TriggerCondition , len (rawTriggers ))
350423 for i := range rawTriggers {
@@ -359,7 +432,10 @@ func resourceToMonitorsLibraryMonitor(d *schema.ResourceData) MonitorsLibraryMon
359432 t .DetectionMethod = triggerDict ["detection_method" ].(string )
360433 triggers [i ] = t
361434 }
362- // handle queries
435+ return triggers
436+ }
437+
438+ func getQueries (d * schema.ResourceData ) []MonitorQuery {
363439 rawQueries := d .Get ("queries" ).([]interface {})
364440 queries := make ([]MonitorQuery , len (rawQueries ))
365441 for i := range rawQueries {
@@ -369,6 +445,13 @@ func resourceToMonitorsLibraryMonitor(d *schema.ResourceData) MonitorsLibraryMon
369445 q .RowID = queryDict ["row_id" ].(string )
370446 queries [i ] = q
371447 }
448+ return queries
449+ }
450+
451+ func resourceToMonitorsLibraryMonitor (d * schema.ResourceData ) MonitorsLibraryMonitor {
452+ notifications := getNotifications (d )
453+ triggers := getTriggers (d )
454+ queries := getQueries (d )
372455 rawStatus := d .Get ("status" ).([]interface {})
373456 status := make ([]string , len (rawStatus ))
374457 for i := range rawStatus {
0 commit comments