Skip to content

Commit 998af82

Browse files
authored
Simplify code in monitors library (#149)
1 parent a3c97c2 commit 998af82

File tree

1 file changed

+54
-52
lines changed

1 file changed

+54
-52
lines changed

sumologic/resource_sumologic_monitors_library_monitor.go

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ func resourceSumologicMonitorsLibraryMonitorCreate(d *schema.ResourceData, meta
250250
c := meta.(*Client)
251251
if d.Id() == "" {
252252
monitor := resourceToMonitorsLibraryMonitor(d)
253-
paramMap := make(map[string]string)
254253
if monitor.ParentID == "" {
255254
rootFolder, err := c.GetMonitorsLibraryFolder("root")
256255
if err != nil {
@@ -259,7 +258,9 @@ func resourceSumologicMonitorsLibraryMonitorCreate(d *schema.ResourceData, meta
259258

260259
monitor.ParentID = rootFolder.ID
261260
}
262-
paramMap["parentId"] = monitor.ParentID
261+
paramMap := map[string]string{
262+
"parentId": monitor.ParentID,
263+
}
263264
monitorDefinitionID, err := c.CreateMonitorsLibraryMonitor(monitor, paramMap)
264265
if err != nil {
265266
return err
@@ -303,9 +304,7 @@ func resourceSumologicMonitorsLibraryMonitorRead(d *schema.ResourceData, meta in
303304
// set notifications
304305
notifications := make([]interface{}, len(monitor.Notifications))
305306
for i, n := range monitor.Notifications {
306-
schemaNotification := make(map[string]interface{})
307307
// notification in schema should be a list of length exactly 1
308-
schemaInternalNotification := make([]interface{}, 1)
309308
internalNotification := make(map[string]interface{})
310309
internalNotificationDict := n.Notification.(map[string]interface{})
311310
// log.Printf("monitor.Notification %v", n.Notification)
@@ -336,39 +335,43 @@ func resourceSumologicMonitorsLibraryMonitorRead(d *schema.ResourceData, meta in
336335
internalNotification["payload_override"] = internalNotificationDict["payloadOverride"].(string)
337336
}
338337
}
339-
schemaInternalNotification[0] = internalNotification
340338

341-
schemaNotification["notification"] = schemaInternalNotification
342-
schemaNotification["run_for_trigger_types"] = n.RunForTriggerTypes
343-
notifications[i] = schemaNotification
339+
schemaInternalNotification := []interface{}{
340+
internalNotification,
341+
}
342+
343+
notifications[i] = map[string]interface{}{
344+
"notification": schemaInternalNotification,
345+
"run_for_trigger_types": n.RunForTriggerTypes,
346+
}
344347
}
345348
if err := d.Set("notifications", notifications); err != nil {
346349
return err
347350
}
348351
// set triggers
349352
triggers := make([]interface{}, len(monitor.Triggers))
350353
for i, t := range monitor.Triggers {
351-
schemaTrigger := make(map[string]interface{})
352-
schemaTrigger["trigger_type"] = t.TriggerType
353-
schemaTrigger["threshold"] = t.Threshold
354-
schemaTrigger["threshold_type"] = t.ThresholdType
355-
// we don't read the TimeRange because it overwrites our local timerange and leads to errors
356-
schemaTrigger["time_range"] = d.Get(fmt.Sprintf("triggers.%d.time_range", i))
357-
schemaTrigger["occurrence_type"] = t.OccurrenceType
358-
schemaTrigger["trigger_source"] = t.TriggerSource
359-
schemaTrigger["detection_method"] = t.DetectionMethod
360-
triggers[i] = schemaTrigger
354+
triggers[i] = map[string]interface{}{
355+
"trigger_type": t.TriggerType,
356+
"threshold": t.Threshold,
357+
"threshold_type": t.ThresholdType,
358+
// we don't read the TimeRange because it overwrites our local timerange and leads to errors
359+
"time_range": d.Get(fmt.Sprintf("triggers.%d.time_range", i)),
360+
"occurrence_type": t.OccurrenceType,
361+
"trigger_source": t.TriggerSource,
362+
"detection_method": t.DetectionMethod,
363+
}
361364
}
362365
if err := d.Set("triggers", triggers); err != nil {
363366
return err
364367
}
365368
// set queries
366369
queries := make([]interface{}, len(monitor.Queries))
367370
for i, q := range monitor.Queries {
368-
schemaQuery := make(map[string]interface{})
369-
schemaQuery["row_id"] = q.RowID
370-
schemaQuery["query"] = q.Query
371-
queries[i] = schemaQuery
371+
queries[i] = map[string]interface{}{
372+
"row_id": q.RowID,
373+
"query": q.Query,
374+
}
372375
}
373376
if err := d.Set("queries", queries); err != nil {
374377
return err
@@ -412,7 +415,6 @@ func getNotifications(d *schema.ResourceData) []MonitorNotification {
412415
notifications := make([]MonitorNotification, len(rawNotifications))
413416
for i := range rawNotifications {
414417
notificationDict := rawNotifications[i].(map[string]interface{})
415-
n := MonitorNotification{}
416418
rawNotificationAction := notificationDict["notification"].([]interface{})
417419
notificationActionDict := rawNotificationAction[0].(map[string]interface{})
418420
connectionType := ""
@@ -433,24 +435,24 @@ func getNotifications(d *schema.ResourceData) []MonitorNotification {
433435
connectionType = "Webhook"
434436
}
435437
}
438+
439+
var n MonitorNotification
436440
if connectionType == "Email" {
437-
notificationAction := EmailNotification{}
438-
actionType = "EmailAction"
439-
notificationAction.ActionType = actionType
440-
notificationAction.ConnectionType = connectionType
441-
notificationAction.Subject = notificationActionDict["subject"].(string)
442-
notificationAction.Recipients = notificationActionDict["recipients"].([]interface{})
443-
notificationAction.MessageBody = notificationActionDict["message_body"].(string)
444-
notificationAction.TimeZone = notificationActionDict["time_zone"].(string)
445-
n.Notification = notificationAction
441+
n.Notification = EmailNotification{
442+
ActionType: "EmailAction",
443+
ConnectionType: connectionType,
444+
Subject: notificationActionDict["subject"].(string),
445+
Recipients: notificationActionDict["recipients"].([]interface{}),
446+
MessageBody: notificationActionDict["message_body"].(string),
447+
TimeZone: notificationActionDict["time_zone"].(string),
448+
}
446449
} else {
447-
notificationAction := WebhookNotificiation{}
448-
actionType = "NamedConnectionAction"
449-
notificationAction.ActionType = actionType
450-
notificationAction.ConnectionType = connectionType
451-
notificationAction.ConnectionID = notificationActionDict["connection_id"].(string)
452-
notificationAction.PayloadOverride = notificationActionDict["payload_override"].(string)
453-
n.Notification = notificationAction
450+
n.Notification = WebhookNotificiation{
451+
ActionType: "NamedConnectionAction",
452+
ConnectionType: connectionType,
453+
ConnectionID: notificationActionDict["connection_id"].(string),
454+
PayloadOverride: notificationActionDict["payload_override"].(string),
455+
}
454456
}
455457
n.RunForTriggerTypes = notificationDict["run_for_trigger_types"].([]interface{})
456458
notifications[i] = n
@@ -463,15 +465,15 @@ func getTriggers(d *schema.ResourceData) []TriggerCondition {
463465
triggers := make([]TriggerCondition, len(rawTriggers))
464466
for i := range rawTriggers {
465467
triggerDict := rawTriggers[i].(map[string]interface{})
466-
t := TriggerCondition{}
467-
t.TriggerType = triggerDict["trigger_type"].(string)
468-
t.Threshold = triggerDict["threshold"].(float64)
469-
t.ThresholdType = triggerDict["threshold_type"].(string)
470-
t.TimeRange = triggerDict["time_range"].(string)
471-
t.OccurrenceType = triggerDict["occurrence_type"].(string)
472-
t.TriggerSource = triggerDict["trigger_source"].(string)
473-
t.DetectionMethod = triggerDict["detection_method"].(string)
474-
triggers[i] = t
468+
triggers[i] = TriggerCondition{
469+
TriggerType: triggerDict["trigger_type"].(string),
470+
Threshold: triggerDict["threshold"].(float64),
471+
ThresholdType: triggerDict["threshold_type"].(string),
472+
TimeRange: triggerDict["time_range"].(string),
473+
OccurrenceType: triggerDict["occurrence_type"].(string),
474+
TriggerSource: triggerDict["trigger_source"].(string),
475+
DetectionMethod: triggerDict["detection_method"].(string),
476+
}
475477
}
476478
return triggers
477479
}
@@ -481,10 +483,10 @@ func getQueries(d *schema.ResourceData) []MonitorQuery {
481483
queries := make([]MonitorQuery, len(rawQueries))
482484
for i := range rawQueries {
483485
queryDict := rawQueries[i].(map[string]interface{})
484-
q := MonitorQuery{}
485-
q.Query = queryDict["query"].(string)
486-
q.RowID = queryDict["row_id"].(string)
487-
queries[i] = q
486+
queries[i] = MonitorQuery{
487+
Query: queryDict["query"].(string),
488+
RowID: queryDict["row_id"].(string),
489+
}
488490
}
489491
return queries
490492
}

0 commit comments

Comments
 (0)