diff --git a/coralogix/data_source_coralogix_alert.go b/coralogix/data_source_coralogix_alert.go index 8d8c84b7..ca66ea14 100644 --- a/coralogix/data_source_coralogix_alert.go +++ b/coralogix/data_source_coralogix_alert.go @@ -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, @@ -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 diff --git a/coralogix/helpers.go b/coralogix/helpers.go index 7c53942a..8b36410d 100644 --- a/coralogix/helpers.go +++ b/coralogix/helpers.go @@ -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 { @@ -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] diff --git a/coralogix/resource_coralogix_alert.go b/coralogix/resource_coralogix_alert.go index 39369d47..9ec5aed4 100644 --- a/coralogix/resource_coralogix_alert.go +++ b/coralogix/resource_coralogix_alert.go @@ -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", @@ -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", @@ -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, @@ -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), @@ -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 } @@ -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 diff --git a/coralogix/resource_coralogix_rule.go b/coralogix/resource_coralogix_rule.go index 2fd374fa..e7400b22 100644 --- a/coralogix/resource_coralogix_rule.go +++ b/coralogix/resource_coralogix_rule.go @@ -82,7 +82,6 @@ func resourceCoralogixRule() *schema.Resource { }, }, }, - Set: schema.HashString, }, "expression": { Type: schema.TypeString, diff --git a/coralogix/resource_coralogix_rules_group.go b/coralogix/resource_coralogix_rules_group.go index d713c352..5b063485 100644 --- a/coralogix/resource_coralogix_rules_group.go +++ b/coralogix/resource_coralogix_rules_group.go @@ -61,7 +61,6 @@ func resourceCoralogixRulesGroup() *schema.Resource { }, }, }, - Set: schema.HashString, }, }, } diff --git a/docs/data-sources/alert.md b/docs/data-sources/alert.md index cb0b2086..3a3b05a5 100644 --- a/docs/data-sources/alert.md +++ b/docs/data-sources/alert.md @@ -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. \ No newline at end of file diff --git a/docs/resources/alert.md b/docs/resources/alert.md index 760ca7b9..f273e221 100644 --- a/docs/resources/alert.md +++ b/docs/resources/alert.md @@ -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. --- @@ -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.