PageRenderTime 171ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/identity/v2/identity.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 186 lines | 121 code | 39 blank | 26 comment | 27 complexity | 92a85683c9ada8caa0480d58d28e08bd MD5 | raw file
  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. import (
  5. "testing"
  6. "github.com/gophercloud/gophercloud"
  7. "github.com/gophercloud/gophercloud/acceptance/tools"
  8. "github.com/gophercloud/gophercloud/openstack/identity/v2/extensions/admin/roles"
  9. "github.com/gophercloud/gophercloud/openstack/identity/v2/tenants"
  10. "github.com/gophercloud/gophercloud/openstack/identity/v2/users"
  11. )
  12. // AddUserRole will grant a role to a user in a tenant. An error will be
  13. // returned if the grant was unsuccessful.
  14. func AddUserRole(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant, user *users.User, role *roles.Role) error {
  15. t.Logf("Attempting to grant user %s role %s in tenant %s", user.ID, role.ID, tenant.ID)
  16. err := roles.AddUser(client, tenant.ID, user.ID, role.ID).ExtractErr()
  17. if err != nil {
  18. return err
  19. }
  20. t.Logf("Granted user %s role %s in tenant %s", user.ID, role.ID, tenant.ID)
  21. return nil
  22. }
  23. // CreateTenant will create a project with a random name.
  24. // It takes an optional createOpts parameter since creating a project
  25. // has so many options. An error will be returned if the project was
  26. // unable to be created.
  27. func CreateTenant(t *testing.T, client *gophercloud.ServiceClient, c *tenants.CreateOpts) (*tenants.Tenant, error) {
  28. name := tools.RandomString("ACPTTEST", 8)
  29. t.Logf("Attempting to create tenant: %s", name)
  30. var createOpts tenants.CreateOpts
  31. if c != nil {
  32. createOpts = *c
  33. } else {
  34. createOpts = tenants.CreateOpts{}
  35. }
  36. createOpts.Name = name
  37. tenant, err := tenants.Create(client, createOpts).Extract()
  38. if err != nil {
  39. t.Logf("Foo")
  40. return tenant, err
  41. }
  42. t.Logf("Successfully created project %s with ID %s", name, tenant.ID)
  43. return tenant, nil
  44. }
  45. // CreateUser will create a user with a random name and adds them to the given
  46. // tenant. An error will be returned if the user was unable to be created.
  47. func CreateUser(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant) (*users.User, error) {
  48. userName := tools.RandomString("user_", 5)
  49. userEmail := userName + "@foo.com"
  50. t.Logf("Creating user: %s", userName)
  51. createOpts := users.CreateOpts{
  52. Name: userName,
  53. Enabled: gophercloud.Disabled,
  54. TenantID: tenant.ID,
  55. Email: userEmail,
  56. }
  57. user, err := users.Create(client, createOpts).Extract()
  58. if err != nil {
  59. return user, err
  60. }
  61. return user, nil
  62. }
  63. // DeleteTenant will delete a tenant by ID. A fatal error will occur if
  64. // the tenant ID failed to be deleted. This works best when using it as
  65. // a deferred function.
  66. func DeleteTenant(t *testing.T, client *gophercloud.ServiceClient, tenantID string) {
  67. err := tenants.Delete(client, tenantID).ExtractErr()
  68. if err != nil {
  69. t.Fatalf("Unable to delete tenant %s: %v", tenantID, err)
  70. }
  71. t.Logf("Deleted tenant: %s", tenantID)
  72. }
  73. // DeleteUser will delete a user. A fatal error will occur if the delete was
  74. // unsuccessful. This works best when used as a deferred function.
  75. func DeleteUser(t *testing.T, client *gophercloud.ServiceClient, user *users.User) {
  76. t.Logf("Attempting to delete user: %s", user.Name)
  77. result := users.Delete(client, user.ID)
  78. if result.Err != nil {
  79. t.Fatalf("Unable to delete user")
  80. }
  81. t.Logf("Deleted user: %s", user.Name)
  82. }
  83. // DeleteUserRole will revoke a role of a user in a tenant. A fatal error will
  84. // occur if the revoke was unsuccessful. This works best when used as a
  85. // deferred function.
  86. func DeleteUserRole(t *testing.T, client *gophercloud.ServiceClient, tenant *tenants.Tenant, user *users.User, role *roles.Role) {
  87. t.Logf("Attempting to remove role %s from user %s in tenant %s", role.ID, user.ID, tenant.ID)
  88. err := roles.DeleteUser(client, tenant.ID, user.ID, role.ID).ExtractErr()
  89. if err != nil {
  90. t.Fatalf("Unable to remove role")
  91. }
  92. t.Logf("Removed role %s from user %s in tenant %s", role.ID, user.ID, tenant.ID)
  93. }
  94. // FindRole finds all roles that the current authenticated client has access
  95. // to and returns the first one found. An error will be returned if the lookup
  96. // was unsuccessful.
  97. func FindRole(t *testing.T, client *gophercloud.ServiceClient) (*roles.Role, error) {
  98. var role *roles.Role
  99. allPages, err := roles.List(client).AllPages()
  100. if err != nil {
  101. return role, err
  102. }
  103. allRoles, err := roles.ExtractRoles(allPages)
  104. if err != nil {
  105. return role, err
  106. }
  107. for _, r := range allRoles {
  108. role = &r
  109. break
  110. }
  111. return role, nil
  112. }
  113. // FindTenant finds all tenants that the current authenticated client has access
  114. // to and returns the first one found. An error will be returned if the lookup
  115. // was unsuccessful.
  116. func FindTenant(t *testing.T, client *gophercloud.ServiceClient) (*tenants.Tenant, error) {
  117. var tenant *tenants.Tenant
  118. allPages, err := tenants.List(client, nil).AllPages()
  119. if err != nil {
  120. return tenant, err
  121. }
  122. allTenants, err := tenants.ExtractTenants(allPages)
  123. if err != nil {
  124. return tenant, err
  125. }
  126. for _, t := range allTenants {
  127. tenant = &t
  128. break
  129. }
  130. return tenant, nil
  131. }
  132. // UpdateUser will update an existing user with a new randomly generated name.
  133. // An error will be returned if the update was unsuccessful.
  134. func UpdateUser(t *testing.T, client *gophercloud.ServiceClient, user *users.User) (*users.User, error) {
  135. userName := tools.RandomString("user_", 5)
  136. userEmail := userName + "@foo.com"
  137. t.Logf("Attempting to update user name from %s to %s", user.Name, userName)
  138. updateOpts := users.UpdateOpts{
  139. Name: userName,
  140. Email: userEmail,
  141. }
  142. newUser, err := users.Update(client, user.ID, updateOpts).Extract()
  143. if err != nil {
  144. return newUser, err
  145. }
  146. return newUser, nil
  147. }