forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_integration.go
352 lines (284 loc) · 11.1 KB
/
service_integration.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package pagerduty
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/google/go-querystring/query"
)
// IntegrationEmailFilterMode is a type to respresent the different filter modes
// for a Generic Email Integration. This defines how the email filter rules
// (IntegrationEmailFilterRuleMode) are used when emails are ingested.
type IntegrationEmailFilterMode uint8
const (
// EmailFilterModeInvalid only exists to make it harder to use values of
// this type incorrectly. Please instead use one of EmailFilterModeAll,
// EmailFilterModeOr, EmailFilterModeAnd
//
// This value should not get marshaled to JSON by the encoding/json package.
EmailFilterModeInvalid IntegrationEmailFilterMode = iota
// EmailFilterModeAll means that all incoming email will be be accepted, and
// no email rules will be considered.
EmailFilterModeAll
// EmailFilterModeOr instructs the email filtering system to accept the
// email if one or more rules match the message.
EmailFilterModeOr
// EmailFilterModeAnd instructs the email filtering system to accept the
// email only if all of the rules match the message.
EmailFilterModeAnd
)
// string values for each IntegrationEmailFilterMode value
const (
efmAll = "all-email" // EmailFilterModeAll
efmOr = "or-rules-email" // EmailFilterModeOr
efmAnd = "and-rules-email" // EmailFilterModeAnd
)
func (i IntegrationEmailFilterMode) String() string {
switch i {
case EmailFilterModeAll:
return efmAll
case EmailFilterModeOr:
return efmOr
case EmailFilterModeAnd:
return efmAnd
default:
return "invalid"
}
}
// compile time encoding/json interface satisfaction assertions
var (
_ json.Marshaler = IntegrationEmailFilterMode(0)
_ json.Unmarshaler = (*IntegrationEmailFilterMode)(nil)
)
// MarshalJSON satisfies json.Marshaler
func (i IntegrationEmailFilterMode) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", i.String())), nil
}
// UnmarshalJSON satisfies json.Unmarshaler
func (i *IntegrationEmailFilterMode) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
if bytes.Equal(b, []byte(`null`)) {
return errors.New("value cannot be null")
}
// just return json.Unmarshal error
var s string
err := json.Unmarshal(b, &s)
if err == nil {
panic("this should not be possible...")
}
return err
}
v := string(b[1 : len(b)-1])
switch v {
case efmAll:
*i = EmailFilterModeAll
case efmOr:
*i = EmailFilterModeOr
case efmAnd:
*i = EmailFilterModeAnd
default:
return fmt.Errorf("unknown value %q", v)
}
return nil
}
// IntegrationEmailFilterRuleMode is a type to represent the different matching
// modes of Generic Email Integration Filer Rules without consumers of this
// package needing to be intimately familiar with the specifics of the REST API.
type IntegrationEmailFilterRuleMode uint8
const (
// EmailFilterRuleModeInvalid only exists to make it harder to use values of this
// type incorrectly. Please instead use one of EmailFilterRuleModeAlways,
// EmailFilterRuleModeMatch, or EmailFilterRuleModeNoMatch.
//
// This value should not get marshaled to JSON by the encoding/json package.
EmailFilterRuleModeInvalid IntegrationEmailFilterRuleMode = iota
// EmailFilterRuleModeAlways means that the specific value can be anything. Any
// associated regular expression will be ignored.
EmailFilterRuleModeAlways
// EmailFilterRuleModeMatch means that the associated regular expression must
// match the associated value.
EmailFilterRuleModeMatch
// EmailFilterRuleModeNoMatch means that the associated regular expression must NOT
// match the associated value.
EmailFilterRuleModeNoMatch
)
// string values for each IntegrationEmailFilterRuleMode value
const (
efrmAlways = "always" // EmailFilterRuleModeAlways
efrmMatch = "match" // EmailFilterRuleModeMatch
efrmNoMatch = "no-match" // EmailFilterRuleModeNoMatch
)
func (i IntegrationEmailFilterRuleMode) String() string {
switch i {
case EmailFilterRuleModeMatch:
return efrmMatch
case EmailFilterRuleModeNoMatch:
return efrmNoMatch
case EmailFilterRuleModeAlways:
return efrmAlways
default:
return "invalid"
}
}
// compile time encoding/json interface satisfaction assertions
var (
_ json.Marshaler = IntegrationEmailFilterRuleMode(0)
_ json.Unmarshaler = (*IntegrationEmailFilterRuleMode)(nil)
)
// MarshalJSON satisfies json.Marshaler
func (i IntegrationEmailFilterRuleMode) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", i.String())), nil
}
// UnmarshalJSON satisfies json.Unmarshaler
func (i *IntegrationEmailFilterRuleMode) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
if bytes.Equal(b, []byte(`null`)) {
return errors.New("value cannot be null")
}
// just return json.Unmarshal error
var s string
err := json.Unmarshal(b, &s)
if err == nil {
panic("this should not be possible...")
}
return err
}
v := string(b[1 : len(b)-1])
switch v {
case efrmMatch:
*i = EmailFilterRuleModeMatch
case efrmNoMatch:
*i = EmailFilterRuleModeNoMatch
case efrmAlways:
*i = EmailFilterRuleModeAlways
default:
return fmt.Errorf("unknown value %q", v)
}
return nil
}
// IntegrationEmailFilterRule represents a single email filter rule for an
// integration of type generic_email_inbound_integration. Information about how
// to configure email rules can be found here:
// https://support.pagerduty.com/docs/email-management-filters-and-rules.
type IntegrationEmailFilterRule struct {
// SubjectMode and SubjectRegex control the behaviors of how this filter
// matches the subject of an inbound email.
SubjectMode IntegrationEmailFilterRuleMode `json:"subject_mode,omitempty"`
SubjectRegex *string `json:"subject_regex,omitempty"`
// BodyMode and BodyRegex control the behaviors of how this filter matches
// the body of an inbound email.
BodyMode IntegrationEmailFilterRuleMode `json:"body_mode,omitempty"`
BodyRegex *string `json:"body_regex,omitempty"`
FromEmailMode IntegrationEmailFilterRuleMode `json:"from_email_mode,omitempty"`
FromEmailRegex *string `json:"from_email_regex,omitempty"`
}
// UnmarshalJSON satisfies json.Unmarshaler.
func (i *IntegrationEmailFilterRule) UnmarshalJSON(b []byte) error {
// the purpose of this function is to ensure that when unmarshaling, the
// different *string values are never nil pointers.
//
// this is not a communicated feature of the API, so if it chnages
// it's not a breaking change -- doesn't mean we can't try.
var ief integrationEmailFilterRule
if err := json.Unmarshal(b, &ief); err != nil {
return err
}
i.BodyMode = ief.BodyMode
i.SubjectMode = ief.SubjectMode
i.FromEmailMode = ief.FromEmailMode
// if the *string is nil, set it to a *string with value ""
if ief.SubjectRegex == nil {
i.SubjectRegex = new(string)
} else {
i.SubjectRegex = ief.SubjectRegex
}
if ief.BodyRegex == nil {
i.BodyRegex = new(string)
} else {
i.BodyRegex = ief.BodyRegex
}
if ief.FromEmailRegex == nil {
i.FromEmailRegex = new(string)
} else {
i.FromEmailRegex = ief.FromEmailRegex
}
return nil
}
type integrationEmailFilterRule struct {
SubjectMode IntegrationEmailFilterRuleMode `json:"subject_mode"`
SubjectRegex *string `json:"subject_regex,omitempty"`
BodyMode IntegrationEmailFilterRuleMode `json:"body_mode"`
BodyRegex *string `json:"body_regex,omitempty"`
FromEmailMode IntegrationEmailFilterRuleMode `json:"from_email_mode"`
FromEmailRegex *string `json:"from_email_regex,omitempty"`
}
// Integration is an endpoint (like Nagios, email, or an API call) that
// generates events, which are normalized and de-duplicated by PagerDuty to
// create incidents.
type Integration struct {
APIObject
Name string `json:"name,omitempty"`
Service *APIObject `json:"service,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Vendor *APIObject `json:"vendor,omitempty"`
IntegrationKey string `json:"integration_key,omitempty"`
IntegrationEmail string `json:"integration_email,omitempty"`
EmailFilterMode IntegrationEmailFilterMode `json:"email_filter_mode,omitempty"`
EmailFilters []IntegrationEmailFilterRule `json:"email_filters,omitempty"`
}
// CreateIntegration creates a new integration belonging to a service.
//
// Deprecated: Use CreateIntegrationWithContext instead.
func (c *Client) CreateIntegration(id string, i Integration) (*Integration, error) {
return c.CreateIntegrationWithContext(context.Background(), id, i)
}
// CreateIntegrationWithContext creates a new integration belonging to a service.
func (c *Client) CreateIntegrationWithContext(ctx context.Context, id string, i Integration) (*Integration, error) {
d := map[string]Integration{
"integration": i,
}
resp, err := c.post(ctx, "/services/"+id+"/integrations", d, nil)
return getIntegrationFromResponse(c, resp, err)
}
// GetIntegrationOptions is the data structure used when calling the GetIntegration API endpoint.
type GetIntegrationOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}
// GetIntegration gets details about an integration belonging to a service.
//
// Deprecated: Use GetIntegrationWithContext instead.
func (c *Client) GetIntegration(serviceID, integrationID string, o GetIntegrationOptions) (*Integration, error) {
return c.GetIntegrationWithContext(context.Background(), serviceID, integrationID, o)
}
// GetIntegrationWithContext gets details about an integration belonging to a service.
func (c *Client) GetIntegrationWithContext(ctx context.Context, serviceID, integrationID string, o GetIntegrationOptions) (*Integration, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/services/"+serviceID+"/integrations/"+integrationID+"?"+v.Encode(), nil)
return getIntegrationFromResponse(c, resp, err)
}
// UpdateIntegration updates an integration belonging to a service.
//
// Deprecated: Use UpdateIntegrationWithContext instead.
func (c *Client) UpdateIntegration(serviceID string, i Integration) (*Integration, error) {
return c.UpdateIntegrationWithContext(context.Background(), serviceID, i)
}
// UpdateIntegrationWithContext updates an integration belonging to a service.
func (c *Client) UpdateIntegrationWithContext(ctx context.Context, serviceID string, i Integration) (*Integration, error) {
resp, err := c.put(ctx, "/services/"+serviceID+"/integrations/"+i.ID, i, nil)
return getIntegrationFromResponse(c, resp, err)
}
// DeleteIntegration deletes an existing integration.
//
// Deprecated: Use DeleteIntegrationWithContext instead.
func (c *Client) DeleteIntegration(serviceID string, integrationID string) error {
return c.DeleteIntegrationWithContext(context.Background(), serviceID, integrationID)
}
// DeleteIntegrationWithContext deletes an existing integration.
func (c *Client) DeleteIntegrationWithContext(ctx context.Context, serviceID string, integrationID string) error {
_, err := c.delete(ctx, "/services/"+serviceID+"/integrations/"+integrationID)
return err
}