Skip to content

Commit 165bcce

Browse files
Merge pull request #601 from SumoLogic/SUMO-231087-muting-schedule-group-support-pr-view
SUMO-231087:Alert Grouping Support for Muting schedules: update TF f…
2 parents fb1af3d + 0a79200 commit 165bcce

6 files changed

+199
-88
lines changed

.DS_Store

6 KB
Binary file not shown.

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2.29.1 (Unreleased)
2-
2+
FEATURES:
3+
* resource/sumologic_muting_schedule: Added support for Muting Schedule for an alert group (GH-601)
34

45
## 2.29.0 (April 9, 2024)
56

sumologic/resource_sumologic_muting_schedules_library_muting_schedule.go

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ func getMutingScheduleBaseSchema() map[string]*schema.Schema {
7272
Schema: getScheduleDefinitionSchemma(),
7373
},
7474
},
75+
"notification_groups": {
76+
Type: schema.TypeList,
77+
Optional: true,
78+
MaxItems: 10,
79+
Elem: &schema.Resource{
80+
Schema: map[string]*schema.Schema{
81+
"group_key": {
82+
Type: schema.TypeString,
83+
ValidateFunc: validation.StringLenBetween(1, 128),
84+
Required: true,
85+
},
86+
"group_values": {
87+
Type: schema.TypeList,
88+
Required: true,
89+
Elem: &schema.Schema{
90+
Type: schema.TypeString,
91+
ValidateFunc: validation.StringLenBetween(1, 256),
92+
},
93+
},
94+
},
95+
},
96+
},
7597

7698
"version": {
7799
Type: schema.TypeInt,
@@ -265,6 +287,7 @@ func resourceSumologicMutingSchedulesLibraryMutingScheduleRead(d *schema.Resourc
265287
d.Set("is_system", mutingSchedule.IsSystem)
266288
d.Set("monitor", monitorScope)
267289
d.Set("schedule", schedule)
290+
d.Set("notification_groups", notificationGroupArrayToResource(mutingSchedule.NotificationGroups))
268291

269292
return nil
270293
}
@@ -319,6 +342,31 @@ func getScheduleDefinition(d *schema.ResourceData) ScheduleDefinition {
319342
return scheduleDefinition
320343
}
321344

345+
func notificationGroupArrayToResource(notificationGroups []NotificationGroupDefinition) []map[string]interface{} {
346+
result := make([]map[string]interface{}, len(notificationGroups))
347+
348+
for i, notificationGroup := range notificationGroups {
349+
result[i] = map[string]interface{}{
350+
"group_key": notificationGroup.GroupKey,
351+
"group_values": notificationGroup.GroupValues,
352+
}
353+
}
354+
return result
355+
}
356+
357+
func getNotificationGroupArray(resourceNotificationGroups []interface{}) []NotificationGroupDefinition {
358+
result := make([]NotificationGroupDefinition, len(resourceNotificationGroups))
359+
360+
for i, resourceNotificationGroup := range resourceNotificationGroups {
361+
resourceNotificationGroupMap := resourceNotificationGroup.(map[string]interface{})
362+
result[i] = NotificationGroupDefinition{
363+
GroupKey: resourceNotificationGroupMap["group_key"].(string),
364+
GroupValues: resourceToStringArray(resourceNotificationGroupMap["group_values"].([]interface{})),
365+
}
366+
}
367+
return result
368+
}
369+
322370
func StartDateIsAfterYesterday() schema.SchemaValidateFunc {
323371
return func(i interface{}, k string) (warnings []string, errors []error) {
324372
v, ok := i.(string)
@@ -351,20 +399,21 @@ func resourceToMutingSchedulesLibraryMutingSchedule(d *schema.ResourceData) Muti
351399
scheduleDefinition := getScheduleDefinition(d)
352400

353401
return MutingSchedulesLibraryMutingSchedule{
354-
CreatedBy: d.Get("created_by").(string),
355-
Name: d.Get("name").(string),
356-
ID: d.Id(),
357-
CreatedAt: d.Get("created_at").(string),
358-
Description: d.Get("description").(string),
359-
ModifiedBy: d.Get("modified_by").(string),
360-
IsMutable: d.Get("is_mutable").(bool),
361-
Version: d.Get("version").(int),
362-
Type: d.Get("type").(string),
363-
ParentID: d.Get("parent_id").(string),
364-
ModifiedAt: d.Get("modified_at").(string),
365-
ContentType: d.Get("content_type").(string),
366-
IsSystem: d.Get("is_system").(bool),
367-
Schedule: scheduleDefinition,
368-
Monitor: monitorScope,
402+
CreatedBy: d.Get("created_by").(string),
403+
Name: d.Get("name").(string),
404+
ID: d.Id(),
405+
CreatedAt: d.Get("created_at").(string),
406+
Description: d.Get("description").(string),
407+
ModifiedBy: d.Get("modified_by").(string),
408+
IsMutable: d.Get("is_mutable").(bool),
409+
Version: d.Get("version").(int),
410+
Type: d.Get("type").(string),
411+
ParentID: d.Get("parent_id").(string),
412+
ModifiedAt: d.Get("modified_at").(string),
413+
ContentType: d.Get("content_type").(string),
414+
IsSystem: d.Get("is_system").(bool),
415+
Schedule: scheduleDefinition,
416+
Monitor: monitorScope,
417+
NotificationGroups: getNotificationGroupArray(d.Get("notification_groups").([]interface{})),
369418
}
370419
}

sumologic/resource_sumologic_muting_schedules_library_muting_schedule_test.go

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_create(t *testing.T) {
5353
StartTime: "00:00",
5454
Duration: 40,
5555
RRule: "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10",
56-
IsForm: false,
5756
}
5857
resource.Test(t, resource.TestCase{
5958
PreCheck: func() { testAccPreCheck(t) },
@@ -80,6 +79,57 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_create(t *testing.T) {
8079
})
8180
}
8281

82+
func TestAccSumologicMutingSchedulesLibraryMutingSchedule_createWithGroup(t *testing.T) {
83+
var mutingSchedulesLibraryMutingSchedule MutingSchedulesLibraryMutingSchedule
84+
testNameSuffix := acctest.RandString(16)
85+
tomorrow := time.Now().AddDate(0, 0, 1)
86+
87+
testName := "terraform_test_muting_schedule_" + testNameSuffix
88+
testDescription := "terraform_test_muting_schedule_description"
89+
testType := "MutingSchedulesLibraryMutingSchedule"
90+
testContentType := "MutingSchedule"
91+
testMonitor := MonitorScope{
92+
All: true,
93+
}
94+
testSchedule := ScheduleDefinition{
95+
TimeZone: "America/Los_Angeles",
96+
StartDate: tomorrow.Format("2006-01-02"),
97+
StartTime: "00:00",
98+
Duration: 40,
99+
RRule: "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10",
100+
}
101+
testNotificationGroup := []NotificationGroupDefinition{{
102+
GroupKey: "host",
103+
GroupValues: []string{"localhost", "127.0.0.1"},
104+
}}
105+
resource.Test(t, resource.TestCase{
106+
PreCheck: func() { testAccPreCheck(t) },
107+
Providers: testAccProviders,
108+
CheckDestroy: testAccCheckMutingSchedulesLibraryMutingScheduleDestroy(mutingSchedulesLibraryMutingSchedule),
109+
Steps: []resource.TestStep{
110+
{
111+
Config: testAccSumologicMutingSchedulesLibraryMutingScheduleWithNotificationGroups(testNameSuffix),
112+
Check: resource.ComposeTestCheckFunc(
113+
testAccCheckMutingSchedulesLibraryMutingScheduleExists("sumologic_muting_schedule.test", &mutingSchedulesLibraryMutingSchedule, t),
114+
testAccCheckMutingSchedulesLibraryMutingScheduleAttributes("sumologic_muting_schedule.test"),
115+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "name", testName),
116+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "type", testType),
117+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "description", testDescription),
118+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "content_type", testContentType),
119+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "monitor.0.all", strconv.FormatBool(testMonitor.All)),
120+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.timezone", testSchedule.TimeZone),
121+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.start_date", testSchedule.StartDate),
122+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.start_time", testSchedule.StartTime),
123+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.rrule", testSchedule.RRule),
124+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "notification_groups.0.group_key", testNotificationGroup[0].GroupKey),
125+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "notification_groups.0.group_values.0", testNotificationGroup[0].GroupValues[0]),
126+
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "notification_groups.0.group_values.1", testNotificationGroup[0].GroupValues[1]),
127+
),
128+
},
129+
},
130+
})
131+
}
132+
83133
func TestAccSumologicMutingSchedulesLibraryMutingSchedule_update(t *testing.T) {
84134
var mutingSchedulesLibraryMutingSchedule MutingSchedulesLibraryMutingSchedule
85135
testNameSuffix := acctest.RandString(16)
@@ -98,7 +148,6 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_update(t *testing.T) {
98148
StartTime: "00:00",
99149
Duration: 40,
100150
RRule: "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10",
101-
IsForm: false,
102151
}
103152

104153
// updated fields
@@ -115,7 +164,6 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_update(t *testing.T) {
115164
StartTime: "01:00",
116165
Duration: 50,
117166
RRule: "FREQ=DAILY;INTERVAL=1",
118-
IsForm: false,
119167
}
120168

121169
resource.Test(t, resource.TestCase{
@@ -287,7 +335,7 @@ func testAccSumologicMutingSchedulesLibraryMutingScheduleBadMonitorScope(testNam
287335
tomorrow := time.Now().AddDate(0, 0, 1)
288336
startDate := tomorrow.Format("2006-01-02")
289337
return fmt.Sprintf(`
290-
resource "sumologic_muting_schedule" "test" {
338+
resource "sumologic_muting_schedule" "test" {
291339
name = "terraform_test_muting_schedule_%s"
292340
description = "terraform_test_muting_schedule_description"
293341
type = "MutingSchedulesLibraryMutingSchedule"
@@ -305,3 +353,31 @@ func testAccSumologicMutingSchedulesLibraryMutingScheduleBadMonitorScope(testNam
305353
}
306354
`, testName, startDate)
307355
}
356+
357+
func testAccSumologicMutingSchedulesLibraryMutingScheduleWithNotificationGroups(testName string) string {
358+
tomorrow := time.Now().AddDate(0, 0, 1)
359+
startDate := tomorrow.Format("2006-01-02")
360+
return fmt.Sprintf(`
361+
resource "sumologic_muting_schedule" "test" {
362+
name = "terraform_test_muting_schedule_%s"
363+
description = "terraform_test_muting_schedule_description"
364+
type = "MutingSchedulesLibraryMutingSchedule"
365+
content_type = "MutingSchedule"
366+
monitor {
367+
ids = []
368+
all = true
369+
}
370+
schedule {
371+
timezone = "America/Los_Angeles"
372+
start_date = "%s"
373+
start_time = "00:00"
374+
duration = 40
375+
rrule = "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10"
376+
}
377+
notification_groups {
378+
group_key = "host"
379+
group_values =["localhost","127.0.0.1"]
380+
}
381+
}
382+
`, testName, startDate)
383+
}

sumologic/sumologic_muting_schedules_library_muting_schedule.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,22 @@ func (s *Client) GetMutingSchedulesLibraryFolder(id string) (*MutingSchedulesLib
116116

117117
// ---------- TYPES ----------
118118
type MutingSchedulesLibraryMutingSchedule struct {
119-
ID string `json:"id"`
120-
Type string `json:"type"`
121-
IsSystem bool `json:"isSystem"`
122-
IsMutable bool `json:"isMutable"`
123-
Schedule ScheduleDefinition `json:"schedule"`
124-
Monitor *MonitorScope `json:"monitor"`
125-
ParentID string `json:"parentId"`
126-
Name string `json:"name"`
127-
Version int `json:"version"`
128-
CreatedBy string `json:"createdBy"`
129-
Description string `json:"description"`
130-
CreatedAt string `json:"createdAt"`
131-
ModifiedAt string `json:"modifiedAt"`
132-
ContentType string `json:"contentType"`
133-
ModifiedBy string `json:"modifiedBy"`
119+
ID string `json:"id"`
120+
Type string `json:"type"`
121+
IsSystem bool `json:"isSystem"`
122+
IsMutable bool `json:"isMutable"`
123+
Schedule ScheduleDefinition `json:"schedule"`
124+
Monitor *MonitorScope `json:"monitor"`
125+
ParentID string `json:"parentId"`
126+
Name string `json:"name"`
127+
Version int `json:"version"`
128+
CreatedBy string `json:"createdBy"`
129+
Description string `json:"description"`
130+
CreatedAt string `json:"createdAt"`
131+
ModifiedAt string `json:"modifiedAt"`
132+
ContentType string `json:"contentType"`
133+
ModifiedBy string `json:"modifiedBy"`
134+
NotificationGroups []NotificationGroupDefinition `json:"notificationGroups"`
134135
}
135136

136137
type ScheduleDefinition struct {
@@ -139,14 +140,18 @@ type ScheduleDefinition struct {
139140
StartTime string `json:"startTime"`
140141
Duration int `json:"duration"`
141142
RRule string `json:"rrule,omitempty"`
142-
IsForm bool `json:"isForm,omitempty"`
143143
}
144144

145145
type MonitorScope struct {
146146
Ids []string `json:"ids,omitempty"`
147147
All bool `json:"all,omitempty"`
148148
}
149149

150+
type NotificationGroupDefinition struct {
151+
GroupKey string `json:"groupKey"`
152+
GroupValues []string `json:"groupValues"`
153+
}
154+
150155
type MutingSchedulesLibraryFolder struct {
151156
ID string `json:"id,omitempty"`
152157
Type string `json:"type"`

0 commit comments

Comments
 (0)