Skip to content

Commit 151065c

Browse files
authored
Merge pull request #79 from SumoLogic/rohit-SUMO-147334-fix-monitor-state-refresh
SUMO-147334: fix monitor state refresh
2 parents b32a006 + 8a6e76c commit 151065c

File tree

3 files changed

+151
-21
lines changed

3 files changed

+151
-21
lines changed

sumologic/resource_sumologic_monitors_library_monitor.go

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sumologic
22

33
import (
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 {

sumologic/resource_sumologic_monitors_library_monitor_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ func TestAccMonitorsLibraryMonitor_create(t *testing.T) {
5959
TriggerType: "Critical",
6060
DetectionMethod: "StaticCondition",
6161
},
62+
{
63+
ThresholdType: "LessThanOrEqual",
64+
Threshold: 40.0,
65+
TimeRange: "15m",
66+
OccurrenceType: "ResultCount",
67+
TriggerSource: "AllResults",
68+
TriggerType: "ResolvedCritical",
69+
DetectionMethod: "StaticCondition",
70+
},
6271
}
6372
recipients := []string{"[email protected]"}
6473
testRecipients := make([]interface{}, len(recipients))
@@ -135,6 +144,15 @@ func TestAccMonitorsLibraryMonitor_update(t *testing.T) {
135144
TriggerType: "Critical",
136145
DetectionMethod: "StaticCondition",
137146
},
147+
{
148+
ThresholdType: "LessThanOrEqual",
149+
Threshold: 40.0,
150+
TimeRange: "15m",
151+
OccurrenceType: "ResultCount",
152+
TriggerSource: "AllResults",
153+
TriggerType: "ResolvedCritical",
154+
DetectionMethod: "StaticCondition",
155+
},
138156
}
139157
recipients := []string{"[email protected]"}
140158
testRecipients := make([]interface{}, len(recipients))
@@ -183,6 +201,15 @@ func TestAccMonitorsLibraryMonitor_update(t *testing.T) {
183201
TriggerType: "Critical",
184202
DetectionMethod: "StaticCondition",
185203
},
204+
{
205+
ThresholdType: "LessThanOrEqual",
206+
Threshold: 40.0,
207+
TimeRange: "15m",
208+
OccurrenceType: "ResultCount",
209+
TriggerSource: "AllResults",
210+
TriggerType: "ResolvedCritical",
211+
DetectionMethod: "StaticCondition",
212+
},
186213
}
187214
updatedRecipients := []string{"[email protected]"}
188215
testUpdatedRecipients := make([]interface{}, len(updatedRecipients))
@@ -333,6 +360,15 @@ resource "sumologic_monitor" "test" {
333360
trigger_type = "Critical"
334361
detection_method = "StaticCondition"
335362
}
363+
triggers {
364+
threshold_type = "LessThanOrEqual"
365+
threshold = 40.0
366+
time_range = "15m"
367+
occurrence_type = "ResultCount"
368+
trigger_source = "AllResults"
369+
trigger_type = "ResolvedCritical"
370+
detection_method = "StaticCondition"
371+
}
336372
notifications {
337373
notification {
338374
action_type = "EmailAction"
@@ -368,6 +404,15 @@ resource "sumologic_monitor" "test" {
368404
trigger_type = "Critical"
369405
detection_method = "StaticCondition"
370406
}
407+
triggers {
408+
threshold_type = "LessThanOrEqual"
409+
threshold = 40.0
410+
time_range = "15m"
411+
occurrence_type = "ResultCount"
412+
trigger_source = "AllResults"
413+
trigger_type = "ResolvedCritical"
414+
detection_method = "StaticCondition"
415+
}
371416
notifications {
372417
notification {
373418
action_type = "EmailAction"

sumologic/sumologic_monitors_library_monitor.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ type MonitorQuery struct {
129129
type TriggerCondition struct {
130130
TimeRange string `json:"timeRange"`
131131
TriggerType string `json:"triggerType"`
132-
Threshold float64 `json:"threshold"`
133-
ThresholdType string `json:"thresholdType"`
132+
Threshold float64 `json:"threshold,omitempty"`
133+
ThresholdType string `json:"thresholdType,omitempty"`
134134
OccurrenceType string `json:"occurrenceType"`
135135
TriggerSource string `json:"triggerSource"`
136136
DetectionMethod string `json:"detectionMethod"`
@@ -142,17 +142,19 @@ type MonitorNotification struct {
142142
}
143143

144144
type EmailNotification struct {
145-
ActionType string `json:"actionType"`
146-
Subject string `json:"subject"`
147-
Recipients []interface{} `json:"recipients"`
148-
MessageBody string `json:"messageBody"`
149-
TimeZone string `json:"timeZone"`
145+
ActionType string `json:"actionType,omitempty"`
146+
ConnectionType string `json:"connectionType,omitempty"`
147+
Subject string `json:"subject"`
148+
Recipients []interface{} `json:"recipients"`
149+
MessageBody string `json:"messageBody"`
150+
TimeZone string `json:"timeZone"`
150151
}
151152

152153
type WebhookNotificiation struct {
153-
ActionType string `json:"actionType"`
154+
ActionType string `json:"actionType,omitempty"`
155+
ConnectionType string `json:"connectionType,omitempty"`
154156
ConnectionID string `json:"connectionId"`
155-
PayloadOverride string `json:"payloadOverride"`
157+
PayloadOverride string `json:"payloadOverride,omitempty"`
156158
}
157159

158160
// ---------- END ----------

0 commit comments

Comments
 (0)