usage_alerts.go raw
1 package rest
2
3 import (
4 "fmt"
5
6 "gopkg.in/ns1/ns1-go.v2/rest/model/alerting"
7 )
8
9 // NewUsageAlert creates a new usage alert with proper type and validation
10 func NewUsageAlert(name string, subtype string, alertAtPercent int, notifierListIds []string) (*alerting.Alert, error) {
11 alert := alerting.NewUsageAlert(name, subtype, alertAtPercent, notifierListIds)
12
13 if err := alerting.ValidateUsageAlert(alert); err != nil {
14 return nil, err
15 }
16
17 return alert, nil
18 }
19
20 // UsageAlertPatch defines the fields that can be updated on a usage alert
21 // Intentionally excluding Type and Subtype which cannot be changed via PATCH
22 type UsageAlertPatch struct {
23 Name *string `json:"name,omitempty"`
24 Data *alerting.UsageAlertData `json:"data,omitempty"`
25 NotifierListIds *[]string `json:"notifier_list_ids,omitempty"`
26 ZoneNames *[]string `json:"zone_names,omitempty"`
27 }
28
29 // NewUsageAlertPatchFunc is a function that modifies a UsageAlertPatch
30 type NewUsageAlertPatchFunc func(*UsageAlertPatch)
31
32 // NewUsageAlertPatch creates a new patch for updating a usage alert
33 func NewUsageAlertPatch(opts ...NewUsageAlertPatchFunc) (*UsageAlertPatch, error) {
34 patch := &UsageAlertPatch{}
35
36 for _, opt := range opts {
37 opt(patch)
38 }
39
40 // Validate data if it's being updated
41 if patch.Data != nil {
42 if patch.Data.AlertAtPercent < 1 || patch.Data.AlertAtPercent > 100 {
43 return nil, fmt.Errorf("data.alert_at_percent must be between 1 and 100")
44 }
45 }
46
47 return patch, nil
48 }
49
50 // WithName sets the name field in the patch
51 func WithName(name string) NewUsageAlertPatchFunc {
52 return func(p *UsageAlertPatch) {
53 p.Name = &name
54 }
55 }
56
57 // WithAlertAtPercent sets the alert_at_percent field in the patch
58 func WithAlertAtPercent(percent int) NewUsageAlertPatchFunc {
59 return func(p *UsageAlertPatch) {
60 p.Data = &alerting.UsageAlertData{AlertAtPercent: percent}
61 }
62 }
63
64 // WithNotifierListIds sets the notifier_list_ids field in the patch
65 func WithNotifierListIds(ids []string) NewUsageAlertPatchFunc {
66 return func(p *UsageAlertPatch) {
67 p.NotifierListIds = &ids
68 }
69 }
70
71 // WithZoneNames sets the zone_names field in the patch
72 func WithZoneNames(names []string) NewUsageAlertPatchFunc {
73 return func(p *UsageAlertPatch) {
74 p.ZoneNames = &names
75 }
76 }
77
78 // IsUsageAlert checks if an alert is a usage alert
79 func IsUsageAlert(alert *alerting.Alert) bool {
80 if alert == nil || alert.Type == nil || alert.Subtype == nil {
81 return false
82 }
83
84 if *alert.Type != alerting.AlertTypeAccount {
85 return false
86 }
87
88 if ok := alerting.AllowedUsageSubtypes[*alert.Subtype]; !ok {
89 return false
90 }
91
92 return true
93 }
94
95 // FilterUsageAlerts filters a list of alerts to only include usage alerts
96 func FilterUsageAlerts(alerts []*alerting.Alert) []*alerting.Alert {
97 usageAlerts := make([]*alerting.Alert, 0)
98 for _, alert := range alerts {
99 if IsUsageAlert(alert) {
100 usageAlerts = append(usageAlerts, alert)
101 }
102 }
103 return usageAlerts
104 }
105