blob: efaeb75cde8a94d89e9a2c3e61d389a4a129416b [file] [log] [blame]
Jamie Hannaford41a9c5d2014-10-30 14:42:44 +01001// +build acceptance identity roles
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 os "github.com/rackspace/gophercloud/openstack/identity/v2/extensions/admin/roles"
10
11 "github.com/rackspace/gophercloud/pagination"
12 "github.com/rackspace/gophercloud/rackspace/identity/v2/roles"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func TestRoles(t *testing.T) {
17 client := authenticatedClient(t)
18
19 userID := createUser(t, client)
20 roleID := listRoles(t, client)
21
22 addUserRole(t, client, userID, roleID)
23
24 deleteUserRole(t, client, userID, roleID)
25
26 deleteUser(t, client, userID)
27}
28
29func listRoles(t *testing.T, client *gophercloud.ServiceClient) string {
30 var roleID string
31
32 err := roles.List(client).EachPage(func(page pagination.Page) (bool, error) {
33 roleList, err := os.ExtractRoles(page)
34 th.AssertNoErr(t, err)
35
36 for _, role := range roleList {
37 t.Logf("Listing role: ID [%s] Name [%s]", role.ID, role.Name)
38 roleID = role.ID
39 }
40
41 return true, nil
42 })
43
44 th.AssertNoErr(t, err)
45
46 return roleID
47}
48
49func addUserRole(t *testing.T, client *gophercloud.ServiceClient, userID, roleID string) {
50 err := roles.AddUserRole(client, userID, roleID).ExtractErr()
51 th.AssertNoErr(t, err)
52 t.Logf("Added role %s to user %s", roleID, userID)
53}
54
55func deleteUserRole(t *testing.T, client *gophercloud.ServiceClient, userID, roleID string) {
56 err := roles.DeleteUserRole(client, userID, roleID).ExtractErr()
57 th.AssertNoErr(t, err)
58 t.Logf("Removed role %s from user %s", roleID, userID)
59}