-
Notifications
You must be signed in to change notification settings - Fork 8
/
notifiers.go
122 lines (101 loc) · 4.03 KB
/
notifiers.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
package scalingo
import (
"context"
"encoding/json"
"time"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/debug"
)
type NotifiersService interface {
NotifiersList(ctx context.Context, app string) (Notifiers, error)
NotifierProvision(ctx context.Context, app string, params NotifierParams) (*Notifier, error)
NotifierByID(ctx context.Context, app, ID string) (*Notifier, error)
NotifierUpdate(ctx context.Context, app, ID string, params NotifierParams) (*Notifier, error)
NotifierDestroy(ctx context.Context, app, ID string) error
}
var _ NotifiersService = (*Client)(nil)
// Struct used to represent a notifier.
type Notifier struct {
ID string `json:"id"`
AppID string `json:"app_id"`
Active *bool `json:"active,omitempty"`
Name string `json:"name,omitempty"`
Type NotifierType `json:"type"`
SendAllEvents *bool `json:"send_all_events,omitempty"`
SendAllAlerts *bool `json:"send_all_alerts,omitempty"`
SelectedEventIDs []string `json:"selected_event_ids,omitempty"`
TypeData map[string]interface{} `json:"-"`
RawTypeData json.RawMessage `json:"type_data"`
PlatformID string `json:"platform_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// NotifierParams will be given as a parameter in notifiers function's
type NotifierParams struct {
Active *bool
Name string
SendAllEvents *bool
SendAllAlerts *bool
SelectedEventIDs []string
PlatformID string
// Options
PhoneNumber string // SMS notifier
Emails []string // Email notifier
UserIDs []string // Email notifier
WebhookURL string // Webhook and Slack notifier
}
// The struct that will be serialized in all notifier's request
type notifierRequestParams struct {
NotifierOutput `json:"notifier"`
}
// The struct that will be deserialized from all notifier request response
type notifierRequestRes struct {
Notifier Notifier `json:"notifier"`
}
type notifiersRequestRes struct {
Notifiers []*Notifier `json:"notifiers"`
}
func (c *Client) NotifiersList(ctx context.Context, app string) (Notifiers, error) {
var notifiersRes notifiersRequestRes
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "notifiers", nil, ¬ifiersRes)
if err != nil {
return nil, errgo.Mask(err)
}
var notifiers Notifiers
for _, not := range notifiersRes.Notifiers {
notifiers = append(notifiers, not.Specialize())
}
return notifiers, nil
}
func (c *Client) NotifierProvision(ctx context.Context, app string, params NotifierParams) (*Notifier, error) {
var notifierRes notifierRequestRes
notifier := newOutputNotifier(params)
notifierRequestParams := ¬ifierRequestParams{notifier}
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", app, "notifiers", notifierRequestParams, ¬ifierRes)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return ¬ifierRes.Notifier, nil
}
func (c *Client) NotifierByID(ctx context.Context, app, ID string) (*Notifier, error) {
var notifierRes notifierRequestRes
err := c.ScalingoAPI().SubresourceGet(ctx, "apps", app, "notifiers", ID, nil, ¬ifierRes)
if err != nil {
return nil, errgo.Mask(err)
}
return ¬ifierRes.Notifier, nil
}
func (c *Client) NotifierUpdate(ctx context.Context, app, ID string, params NotifierParams) (*Notifier, error) {
var notifierRes notifierRequestRes
notifier := newOutputNotifier(params)
notifierRequestParams := ¬ifierRequestParams{notifier}
debug.Printf("[Notifier params]\n%+v", notifier)
err := c.ScalingoAPI().SubresourceUpdate(ctx, "apps", app, "notifiers", ID, notifierRequestParams, ¬ifierRes)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return ¬ifierRes.Notifier, nil
}
func (c *Client) NotifierDestroy(ctx context.Context, app, ID string) error {
return c.ScalingoAPI().SubresourceDelete(ctx, "apps", app, "notifiers", ID)
}