-
Notifications
You must be signed in to change notification settings - Fork 5
/
contact_group.go
277 lines (228 loc) · 8.7 KB
/
contact_group.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
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Contact Group API support - Fetch, Create, Update, Delete, and Search
// See: https://login.circonus.com/resources/api/calls/contact_group
package apiclient
import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"
"github.com/circonus-labs/go-apiclient/config"
"github.com/pkg/errors"
)
// ContactGroupAlertFormats define alert formats
type ContactGroupAlertFormats struct {
LongMessage *string `json:"long_message,omitempty"` // string - 2019-10-09 null no longer accepted, must be omitted if not set
LongSubject *string `json:"long_subject,omitempty"` // string - 2019-10-09 null no longer accepted, must be omitted if not set
LongSummary *string `json:"long_summary,omitempty"` // string - 2019-10-09 null no longer accepted, must be omitted if not set
ShortMessage *string `json:"short_message,omitempty"` // string - 2019-10-09 null no longer accepted, must be omitted if not set
ShortSummary *string `json:"short_summary,omitempty"` // string - 2019-10-09 null no longer accepted, must be omitted if not set
}
// ContactGroupContactsExternal external contacts
type ContactGroupContactsExternal struct {
Info string `json:"contact_info"` // string
Method string `json:"method"` // string
}
// ContactGroupContactsUser user contacts
type ContactGroupContactsUser struct {
Info string `json:"_contact_info,omitempty"` // string
Method string `json:"method"` // string
UserCID string `json:"user"` // string
}
// ContactGroupContacts list of contacts
type ContactGroupContacts struct {
External []ContactGroupContactsExternal `json:"external"` // [] len >= 0
Users []ContactGroupContactsUser `json:"users"` // [] len >= 0
}
// ContactGroupEscalation defines escalations for severity levels
type ContactGroupEscalation struct {
ContactGroupCID string `json:"contact_group"` // string
After uint `json:"after"` // uint
}
// ContactGroup defines a contact group. See https://login.circonus.com/resources/api/calls/contact_group for more information.
type ContactGroup struct {
CID string `json:"_cid,omitempty"` // string
LastModifiedBy string `json:"_last_modified_by,omitempty"` // string
Name string `json:"name,omitempty"` // string
GroupType string `json:"group_type,omitempty"` // string - new 2019-10-09
AlertFormats ContactGroupAlertFormats `json:"alert_formats,omitempty"` // ContactGroupAlertFormats
Contacts ContactGroupContacts `json:"contacts,omitempty"` // ContactGroupContacts
Escalations []*ContactGroupEscalation `json:"escalations,omitempty"` // [] len == 5, elements: ContactGroupEscalation or null
Reminders []uint `json:"reminders,omitempty"` // [] len == 5
Tags []string `json:"tags,omitempty"` // [] len >= 0
LastModified uint `json:"_last_modified,omitempty"` // uint
AggregationWindow uint `json:"aggregation_window,omitempty"` // uint
AlwaysSendClear bool `json:"always_send_clear,omitempty"` // bool - new 2019-10-09
}
// NewContactGroup returns a ContactGroup (with defaults, if applicable)
func NewContactGroup() *ContactGroup {
return &ContactGroup{
Escalations: make([]*ContactGroupEscalation, config.NumSeverityLevels),
Reminders: make([]uint, config.NumSeverityLevels),
Contacts: ContactGroupContacts{
External: []ContactGroupContactsExternal{},
Users: []ContactGroupContactsUser{},
},
}
}
// FetchContactGroup retrieves contact group with passed cid.
func (a *API) FetchContactGroup(cid CIDType) (*ContactGroup, error) {
if cid == nil || *cid == "" {
return nil, errors.New("invalid contact group CID (none)")
}
var groupCID string
if !strings.HasPrefix(*cid, config.ContactGroupPrefix) {
groupCID = fmt.Sprintf("%s/%s", config.ContactGroupPrefix, *cid)
} else {
groupCID = *cid
}
matched, err := regexp.MatchString(config.ContactGroupCIDRegex, groupCID)
if err != nil {
return nil, err
}
if !matched {
return nil, errors.Errorf("invalid contact group CID (%s)", groupCID)
}
result, err := a.Get(groupCID)
if err != nil {
return nil, errors.Wrap(err, "fetching contact group")
}
if a.Debug {
a.Log.Printf("fetch contact group, received JSON: %s", string(result))
}
group := new(ContactGroup)
if err := json.Unmarshal(result, group); err != nil {
return nil, errors.Wrap(err, "parsing contact group")
}
return group, nil
}
// FetchContactGroups retrieves all contact groups available to the API Token.
func (a *API) FetchContactGroups() (*[]ContactGroup, error) {
result, err := a.Get(config.ContactGroupPrefix)
if err != nil {
return nil, errors.Wrap(err, "fetching contact groups")
}
var groups []ContactGroup
if err := json.Unmarshal(result, &groups); err != nil {
return nil, errors.Wrap(err, "parsing contact groups")
}
return &groups, nil
}
// UpdateContactGroup updates passed contact group.
func (a *API) UpdateContactGroup(cfg *ContactGroup) (*ContactGroup, error) {
if cfg == nil {
return nil, errors.New("invalid contact group config (nil)")
}
groupCID := cfg.CID
matched, err := regexp.MatchString(config.ContactGroupCIDRegex, groupCID)
if err != nil {
return nil, err
}
if !matched {
return nil, errors.Errorf("invalid contact group CID (%s)", groupCID)
}
jsonCfg, err := json.Marshal(cfg)
if err != nil {
return nil, err
}
if a.Debug {
a.Log.Printf("update contact group, sending JSON: %s", string(jsonCfg))
}
result, err := a.Put(groupCID, jsonCfg)
if err != nil {
return nil, errors.Wrap(err, "updating contact group")
}
group := &ContactGroup{}
if err := json.Unmarshal(result, group); err != nil {
return nil, errors.Wrap(err, "parsing contact group")
}
return group, nil
}
// CreateContactGroup creates a new contact group.
func (a *API) CreateContactGroup(cfg *ContactGroup) (*ContactGroup, error) {
if cfg == nil {
return nil, errors.New("invalid contact group config (nil)")
}
jsonCfg, err := json.Marshal(cfg)
if err != nil {
return nil, err
}
if a.Debug {
a.Log.Printf("create contact group, sending JSON: %s", string(jsonCfg))
}
result, err := a.Post(config.ContactGroupPrefix, jsonCfg)
if err != nil {
return nil, errors.Wrap(err, "creating contact group")
}
group := &ContactGroup{}
if err := json.Unmarshal(result, group); err != nil {
return nil, errors.Wrap(err, "parsing contact group")
}
return group, nil
}
// DeleteContactGroup deletes passed contact group.
func (a *API) DeleteContactGroup(cfg *ContactGroup) (bool, error) {
if cfg == nil {
return false, errors.New("invalid contact group config (nil)")
}
return a.DeleteContactGroupByCID(CIDType(&cfg.CID))
}
// DeleteContactGroupByCID deletes contact group with passed cid.
func (a *API) DeleteContactGroupByCID(cid CIDType) (bool, error) {
if cid == nil || *cid == "" {
return false, errors.New("invalid contact group CID (none)")
}
var groupCID string
if !strings.HasPrefix(*cid, config.ContactGroupPrefix) {
groupCID = fmt.Sprintf("%s/%s", config.ContactGroupPrefix, *cid)
} else {
groupCID = *cid
}
matched, err := regexp.MatchString(config.ContactGroupCIDRegex, groupCID)
if err != nil {
return false, err
}
if !matched {
return false, errors.Errorf("invalid contact group CID (%s)", groupCID)
}
_, err = a.Delete(groupCID)
if err != nil {
return false, errors.Wrap(err, "deleting contact group")
}
return true, nil
}
// SearchContactGroups returns contact groups matching the specified
// search query and/or filter. If nil is passed for both parameters
// all contact groups will be returned.
func (a *API) SearchContactGroups(searchCriteria *SearchQueryType, filterCriteria *SearchFilterType) (*[]ContactGroup, error) {
q := url.Values{}
if searchCriteria != nil && *searchCriteria != "" {
q.Set("search", string(*searchCriteria))
}
if filterCriteria != nil && len(*filterCriteria) > 0 {
for filter, criteria := range *filterCriteria {
for _, val := range criteria {
q.Add(filter, val)
}
}
}
if q.Encode() == "" {
return a.FetchContactGroups()
}
reqURL := url.URL{
Path: config.ContactGroupPrefix,
RawQuery: q.Encode(),
}
result, err := a.Get(reqURL.String())
if err != nil {
return nil, errors.Wrap(err, "searching contact groups")
}
var groups []ContactGroup
if err := json.Unmarshal(result, &groups); err != nil {
return nil, errors.Wrap(err, "parsing contact groups")
}
return &groups, nil
}