PageRenderTime 115ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2/extensions/layer3/layer3.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 250 lines | 178 code | 55 blank | 17 comment | 45 complexity | ee445b1e166e98cd211bea552fe2a12f MD5 | raw file
  1. package layer3
  2. import (
  3. "testing"
  4. "github.com/gophercloud/gophercloud"
  5. "github.com/gophercloud/gophercloud/acceptance/clients"
  6. "github.com/gophercloud/gophercloud/acceptance/tools"
  7. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
  8. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
  9. "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
  10. )
  11. // CreateFloatingIP creates a floating IP on a given network and port. An error
  12. // will be returned if the creation failed.
  13. func CreateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, networkID, portID string) (*floatingips.FloatingIP, error) {
  14. t.Logf("Attempting to create floating IP on port: %s", portID)
  15. createOpts := &floatingips.CreateOpts{
  16. FloatingNetworkID: networkID,
  17. PortID: portID,
  18. }
  19. floatingIP, err := floatingips.Create(client, createOpts).Extract()
  20. if err != nil {
  21. return floatingIP, err
  22. }
  23. t.Logf("Created floating IP.")
  24. return floatingIP, err
  25. }
  26. // CreateExternalRouter creates a router on the external network. This requires
  27. // the OS_EXTGW_ID environment variable to be set. An error is returned if the
  28. // creation failed.
  29. func CreateExternalRouter(t *testing.T, client *gophercloud.ServiceClient) (*routers.Router, error) {
  30. var router *routers.Router
  31. choices, err := clients.AcceptanceTestChoicesFromEnv()
  32. if err != nil {
  33. return router, err
  34. }
  35. routerName := tools.RandomString("TESTACC-", 8)
  36. t.Logf("Attempting to create external router: %s", routerName)
  37. adminStateUp := true
  38. enableSNAT := false
  39. gatewayInfo := routers.GatewayInfo{
  40. NetworkID: choices.ExternalNetworkID,
  41. EnableSNAT: &enableSNAT,
  42. }
  43. createOpts := routers.CreateOpts{
  44. Name: routerName,
  45. AdminStateUp: &adminStateUp,
  46. GatewayInfo: &gatewayInfo,
  47. }
  48. router, err = routers.Create(client, createOpts).Extract()
  49. if err != nil {
  50. return router, err
  51. }
  52. if err := WaitForRouterToCreate(client, router.ID, 60); err != nil {
  53. return router, err
  54. }
  55. t.Logf("Created router: %s", routerName)
  56. return router, nil
  57. }
  58. // CreateRouter creates a router on a specified Network ID. An error will be
  59. // returned if the creation failed.
  60. func CreateRouter(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*routers.Router, error) {
  61. routerName := tools.RandomString("TESTACC-", 8)
  62. t.Logf("Attempting to create router: %s", routerName)
  63. adminStateUp := true
  64. gatewayInfo := routers.GatewayInfo{
  65. NetworkID: networkID,
  66. }
  67. createOpts := routers.CreateOpts{
  68. Name: routerName,
  69. AdminStateUp: &adminStateUp,
  70. GatewayInfo: &gatewayInfo,
  71. }
  72. router, err := routers.Create(client, createOpts).Extract()
  73. if err != nil {
  74. return router, err
  75. }
  76. if err := WaitForRouterToCreate(client, router.ID, 60); err != nil {
  77. return router, err
  78. }
  79. t.Logf("Created router: %s", routerName)
  80. return router, nil
  81. }
  82. // CreateRouterInterface will attach a subnet to a router. An error will be
  83. // returned if the operation fails.
  84. func CreateRouterInterface(t *testing.T, client *gophercloud.ServiceClient, portID, routerID string) (*routers.InterfaceInfo, error) {
  85. t.Logf("Attempting to add port %s to router %s", portID, routerID)
  86. aiOpts := routers.AddInterfaceOpts{
  87. PortID: portID,
  88. }
  89. iface, err := routers.AddInterface(client, routerID, aiOpts).Extract()
  90. if err != nil {
  91. return iface, err
  92. }
  93. if err := WaitForRouterInterfaceToAttach(client, portID, 60); err != nil {
  94. return iface, err
  95. }
  96. t.Logf("Successfully added port %s to router %s", portID, routerID)
  97. return iface, nil
  98. }
  99. // DeleteRouter deletes a router of a specified ID. A fatal error will occur
  100. // if the deletion failed. This works best when used as a deferred function.
  101. func DeleteRouter(t *testing.T, client *gophercloud.ServiceClient, routerID string) {
  102. t.Logf("Attempting to delete router: %s", routerID)
  103. err := routers.Delete(client, routerID).ExtractErr()
  104. if err != nil {
  105. t.Fatalf("Error deleting router: %v", err)
  106. }
  107. if err := WaitForRouterToDelete(client, routerID, 60); err != nil {
  108. t.Fatalf("Error waiting for router to delete: %v", err)
  109. }
  110. t.Logf("Deleted router: %s", routerID)
  111. }
  112. // DeleteRouterInterface will detach a subnet to a router. A fatal error will
  113. // occur if the deletion failed. This works best when used as a deferred
  114. // function.
  115. func DeleteRouterInterface(t *testing.T, client *gophercloud.ServiceClient, portID, routerID string) {
  116. t.Logf("Attempting to detach port %s from router %s", portID, routerID)
  117. riOpts := routers.RemoveInterfaceOpts{
  118. PortID: portID,
  119. }
  120. _, err := routers.RemoveInterface(client, routerID, riOpts).Extract()
  121. if err != nil {
  122. t.Fatalf("Failed to detach port %s from router %s", portID, routerID)
  123. }
  124. if err := WaitForRouterInterfaceToDetach(client, portID, 60); err != nil {
  125. t.Fatalf("Failed to wait for port %s to detach from router %s", portID, routerID)
  126. }
  127. t.Logf("Successfully detached port %s from router %s", portID, routerID)
  128. }
  129. // DeleteFloatingIP deletes a floatingIP of a specified ID. A fatal error will
  130. // occur if the deletion failed. This works best when used as a deferred
  131. // function.
  132. func DeleteFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIPID string) {
  133. t.Logf("Attempting to delete floating IP: %s", floatingIPID)
  134. err := floatingips.Delete(client, floatingIPID).ExtractErr()
  135. if err != nil {
  136. t.Fatalf("Failed to delete floating IP: %v", err)
  137. }
  138. t.Logf("Deleted floating IP: %s", floatingIPID)
  139. }
  140. func WaitForRouterToCreate(client *gophercloud.ServiceClient, routerID string, secs int) error {
  141. return gophercloud.WaitFor(secs, func() (bool, error) {
  142. r, err := routers.Get(client, routerID).Extract()
  143. if err != nil {
  144. return false, err
  145. }
  146. if r.Status == "ACTIVE" {
  147. return true, nil
  148. }
  149. return false, nil
  150. })
  151. }
  152. func WaitForRouterToDelete(client *gophercloud.ServiceClient, routerID string, secs int) error {
  153. return gophercloud.WaitFor(secs, func() (bool, error) {
  154. _, err := routers.Get(client, routerID).Extract()
  155. if err != nil {
  156. if _, ok := err.(gophercloud.ErrDefault404); ok {
  157. return true, nil
  158. }
  159. return false, err
  160. }
  161. return false, nil
  162. })
  163. }
  164. func WaitForRouterInterfaceToAttach(client *gophercloud.ServiceClient, routerInterfaceID string, secs int) error {
  165. return gophercloud.WaitFor(secs, func() (bool, error) {
  166. r, err := ports.Get(client, routerInterfaceID).Extract()
  167. if err != nil {
  168. return false, err
  169. }
  170. if r.Status == "ACTIVE" {
  171. return true, nil
  172. }
  173. return false, nil
  174. })
  175. }
  176. func WaitForRouterInterfaceToDetach(client *gophercloud.ServiceClient, routerInterfaceID string, secs int) error {
  177. return gophercloud.WaitFor(secs, func() (bool, error) {
  178. r, err := ports.Get(client, routerInterfaceID).Extract()
  179. if err != nil {
  180. if _, ok := err.(gophercloud.ErrDefault404); ok {
  181. return true, nil
  182. }
  183. if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
  184. if errCode.Actual == 409 {
  185. return false, nil
  186. }
  187. }
  188. return false, err
  189. }
  190. if r.Status == "ACTIVE" {
  191. return true, nil
  192. }
  193. return false, nil
  194. })
  195. }