forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
172 lines (140 loc) · 4.61 KB
/
target.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
package wavefront
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// Target represents a Wavefront Alert Target, for routing notifications
// associated with Alerts.
// Targets can be either email or webhook targets, and the Method must be set
// appropriately.
type Target struct {
// Description is a description of the target Target
Description string `json:"description"`
// ID is the Wavefront-assigned ID of an existing Target
ID *string `json:"id"`
// Template is the Mustache template for the notification body
Template string `json:"template"`
// Title is the title(name) of the Target
Title string `json:"title"`
// Method must be EMAIL, WEBHOOK or PAGERDUTY
Method string `json:"method"`
// Recipient is a comma-separated list of email addresses, webhook URL,
// or 32-digit PagerDuty key to which notifications will be sent for this Target
Recipient string `json:"recipient"`
// EmailSubject is the subject of the email which will be sent for this Target
// (EMAIL targets only)
EmailSubject string `json:"emailSubject"`
// IsHTMLContent is a boolean value for wavefront to add HTML Boilerplate
// while using HTML Templates as email.
// (EMAIL targets only)
IsHtmlContent bool `json:"isHtmlContent"`
// ContentType is the content type for webhook posts (e.g. application/json)
// (WEBHOOK targets only)
ContentType string `json:"contentType"`
// CustomHeaders are any custom HTTP headers that should be sent with webhook,
// in key:value syntax (WEBHOOK targets only)
CustomHeaders map[string]string `json:"customHttpHeaders"`
// Triggers is a list of Alert states that will trigger this notification
// and can include ALERT_OPENED, ALERT_RESOLVED, ALERT_STATUS_RESOLVED,
// ALERT_AFFECTED_BY_MAINTENANCE_WINDOW, ALERT_SNOOZED, ALERT_NO_DATA,
// ALERT_NO_DATA_RESOLVED
Triggers []string `json:"triggers"`
}
// Targets is used to perform target-related operations against the Wavefront API
type Targets struct {
// client is the Wavefront client used to perform target-related operations
client Wavefronter
}
const baseTargetPath = "/api/v2/notificant"
// Targets is used to return a client for target-related operations
func (c *Client) Targets() *Targets {
return &Targets{client: c}
}
// Get is used to retrieve an existing Target by ID.
// The ID field must be provided
func (t Targets) Get(target *Target) error {
if *target.ID == "" {
return fmt.Errorf("Target id field is not set")
}
return t.crudTarget("GET", fmt.Sprintf("%s/%s", baseTargetPath, *target.ID), target)
}
// Find returns all targets filtered by the given search conditions.
// If filter is nil, all targets are returned.
func (t Targets) Find(filter []*SearchCondition) ([]*Target, error) {
search := &Search{
client: t.client,
Type: "notificant",
Params: &SearchParams{
Conditions: filter,
},
}
var results []*Target
moreItems := true
for moreItems == true {
resp, err := search.Execute()
if err != nil {
return nil, err
}
var tmpres []*Target
err = json.Unmarshal(resp.Response.Items, &tmpres)
if err != nil {
return nil, err
}
results = append(results, tmpres...)
moreItems = resp.Response.MoreItems
search.Params.Offset = resp.NextOffset
}
return results, nil
}
// Create is used to create a Target in Wavefront.
// If successful, the ID field of the target will be populated.
func (t Targets) Create(target *Target) error {
return t.crudTarget("POST", baseTargetPath, target)
}
// Update is used to update an existing Target.
// The ID field of the target must be populated
func (t Targets) Update(target *Target) error {
if target.ID == nil {
return fmt.Errorf("target id field not set")
}
return t.crudTarget("PUT", fmt.Sprintf("%s/%s", baseTargetPath, *target.ID), target)
}
// Delete is used to delete an existing Target.
// The ID field of the target must be populated
func (t Targets) Delete(target *Target) error {
if target.ID == nil {
return fmt.Errorf("target id field not set")
}
err := t.crudTarget("DELETE", fmt.Sprintf("%s/%s", baseTargetPath, *target.ID), target)
if err != nil {
return err
}
//reset the ID field so deletion is not attempted again
target.ID = nil
return nil
}
func (t Targets) crudTarget(method, path string, target *Target) error {
payload, err := json.Marshal(target)
if err != nil {
return err
}
req, err := t.client.NewRequest(method, path, nil, payload)
if err != nil {
return err
}
resp, err := t.client.Do(req)
if err != nil {
return err
}
defer resp.Close()
body, err := ioutil.ReadAll(resp)
if err != nil {
return err
}
return json.Unmarshal(body, &struct {
Response *Target `json:"response"`
}{
Response: target,
})
}