Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUMO-231087:Alert Grouping Support for Muting schedules: update TF f… #596

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.28.0 (November 23, 2023)
FEATURES:
* resource/sumologic_monitor: Added support for setting `time_zone` at Monitor level for notifications content (GH-586)
* resource/sumologic_muting_schedule: Added group supporting for muting schedule (GH-593)

BUG FIXES:
* Fixes `resource_sumologic_cse_match_list` constant change when defining a match list containing a custom column using the custom columns name instead of ID (GH-591)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ func getMutingScheduleBaseSchema() map[string]*schema.Schema {
Schema: getScheduleDefinitionSchemma(),
},
},
"notification_groups": {
Type: schema.TypeList,
Optional: true,
MaxItems: 10,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_key": {
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(1, 128),
Required: true,
},
"group_values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(1, 256),
},
},
},
},
},

"version": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -265,6 +287,7 @@ func resourceSumologicMutingSchedulesLibraryMutingScheduleRead(d *schema.Resourc
d.Set("is_system", mutingSchedule.IsSystem)
d.Set("monitor", monitorScope)
d.Set("schedule", schedule)
d.Set("notification_groups", notificationGroupArrayToResource(mutingSchedule.NotificationGroups))

return nil
}
Expand Down Expand Up @@ -319,6 +342,31 @@ func getScheduleDefinition(d *schema.ResourceData) ScheduleDefinition {
return scheduleDefinition
}

func notificationGroupArrayToResource(notificationGroups []NotificationGroupDefinition) []map[string]interface{} {
result := make([]map[string]interface{}, len(notificationGroups))

for i, notificationGroup := range notificationGroups {
result[i] = map[string]interface{}{
"group_key": notificationGroup.GroupKey,
"group_values": notificationGroup.GroupValues,
}
}
return result
}

func getNotificationGroupArray(resourceNotificationGroups []interface{}) []NotificationGroupDefinition {
result := make([]NotificationGroupDefinition, len(resourceNotificationGroups))

for i, resourceNotificationGroup := range resourceNotificationGroups {
resourceNotificationGroupMap := resourceNotificationGroup.(map[string]interface{})
result[i] = NotificationGroupDefinition{
GroupKey: resourceNotificationGroupMap["group_key"].(string),
GroupValues: resourceToStringArray(resourceNotificationGroupMap["group_values"].([]interface{})),
}
}
return result
}

func StartDateIsAfterYesterday() schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
Expand Down Expand Up @@ -351,20 +399,21 @@ func resourceToMutingSchedulesLibraryMutingSchedule(d *schema.ResourceData) Muti
scheduleDefinition := getScheduleDefinition(d)

return MutingSchedulesLibraryMutingSchedule{
CreatedBy: d.Get("created_by").(string),
Name: d.Get("name").(string),
ID: d.Id(),
CreatedAt: d.Get("created_at").(string),
Description: d.Get("description").(string),
ModifiedBy: d.Get("modified_by").(string),
IsMutable: d.Get("is_mutable").(bool),
Version: d.Get("version").(int),
Type: d.Get("type").(string),
ParentID: d.Get("parent_id").(string),
ModifiedAt: d.Get("modified_at").(string),
ContentType: d.Get("content_type").(string),
IsSystem: d.Get("is_system").(bool),
Schedule: scheduleDefinition,
Monitor: monitorScope,
CreatedBy: d.Get("created_by").(string),
Name: d.Get("name").(string),
ID: d.Id(),
CreatedAt: d.Get("created_at").(string),
Description: d.Get("description").(string),
ModifiedBy: d.Get("modified_by").(string),
IsMutable: d.Get("is_mutable").(bool),
Version: d.Get("version").(int),
Type: d.Get("type").(string),
ParentID: d.Get("parent_id").(string),
ModifiedAt: d.Get("modified_at").(string),
ContentType: d.Get("content_type").(string),
IsSystem: d.Get("is_system").(bool),
Schedule: scheduleDefinition,
Monitor: monitorScope,
NotificationGroups: getNotificationGroupArray(d.Get("notification_groups").([]interface{})),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_create(t *testing.T) {
StartTime: "00:00",
Duration: 40,
RRule: "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10",
IsForm: false,
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -80,6 +79,57 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_create(t *testing.T) {
})
}

func TestAccSumologicMutingSchedulesLibraryMutingScheduleWithNotificationGroup_create(t *testing.T) {
var mutingSchedulesLibraryMutingSchedule MutingSchedulesLibraryMutingSchedule
testNameSuffix := acctest.RandString(16)
tomorrow := time.Now().AddDate(0, 0, 1)

testName := "terraform_test_muting_schedule_" + testNameSuffix
testDescription := "terraform_test_muting_schedule_description"
testType := "MutingSchedulesLibraryMutingSchedule"
testContentType := "MutingSchedule"
testMonitor := MonitorScope{
All: true,
}
testSchedule := ScheduleDefinition{
TimeZone: "America/Los_Angeles",
StartDate: tomorrow.Format("2006-01-02"),
StartTime: "00:00",
Duration: 40,
RRule: "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10",
}
testNotificationGroup := []NotificationGroupDefinition{{
GroupKey: "host",
GroupValues: []string{"localhost", "127.0.0.1"},
}}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMutingSchedulesLibraryMutingScheduleDestroy(mutingSchedulesLibraryMutingSchedule),
Steps: []resource.TestStep{
{
Config: testAccSumologicMutingSchedulesLibraryMutingScheduleWithNotificationGroups(testNameSuffix),
Check: resource.ComposeTestCheckFunc(
testAccCheckMutingSchedulesLibraryMutingScheduleExists("sumologic_muting_schedule.test", &mutingSchedulesLibraryMutingSchedule, t),
testAccCheckMutingSchedulesLibraryMutingScheduleAttributes("sumologic_muting_schedule.test"),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "name", testName),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "type", testType),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "description", testDescription),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "content_type", testContentType),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "monitor.0.all", strconv.FormatBool(testMonitor.All)),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.timezone", testSchedule.TimeZone),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.start_date", testSchedule.StartDate),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.start_time", testSchedule.StartTime),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "schedule.0.rrule", testSchedule.RRule),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "notification_groups.0.group_key", testNotificationGroup[0].GroupKey),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "notification_groups.0.group_values.0", testNotificationGroup[0].GroupValues[0]),
resource.TestCheckResourceAttr("sumologic_muting_schedule.test", "notification_groups.0.group_values.1", testNotificationGroup[0].GroupValues[1]),
),
},
},
})
}

func TestAccSumologicMutingSchedulesLibraryMutingSchedule_update(t *testing.T) {
var mutingSchedulesLibraryMutingSchedule MutingSchedulesLibraryMutingSchedule
testNameSuffix := acctest.RandString(16)
Expand All @@ -98,7 +148,6 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_update(t *testing.T) {
StartTime: "00:00",
Duration: 40,
RRule: "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10",
IsForm: false,
}

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

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -305,3 +353,31 @@ func testAccSumologicMutingSchedulesLibraryMutingScheduleBadMonitorScope(testNam
}
`, testName, startDate)
}

func testAccSumologicMutingSchedulesLibraryMutingScheduleWithNotificationGroups(testName string) string {
tomorrow := time.Now().AddDate(0, 0, 1)
startDate := tomorrow.Format("2006-01-02")
return fmt.Sprintf(`
resource "sumologic_muting_schedule" "test" {
name = "terraform_test_muting_schedule_%s"
description = "terraform_test_muting_schedule_description"
type = "MutingSchedulesLibraryMutingSchedule"
content_type = "MutingSchedule"
monitor {
ids = []
all = true
}
schedule {
timezone = "America/Los_Angeles"
start_date = "%s"
start_time = "00:00"
duration = 40
rrule = "FREQ=DAILY;INTERVAL=1;BYHOUR=9,10"
}
notification_groups {
group_key = "host"
group_values =["localhost","127.0.0.1"]
}
}
`, testName, startDate)
}
37 changes: 21 additions & 16 deletions sumologic/sumologic_muting_schedules_library_muting_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,22 @@ func (s *Client) GetMutingSchedulesLibraryFolder(id string) (*MutingSchedulesLib

// ---------- TYPES ----------
type MutingSchedulesLibraryMutingSchedule struct {
ID string `json:"id"`
Type string `json:"type"`
IsSystem bool `json:"isSystem"`
IsMutable bool `json:"isMutable"`
Schedule ScheduleDefinition `json:"schedule"`
Monitor *MonitorScope `json:"monitor"`
ParentID string `json:"parentId"`
Name string `json:"name"`
Version int `json:"version"`
CreatedBy string `json:"createdBy"`
Description string `json:"description"`
CreatedAt string `json:"createdAt"`
ModifiedAt string `json:"modifiedAt"`
ContentType string `json:"contentType"`
ModifiedBy string `json:"modifiedBy"`
ID string `json:"id"`
Type string `json:"type"`
IsSystem bool `json:"isSystem"`
IsMutable bool `json:"isMutable"`
Schedule ScheduleDefinition `json:"schedule"`
Monitor *MonitorScope `json:"monitor"`
ParentID string `json:"parentId"`
Name string `json:"name"`
Version int `json:"version"`
CreatedBy string `json:"createdBy"`
Description string `json:"description"`
CreatedAt string `json:"createdAt"`
ModifiedAt string `json:"modifiedAt"`
ContentType string `json:"contentType"`
ModifiedBy string `json:"modifiedBy"`
NotificationGroups []NotificationGroupDefinition `json:"notificationGroups"`
}

type ScheduleDefinition struct {
Expand All @@ -139,14 +140,18 @@ type ScheduleDefinition struct {
StartTime string `json:"startTime"`
Duration int `json:"duration"`
RRule string `json:"rrule,omitempty"`
IsForm bool `json:"isForm,omitempty"`
}

type MonitorScope struct {
Ids []string `json:"ids,omitempty"`
All bool `json:"all,omitempty"`
}

type NotificationGroupDefinition struct {
GroupKey string `json:"groupKey"`
GroupValues []string `json:"groupValues"`
}

type MutingSchedulesLibraryFolder struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/muting_schedule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ The following arguments are supported:
- `MutingSchedule`
- `monitor` - (Optional) The monitors which need to put in the muting schedule. see `monitor_scope_type`:
- `schedule` - (Required) The schedule information. see `schedule_type`.
- `notification_groups` -(Optinal) The muting schedule group supporting key and values. see `notification_group_type`

#### schedule_type
- `timezone` - (Required) Time zone for the schedule per
Expand All @@ -120,4 +121,8 @@ The following arguments are supported:
- `ids` - (Optional) List of monitor Ids in hex. Must be empty if `all` is true.
- `all` - (Optional) True if the schedule applies to all monitors

#### notification_group_type
- `group_key` - (Required) the monitor notification group key .
- `group_values` - (Required) List of monitor notification group values.

[1]: https://help.sumologic.com/docs/alerts/monitors/muting-schedules/
Loading