-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathalerts.go
124 lines (110 loc) · 4.27 KB
/
alerts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package scalingo
import (
"context"
"time"
"gopkg.in/errgo.v1"
)
type AlertsService interface {
AlertsList(ctx context.Context, app string) ([]*Alert, error)
AlertAdd(ctx context.Context, app string, params AlertAddParams) (*Alert, error)
AlertShow(ctx context.Context, app, id string) (*Alert, error)
AlertUpdate(ctx context.Context, app, id string, params AlertUpdateParams) (*Alert, error)
AlertRemove(ctx context.Context, app, id string) error
}
var _ AlertsService = (*Client)(nil)
type Alert struct {
ID string `json:"id"`
AppID string `json:"app_id"`
ContainerType string `json:"container_type"`
Metric string `json:"metric"`
Limit float64 `json:"limit"`
Disabled bool `json:"disabled"`
SendWhenBelow bool `json:"send_when_below"`
DurationBeforeTrigger time.Duration `json:"duration_before_trigger"`
RemindEvery string `json:"remind_every"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Metadata map[string]interface{} `json:"metadata"`
Notifiers []string `json:"notifiers"`
}
type AlertsRes struct {
Alerts []*Alert `json:"alerts"`
}
type AlertRes struct {
Alert *Alert `json:"alert"`
}
func (c *Client) AlertsList(ctx context.Context, app string) ([]*Alert, error) {
var alertsRes AlertsRes
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "alerts", nil, &alertsRes)
if err != nil {
return nil, errgo.Notef(err, "fail to query the API to list an alert")
}
return alertsRes.Alerts, nil
}
type AlertAddParams struct {
ContainerType string
Metric string
Limit float64
Disabled bool
RemindEvery *time.Duration
DurationBeforeTrigger *time.Duration
SendWhenBelow bool
Notifiers []string
}
func (c *Client) AlertAdd(ctx context.Context, app string, params AlertAddParams) (*Alert, error) {
var alertRes AlertRes
a := &Alert{
ContainerType: params.ContainerType,
Metric: params.Metric,
Limit: params.Limit,
SendWhenBelow: params.SendWhenBelow,
Disabled: params.Disabled,
Notifiers: params.Notifiers,
}
if params.RemindEvery != nil {
a.RemindEvery = params.RemindEvery.String()
}
if params.DurationBeforeTrigger != nil {
a.DurationBeforeTrigger = *params.DurationBeforeTrigger
}
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", app, "alerts", AlertRes{
Alert: a,
}, &alertRes)
if err != nil {
return nil, errgo.Notef(err, "fail to query the API to create an alert")
}
return alertRes.Alert, nil
}
func (c *Client) AlertShow(ctx context.Context, app, id string) (*Alert, error) {
var alertRes AlertRes
err := c.ScalingoAPI().SubresourceGet(ctx, "apps", app, "alerts", id, nil, &alertRes)
if err != nil {
return nil, errgo.Notef(err, "fail to query the API to show an alert")
}
return alertRes.Alert, nil
}
type AlertUpdateParams struct {
ContainerType *string `json:"container_type,omitempty"`
Metric *string `json:"metric,omitempty"`
Limit *float64 `json:"limit,omitempty"`
Disabled *bool `json:"disabled,omitempty"`
DurationBeforeTrigger *time.Duration `json:"duration_before_trigger,omitempty"`
RemindEvery *time.Duration `json:"remind_every,omitempty"`
SendWhenBelow *bool `json:"send_when_below,omitempty"`
Notifiers *[]string `json:"notifiers,omitempty"`
}
func (c *Client) AlertUpdate(ctx context.Context, app, id string, params AlertUpdateParams) (*Alert, error) {
var alertRes AlertRes
err := c.ScalingoAPI().SubresourceUpdate(ctx, "apps", app, "alerts", id, params, &alertRes)
if err != nil {
return nil, errgo.Notef(err, "fail to query the API to update an alert")
}
return alertRes.Alert, nil
}
func (c *Client) AlertRemove(ctx context.Context, app, id string) error {
err := c.ScalingoAPI().SubresourceDelete(ctx, "apps", app, "alerts", id)
if err != nil {
return errgo.Notef(err, "fail to query the API to remove an alert")
}
return nil
}