-
Notifications
You must be signed in to change notification settings - Fork 3
/
role.go
192 lines (160 loc) · 4.68 KB
/
role.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
)
// Compile-time proof of interface implementation.
var _ Roles = (*roles)(nil)
// Roles describes all the role related methods that the
// Scalr IACP API supports.
type Roles interface {
List(ctx context.Context, options RoleListOptions) (*RoleList, error)
Read(ctx context.Context, roleID string) (*Role, error)
Create(ctx context.Context, options RoleCreateOptions) (*Role, error)
Update(ctx context.Context, roleID string, options RoleUpdateOptions) (*Role, error)
Delete(ctx context.Context, roleID string) error
}
// roles implements Roles.
type roles struct {
client *Client
}
// Permission relationship
type Permission struct {
ID string `jsonapi:"primary,permissions,omitempty"`
}
// RoleList represents a list of roles.
type RoleList struct {
*Pagination
Items []*Role
}
// Role represents a Scalr role.
type Role struct {
ID string `jsonapi:"primary,roles"`
Name string `jsonapi:"attr,name"`
Description string `jsonapi:"attr,description"`
IsSystem bool `jsonapi:"attr,is-system"`
// Relations
Account *Account `jsonapi:"relation,account"`
Permissions []*Permission `jsonapi:"relation,permissions,omitempty"`
}
// RoleCreateOptions represents the options for creating a new Role.
type RoleCreateOptions struct {
ID string `jsonapi:"primary,roles"`
Name *string `jsonapi:"attr,name"`
Description *string `jsonapi:"attr,description,omitempty"`
// Relations
Account *Account `jsonapi:"relation,account"`
Permissions []*Permission `jsonapi:"relation,permissions,omitempty"`
}
func (o RoleCreateOptions) valid() error {
if o.Account == nil {
return errors.New("account is required")
}
if !validStringID(&o.Account.ID) {
return errors.New("invalid value for account ID")
}
if o.Name == nil {
return errors.New("name is required")
}
if strings.TrimSpace(*o.Name) == "" {
return errors.New("invalid value for name")
}
return nil
}
// RoleListOptions represents the options for listing roles.
type RoleListOptions struct {
ListOptions
Account *string `url:"filter[account],omitempty"`
Name string `url:"filter[name],omitempty"`
Role string `url:"filter[role],omitempty"`
Query string `url:"query,omitempty"`
Include string `url:"include,omitempty"`
}
// List all the roles.
func (s *roles) List(ctx context.Context, options RoleListOptions) (*RoleList, error) {
req, err := s.client.newRequest("GET", "roles", &options)
if err != nil {
return nil, err
}
rolel := &RoleList{}
err = s.client.do(ctx, req, rolel)
if err != nil {
return nil, err
}
return rolel, nil
}
// Create is used to create a new Role.
func (s *roles) Create(ctx context.Context, options RoleCreateOptions) (*Role, error) {
if err := options.valid(); err != nil {
return nil, err
}
// Make sure we don't send a user provided ID.
options.ID = ""
req, err := s.client.newRequest("POST", "roles", &options)
if err != nil {
return nil, err
}
role := &Role{}
err = s.client.do(ctx, req, role)
if err != nil {
return nil, err
}
return role, nil
}
// Read an role by its ID.
func (s *roles) Read(ctx context.Context, roleID string) (*Role, error) {
if !validStringID(&roleID) {
return nil, errors.New("invalid value for role ID")
}
u := fmt.Sprintf("roles/%s", url.QueryEscape(roleID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
role := &Role{}
err = s.client.do(ctx, req, role)
if err != nil {
return nil, err
}
return role, nil
}
// RoleUpdateOptions represents the options for updating an role.
type RoleUpdateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,roles"`
Name *string `jsonapi:"attr,name,omitempty"`
Description *string `jsonapi:"attr,description,omitempty"`
// Relations
Permissions []*Permission `jsonapi:"relation,permissions,omitempty"`
}
// Update settings of an existing role.
func (s *roles) Update(ctx context.Context, roleID string, options RoleUpdateOptions) (*Role, error) {
// Make sure we don't send a user provided ID.
options.ID = ""
u := fmt.Sprintf("roles/%s", url.QueryEscape(roleID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
role := &Role{}
err = s.client.do(ctx, req, role)
if err != nil {
return nil, err
}
return role, nil
}
// Delete an role by its ID.
func (s *roles) Delete(ctx context.Context, roleID string) error {
if !validStringID(&roleID) {
return errors.New("invalid value for role ID")
}
u := fmt.Sprintf("roles/%s", url.QueryEscape(roleID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}