forked from grafana/amixr-api-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_notification_rule.go
158 lines (129 loc) · 5.2 KB
/
user_notification_rule.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
package aapi
import (
"fmt"
"log"
"net/http"
)
// UserNotificationRuleService handles requests to user notification rule endpoints
//
// https://grafana.com/docs/oncall/latest/oncall-api-reference/personal_notification_rules/
type UserNotificationRuleService struct {
client *Client
url string
}
// NewUserNotificationRuleService creates UserNotificationRuleService with defined url
func NewUserNotificationRuleService(client *Client) *UserNotificationRuleService {
userNotificationRuleService := UserNotificationRuleService{client: client, url: "personal_notification_rules"}
return &userNotificationRuleService
}
type PaginatedUserNotificationRulesResponse struct {
PaginatedResponse
UserNotificationRules []*UserNotificationRule `json:"results"`
}
type UserNotificationRule struct {
ID string `json:"id"`
UserId string `json:"user_id"`
Position int `json:"position"`
Duration int `json:"duration"`
Important bool `json:"important"`
Type string `json:"type"`
}
type ListUserNotificationRuleOptions struct {
ListOptions
UserId string `url:"user_id,omitempty" json:"user_id,omitempty"`
Important string `url:"important,omitempty" json:"important,omitempty"`
}
// ListUserNotificationRules fetches all user notification rules for authorized organization
//
// https://grafana.com/docs/oncall/latest/oncall-api-reference/personal_notification_rules/#list-personal-notification-rules
func (service *UserNotificationRuleService) ListUserNotificationRules(opt *ListUserNotificationRuleOptions) (*PaginatedUserNotificationRulesResponse, *http.Response, error) {
req, err := service.client.NewRequest("GET", service.url, opt)
if err != nil {
return nil, nil, err
}
var userNotificationRules *PaginatedUserNotificationRulesResponse
resp, err := service.client.Do(req, &userNotificationRules)
if err != nil {
return nil, resp, err
}
return userNotificationRules, resp, err
}
type GetUserNotificationRuleOptions struct {
}
// GetUserNotificationRule fetches a user notification rule by given id
//
// https://grafana.com/docs/oncall/latest/oncall-api-reference/personal_notification_rules/#get-personal-notification-rule
func (service *UserNotificationRuleService) GetUserNotificationRule(id string, opt *GetUserNotificationRuleOptions) (*UserNotificationRule, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}
userNotificationRule := new(UserNotificationRule)
resp, err := service.client.Do(req, userNotificationRule)
if err != nil {
return nil, resp, err
}
return userNotificationRule, resp, err
}
type CreateUserNotificationRuleOptions struct {
UserId string `json:"user_id,omitempty"`
Position *int `json:"position,omitempty"`
Duration *int `json:"duration,omitempty"`
Important bool `json:"important,omitempty"`
Type string `json:"type,omitempty"`
ManualOrder bool `json:"manual_order,omitempty"`
}
// CreateUserNotificationRule creates a user notification rule for the given user, type, and position
//
// https://grafana.com/docs/oncall/latest/oncall-api-reference/personal_notification_rules/#post-a-personal-notification-rule
func (service *UserNotificationRuleService) CreateUserNotificationRule(opt *CreateUserNotificationRuleOptions) (*UserNotificationRule, *http.Response, error) {
u := fmt.Sprintf("%s/", service.url)
req, err := service.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}
userNotificationRule := new(UserNotificationRule)
resp, err := service.client.Do(req, userNotificationRule)
log.Printf("[DEBUG] request success")
if err != nil {
return nil, resp, err
}
return userNotificationRule, resp, err
}
type UpdateUserNotificationRuleOptions struct {
Position *int `json:"position,omitempty"`
Duration *int `json:"duration,omitempty"`
Type string `json:"type,omitempty"`
ManualOrder bool `json:"manual_order,omitempty"`
}
// UpdateUserNotificationRule updates user notification rule with new position, duration, and type
//
// NOTE: this endpoint is not currently publicly documented, but it does exist
func (service *UserNotificationRuleService) UpdateUserNotificationRule(id string, opt *UpdateUserNotificationRuleOptions) (*UserNotificationRule, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, nil, err
}
userNotificationRule := new(UserNotificationRule)
resp, err := service.client.Do(req, userNotificationRule)
if err != nil {
return nil, resp, err
}
return userNotificationRule, resp, err
}
type DeleteUserNotificationRuleOptions struct {
}
// DeleteUserNotificationRule deletes user notification rule
//
// https://grafana.com/docs/oncall/latest/oncall-api-reference/personal_notification_rules/#delete-a-personal-notification-rule
func (service *UserNotificationRuleService) DeleteUserNotificationRule(id string, opt *DeleteUserNotificationRuleOptions) (*http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("DELETE", u, opt)
if err != nil {
return nil, err
}
resp, err := service.client.Do(req, nil)
return resp, err
}