forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.go
125 lines (102 loc) · 3.88 KB
/
extension.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
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// Extension represents a single PagerDuty extension. These are addtional
// features to be used as part of the incident management process.
type Extension struct {
APIObject
Name string `json:"name"`
EndpointURL string `json:"endpoint_url,omitempty"`
ExtensionObjects []APIObject `json:"extension_objects"`
ExtensionSchema APIObject `json:"extension_schema"`
Config interface{} `json:"config"`
}
// ListExtensionResponse represents the single response from the PagerDuty API
// when listing extensions.
type ListExtensionResponse struct {
APIListObject
Extensions []Extension `json:"extensions"`
}
// ListExtensionOptions are the options to use when listing extensions.
type ListExtensionOptions struct {
APIListObject
ExtensionObjectID string `url:"extension_object_id,omitempty"`
ExtensionSchemaID string `url:"extension_schema_id,omitempty"`
Query string `url:"query,omitempty"`
}
// ListExtensions lists the extensions from the API. It's recommended to use
// ListExtensionsWithContext instead.
func (c *Client) ListExtensions(o ListExtensionOptions) (*ListExtensionResponse, error) {
return c.ListExtensionsWithContext(context.Background(), o)
}
// ListExtensionsWithContext lists the extensions from the API.
func (c *Client) ListExtensionsWithContext(ctx context.Context, o ListExtensionOptions) (*ListExtensionResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/extensions?"+v.Encode())
if err != nil {
return nil, err
}
var result ListExtensionResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// CreateExtension creates a single extension.
func (c *Client) CreateExtension(e *Extension) (*Extension, error) {
return c.CreateExtensionWithContext(context.Background(), e)
}
// CreateExtensionWithContext creates a single extension.
func (c *Client) CreateExtensionWithContext(ctx context.Context, e *Extension) (*Extension, error) {
resp, err := c.post(ctx, "/extensions", e, nil)
return getExtensionFromResponse(c, resp, err)
}
// DeleteExtension deletes an extension by its ID.
func (c *Client) DeleteExtension(id string) error {
return c.DeleteExtensionWithContext(context.Background(), id)
}
// DeleteExtensionWithContext deletes an extension by its ID.
func (c *Client) DeleteExtensionWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/extensions/"+id)
return err
}
// GetExtension gets an extension by its ID.
func (c *Client) GetExtension(id string) (*Extension, error) {
return c.GetExtensionWithContext(context.Background(), id)
}
// GetExtensionWithContext gets an extension by its ID.
func (c *Client) GetExtensionWithContext(ctx context.Context, id string) (*Extension, error) {
resp, err := c.get(ctx, "/extensions/"+id)
return getExtensionFromResponse(c, resp, err)
}
// UpdateExtension updates an extension by its ID.
func (c *Client) UpdateExtension(id string, e *Extension) (*Extension, error) {
return c.UpdateExtensionWithContext(context.Background(), id, e)
}
// UpdateExtensionWithContext updates an extension by its ID.
func (c *Client) UpdateExtensionWithContext(ctx context.Context, id string, e *Extension) (*Extension, error) {
resp, err := c.put(ctx, "/extensions/"+id, e, nil)
return getExtensionFromResponse(c, resp, err)
}
func getExtensionFromResponse(c *Client, resp *http.Response, err error) (*Extension, error) {
if err != nil {
return nil, err
}
var target map[string]Extension
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "extension"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}