Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 1 | // Package v2 contains common functions for creating identity-based resources |
| 2 | // for use in acceptance tests. See the `*_test.go` files for example usages. |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/gophercloud/gophercloud" |
| 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 10 | "github.com/gophercloud/gophercloud/openstack/identity/v2/extensions/admin/roles" |
| 11 | "github.com/gophercloud/gophercloud/openstack/identity/v2/tenants" |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 12 | "github.com/gophercloud/gophercloud/openstack/identity/v2/users" |
| 13 | ) |
| 14 | |
| 15 | // AddUserRole will grant a role to a user in a tenant. An error will be |
| 16 | // returned if the grant was unsuccessful. |
| 17 | func AddUserRole(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant, user *users.User, role *roles.Role) error { |
| 18 | t.Logf("Attempting to grant user %s role %s in tenant %s", user.ID, role.ID, tenant.ID) |
| 19 | |
| 20 | err := roles.AddUser(client, tenant.ID, user.ID, role.ID).ExtractErr() |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | t.Logf("Granted user %s role %s in tenant %s", user.ID, role.ID, tenant.ID) |
| 26 | |
| 27 | return nil |
| 28 | } |
| 29 | |
| 30 | // CreateUser will create a user with a random name and adds them to the given |
| 31 | // tenant. An error will be returned if the user was unable to be created. |
| 32 | func CreateUser(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant) (*users.User, error) { |
| 33 | userName := tools.RandomString("user_", 5) |
| 34 | userEmail := userName + "@foo.com" |
| 35 | t.Logf("Creating user: %s", userName) |
| 36 | |
| 37 | createOpts := users.CreateOpts{ |
| 38 | Name: userName, |
| 39 | Enabled: gophercloud.Disabled, |
| 40 | TenantID: tenant.ID, |
| 41 | Email: userEmail, |
| 42 | } |
| 43 | |
| 44 | user, err := users.Create(client, createOpts).Extract() |
| 45 | if err != nil { |
| 46 | return user, err |
| 47 | } |
| 48 | |
| 49 | return user, nil |
| 50 | } |
| 51 | |
| 52 | // DeleteUser will delete a user. A fatal error will occur if the delete was |
| 53 | // unsuccessful. This works best when used as a deferred function. |
| 54 | func DeleteUser(t *testing.T, client *gophercloud.ServiceClient, user *users.User) { |
| 55 | t.Logf("Attempting to delete user: %s", user.Name) |
| 56 | |
| 57 | result := users.Delete(client, user.ID) |
| 58 | if result.Err != nil { |
| 59 | t.Fatalf("Unable to delete user") |
| 60 | } |
| 61 | |
| 62 | t.Logf("Deleted user: %s", user.Name) |
| 63 | } |
| 64 | |
| 65 | // DeleteUserRole will revoke a role of a user in a tenant. A fatal error will |
| 66 | // occur if the revoke was unsuccessful. This works best when used as a |
| 67 | // deferred function. |
| 68 | func DeleteUserRole(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant, user *users.User, role *roles.Role) { |
| 69 | t.Logf("Attempting to remove role %s from user %s in tenant %s", role.ID, user.ID, tenant.ID) |
| 70 | |
| 71 | err := roles.DeleteUser(client, tenant.ID, user.ID, role.ID).ExtractErr() |
| 72 | if err != nil { |
| 73 | t.Fatalf("Unable to remove role") |
| 74 | } |
| 75 | |
| 76 | t.Logf("Removed role %s from user %s in tenant %s", role.ID, user.ID, tenant.ID) |
| 77 | } |
| 78 | |
| 79 | // FindRole finds all roles that the current authenticated client has access |
| 80 | // to and returns the first one found. An error will be returned if the lookup |
| 81 | // was unsuccessful. |
| 82 | func FindRole(t *testing.T, client *gophercloud.ServiceClient) (*roles.Role, error) { |
| 83 | var role *roles.Role |
| 84 | |
| 85 | allPages, err := roles.List(client).AllPages() |
| 86 | if err != nil { |
| 87 | return role, err |
| 88 | } |
| 89 | |
| 90 | allRoles, err := roles.ExtractRoles(allPages) |
| 91 | if err != nil { |
| 92 | return role, err |
| 93 | } |
| 94 | |
| 95 | for _, r := range allRoles { |
| 96 | role = &r |
| 97 | break |
| 98 | } |
| 99 | |
| 100 | return role, nil |
| 101 | } |
| 102 | |
| 103 | // FindTenant finds all tenants that the current authenticated client has access |
| 104 | // to and returns the first one found. An error will be returned if the lookup |
| 105 | // was unsuccessful. |
| 106 | func FindTenant(t *testing.T, client *gophercloud.ServiceClient) (*tenants.Tenant, error) { |
| 107 | var tenant *tenants.Tenant |
| 108 | |
| 109 | allPages, err := tenants.List(client, nil).AllPages() |
| 110 | if err != nil { |
| 111 | return tenant, err |
| 112 | } |
| 113 | |
| 114 | allTenants, err := tenants.ExtractTenants(allPages) |
| 115 | if err != nil { |
| 116 | return tenant, err |
| 117 | } |
| 118 | |
| 119 | for _, t := range allTenants { |
| 120 | tenant = &t |
| 121 | break |
| 122 | } |
| 123 | |
| 124 | return tenant, nil |
| 125 | } |
| 126 | |
| 127 | // UpdateUser will update an existing user with a new randomly generated name. |
| 128 | // An error will be returned if the update was unsuccessful. |
| 129 | func UpdateUser(t *testing.T, client *gophercloud.ServiceClient, user *users.User) (*users.User, error) { |
| 130 | userName := tools.RandomString("user_", 5) |
| 131 | userEmail := userName + "@foo.com" |
| 132 | |
| 133 | t.Logf("Attempting to update user name from %s to %s", user.Name, userName) |
| 134 | |
| 135 | updateOpts := users.UpdateOpts{ |
| 136 | Name: userName, |
| 137 | Email: userEmail, |
| 138 | } |
| 139 | |
| 140 | newUser, err := users.Update(client, user.ID, updateOpts).Extract() |
| 141 | if err != nil { |
| 142 | return newUser, err |
| 143 | } |
| 144 | |
| 145 | return newUser, nil |
| 146 | } |