This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
identity_role_assignment_v3.go
63 lines (49 loc) · 1.66 KB
/
identity_role_assignment_v3.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
package openstack
import (
"fmt"
"strings"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/identity/v3/roles"
"github.com/gophercloud/gophercloud/pagination"
)
// Role assignments have no ID in OpenStack.
// Build an ID out of the IDs that make up the role assignment
func identityRoleAssignmentV3ID(domainID, projectID, groupID, userID, roleID string) string {
return fmt.Sprintf("%s/%s/%s/%s/%s", domainID, projectID, groupID, userID, roleID)
}
func identityRoleAssignmentV3ParseID(roleAssignmentID string) (string, string, string, string, string, error) {
split := strings.Split(roleAssignmentID, "/")
if len(split) != 5 {
return "", "", "", "", "", fmt.Errorf("Malformed ID: %s", roleAssignmentID)
}
return split[0], split[1], split[2], split[3], split[4], nil
}
func identityRoleAssignmentV3FindAssignment(identityClient *gophercloud.ServiceClient, id string) (roles.RoleAssignment, error) {
var assignment roles.RoleAssignment
domainID, projectID, groupID, userID, roleID, err := identityRoleAssignmentV3ParseID(id)
if err != nil {
return assignment, err
}
var opts roles.ListAssignmentsOpts
opts = roles.ListAssignmentsOpts{
GroupID: groupID,
ScopeDomainID: domainID,
ScopeProjectID: projectID,
UserID: userID,
}
pager := roles.ListAssignments(identityClient, opts)
err = pager.EachPage(func(page pagination.Page) (bool, error) {
assignmentList, err := roles.ExtractRoleAssignments(page)
if err != nil {
return false, err
}
for _, a := range assignmentList {
if a.Role.ID == roleID {
assignment = a
return false, nil
}
}
return true, nil
})
return assignment, err
}