blob: f1c6624e33720dbacf5afe761cabb5d73b702c70 [file] [log] [blame]
Joe Topjian918f5732016-08-15 08:47:08 -06001// Package v2 contains common functions for creating identity-based resources
2// for use in acceptance tests. See the `*_test.go` files for example usages.
3package v2
4
5import (
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"
12 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
13 "github.com/gophercloud/gophercloud/openstack/identity/v2/users"
14)
15
16// AddUserRole will grant a role to a user in a tenant. An error will be
17// returned if the grant was unsuccessful.
18func AddUserRole(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant, user *users.User, role *roles.Role) error {
19 t.Logf("Attempting to grant user %s role %s in tenant %s", user.ID, role.ID, tenant.ID)
20
21 err := roles.AddUser(client, tenant.ID, user.ID, role.ID).ExtractErr()
22 if err != nil {
23 return err
24 }
25
26 t.Logf("Granted user %s role %s in tenant %s", user.ID, role.ID, tenant.ID)
27
28 return nil
29}
30
31// CreateUser will create a user with a random name and adds them to the given
32// tenant. An error will be returned if the user was unable to be created.
33func CreateUser(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant) (*users.User, error) {
34 userName := tools.RandomString("user_", 5)
35 userEmail := userName + "@foo.com"
36 t.Logf("Creating user: %s", userName)
37
38 createOpts := users.CreateOpts{
39 Name: userName,
40 Enabled: gophercloud.Disabled,
41 TenantID: tenant.ID,
42 Email: userEmail,
43 }
44
45 user, err := users.Create(client, createOpts).Extract()
46 if err != nil {
47 return user, err
48 }
49
50 return user, nil
51}
52
53// DeleteUser will delete a user. A fatal error will occur if the delete was
54// unsuccessful. This works best when used as a deferred function.
55func DeleteUser(t *testing.T, client *gophercloud.ServiceClient, user *users.User) {
56 t.Logf("Attempting to delete user: %s", user.Name)
57
58 result := users.Delete(client, user.ID)
59 if result.Err != nil {
60 t.Fatalf("Unable to delete user")
61 }
62
63 t.Logf("Deleted user: %s", user.Name)
64}
65
66// DeleteUserRole will revoke a role of a user in a tenant. A fatal error will
67// occur if the revoke was unsuccessful. This works best when used as a
68// deferred function.
69func DeleteUserRole(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant, user *users.User, role *roles.Role) {
70 t.Logf("Attempting to remove role %s from user %s in tenant %s", role.ID, user.ID, tenant.ID)
71
72 err := roles.DeleteUser(client, tenant.ID, user.ID, role.ID).ExtractErr()
73 if err != nil {
74 t.Fatalf("Unable to remove role")
75 }
76
77 t.Logf("Removed role %s from user %s in tenant %s", role.ID, user.ID, tenant.ID)
78}
79
80// FindRole finds all roles that the current authenticated client has access
81// to and returns the first one found. An error will be returned if the lookup
82// was unsuccessful.
83func FindRole(t *testing.T, client *gophercloud.ServiceClient) (*roles.Role, error) {
84 var role *roles.Role
85
86 allPages, err := roles.List(client).AllPages()
87 if err != nil {
88 return role, err
89 }
90
91 allRoles, err := roles.ExtractRoles(allPages)
92 if err != nil {
93 return role, err
94 }
95
96 for _, r := range allRoles {
97 role = &r
98 break
99 }
100
101 return role, nil
102}
103
104// FindTenant finds all tenants that the current authenticated client has access
105// to and returns the first one found. An error will be returned if the lookup
106// was unsuccessful.
107func FindTenant(t *testing.T, client *gophercloud.ServiceClient) (*tenants.Tenant, error) {
108 var tenant *tenants.Tenant
109
110 allPages, err := tenants.List(client, nil).AllPages()
111 if err != nil {
112 return tenant, err
113 }
114
115 allTenants, err := tenants.ExtractTenants(allPages)
116 if err != nil {
117 return tenant, err
118 }
119
120 for _, t := range allTenants {
121 tenant = &t
122 break
123 }
124
125 return tenant, nil
126}
127
128// UpdateUser will update an existing user with a new randomly generated name.
129// An error will be returned if the update was unsuccessful.
130func UpdateUser(t *testing.T, client *gophercloud.ServiceClient, user *users.User) (*users.User, error) {
131 userName := tools.RandomString("user_", 5)
132 userEmail := userName + "@foo.com"
133
134 t.Logf("Attempting to update user name from %s to %s", user.Name, userName)
135
136 updateOpts := users.UpdateOpts{
137 Name: userName,
138 Email: userEmail,
139 }
140
141 newUser, err := users.Update(client, user.ID, updateOpts).Extract()
142 if err != nil {
143 return newUser, err
144 }
145
146 return newUser, nil
147}
148
149// PrintCatalogEntry will print a catalog entry and all of its attributes.
150func PrintCatalogEntry(t *testing.T, catalogEntry *tokens.CatalogEntry) {
151 t.Logf("Name: %s", catalogEntry.Name)
152 t.Logf("Type: %s", catalogEntry.Type)
153
154 t.Log("Endpoints:")
155 for _, endpoint := range catalogEntry.Endpoints {
156 t.Logf("\tTenantID: %s", endpoint.TenantID)
157 t.Logf("\tPublicURL: %s", endpoint.PublicURL)
158 t.Logf("\tInternalURL: %s", endpoint.InternalURL)
159 t.Logf("\tAdminURL: %s", endpoint.AdminURL)
160 t.Logf("\tRegion: %s", endpoint.Region)
161 t.Logf("\tVersionID: %s", endpoint.VersionID)
162 t.Logf("\tVersionInfo: %s", endpoint.VersionInfo)
163 t.Logf("\tVersionList: %s", endpoint.VersionList)
164 }
165}
166
167// PrintRole will print a role and all of its attributes.
168func PrintRole(t *testing.T, role *roles.Role) {
169 t.Logf("ID: %s", role.ID)
170 t.Logf("Name: %v", role.Name)
171 t.Logf("Description: %s", role.Description)
172 t.Logf("ServiceID: %s", role.ServiceID)
173}
174
175// PrintTenant will print a tenant and all of its attributes.
176func PrintTenant(t *testing.T, tenant *tenants.Tenant) {
177 t.Logf("ID: %s", tenant.ID)
178 t.Logf("Name: %s", tenant.Name)
179 t.Logf("Description: %s", tenant.Description)
180 t.Logf("Enabled: %t", tenant.Enabled)
181}
182
183// PrintToken will print a token and all of its attributes.
184func PrintToken(t *testing.T, token *tokens.Token) {
185 t.Logf("ID: %s", token.ID)
186 t.Logf("ExpiresAt: %v", token.ExpiresAt)
187 t.Logf("TenantID: %s", token.Tenant.ID)
188}
189
190// PrintTokenUser will print the user information of a token and all attributes.
191func PrintTokenUser(t *testing.T, user *tokens.User) {
192 t.Logf("ID: %s", user.ID)
193 t.Logf("Name: %s", user.Name)
194 t.Logf("Username: %s", user.UserName)
195
196 t.Log("Roles")
197 for _, role := range user.Roles {
198 t.Logf("\t%s", role)
199 }
200}
201
202// PrintUser will print a user and all of its attributes.
203func PrintUser(t *testing.T, user *users.User) {
204 t.Logf("ID: %s", user.ID)
205 t.Logf("Name: %s", user.Name)
206 t.Logf("Username: %s", user.Username)
207 t.Logf("Enabled: %t", user.Enabled)
208 t.Logf("Email: %s", user.Email)
209 t.Logf("TenantID: %s", user.TenantID)
210}
211
212// PrintUserRole will print the roles that a user has been granted.
213func PrintUserRole(t *testing.T, role *users.Role) {
214 t.Logf("ID: %s", role.ID)
215 t.Logf("Name: %s", role.Name)
216}