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… #601

Merged
Merged
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
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.29.1 (Unreleased)

FEATURES:
* resource/sumologic_muting_schedule: Added support for Muting Schedule for an alert group (GH-601)

## 2.29.0 (April 9, 2024)

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 TestAccSumologicMutingSchedulesLibraryMutingSchedule_createWithGroup(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 @@ -287,7 +335,7 @@ func testAccSumologicMutingSchedulesLibraryMutingScheduleBadMonitorScope(testNam
tomorrow := time.Now().AddDate(0, 0, 1)
startDate := tomorrow.Format("2006-01-02")
return fmt.Sprintf(`
resource "sumologic_muting_schedule" "test" {
resource "sumologic_muting_schedule" "test" {
name = "terraform_test_muting_schedule_%s"
description = "terraform_test_muting_schedule_description"
type = "MutingSchedulesLibraryMutingSchedule"
Expand All @@ -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" {
jxie418 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading
Loading