PageRenderTime 71ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 282 lines | 190 code | 65 blank | 27 comment | 49 complexity | 61f6f681d0b10755dd9c34478c6fb943 MD5 | raw file
  1. package lbaas_v2
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/gophercloud/gophercloud"
  7. "github.com/gophercloud/gophercloud/acceptance/tools"
  8. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/listeners"
  9. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/loadbalancers"
  10. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/monitors"
  11. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/pools"
  12. )
  13. const loadbalancerActiveTimeoutSeconds = 300
  14. const loadbalancerDeleteTimeoutSeconds = 300
  15. // CreateListener will create a listener for a given load balancer on a random
  16. // port with a random name. An error will be returned if the listener could not
  17. // be created.
  18. func CreateListener(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer) (*listeners.Listener, error) {
  19. listenerName := tools.RandomString("TESTACCT-", 8)
  20. listenerPort := tools.RandomInt(1, 100)
  21. t.Logf("Attempting to create listener %s on port %d", listenerName, listenerPort)
  22. createOpts := listeners.CreateOpts{
  23. Name: listenerName,
  24. LoadbalancerID: lb.ID,
  25. Protocol: "TCP",
  26. ProtocolPort: listenerPort,
  27. }
  28. listener, err := listeners.Create(client, createOpts).Extract()
  29. if err != nil {
  30. return listener, err
  31. }
  32. t.Logf("Successfully created listener %s", listenerName)
  33. if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  34. return listener, fmt.Errorf("Timed out waiting for loadbalancer to become active")
  35. }
  36. return listener, nil
  37. }
  38. // CreateLoadBalancer will create a load balancer with a random name on a given
  39. // subnet. An error will be returned if the loadbalancer could not be created.
  40. func CreateLoadBalancer(t *testing.T, client *gophercloud.ServiceClient, subnetID string) (*loadbalancers.LoadBalancer, error) {
  41. lbName := tools.RandomString("TESTACCT-", 8)
  42. t.Logf("Attempting to create loadbalancer %s on subnet %s", lbName, subnetID)
  43. createOpts := loadbalancers.CreateOpts{
  44. Name: lbName,
  45. VipSubnetID: subnetID,
  46. AdminStateUp: gophercloud.Enabled,
  47. }
  48. lb, err := loadbalancers.Create(client, createOpts).Extract()
  49. if err != nil {
  50. return lb, err
  51. }
  52. t.Logf("Successfully created loadbalancer %s on subnet %s", lbName, subnetID)
  53. t.Logf("Waiting for loadbalancer %s to become active", lbName)
  54. if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  55. return lb, err
  56. }
  57. t.Logf("LoadBalancer %s is active", lbName)
  58. return lb, nil
  59. }
  60. // CreateMember will create a member with a random name, port, address, and
  61. // weight. An error will be returned if the member could not be created.
  62. func CreateMember(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer, pool *pools.Pool, subnetID, subnetCIDR string) (*pools.Member, error) {
  63. memberName := tools.RandomString("TESTACCT-", 8)
  64. memberPort := tools.RandomInt(100, 1000)
  65. memberWeight := tools.RandomInt(1, 10)
  66. cidrParts := strings.Split(subnetCIDR, "/")
  67. subnetParts := strings.Split(cidrParts[0], ".")
  68. memberAddress := fmt.Sprintf("%s.%s.%s.%d", subnetParts[0], subnetParts[1], subnetParts[2], tools.RandomInt(10, 100))
  69. t.Logf("Attempting to create member %s", memberName)
  70. createOpts := pools.CreateMemberOpts{
  71. Name: memberName,
  72. ProtocolPort: memberPort,
  73. Weight: memberWeight,
  74. Address: memberAddress,
  75. SubnetID: subnetID,
  76. }
  77. t.Logf("Member create opts: %#v", createOpts)
  78. member, err := pools.CreateMember(client, pool.ID, createOpts).Extract()
  79. if err != nil {
  80. return member, err
  81. }
  82. t.Logf("Successfully created member %s", memberName)
  83. if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  84. return member, fmt.Errorf("Timed out waiting for loadbalancer to become active")
  85. }
  86. return member, nil
  87. }
  88. // CreateMonitor will create a monitor with a random name for a specific pool.
  89. // An error will be returned if the monitor could not be created.
  90. func CreateMonitor(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer, pool *pools.Pool) (*monitors.Monitor, error) {
  91. monitorName := tools.RandomString("TESTACCT-", 8)
  92. t.Logf("Attempting to create monitor %s", monitorName)
  93. createOpts := monitors.CreateOpts{
  94. PoolID: pool.ID,
  95. Name: monitorName,
  96. Delay: 10,
  97. Timeout: 5,
  98. MaxRetries: 5,
  99. Type: "PING",
  100. }
  101. monitor, err := monitors.Create(client, createOpts).Extract()
  102. if err != nil {
  103. return monitor, err
  104. }
  105. t.Logf("Successfully created monitor: %s", monitorName)
  106. if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  107. return monitor, fmt.Errorf("Timed out waiting for loadbalancer to become active")
  108. }
  109. return monitor, nil
  110. }
  111. // CreatePool will create a pool with a random name with a specified listener
  112. // and loadbalancer. An error will be returned if the pool could not be
  113. // created.
  114. func CreatePool(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer) (*pools.Pool, error) {
  115. poolName := tools.RandomString("TESTACCT-", 8)
  116. t.Logf("Attempting to create pool %s", poolName)
  117. createOpts := pools.CreateOpts{
  118. Name: poolName,
  119. Protocol: pools.ProtocolTCP,
  120. LoadbalancerID: lb.ID,
  121. LBMethod: pools.LBMethodLeastConnections,
  122. }
  123. pool, err := pools.Create(client, createOpts).Extract()
  124. if err != nil {
  125. return pool, err
  126. }
  127. t.Logf("Successfully created pool %s", poolName)
  128. if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  129. return pool, fmt.Errorf("Timed out waiting for loadbalancer to become active")
  130. }
  131. return pool, nil
  132. }
  133. // DeleteListener will delete a specified listener. A fatal error will occur if
  134. // the listener could not be deleted. This works best when used as a deferred
  135. // function.
  136. func DeleteListener(t *testing.T, client *gophercloud.ServiceClient, lbID, listenerID string) {
  137. t.Logf("Attempting to delete listener %s", listenerID)
  138. if err := listeners.Delete(client, listenerID).ExtractErr(); err != nil {
  139. t.Fatalf("Unable to delete listener: %v", err)
  140. }
  141. if err := WaitForLoadBalancerState(client, lbID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  142. t.Fatalf("Timed out waiting for loadbalancer to become active")
  143. }
  144. t.Logf("Successfully deleted listener %s", listenerID)
  145. }
  146. // DeleteMember will delete a specified member. A fatal error will occur if the
  147. // member could not be deleted. This works best when used as a deferred
  148. // function.
  149. func DeleteMember(t *testing.T, client *gophercloud.ServiceClient, lbID, poolID, memberID string) {
  150. t.Logf("Attempting to delete member %s", memberID)
  151. if err := pools.DeleteMember(client, poolID, memberID).ExtractErr(); err != nil {
  152. t.Fatalf("Unable to delete member: %s", memberID)
  153. }
  154. if err := WaitForLoadBalancerState(client, lbID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  155. t.Fatalf("Timed out waiting for loadbalancer to become active")
  156. }
  157. t.Logf("Successfully deleted member %s", memberID)
  158. }
  159. // DeleteLoadBalancer will delete a specified loadbalancer. A fatal error will
  160. // occur if the loadbalancer could not be deleted. This works best when used
  161. // as a deferred function.
  162. func DeleteLoadBalancer(t *testing.T, client *gophercloud.ServiceClient, lbID string) {
  163. t.Logf("Attempting to delete loadbalancer %s", lbID)
  164. if err := loadbalancers.Delete(client, lbID).ExtractErr(); err != nil {
  165. t.Fatalf("Unable to delete loadbalancer: %v", err)
  166. }
  167. t.Logf("Waiting for loadbalancer %s to delete", lbID)
  168. if err := WaitForLoadBalancerState(client, lbID, "DELETED", loadbalancerActiveTimeoutSeconds); err != nil {
  169. t.Fatalf("Loadbalancer did not delete in time.")
  170. }
  171. t.Logf("Successfully deleted loadbalancer %s", lbID)
  172. }
  173. // DeleteMonitor will delete a specified monitor. A fatal error will occur if
  174. // the monitor could not be deleted. This works best when used as a deferred
  175. // function.
  176. func DeleteMonitor(t *testing.T, client *gophercloud.ServiceClient, lbID, monitorID string) {
  177. t.Logf("Attempting to delete monitor %s", monitorID)
  178. if err := monitors.Delete(client, monitorID).ExtractErr(); err != nil {
  179. t.Fatalf("Unable to delete monitor: %v", err)
  180. }
  181. if err := WaitForLoadBalancerState(client, lbID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  182. t.Fatalf("Timed out waiting for loadbalancer to become active")
  183. }
  184. t.Logf("Successfully deleted monitor %s", monitorID)
  185. }
  186. // DeletePool will delete a specified pool. A fatal error will occur if the
  187. // pool could not be deleted. This works best when used as a deferred function.
  188. func DeletePool(t *testing.T, client *gophercloud.ServiceClient, lbID, poolID string) {
  189. t.Logf("Attempting to delete pool %s", poolID)
  190. if err := pools.Delete(client, poolID).ExtractErr(); err != nil {
  191. t.Fatalf("Unable to delete pool: %v", err)
  192. }
  193. if err := WaitForLoadBalancerState(client, lbID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
  194. t.Fatalf("Timed out waiting for loadbalancer to become active")
  195. }
  196. t.Logf("Successfully deleted pool %s", poolID)
  197. }
  198. // WaitForLoadBalancerState will wait until a loadbalancer reaches a given state.
  199. func WaitForLoadBalancerState(client *gophercloud.ServiceClient, lbID, status string, secs int) error {
  200. return gophercloud.WaitFor(secs, func() (bool, error) {
  201. current, err := loadbalancers.Get(client, lbID).Extract()
  202. if err != nil {
  203. if httpStatus, ok := err.(gophercloud.ErrDefault404); ok {
  204. if httpStatus.Actual == 404 {
  205. if status == "DELETED" {
  206. return true, nil
  207. }
  208. }
  209. }
  210. return false, err
  211. }
  212. if current.ProvisioningStatus == status {
  213. return true, nil
  214. }
  215. return false, nil
  216. })
  217. }