-
Notifications
You must be signed in to change notification settings - Fork 242
/
response_play.go
148 lines (120 loc) · 4.42 KB
/
response_play.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
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// ResponsePlay represents the API object for a response object:
//
// https://developer.pagerduty.com/api-reference/b3A6Mjc0ODE2Ng-create-a-response-play
type ResponsePlay struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Summary string `json:"summary,omitempty"`
Self string `json:"self,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description"`
Team *APIReference `json:"team,omitempty"`
Subscribers []*APIReference `json:"subscribers,omitempty"`
SubscribersMessage string `json:"subscribers_message"`
Responders []*APIReference `json:"responders,omitempty"`
RespondersMessage string `json:"responders_message"`
Runnability *string `json:"runnability"`
ConferenceNumber *string `json:"conference_number"`
ConferenceURL *string `json:"conference_url"`
ConferenceType *string `json:"conference_type"`
}
// ListResponsePlaysResponse represents the list of response plays.
type ListResponsePlaysResponse struct {
ResponsePlays []ResponsePlay `json:"response_plays"`
}
// ListResponsePlaysOptions are the options for listing response plays.
type ListResponsePlaysOptions struct {
// FilterForManualRun limits results to show only response plays that can be
// invoked manually.
FilterForManualRun bool `url:"filter_for_manual_run,omitempty"`
Query string `url:"query,omitempty"`
From string
}
// ListResponsePlays lists existing response plays.
func (c *Client) ListResponsePlays(ctx context.Context, o ListResponsePlaysOptions) ([]ResponsePlay, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
h := map[string]string{
"From": o.From,
}
resp, err := c.get(ctx, "/response_plays?"+v.Encode(), h)
if err != nil {
return nil, err
}
var result ListResponsePlaysResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return result.ResponsePlays, nil
}
// CreateResponsePlay creates a new response play.
func (c *Client) CreateResponsePlay(ctx context.Context, rp ResponsePlay) (ResponsePlay, error) {
d := map[string]ResponsePlay{
"response_play": rp,
}
resp, err := c.post(ctx, "/response_plays", d, nil)
return getResponsePlayFromResponse(c, resp, err)
}
// GetResponsePlay gets details about an existing response play.
func (c *Client) GetResponsePlay(ctx context.Context, id string) (ResponsePlay, error) {
resp, err := c.get(ctx, "/response_plays/"+id, nil)
return getResponsePlayFromResponse(c, resp, err)
}
// UpdateResponsePlay updates an existing response play.
func (c *Client) UpdateResponsePlay(ctx context.Context, rp ResponsePlay) (ResponsePlay, error) {
d := map[string]ResponsePlay{
"response_play": rp,
}
resp, err := c.put(ctx, "/response_plays/"+rp.ID, d, nil)
return getResponsePlayFromResponse(c, resp, err)
}
// DeleteResponsePlay deletes an existing response play.
func (c *Client) DeleteResponsePlay(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/response_plays/"+id)
return err
}
// RunResponsePlay runs a response play on a given incident.
func (c *Client) RunResponsePlay(ctx context.Context, from string, responsePlayID string, incidentID string) error {
d := map[string]APIReference{
"incident": {
ID: incidentID,
Type: "incident_reference",
},
}
h := map[string]string{
"From": from,
}
resp, err := c.post(ctx, "/response_plays/"+responsePlayID+"/run", d, h)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to run response play %s on incident %s (status code: %d)", responsePlayID, incidentID, resp.StatusCode)
}
return nil
}
func getResponsePlayFromResponse(c *Client, resp *http.Response, err error) (ResponsePlay, error) {
if err != nil {
return ResponsePlay{}, err
}
var target map[string]ResponsePlay
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return ResponsePlay{}, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "response_play"
t, nodeOK := target[rootNode]
if !nodeOK {
return ResponsePlay{}, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return t, nil
}