Skip to content

Commit

Permalink
Alerts Schedule API added (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarAliiev authored Jul 26, 2021
1 parent 6a8a46f commit 44c940b
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 6 deletions.
41 changes: 40 additions & 1 deletion coralogix/data_source_coralogix_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ func dataSourceCoralogixAlert() *schema.Resource {
},
},
},
"schedule": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"days": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"start": {
Type: schema.TypeString,
Computed: true,
},
"end": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"content": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"notifications": {
Type: schema.TypeSet,
Computed: true,
Expand Down Expand Up @@ -125,11 +151,24 @@ func dataSourceCoralogixAlertRead(d *schema.ResourceData, meta interface{}) erro
d.Set("severity", alert["severity"].(string))
d.Set("enabled", alert["is_active"].(bool))
d.Set("type", alert["log_filter"].(map[string]interface{})["filter_type"].(string))
d.Set("description", alert["description"].(string))
d.Set("filter", []interface{}{flattenAlertFilter(alert)})
d.Set("condition", []interface{}{flattenAlertCondition(alert)})
d.Set("notifications", []interface{}{flattenAlertNotifications(alert)})

if alert["description"] != nil {
d.Set("description", alert["description"].(string))
} else {
d.Set("description", "")
}

if alert["notif_payload_filter"] != nil && len(alert["notif_payload_filter"].([]interface{})) > 0 {
d.Set("content", alert["notif_payload_filter"])
}

if alert["active_when"] != nil && len(alert["active_when"].(map[string]interface{})["timeframes"].([]interface{})) > 0 {
d.Set("schedule", []interface{}{flattenAlertSchedule(alert)})
}

d.SetId(alert["id"].(string))

return nil
Expand Down
27 changes: 27 additions & 0 deletions coralogix/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ func flattenAlertNotifications(alert interface{}) interface{} {
return "disabled"
}

func flattenAlertSchedule(alert interface{}) interface{} {
alertSchedule := alert.(map[string]interface{})["active_when"].(map[string]interface{})["timeframes"].([]interface{})[0].(map[string]interface{})
return map[string]interface{}{
"days": transformWeekListReverse(alertSchedule["days_of_week"].([]interface{})),
"start": alertSchedule["activity_starts"],
"end": alertSchedule["activity_ends"],
}
}

func flattenRules(rules []interface{}) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(rules))
for _, rule := range rules {
Expand Down Expand Up @@ -98,6 +107,24 @@ func flattenRuleMatchers(ruleMatchers []interface{}) []map[string]interface{} {
return nil
}

func transformWeekList(days []interface{}) []int {
week := make([]int, 0, len(days))
week_days := map[string]int{"Mo": 0, "Tu": 1, "We": 2, "Th": 3, "Fr": 4, "Sa": 5, "Su": 6}
for _, day := range days {
week = append(week, week_days[day.(string)])
}
return week
}

func transformWeekListReverse(days []interface{}) []string {
week := make([]string, 0, len(days))
week_days := map[float64]string{0: "Mo", 1: "Tu", 2: "We", 3: "Th", 4: "Fr", 5: "Sa", 6: "Su"}
for _, day := range days {
week = append(week, week_days[day.(float64)])
}
return week
}

func getFirstOrNil(list []interface{}) interface{} {
if len(list) > 0 {
return list[0]
Expand Down
94 changes: 91 additions & 3 deletions coralogix/resource_coralogix_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func resourceCoralogixAlert() *schema.Resource {
"condition_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"less_than",
"more_than",
Expand All @@ -110,11 +111,13 @@ func resourceCoralogixAlert() *schema.Resource {
"threshold": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(0),
},
"timeframe": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"5MIN",
"10MIN",
Expand All @@ -137,11 +140,64 @@ func resourceCoralogixAlert() *schema.Resource {
"group_by": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "",
},
},
},
},
"schedule": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"days": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MinItems: 1,
MaxItems: 7,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"Mo",
"Tu",
"We",
"Th",
"Fr",
"Sa",
"Su",
}, false),
},
},
"start": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"end": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
"content": {
Type: schema.TypeList,
Optional: true,
Default: nil,
ForceNew: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
"notifications": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -182,7 +238,7 @@ func resourceCoralogixAlertCreate(d *schema.ResourceData, meta interface{}) erro
}
}

alert, err := apiClient.Post("/external/alerts", map[string]interface{}{
alertParameters := map[string]interface{}{
"name": d.Get("name").(string),
"severity": d.Get("severity").(string),
"is_active": d.Get("enabled").(bool),
Expand All @@ -196,7 +252,26 @@ func resourceCoralogixAlertCreate(d *schema.ResourceData, meta interface{}) erro
},
"condition": condition,
"notifications": getFirstOrNil(d.Get("notifications").(*schema.Set).List()),
})
}

schedule := getFirstOrNil(d.Get("schedule").(*schema.Set).List())
if schedule != nil {
alertParameters["active_when"] = map[string]interface{}{
"timeframes": []interface{}{
map[string]interface{}{
"days_of_week": transformWeekList(d.Get("schedule").(*schema.Set).List()[0].(map[string]interface{})["days"].([]interface{})),
"activity_starts": d.Get("schedule").(*schema.Set).List()[0].(map[string]interface{})["start"].(string),
"activity_ends": d.Get("schedule").(*schema.Set).List()[0].(map[string]interface{})["end"].(string),
},
},
}
}

if d.Get("content") != nil {
alertParameters["notif_payload_filter"] = d.Get("content").([]interface{})
}

alert, err := apiClient.Post("/external/alerts", alertParameters)
if err != nil {
return err
}
Expand All @@ -221,13 +296,26 @@ func resourceCoralogixAlertRead(d *schema.ResourceData, meta interface{}) error

d.Set("name", alert["name"].(string))
d.Set("severity", alert["severity"].(string))
d.Set("description", alert["description"].(string))
d.Set("enabled", alert["is_active"].(bool))
d.Set("type", alert["log_filter"].(map[string]interface{})["filter_type"].(string))
d.Set("filter", []interface{}{flattenAlertFilter(alert)})
d.Set("condition", []interface{}{flattenAlertCondition(alert)})
d.Set("notifications", []interface{}{flattenAlertNotifications(alert)})

if alert["description"] != nil {
d.Set("description", alert["description"].(string))
} else {
d.Set("description", "")
}

if alert["notif_payload_filter"] != nil && len(alert["notif_payload_filter"].([]interface{})) > 0 {
d.Set("content", alert["notif_payload_filter"])
}

if alert["active_when"] != nil && len(alert["active_when"].(map[string]interface{})["timeframes"].([]interface{})) > 0 {
d.Set("schedule", []interface{}{flattenAlertSchedule(alert)})
}

d.SetId(alert["id"].(string))

return nil
Expand Down
1 change: 0 additions & 1 deletion coralogix/resource_coralogix_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func resourceCoralogixRule() *schema.Resource {
},
},
},
Set: schema.HashString,
},
"expression": {
Type: schema.TypeString,
Expand Down
1 change: 0 additions & 1 deletion coralogix/resource_coralogix_rules_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func resourceCoralogixRulesGroup() *schema.Resource {
},
},
},
Set: schema.HashString,
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions docs/data-sources/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ data "coralogix_alert" "alert" {
* `type` - Alert type.
* `filter` - Alert filter.
* `condition` - Alert condition.
* `content` - List of fields attached to alert notification.
* `schedule` - Configuration of period when alert triggering will be allowed.
* `notifications` - Alert notifications.
8 changes: 8 additions & 0 deletions docs/resources/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ resource "coralogix_alert" "example" {
* `filter` - (Required) A `filter` block as documented below.
* `description` - (Optional) Alert description.
* `condition` - (Optional) A `condition` block as documented below.
* `schedule` - (Optional) A `schedule` block as documented below.
* `content` - (Optional) An array that contains log fields to be included with the alert notification.
* `notifications` - (Optional) A `notifications` block as documented below.

---
Expand All @@ -62,6 +64,12 @@ Each `condition` block should contains the following:
* `timeframe` - (Required) The bounded time frame for the threshold to be occurred within, to trigger the alert.
* `group_by` - (Optional) The field to **group by** on.

Each `schedule` block should contains the following:

* `days` - (Required) Days when alert triggering is allowed, one of the following: `Mo`, `Tu`, `We`, `Th`, `Fr`, `Sa`, `Su`.
* `start` - (Required) Time from which alert triggering is allowed, for example `00:00:00`.
* `end` - (Required) Time till which alert triggering is allowed, for example `23:59:59`.

Each `notifications` block should contains the following:

* `emails` - (Optional) List of email address to notify.
Expand Down

0 comments on commit 44c940b

Please sign in to comment.