/
webhook.go
214 lines (173 loc) · 5.56 KB
/
webhook.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ Webhooks = (*webhooks)(nil)
// Webhooks describes all the webhooks related methods that the Scalr
// IACP API supports.
//
// IACP API docs: https://www.scalr.com/docs/en/latest/api/index.html
type Webhooks interface {
// List the webhooks.
List(ctx context.Context, options WebhookListOptions) (*WebhookList, error)
Create(ctx context.Context, options WebhookCreateOptions) (*Webhook, error)
Read(ctx context.Context, webhook string) (*Webhook, error)
Update(ctx context.Context, webhook string, options WebhookUpdateOptions) (*Webhook, error)
Delete(ctx context.Context, webhook string) error
}
// webhooks implements Webhooks.
type webhooks struct {
client *Client
}
// WebhookList represents a list of webhooks.
type WebhookList struct {
*Pagination
Items []*Webhook
}
type EventDefinition struct {
ID string `jsonapi:"primary,event-definitions"`
}
// Webhook represents a Scalr IACP webhook.
type Webhook struct {
ID string `jsonapi:"primary,webhooks"`
Enabled bool `jsonapi:"attr,enabled"`
LastTriggeredAt *time.Time `jsonapi:"attr,last-triggered-at,iso8601"`
Name string `jsonapi:"attr,name"`
// Relations
Workspace *Workspace `jsonapi:"relation,workspace"`
Environment *Environment `jsonapi:"relation,environment"`
Account *Account `jsonapi:"relation,account"`
Endpoint *Endpoint `jsonapi:"relation,endpoint"`
Events []*EventDefinition `jsonapi:"relation,events"`
}
// WebhookListOptions represents the options for listing webhooks.
type WebhookListOptions struct {
ListOptions
// Query string.
Query *string `url:"query,omitempty"`
Name *string `url:"query,omitempty"`
// The comma-separated list of attributes.
Sort *string `url:"sort,omitempty"`
// The comma-separated list of relationship paths.
Include *string `url:"include,omitempty"`
// Filter by enabled
Enabled *bool `url:"filter[enabled],omitempty"`
// Event filter
Event *string `url:"filter[event],omitempty"`
// Scope filters.
Workspace *string `url:"filter[workspace],omitempty"`
Environment *string `url:"filter[environment],omitempty"`
Account *string `url:"filter[account],omitempty"`
}
// List the webhooks.
func (s *webhooks) List(ctx context.Context, options WebhookListOptions) (*WebhookList, error) {
req, err := s.client.newRequest("GET", "webhooks", &options)
if err != nil {
return nil, err
}
wl := &WebhookList{}
err = s.client.do(ctx, req, wl)
if err != nil {
return nil, err
}
return wl, nil
}
// WebhookCreateOptions represents the options for creating a new webhook.
type WebhookCreateOptions struct {
ID string `jsonapi:"primary,webhooks"`
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
Name *string `jsonapi:"attr,name"`
// Relations
Workspace *Workspace `jsonapi:"relation,workspace,omitempty"`
Environment *Environment `jsonapi:"relation,environment,omitempty"`
Account *Account `jsonapi:"relation,account"`
Endpoint *Endpoint `jsonapi:"relation,endpoint"`
Events []*EventDefinition `jsonapi:"relation,events"`
}
func (o WebhookCreateOptions) valid() error {
if o.Name == nil {
return errors.New("missing name")
}
return nil
}
// Create is used to create a new webhook.
func (s *webhooks) Create(ctx context.Context, options WebhookCreateOptions) (*Webhook, error) {
if err := options.valid(); err != nil {
return nil, err
}
// Make sure we don't send a user provided ID.
options.ID = ""
req, err := s.client.newRequest("POST", "webhooks", &options)
if err != nil {
return nil, err
}
w := &Webhook{}
err = s.client.do(ctx, req, w)
if err != nil {
return nil, err
}
return w, nil
}
// Read a webhook by its ID.
func (s *webhooks) Read(ctx context.Context, webhookID string) (*Webhook, error) {
if !validStringID(&webhookID) {
return nil, errors.New("invalid value for webhook ID")
}
u := fmt.Sprintf("webhooks/%s", url.QueryEscape(webhookID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
w := &Webhook{}
err = s.client.do(ctx, req, w)
if err != nil {
return nil, err
}
return w, nil
}
// WebhookUpdateOptions represents the options for updating a webhook.
type WebhookUpdateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,webhooks"`
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
Name *string `jsonapi:"attr,name"`
// Relations
Endpoint *Endpoint `jsonapi:"relation,endpoint"`
Events []*EventDefinition `jsonapi:"relation,events"`
}
// Update settings of an existing webhook.
func (s *webhooks) Update(ctx context.Context, webhookID string, options WebhookUpdateOptions) (*Webhook, error) {
if !validStringID(&webhookID) {
return nil, errors.New("invalid value for webhook ID")
}
// Make sure we don't send a user provided ID.
options.ID = ""
u := fmt.Sprintf("webhooks/%s", url.QueryEscape(webhookID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
w := &Webhook{}
err = s.client.do(ctx, req, w)
if err != nil {
return nil, err
}
return w, nil
}
// Delete an webhook by its ID.
func (s *webhooks) Delete(ctx context.Context, webhookID string) error {
if !validStringID(&webhookID) {
return errors.New("invalid value for webhook ID")
}
u := fmt.Sprintf("webhooks/%s", url.QueryEscape(webhookID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}