PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 326 lines | 206 code | 65 blank | 55 comment | 56 complexity | 4e4b3ea55f2c3aa65fa18cc01205b8f0 MD5 | raw file
  1. package v3
  2. import (
  3. "testing"
  4. "github.com/gophercloud/gophercloud"
  5. "github.com/gophercloud/gophercloud/acceptance/tools"
  6. "github.com/gophercloud/gophercloud/openstack/identity/v3/domains"
  7. "github.com/gophercloud/gophercloud/openstack/identity/v3/groups"
  8. "github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
  9. "github.com/gophercloud/gophercloud/openstack/identity/v3/regions"
  10. "github.com/gophercloud/gophercloud/openstack/identity/v3/roles"
  11. "github.com/gophercloud/gophercloud/openstack/identity/v3/services"
  12. "github.com/gophercloud/gophercloud/openstack/identity/v3/users"
  13. )
  14. // CreateProject will create a project with a random name.
  15. // It takes an optional createOpts parameter since creating a project
  16. // has so many options. An error will be returned if the project was
  17. // unable to be created.
  18. func CreateProject(t *testing.T, client *gophercloud.ServiceClient, c *projects.CreateOpts) (*projects.Project, error) {
  19. name := tools.RandomString("ACPTTEST", 8)
  20. t.Logf("Attempting to create project: %s", name)
  21. var createOpts projects.CreateOpts
  22. if c != nil {
  23. createOpts = *c
  24. } else {
  25. createOpts = projects.CreateOpts{}
  26. }
  27. createOpts.Name = name
  28. project, err := projects.Create(client, createOpts).Extract()
  29. if err != nil {
  30. return project, err
  31. }
  32. t.Logf("Successfully created project %s with ID %s", name, project.ID)
  33. return project, nil
  34. }
  35. // CreateUser will create a user with a random name.
  36. // It takes an optional createOpts parameter since creating a user
  37. // has so many options. An error will be returned if the user was
  38. // unable to be created.
  39. func CreateUser(t *testing.T, client *gophercloud.ServiceClient, c *users.CreateOpts) (*users.User, error) {
  40. name := tools.RandomString("ACPTTEST", 8)
  41. t.Logf("Attempting to create user: %s", name)
  42. var createOpts users.CreateOpts
  43. if c != nil {
  44. createOpts = *c
  45. } else {
  46. createOpts = users.CreateOpts{}
  47. }
  48. createOpts.Name = name
  49. user, err := users.Create(client, createOpts).Extract()
  50. if err != nil {
  51. return user, err
  52. }
  53. t.Logf("Successfully created user %s with ID %s", name, user.ID)
  54. return user, nil
  55. }
  56. // CreateGroup will create a group with a random name.
  57. // It takes an optional createOpts parameter since creating a group
  58. // has so many options. An error will be returned if the group was
  59. // unable to be created.
  60. func CreateGroup(t *testing.T, client *gophercloud.ServiceClient, c *groups.CreateOpts) (*groups.Group, error) {
  61. name := tools.RandomString("ACPTTEST", 8)
  62. t.Logf("Attempting to create group: %s", name)
  63. var createOpts groups.CreateOpts
  64. if c != nil {
  65. createOpts = *c
  66. } else {
  67. createOpts = groups.CreateOpts{}
  68. }
  69. createOpts.Name = name
  70. group, err := groups.Create(client, createOpts).Extract()
  71. if err != nil {
  72. return group, err
  73. }
  74. t.Logf("Successfully created group %s with ID %s", name, group.ID)
  75. return group, nil
  76. }
  77. // CreateDomain will create a domain with a random name.
  78. // It takes an optional createOpts parameter since creating a domain
  79. // has many options. An error will be returned if the domain was
  80. // unable to be created.
  81. func CreateDomain(t *testing.T, client *gophercloud.ServiceClient, c *domains.CreateOpts) (*domains.Domain, error) {
  82. name := tools.RandomString("ACPTTEST", 8)
  83. t.Logf("Attempting to create domain: %s", name)
  84. var createOpts domains.CreateOpts
  85. if c != nil {
  86. createOpts = *c
  87. } else {
  88. createOpts = domains.CreateOpts{}
  89. }
  90. createOpts.Name = name
  91. domain, err := domains.Create(client, createOpts).Extract()
  92. if err != nil {
  93. return domain, err
  94. }
  95. t.Logf("Successfully created domain %s with ID %s", name, domain.ID)
  96. return domain, nil
  97. }
  98. // CreateRole will create a role with a random name.
  99. // It takes an optional createOpts parameter since creating a role
  100. // has so many options. An error will be returned if the role was
  101. // unable to be created.
  102. func CreateRole(t *testing.T, client *gophercloud.ServiceClient, c *roles.CreateOpts) (*roles.Role, error) {
  103. name := tools.RandomString("ACPTTEST", 8)
  104. t.Logf("Attempting to create role: %s", name)
  105. var createOpts roles.CreateOpts
  106. if c != nil {
  107. createOpts = *c
  108. } else {
  109. createOpts = roles.CreateOpts{}
  110. }
  111. createOpts.Name = name
  112. role, err := roles.Create(client, createOpts).Extract()
  113. if err != nil {
  114. return role, err
  115. }
  116. t.Logf("Successfully created role %s with ID %s", name, role.ID)
  117. return role, nil
  118. }
  119. // CreateRegion will create a region with a random name.
  120. // It takes an optional createOpts parameter since creating a region
  121. // has so many options. An error will be returned if the region was
  122. // unable to be created.
  123. func CreateRegion(t *testing.T, client *gophercloud.ServiceClient, c *regions.CreateOpts) (*regions.Region, error) {
  124. id := tools.RandomString("ACPTTEST", 8)
  125. t.Logf("Attempting to create region: %s", id)
  126. var createOpts regions.CreateOpts
  127. if c != nil {
  128. createOpts = *c
  129. } else {
  130. createOpts = regions.CreateOpts{}
  131. }
  132. createOpts.ID = id
  133. region, err := regions.Create(client, createOpts).Extract()
  134. if err != nil {
  135. return region, err
  136. }
  137. t.Logf("Successfully created region %s", id)
  138. return region, nil
  139. }
  140. // CreateService will create a service with a random name.
  141. // It takes an optional createOpts parameter since creating a service
  142. // has so many options. An error will be returned if the service was
  143. // unable to be created.
  144. func CreateService(t *testing.T, client *gophercloud.ServiceClient, c *services.CreateOpts) (*services.Service, error) {
  145. name := tools.RandomString("ACPTTEST", 8)
  146. t.Logf("Attempting to create service: %s", name)
  147. var createOpts services.CreateOpts
  148. if c != nil {
  149. createOpts = *c
  150. } else {
  151. createOpts = services.CreateOpts{}
  152. }
  153. createOpts.Extra["name"] = name
  154. service, err := services.Create(client, createOpts).Extract()
  155. if err != nil {
  156. return service, err
  157. }
  158. t.Logf("Successfully created service %s", service.ID)
  159. return service, nil
  160. }
  161. // DeleteProject will delete a project by ID. A fatal error will occur if
  162. // the project ID failed to be deleted. This works best when using it as
  163. // a deferred function.
  164. func DeleteProject(t *testing.T, client *gophercloud.ServiceClient, projectID string) {
  165. err := projects.Delete(client, projectID).ExtractErr()
  166. if err != nil {
  167. t.Fatalf("Unable to delete project %s: %v", projectID, err)
  168. }
  169. t.Logf("Deleted project: %s", projectID)
  170. }
  171. // DeleteUser will delete a user by ID. A fatal error will occur if
  172. // the user failed to be deleted. This works best when using it as
  173. // a deferred function.
  174. func DeleteUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  175. err := users.Delete(client, userID).ExtractErr()
  176. if err != nil {
  177. t.Fatalf("Unable to delete user with ID %s: %v", userID, err)
  178. }
  179. t.Logf("Deleted user with ID: %s", userID)
  180. }
  181. // DeleteGroup will delete a group by ID. A fatal error will occur if
  182. // the group failed to be deleted. This works best when using it as
  183. // a deferred function.
  184. func DeleteGroup(t *testing.T, client *gophercloud.ServiceClient, groupID string) {
  185. err := groups.Delete(client, groupID).ExtractErr()
  186. if err != nil {
  187. t.Fatalf("Unable to delete group %s: %v", groupID, err)
  188. }
  189. t.Logf("Deleted group: %s", groupID)
  190. }
  191. // DeleteDomain will delete a domain by ID. A fatal error will occur if
  192. // the project ID failed to be deleted. This works best when using it as
  193. // a deferred function.
  194. func DeleteDomain(t *testing.T, client *gophercloud.ServiceClient, domainID string) {
  195. err := domains.Delete(client, domainID).ExtractErr()
  196. if err != nil {
  197. t.Fatalf("Unable to delete domain %s: %v", domainID, err)
  198. }
  199. t.Logf("Deleted domain: %s", domainID)
  200. }
  201. // DeleteRole will delete a role by ID. A fatal error will occur if
  202. // the role failed to be deleted. This works best when using it as
  203. // a deferred function.
  204. func DeleteRole(t *testing.T, client *gophercloud.ServiceClient, roleID string) {
  205. err := roles.Delete(client, roleID).ExtractErr()
  206. if err != nil {
  207. t.Fatalf("Unable to delete role %s: %v", roleID, err)
  208. }
  209. t.Logf("Deleted role: %s", roleID)
  210. }
  211. // DeleteRegion will delete a reg by ID. A fatal error will occur if
  212. // the region failed to be deleted. This works best when using it as
  213. // a deferred function.
  214. func DeleteRegion(t *testing.T, client *gophercloud.ServiceClient, regionID string) {
  215. err := regions.Delete(client, regionID).ExtractErr()
  216. if err != nil {
  217. t.Fatalf("Unable to delete region %s: %v", regionID, err)
  218. }
  219. t.Logf("Deleted region: %s", regionID)
  220. }
  221. // DeleteService will delete a reg by ID. A fatal error will occur if
  222. // the service failed to be deleted. This works best when using it as
  223. // a deferred function.
  224. func DeleteService(t *testing.T, client *gophercloud.ServiceClient, serviceID string) {
  225. err := services.Delete(client, serviceID).ExtractErr()
  226. if err != nil {
  227. t.Fatalf("Unable to delete service %s: %v", serviceID, err)
  228. }
  229. t.Logf("Deleted service: %s", serviceID)
  230. }
  231. // UnassignRole will delete a role assigned to a user/group on a project/domain
  232. // A fatal error will occur if it fails to delete the assignment.
  233. // This works best when using it as a deferred function.
  234. func UnassignRole(t *testing.T, client *gophercloud.ServiceClient, roleID string, opts *roles.UnassignOpts) {
  235. err := roles.Unassign(client, roleID, *opts).ExtractErr()
  236. if err != nil {
  237. t.Fatalf("Unable to unassign a role %v on context %+v: %v", roleID, *opts, err)
  238. }
  239. t.Logf("Unassigned the role %v on context %+v", roleID, *opts)
  240. }
  241. // FindRole finds all roles that the current authenticated client has access
  242. // to and returns the first one found. An error will be returned if the lookup
  243. // was unsuccessful.
  244. func FindRole(t *testing.T, client *gophercloud.ServiceClient) (*roles.Role, error) {
  245. t.Log("Attempting to find a role")
  246. var role *roles.Role
  247. allPages, err := roles.List(client, nil).AllPages()
  248. if err != nil {
  249. return nil, err
  250. }
  251. allRoles, err := roles.ExtractRoles(allPages)
  252. if err != nil {
  253. return nil, err
  254. }
  255. for _, r := range allRoles {
  256. role = &r
  257. break
  258. }
  259. t.Logf("Successfully found a role %s with ID %s", role.Name, role.ID)
  260. return role, nil
  261. }