github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/roles/requests.go (about) 1 package roles 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to 9 // the List request 10 type ListOptsBuilder interface { 11 ToRoleListQuery() (string, error) 12 } 13 14 // ListOpts provides options to filter the List results. 15 type ListOpts struct { 16 // DomainID filters the response by a domain ID. 17 // If this parameter is specified, only custom policies of the account will be returned. 18 // If not specified, all system permissions (including system-defined policies and roles) will be returned. 19 DomainID string `q:"domain_id"` 20 21 // Name filters the response by role name. 22 Name string `q:"name"` 23 24 DisplayName string `q:"display_name"` 25 26 // This parameter is valid only when domain_id is left blank. 27 // policy: system-defined policy; role: system-defined role 28 PermissionType string `q:"permission_type"` 29 30 // Display mode of the permission. The options include domain, project, and all. 31 Type string `q:"type"` 32 33 // Service catalog, which corresponds to the catalog field in policies. 34 Catalog string `q:"catalog"` 35 } 36 37 // ToRoleListQuery formats a ListOpts into a query string. 38 func (opts ListOpts) ToRoleListQuery() (string, error) { 39 q, err := golangsdk.BuildQueryString(opts) 40 return q.String(), err 41 } 42 43 // List enumerates the roles to which the current token has access. 44 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 45 url := listURL(client) 46 if opts != nil { 47 query, err := opts.ToRoleListQuery() 48 if err != nil { 49 return pagination.Pager{Err: err} 50 } 51 url += query 52 } 53 54 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 55 return RolePage{pagination.LinkedPageBase{PageResult: r}} 56 }) 57 } 58 59 // Get retrieves details on a single role, by ID. 60 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 61 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 62 return 63 } 64 65 // CreateOptsBuilder allows extensions to add additional parameters to 66 // the Create request. 67 type CreateOptsBuilder interface { 68 ToRoleCreateMap() (map[string]interface{}, error) 69 } 70 71 // CreateOpts provides options used to create a role. 72 type CreateOpts struct { 73 // Name is the name of the new role. 74 Name string `json:"name" required:"true"` 75 76 // DomainID is the ID of the domain the role belongs to. 77 DomainID string `json:"domain_id,omitempty"` 78 79 // Extra is free-form extra key/value pairs to describe the role. 80 Extra map[string]interface{} `json:"-"` 81 } 82 83 // ToRoleCreateMap formats a CreateOpts into a create request. 84 func (opts CreateOpts) ToRoleCreateMap() (map[string]interface{}, error) { 85 b, err := golangsdk.BuildRequestBody(opts, "role") 86 if err != nil { 87 return nil, err 88 } 89 90 if opts.Extra != nil { 91 if v, ok := b["role"].(map[string]interface{}); ok { 92 for key, value := range opts.Extra { 93 v[key] = value 94 } 95 } 96 } 97 98 return b, nil 99 } 100 101 // Create creates a new Role. 102 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 103 b, err := opts.ToRoleCreateMap() 104 if err != nil { 105 r.Err = err 106 return 107 } 108 _, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{ 109 OkCodes: []int{201}, 110 }) 111 return 112 } 113 114 // UpdateOptsBuilder allows extensions to add additional parameters to 115 // the Update request. 116 type UpdateOptsBuilder interface { 117 ToRoleUpdateMap() (map[string]interface{}, error) 118 } 119 120 // UpdateOpts provides options for updating a role. 121 type UpdateOpts struct { 122 // Name is the name of the new role. 123 Name string `json:"name,omitempty"` 124 125 // Extra is free-form extra key/value pairs to describe the role. 126 Extra map[string]interface{} `json:"-"` 127 } 128 129 // ToRoleUpdateMap formats a UpdateOpts into an update request. 130 func (opts UpdateOpts) ToRoleUpdateMap() (map[string]interface{}, error) { 131 b, err := golangsdk.BuildRequestBody(opts, "role") 132 if err != nil { 133 return nil, err 134 } 135 136 if opts.Extra != nil { 137 if v, ok := b["role"].(map[string]interface{}); ok { 138 for key, value := range opts.Extra { 139 v[key] = value 140 } 141 } 142 } 143 144 return b, nil 145 } 146 147 // Update updates an existing Role. 148 func Update(client *golangsdk.ServiceClient, roleID string, opts UpdateOptsBuilder) (r UpdateResult) { 149 b, err := opts.ToRoleUpdateMap() 150 if err != nil { 151 r.Err = err 152 return 153 } 154 _, r.Err = client.Patch(updateURL(client, roleID), &b, &r.Body, &golangsdk.RequestOpts{ 155 OkCodes: []int{200}, 156 }) 157 return 158 } 159 160 // Delete deletes a role. 161 func Delete(client *golangsdk.ServiceClient, roleID string) (r DeleteResult) { 162 _, r.Err = client.Delete(deleteURL(client, roleID), nil) 163 return 164 } 165 166 // ListAssignmentsOptsBuilder allows extensions to add additional parameters to 167 // the ListAssignments request. 168 type ListAssignmentsOptsBuilder interface { 169 extractAssignment() (string, string, string, string, error) 170 } 171 172 // ListAssignmentsOpts allows you to query the ListAssignments method. 173 // Specify one of or a combination of GroupId, RoleId, ScopeDomainId, 174 // ScopeProjectId, and/or UserId to search for roles assigned to corresponding 175 // entities. 176 type ListAssignmentsOpts struct { 177 // GroupID is the group ID to query. 178 GroupID string `q:"group.id"` 179 180 // ScopeDomainID filters the results by the given domain ID. 181 ScopeDomainID string `q:"scope.domain.id"` 182 183 // ScopeProjectID filters the results by the given Project ID. 184 ScopeProjectID string `q:"scope.project.id"` 185 186 // UserID filterst he results by the given User ID. 187 UserID string `q:"user.id"` 188 } 189 190 // ToRolesListAssignmentsQuery formats a ListAssignmentsOpts into a query string. 191 func (opts ListAssignmentsOpts) extractAssignment() (string, string, string, string, error) { 192 // Get corresponding URL 193 var targetID string 194 var targetType string 195 if opts.ScopeProjectID != "" { 196 targetID = opts.ScopeProjectID 197 targetType = "projects" 198 } else { 199 targetID = opts.ScopeDomainID 200 targetType = "domains" 201 } 202 203 var actorID string 204 var actorType string 205 if opts.UserID != "" { 206 actorID = opts.UserID 207 actorType = "users" 208 } else { 209 actorID = opts.GroupID 210 actorType = "groups" 211 } 212 213 return targetType, targetID, actorType, actorID, nil 214 } 215 216 // ListAssignments enumerates the roles assigned to a specified resource. 217 func ListAssignments(client *golangsdk.ServiceClient, opts ListAssignmentsOptsBuilder) pagination.Pager { 218 targetType, targetID, actorType, actorID, _ := opts.extractAssignment() 219 220 url := listAssignmentsURL(client, targetType, targetID, actorType, actorID) 221 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 222 return RoleAssignmentPage{pagination.LinkedPageBase{PageResult: r}} 223 }) 224 } 225 226 // AssignOpts provides options to assign a role 227 type AssignOpts struct { 228 // UserID is the ID of a user to assign a role 229 // Note: exactly one of UserID or GroupID must be provided 230 UserID string `xor:"GroupID"` 231 232 // GroupID is the ID of a group to assign a role 233 // Note: exactly one of UserID or GroupID must be provided 234 GroupID string `xor:"UserID"` 235 236 // ProjectID is the ID of a project to assign a role on 237 // Note: exactly one of ProjectID or DomainID must be provided 238 ProjectID string `xor:"DomainID"` 239 240 // DomainID is the ID of a domain to assign a role on 241 // Note: exactly one of ProjectID or DomainID must be provided 242 DomainID string `xor:"ProjectID"` 243 } 244 245 // UnassignOpts provides options to unassign a role 246 type UnassignOpts struct { 247 // UserID is the ID of a user to unassign a role 248 // Note: exactly one of UserID or GroupID must be provided 249 UserID string `xor:"GroupID"` 250 251 // GroupID is the ID of a group to unassign a role 252 // Note: exactly one of UserID or GroupID must be provided 253 GroupID string `xor:"UserID"` 254 255 // ProjectID is the ID of a project to unassign a role on 256 // Note: exactly one of ProjectID or DomainID must be provided 257 ProjectID string `xor:"DomainID"` 258 259 // DomainID is the ID of a domain to unassign a role on 260 // Note: exactly one of ProjectID or DomainID must be provided 261 DomainID string `xor:"ProjectID"` 262 } 263 264 // Assign is the operation responsible for assigning a role 265 // to a user/group on a project/domain. 266 func Assign(client *golangsdk.ServiceClient, roleID string, opts AssignOpts) (r AssignmentResult) { 267 // Check xor conditions 268 _, err := golangsdk.BuildRequestBody(opts, "") 269 if err != nil { 270 r.Err = err 271 return 272 } 273 274 // Get corresponding URL 275 var targetID string 276 var targetType string 277 if opts.ProjectID != "" { 278 targetID = opts.ProjectID 279 targetType = "projects" 280 } else { 281 targetID = opts.DomainID 282 targetType = "domains" 283 } 284 285 var actorID string 286 var actorType string 287 if opts.UserID != "" { 288 actorID = opts.UserID 289 actorType = "users" 290 } else { 291 actorID = opts.GroupID 292 actorType = "groups" 293 } 294 295 _, r.Err = client.Put(assignURL(client, targetType, targetID, actorType, actorID, roleID), nil, nil, &golangsdk.RequestOpts{ 296 OkCodes: []int{204}, 297 }) 298 return 299 } 300 301 // Unassign is the operation responsible for unassigning a role 302 // from a user/group on a project/domain. 303 func Unassign(client *golangsdk.ServiceClient, roleID string, opts UnassignOpts) (r UnassignmentResult) { 304 // Check xor conditions 305 _, err := golangsdk.BuildRequestBody(opts, "") 306 if err != nil { 307 r.Err = err 308 return 309 } 310 311 // Get corresponding URL 312 var targetID string 313 var targetType string 314 if opts.ProjectID != "" { 315 targetID = opts.ProjectID 316 targetType = "projects" 317 } else { 318 targetID = opts.DomainID 319 targetType = "domains" 320 } 321 322 var actorID string 323 var actorType string 324 if opts.UserID != "" { 325 actorID = opts.UserID 326 actorType = "users" 327 } else { 328 actorID = opts.GroupID 329 actorType = "groups" 330 } 331 332 _, r.Err = client.Delete(assignURL(client, targetType, targetID, actorType, actorID, roleID), &golangsdk.RequestOpts{ 333 OkCodes: []int{204}, 334 }) 335 return 336 }