forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusiness_service.go
194 lines (159 loc) · 7.04 KB
/
business_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
186
187
188
189
190
191
192
193
194
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// BusinessService represents a business service.
type BusinessService struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Summary string `json:"summary,omitempty"`
Self string `json:"self,omitempty"`
PointOfContact string `json:"point_of_contact,omitempty"`
HTMLUrl string `json:"html_url,omitempty"`
Description string `json:"description,omitempty"`
Team *BusinessServiceTeam `json:"team,omitempty"`
}
// BusinessServiceTeam represents a team object in a business service
type BusinessServiceTeam struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Self string `json:"self,omitempty"`
}
// BusinessServicePayload represents payload with a business service object
type BusinessServicePayload struct {
BusinessService *BusinessService `json:"business_service,omitempty"`
}
// ListBusinessServicesResponse represents a list response of business services.
type ListBusinessServicesResponse struct {
Total uint `json:"total,omitempty"`
BusinessServices []*BusinessService `json:"business_services,omitempty"`
Offset uint `json:"offset,omitempty"`
More bool `json:"more,omitempty"`
Limit uint `json:"limit,omitempty"`
}
// ListBusinessServiceOptions is the data structure used when calling the ListBusinessServices API endpoint.
type ListBusinessServiceOptions struct {
APIListObject
}
// ListBusinessServices lists existing business services. This method currently
// handles pagination of the response, so all business services should be
// present.
//
// Please note that the automatic pagination will be removed in v2 of this
// package, so it's recommended to use ListBusinessServicesPaginated instead.
func (c *Client) ListBusinessServices(o ListBusinessServiceOptions) (*ListBusinessServicesResponse, error) {
bss, err := c.ListBusinessServicesPaginated(context.Background(), o)
if err != nil {
return nil, err
}
return &ListBusinessServicesResponse{BusinessServices: bss}, nil
}
// ListBusinessServicesPaginated lists existing business services, automatically
// handling pagination and returning the full collection.
func (c *Client) ListBusinessServicesPaginated(ctx context.Context, o ListBusinessServiceOptions) ([]*BusinessService, error) {
queryParms, err := query.Values(o)
if err != nil {
return nil, err
}
var businessServices []*BusinessService
// Create a handler closure capable of parsing data from the business_services endpoint
// and appending resultant business_services to the return slice.
responseHandler := func(response *http.Response) (APIListObject, error) {
var result ListBusinessServicesResponse
if err := c.decodeJSON(response, &result); err != nil {
return APIListObject{}, err
}
businessServices = append(businessServices, result.BusinessServices...)
// Return stats on the current page. Caller can use this information to
// adjust for requesting additional pages.
return APIListObject{
More: result.More,
Offset: result.Offset,
Limit: result.Limit,
}, nil
}
// Make call to get all pages associated with the base endpoint.
if err := c.pagedGet(ctx, "/business_services"+queryParms.Encode(), responseHandler); err != nil {
return nil, err
}
return businessServices, nil
}
// CreateBusinessService creates a new business service. It's recommended to use
// CreateBusinessServiceWithContext instead
func (c *Client) CreateBusinessService(b *BusinessService) (*BusinessService, *http.Response, error) {
return c.createBusinessServiceWithContext(context.Background(), b)
}
// CreateBusinessServiceWithContext creates a new business service.
func (c *Client) CreateBusinessServiceWithContext(ctx context.Context, b *BusinessService) (*BusinessService, error) {
bs, _, err := c.createBusinessServiceWithContext(ctx, b)
return bs, err
}
func (c *Client) createBusinessServiceWithContext(ctx context.Context, b *BusinessService) (*BusinessService, *http.Response, error) {
d := map[string]*BusinessService{
"business_service": b,
}
resp, err := c.post(ctx, "/business_services", d, nil)
return getBusinessServiceFromResponse(c, resp, err)
}
// GetBusinessService gets details about a business service. It's recommended to
// use GetBusinessServiceWithContext instead.
func (c *Client) GetBusinessService(id string) (*BusinessService, *http.Response, error) {
return c.getBusinessServiceWithContext(context.Background(), id)
}
// GetBusinessServiceWithContext gets details about a business service.
func (c *Client) GetBusinessServiceWithContext(ctx context.Context, id string) (*BusinessService, error) {
bs, _, err := c.getBusinessServiceWithContext(ctx, id)
return bs, err
}
func (c *Client) getBusinessServiceWithContext(ctx context.Context, id string) (*BusinessService, *http.Response, error) {
resp, err := c.get(ctx, "/business_services/"+id)
return getBusinessServiceFromResponse(c, resp, err)
}
// DeleteBusinessService deletes a business_service. It's recommended to use
// DeleteBusinessServiceWithContext instead.
func (c *Client) DeleteBusinessService(id string) error {
return c.DeleteBusinessServiceWithContext(context.Background(), id)
}
// DeleteBusinessServiceWithContext deletes a business_service.
func (c *Client) DeleteBusinessServiceWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/business_services/"+id)
return err
}
// UpdateBusinessService updates a business_service. It's recommended to use
// UpdateBusinessServiceWithContext instead.
func (c *Client) UpdateBusinessService(b *BusinessService) (*BusinessService, *http.Response, error) {
return c.updateBusinessServiceWithContext(context.Background(), b)
}
// UpdateBusinessServiceWithContext updates a business_service.
func (c *Client) UpdateBusinessServiceWithContext(ctx context.Context, b *BusinessService) (*BusinessService, error) {
bs, _, err := c.updateBusinessServiceWithContext(ctx, b)
return bs, err
}
func (c *Client) updateBusinessServiceWithContext(ctx context.Context, b *BusinessService) (*BusinessService, *http.Response, error) {
id := b.ID
b.ID = ""
d := map[string]*BusinessService{
"business_service": b,
}
resp, err := c.put(ctx, "/business_services/"+id, d, nil)
return getBusinessServiceFromResponse(c, resp, err)
}
func getBusinessServiceFromResponse(c *Client, resp *http.Response, err error) (*BusinessService, *http.Response, error) {
if err != nil {
return nil, nil, err
}
var target map[string]BusinessService
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "business_service"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, resp, nil
}