Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 1 | // +build acceptance identity roles |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/openstack/identity/v2/extensions/admin/roles" |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/identity/v2/users" |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 13 | func TestRolesAddToUser(t *testing.T) { |
| 14 | client, err := clients.NewIdentityV2AdminClient() |
| 15 | if err != nil { |
| 16 | t.Fatalf("Unable to obtain an identity client: %v", err) |
| 17 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 18 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 19 | tenant, err := FindTenant(t, client) |
| 20 | if err != nil { |
| 21 | t.Fatalf("Unable to get a tenant: %v", err) |
| 22 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 23 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 24 | role, err := FindRole(t, client) |
| 25 | if err != nil { |
| 26 | t.Fatalf("Unable to get a role: %v", err) |
| 27 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 28 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 29 | user, err := CreateUser(t, client, tenant) |
| 30 | if err != nil { |
| 31 | t.Fatalf("Unable to create a user: %v", err) |
| 32 | } |
| 33 | defer DeleteUser(t, client, user) |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 34 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 35 | err = AddUserRole(t, client, tenant, user, role) |
| 36 | if err != nil { |
| 37 | t.Fatalf("Unable to add role to user: %v", err) |
| 38 | } |
| 39 | defer DeleteUserRole(t, client, tenant, user, role) |
| 40 | |
| 41 | allPages, err := users.ListRoles(client, tenant.ID, user.ID).AllPages() |
| 42 | if err != nil { |
| 43 | t.Fatalf("Unable to obtain roles for user: %v", err) |
| 44 | } |
| 45 | |
| 46 | allRoles, err := users.ExtractRoles(allPages) |
| 47 | if err != nil { |
| 48 | t.Fatalf("Unable to extract roles: %v", err) |
| 49 | } |
| 50 | |
| 51 | t.Logf("Roles of user %s:", user.Name) |
| 52 | for _, role := range allRoles { |
| 53 | PrintUserRole(t, &role) |
| 54 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 57 | func TestRolesList(t *testing.T) { |
| 58 | client, err := clients.NewIdentityV2AdminClient() |
| 59 | if err != nil { |
| 60 | t.Fatalf("Unable to create an identity client: %v", err) |
| 61 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 62 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 63 | allPages, err := roles.List(client).AllPages() |
| 64 | if err != nil { |
| 65 | t.Fatalf("Unable to list all roles: %v", err) |
| 66 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 67 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 68 | allRoles, err := roles.ExtractRoles(allPages) |
| 69 | if err != nil { |
| 70 | t.Fatalf("Unable to extract roles: %v", err) |
| 71 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 72 | |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 73 | for _, r := range allRoles { |
| 74 | PrintRole(t, &r) |
| 75 | } |
Jamie Hannaford | cebff1b | 2014-10-30 14:31:44 +0100 | [diff] [blame] | 76 | } |