forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_user_grants.go
95 lines (80 loc) · 3.17 KB
/
account_user_grants.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
package linodego
import (
"context"
"encoding/json"
"fmt"
)
type GrantPermissionLevel string
const (
AccessLevelReadOnly GrantPermissionLevel = "read_only"
AccessLevelReadWrite GrantPermissionLevel = "read_write"
)
type GlobalUserGrants struct {
AccountAccess *GrantPermissionLevel `json:"account_access"`
AddDatabases bool `json:"add_databases"`
AddDomains bool `json:"add_domains"`
AddFirewalls bool `json:"add_firewalls"`
AddImages bool `json:"add_images"`
AddLinodes bool `json:"add_linodes"`
AddLongview bool `json:"add_longview"`
AddNodeBalancers bool `json:"add_nodebalancers"`
AddStackScripts bool `json:"add_stackscripts"`
AddVolumes bool `json:"add_volumes"`
CancelAccount bool `json:"cancel_account"`
LongviewSubscription bool `json:"longview_subscription"`
}
type EntityUserGrant struct {
ID int `json:"id"`
Permissions *GrantPermissionLevel `json:"permissions"`
}
type GrantedEntity struct {
ID int `json:"id"`
Label string `json:"label"`
Permissions GrantPermissionLevel `json:"permissions"`
}
type UserGrants struct {
Database []GrantedEntity `json:"database"`
Domain []GrantedEntity `json:"domain"`
Firewall []GrantedEntity `json:"firewall"`
Image []GrantedEntity `json:"image"`
Linode []GrantedEntity `json:"linode"`
Longview []GrantedEntity `json:"longview"`
NodeBalancer []GrantedEntity `json:"nodebalancer"`
StackScript []GrantedEntity `json:"stackscript"`
Volume []GrantedEntity `json:"volume"`
Global GlobalUserGrants `json:"global"`
}
type UserGrantsUpdateOptions struct {
Database []GrantedEntity `json:"database,omitempty"`
Domain []EntityUserGrant `json:"domain,omitempty"`
Firewall []EntityUserGrant `json:"firewall,omitempty"`
Image []EntityUserGrant `json:"image,omitempty"`
Linode []EntityUserGrant `json:"linode,omitempty"`
Longview []EntityUserGrant `json:"longview,omitempty"`
NodeBalancer []EntityUserGrant `json:"nodebalancer,omitempty"`
StackScript []EntityUserGrant `json:"stackscript,omitempty"`
Volume []EntityUserGrant `json:"volume,omitempty"`
Global GlobalUserGrants `json:"global"`
}
func (c *Client) GetUserGrants(ctx context.Context, username string) (*UserGrants, error) {
e := fmt.Sprintf("account/users/%s/grants", username)
req := c.R(ctx).SetResult(&UserGrants{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*UserGrants), nil
}
func (c *Client) UpdateUserGrants(ctx context.Context, username string, opts UserGrantsUpdateOptions) (*UserGrants, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("account/users/%s/grants", username)
req := c.R(ctx).SetResult(&UserGrants{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*UserGrants), nil
}