forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.go
168 lines (137 loc) · 4.79 KB
/
addon.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
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// Addon is a third-party add-on to PagerDuty's UI.
type Addon struct {
APIObject
Name string `json:"name,omitempty"`
Src string `json:"src,omitempty"`
Services []APIObject `json:"services,omitempty"`
}
// ListAddonOptions are the options available when calling the ListAddons API endpoint.
type ListAddonOptions struct {
// Limit is the pagination parameter that limits the number of results per
// page. PagerDuty defaults this value to 25 if omitted, and sets an upper
// bound of 100.
Limit uint `url:"limit,omitempty"`
// Offset is the pagination parameter that specifies the offset at which to
// start pagination results. When trying to request the next page of
// results, the new Offset value should be currentOffset + Limit.
Offset uint `url:"offset,omitempty"`
// Total is the pagination parameter to request that the API return the
// total count of items in the response. If this field is omitted or set to
// false, the total number of results will not be sent back from the PagerDuty API.
//
// Setting this to true will slow down the API response times, and so it's
// recommended to omit it unless you've a specific reason for wanting the
// total count of items in the collection.
Total bool `url:"total,omitempty"`
Includes []string `url:"include,omitempty,brackets"`
ServiceIDs []string `url:"service_ids,omitempty,brackets"`
Filter string `url:"filter,omitempty"`
}
// ListAddonResponse is the response when calling the ListAddons API endpoint.
type ListAddonResponse struct {
APIListObject
Addons []Addon `json:"addons"`
}
// ListAddons lists all of the add-ons installed on your account.
//
// Deprecated: Use ListAddonsWithContext instead.
func (c *Client) ListAddons(o ListAddonOptions) (*ListAddonResponse, error) {
return c.ListAddonsWithContext(context.Background(), o)
}
// ListAddonsWithContext lists all of the add-ons installed on your account.
func (c *Client) ListAddonsWithContext(ctx context.Context, o ListAddonOptions) (*ListAddonResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/addons?"+v.Encode())
if err != nil {
return nil, err
}
var result ListAddonResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// InstallAddon installs an add-on for your account.
//
// Deprecated: Use InstallAddonWithContext instead.
func (c *Client) InstallAddon(a Addon) (*Addon, error) {
return c.InstallAddonWithContext(context.Background(), a)
}
// InstallAddonWithContext installs an add-on for your account.
func (c *Client) InstallAddonWithContext(ctx context.Context, a Addon) (*Addon, error) {
d := map[string]Addon{
"addon": a,
}
resp, err := c.post(ctx, "/addons", d, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("Failed to create. HTTP Status code: %d", resp.StatusCode)
}
return getAddonFromResponse(c, resp)
}
// DeleteAddon deletes an add-on from your account.
//
// Deprecated: Use DeleteAddonWithContext instead.
func (c *Client) DeleteAddon(id string) error {
return c.DeleteAddonWithContext(context.Background(), id)
}
// DeleteAddonWithContext deletes an add-on from your account.
func (c *Client) DeleteAddonWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/addons/"+id)
return err
}
// GetAddon gets details about an existing add-on.
//
// Deprecated: Use GetAddonWithContext instead.
func (c *Client) GetAddon(id string) (*Addon, error) {
return c.GetAddonWithContext(context.Background(), id)
}
// GetAddonWithContext gets details about an existing add-on.
func (c *Client) GetAddonWithContext(ctx context.Context, id string) (*Addon, error) {
resp, err := c.get(ctx, "/addons/"+id)
if err != nil {
return nil, err
}
return getAddonFromResponse(c, resp)
}
// UpdateAddon updates an existing add-on.
//
// Deprecated: Use UpdateAddonWithContext instead.
func (c *Client) UpdateAddon(id string, a Addon) (*Addon, error) {
return c.UpdateAddonWithContext(context.Background(), id, a)
}
// UpdateAddonWithContext updates an existing add-on.
func (c *Client) UpdateAddonWithContext(ctx context.Context, id string, a Addon) (*Addon, error) {
d := map[string]Addon{
"addon": a,
}
resp, err := c.put(ctx, "/addons/"+id, d, nil)
if err != nil {
return nil, err
}
return getAddonFromResponse(c, resp)
}
func getAddonFromResponse(c *Client, resp *http.Response) (*Addon, error) {
var result map[string]Addon
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
const rootNode = "addon"
a, ok := result[rootNode]
if !ok {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &a, nil
}