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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
package roles
import (
"net/url"
"strings"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to
// the List request
type ListOptsBuilder interface {
ToRoleListQuery() (string, error)
}
// ListOpts provides options to filter the List results.
type ListOpts struct {
// DomainID filters the response by a domain ID.
DomainID string `q:"domain_id"`
// Name filters the response by role name.
Name string `q:"name"`
// Filters filters the response by custom filters such as
// 'name__contains=foo'
Filters map[string]string `q:"-"`
}
// ToRoleListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToRoleListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
if err != nil {
return "", err
}
params := q.Query()
for k, v := range opts.Filters {
i := strings.Index(k, "__")
if i > 0 && i < len(k)-2 {
params.Add(k, v)
} else {
return "", InvalidListFilter{FilterName: k}
}
}
q = &url.URL{RawQuery: params.Encode()}
return q.String(), err
}
// List enumerates the roles to which the current token has access.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToRoleListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RolePage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get retrieves details on a single role, by ID.
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
resp, err := client.Get(getURL(client, id), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// CreateOptsBuilder allows extensions to add additional parameters to
// the Create request.
type CreateOptsBuilder interface {
ToRoleCreateMap() (map[string]interface{}, error)
}
// CreateOpts provides options used to create a role.
type CreateOpts struct {
// Name is the name of the new role.
Name string `json:"name" required:"true"`
// DomainID is the ID of the domain the role belongs to.
DomainID string `json:"domain_id,omitempty"`
// Extra is free-form extra key/value pairs to describe the role.
Extra map[string]interface{} `json:"-"`
}
// ToRoleCreateMap formats a CreateOpts into a create request.
func (opts CreateOpts) ToRoleCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "role")
if err != nil {
return nil, err
}
if opts.Extra != nil {
if v, ok := b["role"].(map[string]interface{}); ok {
for key, value := range opts.Extra {
v[key] = value
}
}
}
return b, nil
}
// Create creates a new Role.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToRoleCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to
// the Update request.
type UpdateOptsBuilder interface {
ToRoleUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts provides options for updating a role.
type UpdateOpts struct {
// Name is the name of the new role.
Name string `json:"name,omitempty"`
// Extra is free-form extra key/value pairs to describe the role.
Extra map[string]interface{} `json:"-"`
}
// ToRoleUpdateMap formats a UpdateOpts into an update request.
func (opts UpdateOpts) ToRoleUpdateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "role")
if err != nil {
return nil, err
}
if opts.Extra != nil {
if v, ok := b["role"].(map[string]interface{}); ok {
for key, value := range opts.Extra {
v[key] = value
}
}
}
return b, nil
}
// Update updates an existing Role.
func Update(client *gophercloud.ServiceClient, roleID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToRoleUpdateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Patch(updateURL(client, roleID), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete deletes a role.
func Delete(client *gophercloud.ServiceClient, roleID string) (r DeleteResult) {
resp, err := client.Delete(deleteURL(client, roleID), nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// ListAssignmentsOptsBuilder allows extensions to add additional parameters to
// the ListAssignments request.
type ListAssignmentsOptsBuilder interface {
ToRolesListAssignmentsQuery() (string, error)
}
// ListAssignmentsOpts allows you to query the ListAssignments method.
// Specify one of or a combination of GroupId, RoleId, ScopeDomainId,
// ScopeProjectId, and/or UserId to search for roles assigned to corresponding
// entities.
type ListAssignmentsOpts struct {
// GroupID is the group ID to query.
GroupID string `q:"group.id"`
// RoleID is the specific role to query assignments to.
RoleID string `q:"role.id"`
// ScopeDomainID filters the results by the given domain ID.
ScopeDomainID string `q:"scope.domain.id"`
// ScopeProjectID filters the results by the given Project ID.
ScopeProjectID string `q:"scope.project.id"`
// UserID filterst he results by the given User ID.
UserID string `q:"user.id"`
// Effective lists effective assignments at the user, project, and domain
// level, allowing for the effects of group membership.
Effective *bool `q:"effective"`
}
// ToRolesListAssignmentsQuery formats a ListAssignmentsOpts into a query string.
func (opts ListAssignmentsOpts) ToRolesListAssignmentsQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// ListAssignments enumerates the roles assigned to a specified resource.
func ListAssignments(client *gophercloud.ServiceClient, opts ListAssignmentsOptsBuilder) pagination.Pager {
url := listAssignmentsURL(client)
if opts != nil {
query, err := opts.ToRolesListAssignmentsQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RoleAssignmentPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// ListAssignmentsOnResourceOpts provides options to list role assignments
// for a user/group on a project/domain
type ListAssignmentsOnResourceOpts struct {
// UserID is the ID of a user to assign a role
// Note: exactly one of UserID or GroupID must be provided
UserID string `xor:"GroupID"`
// GroupID is the ID of a group to assign a role
// Note: exactly one of UserID or GroupID must be provided
GroupID string `xor:"UserID"`
// ProjectID is the ID of a project to assign a role on
// Note: exactly one of ProjectID or DomainID must be provided
ProjectID string `xor:"DomainID"`
// DomainID is the ID of a domain to assign a role on
// Note: exactly one of ProjectID or DomainID must be provided
DomainID string `xor:"ProjectID"`
}
// AssignOpts provides options to assign a role
type AssignOpts struct {
// UserID is the ID of a user to assign a role
// Note: exactly one of UserID or GroupID must be provided
UserID string `xor:"GroupID"`
// GroupID is the ID of a group to assign a role
// Note: exactly one of UserID or GroupID must be provided
GroupID string `xor:"UserID"`
// ProjectID is the ID of a project to assign a role on
// Note: exactly one of ProjectID or DomainID must be provided
ProjectID string `xor:"DomainID"`
// DomainID is the ID of a domain to assign a role on
// Note: exactly one of ProjectID or DomainID must be provided
DomainID string `xor:"ProjectID"`
}
// UnassignOpts provides options to unassign a role
type UnassignOpts struct {
// UserID is the ID of a user to unassign a role
// Note: exactly one of UserID or GroupID must be provided
UserID string `xor:"GroupID"`
// GroupID is the ID of a group to unassign a role
// Note: exactly one of UserID or GroupID must be provided
GroupID string `xor:"UserID"`
// ProjectID is the ID of a project to unassign a role on
// Note: exactly one of ProjectID or DomainID must be provided
ProjectID string `xor:"DomainID"`
// DomainID is the ID of a domain to unassign a role on
// Note: exactly one of ProjectID or DomainID must be provided
DomainID string `xor:"ProjectID"`
}
// ListAssignmentsOnResource is the operation responsible for listing role
// assignments for a user/group on a project/domain.
func ListAssignmentsOnResource(client *gophercloud.ServiceClient, opts ListAssignmentsOnResourceOpts) pagination.Pager {
// Check xor conditions
_, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return pagination.Pager{Err: err}
}
// Get corresponding URL
var targetID string
var targetType string
if opts.ProjectID != "" {
targetID = opts.ProjectID
targetType = "projects"
} else {
targetID = opts.DomainID
targetType = "domains"
}
var actorID string
var actorType string
if opts.UserID != "" {
actorID = opts.UserID
actorType = "users"
} else {
actorID = opts.GroupID
actorType = "groups"
}
url := listAssignmentsOnResourceURL(client, targetType, targetID, actorType, actorID)
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RolePage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Assign is the operation responsible for assigning a role
// to a user/group on a project/domain.
func Assign(client *gophercloud.ServiceClient, roleID string, opts AssignOpts) (r AssignmentResult) {
// Check xor conditions
_, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
r.Err = err
return
}
// Get corresponding URL
var targetID string
var targetType string
if opts.ProjectID != "" {
targetID = opts.ProjectID
targetType = "projects"
} else {
targetID = opts.DomainID
targetType = "domains"
}
var actorID string
var actorType string
if opts.UserID != "" {
actorID = opts.UserID
actorType = "users"
} else {
actorID = opts.GroupID
actorType = "groups"
}
resp, err := client.Put(assignURL(client, targetType, targetID, actorType, actorID, roleID), nil, nil, &gophercloud.RequestOpts{
OkCodes: []int{204},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Unassign is the operation responsible for unassigning a role
// from a user/group on a project/domain.
func Unassign(client *gophercloud.ServiceClient, roleID string, opts UnassignOpts) (r UnassignmentResult) {
// Check xor conditions
_, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
r.Err = err
return
}
// Get corresponding URL
var targetID string
var targetType string
if opts.ProjectID != "" {
targetID = opts.ProjectID
targetType = "projects"
} else {
targetID = opts.DomainID
targetType = "domains"
}
var actorID string
var actorType string
if opts.UserID != "" {
actorID = opts.UserID
actorType = "users"
} else {
actorID = opts.GroupID
actorType = "groups"
}
resp, err := client.Delete(assignURL(client, targetType, targetID, actorType, actorID, roleID), &gophercloud.RequestOpts{
OkCodes: []int{204},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
|