1package pagerduty 2 3import ( 4 "fmt" 5 "net/http" 6 7 "github.com/google/go-querystring/query" 8) 9 10// Addon is a third-party add-on to PagerDuty's UI. 11type Addon struct { 12 APIObject 13 Name string `json:"name,omitempty"` 14 Src string `json:"src,omitempty"` 15 Services []APIObject `json:"services,omitempty"` 16} 17 18// ListAddonOptions are the options available when calling the ListAddons API endpoint. 19type ListAddonOptions struct { 20 APIListObject 21 Includes []string `url:"include,omitempty,brackets"` 22 ServiceIDs []string `url:"service_ids,omitempty,brackets"` 23 Filter string `url:"filter,omitempty"` 24} 25 26// ListAddonResponse is the response when calling the ListAddons API endpoint. 27type ListAddonResponse struct { 28 APIListObject 29 Addons []Addon `json:"addons"` 30} 31 32// ListAddons lists all of the add-ons installed on your account. 33func (c *Client) ListAddons(o ListAddonOptions) (*ListAddonResponse, error) { 34 v, err := query.Values(o) 35 if err != nil { 36 return nil, err 37 } 38 resp, err := c.get("/addons?" + v.Encode()) 39 if err != nil { 40 return nil, err 41 } 42 var result ListAddonResponse 43 return &result, c.decodeJSON(resp, &result) 44} 45 46// InstallAddon installs an add-on for your account. 47func (c *Client) InstallAddon(a Addon) (*Addon, error) { 48 data := make(map[string]Addon) 49 data["addon"] = a 50 resp, err := c.post("/addons", data, nil) 51 defer resp.Body.Close() 52 if err != nil { 53 return nil, err 54 } 55 if resp.StatusCode != http.StatusCreated { 56 return nil, fmt.Errorf("Failed to create. HTTP Status code: %d", resp.StatusCode) 57 } 58 return getAddonFromResponse(c, resp) 59} 60 61// DeleteAddon deletes an add-on from your account. 62func (c *Client) DeleteAddon(id string) error { 63 _, err := c.delete("/addons/" + id) 64 return err 65} 66 67// GetAddon gets details about an existing add-on. 68func (c *Client) GetAddon(id string) (*Addon, error) { 69 resp, err := c.get("/addons/" + id) 70 if err != nil { 71 return nil, err 72 } 73 return getAddonFromResponse(c, resp) 74} 75 76// UpdateAddon updates an existing add-on. 77func (c *Client) UpdateAddon(id string, a Addon) (*Addon, error) { 78 v := make(map[string]Addon) 79 v["addon"] = a 80 resp, err := c.put("/addons/"+id, v, nil) 81 if err != nil { 82 return nil, err 83 } 84 return getAddonFromResponse(c, resp) 85} 86 87func getAddonFromResponse(c *Client, resp *http.Response) (*Addon, error) { 88 var result map[string]Addon 89 if err := c.decodeJSON(resp, &result); err != nil { 90 return nil, err 91 } 92 a, ok := result["addon"] 93 if !ok { 94 return nil, fmt.Errorf("JSON response does not have 'addon' field") 95 } 96 return &a, nil 97} 98