-
Notifications
You must be signed in to change notification settings - Fork 89
/
channels.go
181 lines (149 loc) · 5.98 KB
/
channels.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
package alerts
import (
"context"
"fmt"
"github.com/newrelic/newrelic-client-go/pkg/errors"
"github.com/newrelic/newrelic-client-go/internal/serialization"
)
// ChannelType specifies the channel type used when creating the alert channel.
type ChannelType string
var (
// ChannelTypes enumerates the possible channel types for an alert channel.
ChannelTypes = struct {
Email ChannelType
OpsGenie ChannelType
PagerDuty ChannelType
Slack ChannelType
User ChannelType
VictorOps ChannelType
Webhook ChannelType
}{
Email: "email",
OpsGenie: "opsgenie",
PagerDuty: "pagerduty",
Slack: "slack",
User: "user",
VictorOps: "victorops",
Webhook: "webhook",
}
)
// Channel represents a New Relic alert notification channel
type Channel struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type ChannelType `json:"type,omitempty"`
Configuration ChannelConfiguration `json:"configuration,omitempty"`
Links ChannelLinks `json:"links,omitempty"`
}
// ChannelLinks represent the links between policies and alert channels
type ChannelLinks struct {
PolicyIDs []int `json:"policy_ids,omitempty"`
}
// ChannelConfiguration represents a Configuration type within Channels
type ChannelConfiguration struct {
Recipients string `json:"recipients,omitempty"`
IncludeJSONAttachment string `json:"include_json_attachment,omitempty"`
AuthToken string `json:"auth_token,omitempty"`
APIKey string `json:"api_key,omitempty"`
Teams string `json:"teams,omitempty"`
Tags string `json:"tags,omitempty"`
URL string `json:"url,omitempty"`
Channel string `json:"channel,omitempty"`
Key string `json:"key,omitempty"`
RouteKey string `json:"route_key,omitempty"`
ServiceKey string `json:"service_key,omitempty"`
BaseURL string `json:"base_url,omitempty"`
AuthUsername string `json:"auth_username,omitempty"`
AuthPassword string `json:"auth_password,omitempty"`
PayloadType string `json:"payload_type,omitempty"`
Region string `json:"region,omitempty"`
UserID string `json:"user_id,omitempty"`
// Payload is unmarshaled to type map[string]string
Payload serialization.MapStringInterface `json:"payload,omitempty"`
// Headers is unmarshaled to type map[string]string
Headers serialization.MapStringInterface `json:"headers,omitempty"`
}
// ListChannels returns all alert channels for a given account.
func (a *Alerts) ListChannels() ([]*Channel, error) {
return a.ListChannelsWithContext(context.Background())
}
// ListChannelsWithContext returns all alert channels for a given account.
func (a *Alerts) ListChannelsWithContext(ctx context.Context) ([]*Channel, error) {
alertChannels := []*Channel{}
nextURL := a.config.Region().RestURL("/alerts_channels.json")
for nextURL != "" {
response := alertChannelsResponse{}
resp, err := a.client.GetWithContext(ctx, nextURL, nil, &response)
if err != nil {
return nil, err
}
alertChannels = append(alertChannels, response.Channels...)
paging := a.pager.Parse(resp)
nextURL = paging.Next
}
return alertChannels, nil
}
// GetChannel returns a specific alert channel by ID for a given account.
func (a *Alerts) GetChannel(id int) (*Channel, error) {
return a.GetChannelWithContext(context.Background(), id)
}
// GetChannelWithContext returns a specific alert channel by ID for a given account.
func (a *Alerts) GetChannelWithContext(ctx context.Context, id int) (*Channel, error) {
channels, err := a.ListChannelsWithContext(ctx)
if err != nil {
return nil, err
}
for _, channel := range channels {
if channel.ID == id {
return channel, nil
}
}
return nil, errors.NewNotFoundf("no channel found for id %d", id)
}
// CreateChannel creates an alert channel within a given account.
// The configuration options different based on channel type.
// For more information on the different configurations, please
// view the New Relic API documentation for this endpoint.
// Docs: https://docs.newrelic.com/docs/alerts/rest-api-alerts/new-relic-alerts-rest-api/rest-api-calls-new-relic-alerts#channels
func (a *Alerts) CreateChannel(channel Channel) (*Channel, error) {
return a.CreateChannelWithContext(context.Background(), channel)
}
// CreateChannelWithContext creates an alert channel within a given account.
// The configuration options different based on channel type.
// For more information on the different configurations, please
// view the New Relic API documentation for this endpoint.
// Docs: https://docs.newrelic.com/docs/alerts/rest-api-alerts/new-relic-alerts-rest-api/rest-api-calls-new-relic-alerts#channels
func (a *Alerts) CreateChannelWithContext(ctx context.Context, channel Channel) (*Channel, error) {
reqBody := alertChannelRequestBody{
Channel: channel,
}
resp := alertChannelsResponse{}
_, err := a.client.PostWithContext(ctx, a.config.Region().RestURL("alerts_channels.json"), nil, &reqBody, &resp)
if err != nil {
return nil, err
}
return resp.Channels[0], nil
}
// DeleteChannel deletes the alert channel with the specified ID.
func (a *Alerts) DeleteChannel(id int) (*Channel, error) {
return a.DeleteChannelWithContext(context.Background(), id)
}
// DeleteChannelWithContext deletes the alert channel with the specified ID.
func (a *Alerts) DeleteChannelWithContext(ctx context.Context, id int) (*Channel, error) {
resp := alertChannelResponse{}
url := fmt.Sprintf("/alerts_channels/%d.json", id)
_, err := a.client.DeleteWithContext(ctx, a.config.Region().RestURL(url), nil, &resp)
if err != nil {
return nil, err
}
return &resp.Channel, nil
}
type alertChannelsResponse struct {
Channels []*Channel `json:"channels,omitempty"`
}
type alertChannelResponse struct {
Channel Channel `json:"channel,omitempty"`
}
type alertChannelRequestBody struct {
Channel Channel `json:"channel"`
}