1package pagerduty
2
3import (
4	"fmt"
5	"net/http"
6
7	"github.com/google/go-querystring/query"
8)
9
10// Vendor represents a specific type of integration. AWS Cloudwatch, Splunk, Datadog, etc are all examples of vendors that can be integrated in PagerDuty by making an integration.
11type Vendor struct {
12	APIObject
13	Name                string `json:"name,omitempty"`
14	LogoURL             string `json:"logo_url,omitempty"`
15	LongName            string `json:"long_name,omitempty"`
16	WebsiteURL          string `json:"website_url,omitempty"`
17	Description         string `json:"description,omitempty"`
18	Connectable         bool   `json:"connectable,omitempty"`
19	ThumbnailURL        string `json:"thumbnail_url,omitempty"`
20	GenericServiceType  string `json:"generic_service_type,omitempty"`
21	IntegrationGuideURL string `json:"integration_guide_url,omitempty"`
22}
23
24// ListVendorResponse is the data structure returned from calling the ListVendors API endpoint.
25type ListVendorResponse struct {
26	APIListObject
27	Vendors []Vendor
28}
29
30// ListVendorOptions is the data structure used when calling the ListVendors API endpoint.
31type ListVendorOptions struct {
32	APIListObject
33	Query string `url:"query,omitempty"`
34}
35
36// ListVendors lists existing vendors.
37func (c *Client) ListVendors(o ListVendorOptions) (*ListVendorResponse, error) {
38	v, err := query.Values(o)
39
40	if err != nil {
41		return nil, err
42	}
43
44	resp, err := c.get("/vendors?" + v.Encode())
45
46	if err != nil {
47		return nil, err
48	}
49
50	var result ListVendorResponse
51	return &result, c.decodeJSON(resp, &result)
52}
53
54// GetVendor gets details about an existing vendor.
55func (c *Client) GetVendor(id string) (*Vendor, error) {
56	resp, err := c.get("/vendors/" + id)
57	return getVendorFromResponse(c, resp, err)
58}
59
60func getVendorFromResponse(c *Client, resp *http.Response, err error) (*Vendor, error) {
61	if err != nil {
62		return nil, err
63	}
64	var target map[string]Vendor
65	if dErr := c.decodeJSON(resp, &target); dErr != nil {
66		return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
67	}
68	rootNode := "vendor"
69	t, nodeOK := target[rootNode]
70	if !nodeOK {
71		return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
72	}
73	return &t, nil
74}
75