forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
185 lines (163 loc) · 5.61 KB
/
service.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
package pagerduty
import (
"fmt"
"io/ioutil"
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/google/go-querystring/query"
)
type EmailFilter struct {
SubjectMode string `json:"subject_mode,omitempty"`
SubjectRegex string `json:"subject_regex,omitempty"`
BodyMode string `json:"body_mode,omitempty"`
BodyRegex string `json:"body_regex,omitempty"`
FromEmailMode string `json:"from_email_mode,omitempty"`
FromEmailRegex string `json:"from_email_regex,omitempty"`
}
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"`
IntegrationEmail string `json:"integration_email"`
EmailIncidentCreation string `json:"email_incident_creation,omitempty"`
EmailFilterMode string `json:"email_filter_mode"`
EmailFilters []EmailFilter `json:"email_filters,omitempty"`
}
type NamedTime struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
}
type ScheduledAction struct {
Type string `json:"type,omitempty"`
At NamedTime `json:"at,omitempty"`
ToUrgency string `json:"to_urgency"`
}
type SupportHours struct {
Type string `json:"type,omitempty"`
Urgency string `json:"urgency,omitempty"`
}
type SupportHoursDetails struct {
Type string `json:"type,omitempty"`
Timezone string `json:"time_zone"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
DaysOfWeek []uint `json:"days_of_week"`
}
type IncidentUrgencyRule struct {
Type string `json:"type,omitempty"`
DuringSupportHours SupportHours `json:"during_support_hours,omitempty"`
OutsideSupportHours SupportHours `json:"outside_support_hours,omitempty"`
}
type Service struct {
APIObject
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
AutoResolveTimeout uint `json:"auto_resolve_timeout,omitempty"`
AcknowledgementTimeout uint `json:"acknowledgement_timeout,omitempty"`
CreateAt string `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
LastIncidentTimestamp string `json:"last_incident_timestamp,omitempty"`
Integrations []Integration `json:"integrations,omitempty"`
EscalationPolicy EscalationPolicy `json:"escalation_policy,omitempty"`
Teams []Team `json:"teams,omitempty"`
IncidentUrgencyRule IncidentUrgencyRule `json:"incident_urgency_rule,omitempty"`
SupportHours SupportHoursDetails `json:"support_hours,omitempty"`
ScheduledActions []ScheduledAction `json:"scheduled_actions,omitempty"`
}
type ListServiceOptions struct {
APIListObject
TeamIDs []string `url:"team_ids,omitempty,brackets"`
TimeZone string `url:"time_zone,omitempty"`
SortBy string `url:"sort_by,omitempty"`
Query string `url:"query,omitempty"`
Includes []string `url:"include,omitempty,brackets"`
}
type ListServiceResponse struct {
APIListObject
Services []Service
}
func (c *Client) ListServices(o ListServiceOptions) (*ListServiceResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.Get("/services?" + v.Encode())
if err != nil {
return nil, err
}
var result ListServiceResponse
return &result, c.decodeJson(resp, &result)
}
type GetServiceOptions struct {
Includes []string `url:"include,brackets,omitempty"`
}
func (c *Client) GetService(id string, o GetServiceOptions) (*Service, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.Get("/services/" + id + "?" + v.Encode())
if err != nil {
return nil, err
}
var result map[string]Service
if err := c.decodeJson(resp, &result); err != nil {
return nil, err
}
s, ok := result["service"]
if !ok {
return nil, fmt.Errorf("JSON response does not have service field")
}
return &s, nil
}
func (c *Client) CreateService(s Service) error {
data := make(map[string]Service)
data["service"] = s
resp, err := c.Post("/services", data)
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
ct, rErr := ioutil.ReadAll(resp.Body)
if rErr == nil {
log.Debug(string(ct))
}
return fmt.Errorf("Failed to create. HTTP Status code: %d", resp.StatusCode)
}
return err
}
func (c *Client) UpdateService(s Service) error {
_, err := c.Put("/services/"+s.ID, s)
return err
}
func (c *Client) DeleteService(id string) error {
_, err := c.Delete("/services/" + id)
return err
}
func (c *Client) CreateIntegration(id string, i Integration) error {
_, err := c.Post("/services/"+id+"/integrations", i)
return err
}
type GetIntegrationOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}
func (c *Client) GetIntegration(serviceID, integrationID string, o GetIntegrationOptions) (*Integration, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
var result map[string]Integration
resp, err := c.Get("/services/" + serviceID + "/integrations/" + integrationID + "?" + v.Encode())
if err := c.decodeJson(resp, &result); err != nil {
return nil, err
}
i, ok := result["integration"]
if !ok {
return nil, fmt.Errorf("JSON responsde does not have integration field")
}
return &i, nil
}
func (c *Client) UpdateIntegration(serviceID string, i Integration) error {
_, err := c.Put("/services/"+serviceID+"/integrations/"+i.ID, i)
return err
}