From 99575c4d62c256b045713aea99271217df2e700d Mon Sep 17 00:00:00 2001 From: James Xie Date: Mon, 4 Dec 2023 10:39:23 -0800 Subject: [PATCH] SUMO-231087:Alert Grouping Support for Muting schedules: update TF for support group key and value --- CHANGELOG.md | 1 + ...uting_schedules_library_muting_schedule.go | 79 ++++++++++++++---- ..._schedules_library_muting_schedule_test.go | 82 ++++++++++++++++++- ...uting_schedules_library_muting_schedule.go | 37 +++++---- website/docs/r/muting_schedule.html.markdown | 5 ++ 5 files changed, 170 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c18859a..a4862e8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/sumologic/resource_sumologic_muting_schedules_library_muting_schedule.go b/sumologic/resource_sumologic_muting_schedules_library_muting_schedule.go index 3227ac57..fb8d7f91 100644 --- a/sumologic/resource_sumologic_muting_schedules_library_muting_schedule.go +++ b/sumologic/resource_sumologic_muting_schedules_library_muting_schedule.go @@ -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, @@ -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 } @@ -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) @@ -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{})), } } diff --git a/sumologic/resource_sumologic_muting_schedules_library_muting_schedule_test.go b/sumologic/resource_sumologic_muting_schedules_library_muting_schedule_test.go index 2486edd5..26065c84 100644 --- a/sumologic/resource_sumologic_muting_schedules_library_muting_schedule_test.go +++ b/sumologic/resource_sumologic_muting_schedules_library_muting_schedule_test.go @@ -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) }, @@ -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) @@ -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 @@ -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{ @@ -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) +} diff --git a/sumologic/sumologic_muting_schedules_library_muting_schedule.go b/sumologic/sumologic_muting_schedules_library_muting_schedule.go index de40554f..366e8555 100644 --- a/sumologic/sumologic_muting_schedules_library_muting_schedule.go +++ b/sumologic/sumologic_muting_schedules_library_muting_schedule.go @@ -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 { @@ -139,7 +140,6 @@ type ScheduleDefinition struct { StartTime string `json:"startTime"` Duration int `json:"duration"` RRule string `json:"rrule,omitempty"` - IsForm bool `json:"isForm,omitempty"` } type MonitorScope struct { @@ -147,6 +147,11 @@ type MonitorScope struct { 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"` diff --git a/website/docs/r/muting_schedule.html.markdown b/website/docs/r/muting_schedule.html.markdown index 26ffcf94..d8008941 100644 --- a/website/docs/r/muting_schedule.html.markdown +++ b/website/docs/r/muting_schedule.html.markdown @@ -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 @@ -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/ \ No newline at end of file