1package linodego
2
3import (
4	"context"
5	"encoding/json"
6)
7
8type GrantPermissionLevel string
9
10const (
11	AccessLevelReadOnly  GrantPermissionLevel = "read_only"
12	AccessLevelReadWrite GrantPermissionLevel = "read_write"
13)
14
15type GlobalUserGrants struct {
16	AccountAccess        *GrantPermissionLevel `json:"account_access"`
17	AddDomains           bool                  `json:"add_domains"`
18	AddFirewalls         bool                  `json:"add_firewalls"`
19	AddImages            bool                  `json:"add_images"`
20	AddLinodes           bool                  `json:"add_linodes"`
21	AddLongview          bool                  `json:"add_longview"`
22	AddNodeBalancers     bool                  `json:"add_nodebalancers"`
23	AddStackScripts      bool                  `json:"add_stackscripts"`
24	AddVolumes           bool                  `json:"add_volumes"`
25	CancelAccount        bool                  `json:"cancel_account"`
26	LongviewSubscription bool                  `json:"longview_subscription"`
27}
28
29type EntityUserGrant struct {
30	ID          int                   `json:"id"`
31	Permissions *GrantPermissionLevel `json:"permissions"`
32}
33
34type GrantedEntity struct {
35	ID          int                  `json:"id"`
36	Label       string               `json:"label"`
37	Permissions GrantPermissionLevel `json:"permissions"`
38}
39
40type UserGrants struct {
41	Domain       []GrantedEntity `json:"domain"`
42	Firewall     []GrantedEntity `json:"firewall"`
43	Image        []GrantedEntity `json:"image"`
44	Linode       []GrantedEntity `json:"linode"`
45	Longview     []GrantedEntity `json:"longview"`
46	NodeBalancer []GrantedEntity `json:"nodebalancer"`
47	StackScript  []GrantedEntity `json:"stackscript"`
48	Volume       []GrantedEntity `json:"volume"`
49
50	Global GlobalUserGrants `json:"global"`
51}
52
53type UserGrantsUpdateOptions struct {
54	Domain       []EntityUserGrant `json:"domain,omitempty"`
55	Firewall     []EntityUserGrant `json:"firewall,omitempty"`
56	Image        []EntityUserGrant `json:"image,omitempty"`
57	Linode       []EntityUserGrant `json:"linode,omitempty"`
58	Longview     []EntityUserGrant `json:"longview,omitempty"`
59	NodeBalancer []EntityUserGrant `json:"nodebalancer,omitempty"`
60	StackScript  []EntityUserGrant `json:"stackscript,omitempty"`
61	Volume       []EntityUserGrant `json:"volume,omitempty"`
62
63	Global GlobalUserGrants `json:"global"`
64}
65
66func (c *Client) GetUserGrants(ctx context.Context, username string) (*UserGrants, error) {
67	e, err := c.UserGrants.endpointWithParams(username)
68	if err != nil {
69		return nil, err
70	}
71
72	r, err := coupleAPIErrors(c.R(ctx).SetResult(&UserGrants{}).Get(e))
73	if err != nil {
74		return nil, err
75	}
76
77	return r.Result().(*UserGrants), nil
78}
79
80func (c *Client) UpdateUserGrants(ctx context.Context, username string, updateOpts UserGrantsUpdateOptions) (*UserGrants, error) {
81	var body string
82
83	e, err := c.UserGrants.endpointWithParams(username)
84	if err != nil {
85		return nil, err
86	}
87
88	req := c.R(ctx).SetResult(&UserGrants{})
89
90	if bodyData, err := json.Marshal(updateOpts); err == nil {
91		body = string(bodyData)
92	} else {
93		return nil, NewError(err)
94	}
95
96	r, err := coupleAPIErrors(req.SetBody(body).Put(e))
97	if err != nil {
98		return nil, err
99	}
100
101	return r.Result().(*UserGrants), nil
102}
103