1package pagerduty
2
3import (
4	"fmt"
5	"net/http"
6
7	"github.com/google/go-querystring/query"
8)
9
10// ContactMethod is a way of contacting the user.
11type ContactMethod struct {
12	ID             string
13	Label          string
14	Address        string
15	Type           string
16	SendShortEmail bool `json:"send_short_email"`
17}
18
19// NotificationRule is a rule for notifying the user.
20type NotificationRule struct {
21	ID                  string
22	StartDelayInMinutes uint          `json:"start_delay_in_minutes"`
23	CreatedAt           string        `json:"created_at"`
24	ContactMethod       ContactMethod `json:"contact_method"`
25	Urgency             string
26	Type                string
27}
28
29// User is a member of a PagerDuty account that has the ability to interact with incidents and other data on the account.
30type User struct {
31	APIObject
32	Name              string `json:"name"`
33	Email             string `json:"email"`
34	Timezone          string `json:"timezone,omitempty"`
35	Color             string `json:"color,omitempty"`
36	Role              string `json:"role,omitempty"`
37	AvatarURL         string `json:"avatar_url,omitempty"`
38	Description       string `json:"description,omitempty"`
39	InvitationSent    bool
40	ContactMethods    []ContactMethod    `json:"contact_methods"`
41	NotificationRules []NotificationRule `json:"notification_rules"`
42	JobTitle          string             `json:"job_title,omitempty"`
43	Teams             []Team
44}
45
46// ListUsersResponse is the data structure returned from calling the ListUsers API endpoint.
47type ListUsersResponse struct {
48	APIListObject
49	Users []User
50}
51
52// ListUsersOptions is the data structure used when calling the ListUsers API endpoint.
53type ListUsersOptions struct {
54	APIListObject
55	Query    string   `url:"query,omitempty"`
56	TeamIDs  []string `url:"team_ids,omitempty,brackets"`
57	Includes []string `url:"include,omitempty,brackets"`
58}
59
60// GetUserOptions is the data structure used when calling the GetUser API endpoint.
61type GetUserOptions struct {
62	Includes []string `url:"include,omitempty,brackets"`
63}
64
65// ListUsers lists users of your PagerDuty account, optionally filtered by a search query.
66func (c *Client) ListUsers(o ListUsersOptions) (*ListUsersResponse, error) {
67	v, err := query.Values(o)
68	if err != nil {
69		return nil, err
70	}
71	resp, err := c.get("/users?" + v.Encode())
72	if err != nil {
73		return nil, err
74	}
75	var result ListUsersResponse
76	return &result, c.decodeJSON(resp, &result)
77}
78
79// CreateUser creates a new user.
80func (c *Client) CreateUser(u User) (*User, error) {
81	data := make(map[string]User)
82	data["user"] = u
83	resp, err := c.post("/users", data, nil)
84	return getUserFromResponse(c, resp, err)
85}
86
87// DeleteUser deletes a user.
88func (c *Client) DeleteUser(id string) error {
89	_, err := c.delete("/users/" + id)
90	return err
91}
92
93// GetUser gets details about an existing user.
94func (c *Client) GetUser(id string, o GetUserOptions) (*User, error) {
95	v, err := query.Values(o)
96	if err != nil {
97		return nil, err
98	}
99	resp, err := c.get("/users/" + id + "?" + v.Encode())
100	return getUserFromResponse(c, resp, err)
101}
102
103// UpdateUser updates an existing user.
104func (c *Client) UpdateUser(u User) (*User, error) {
105	v := make(map[string]User)
106	v["user"] = u
107	resp, err := c.put("/users/"+u.ID, v, nil)
108	return getUserFromResponse(c, resp, err)
109}
110
111func getUserFromResponse(c *Client, resp *http.Response, err error) (*User, error) {
112	if err != nil {
113		return nil, err
114	}
115	var target map[string]User
116	if dErr := c.decodeJSON(resp, &target); dErr != nil {
117		return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
118	}
119	rootNode := "user"
120	t, nodeOK := target[rootNode]
121	if !nodeOK {
122		return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
123	}
124	return &t, nil
125}
126