PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 160 lines | 105 code | 36 blank | 19 comment | 16 complexity | 8b454579a9d79be7aa5728fc72f4e43e MD5 | raw file
  1. package lbaas
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/gophercloud/gophercloud"
  6. "github.com/gophercloud/gophercloud/acceptance/tools"
  7. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/members"
  8. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/monitors"
  9. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/pools"
  10. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
  11. )
  12. // CreateMember will create a load balancer member in a specified pool on a
  13. // random port. An error will be returned if the member could not be created.
  14. func CreateMember(t *testing.T, client *gophercloud.ServiceClient, poolID string) (*members.Member, error) {
  15. protocolPort := tools.RandomInt(100, 1000)
  16. address := tools.RandomInt(2, 200)
  17. t.Logf("Attempting to create member in port %d", protocolPort)
  18. createOpts := members.CreateOpts{
  19. PoolID: poolID,
  20. ProtocolPort: protocolPort,
  21. Address: fmt.Sprintf("192.168.1.%d", address),
  22. }
  23. member, err := members.Create(client, createOpts).Extract()
  24. if err != nil {
  25. return member, err
  26. }
  27. t.Logf("Successfully created member %s", member.ID)
  28. return member, nil
  29. }
  30. // CreateMonitor will create a monitor with a random name for a specific pool.
  31. // An error will be returned if the monitor could not be created.
  32. func CreateMonitor(t *testing.T, client *gophercloud.ServiceClient) (*monitors.Monitor, error) {
  33. t.Logf("Attempting to create monitor.")
  34. createOpts := monitors.CreateOpts{
  35. Type: monitors.TypePING,
  36. Delay: 90,
  37. Timeout: 60,
  38. MaxRetries: 10,
  39. AdminStateUp: gophercloud.Enabled,
  40. }
  41. monitor, err := monitors.Create(client, createOpts).Extract()
  42. if err != nil {
  43. return monitor, err
  44. }
  45. t.Logf("Successfully created monitor %s", monitor.ID)
  46. return monitor, nil
  47. }
  48. // CreatePool will create a pool with a random name. An error will be returned
  49. // if the pool could not be deleted.
  50. func CreatePool(t *testing.T, client *gophercloud.ServiceClient, subnetID string) (*pools.Pool, error) {
  51. poolName := tools.RandomString("TESTACCT-", 8)
  52. t.Logf("Attempting to create pool %s", poolName)
  53. createOpts := pools.CreateOpts{
  54. Name: poolName,
  55. SubnetID: subnetID,
  56. Protocol: pools.ProtocolTCP,
  57. LBMethod: pools.LBMethodRoundRobin,
  58. }
  59. pool, err := pools.Create(client, createOpts).Extract()
  60. if err != nil {
  61. return pool, err
  62. }
  63. t.Logf("Successfully created pool %s", poolName)
  64. return pool, nil
  65. }
  66. // CreateVIP will create a vip with a random name and a random port in a
  67. // specified subnet and pool. An error will be returned if the vip could
  68. // not be created.
  69. func CreateVIP(t *testing.T, client *gophercloud.ServiceClient, subnetID, poolID string) (*vips.VirtualIP, error) {
  70. vipName := tools.RandomString("TESTACCT-", 8)
  71. vipPort := tools.RandomInt(100, 10000)
  72. t.Logf("Attempting to create VIP %s", vipName)
  73. createOpts := vips.CreateOpts{
  74. Name: vipName,
  75. SubnetID: subnetID,
  76. PoolID: poolID,
  77. Protocol: "TCP",
  78. ProtocolPort: vipPort,
  79. }
  80. vip, err := vips.Create(client, createOpts).Extract()
  81. if err != nil {
  82. return vip, err
  83. }
  84. t.Logf("Successfully created vip %s", vipName)
  85. return vip, nil
  86. }
  87. // DeleteMember will delete a specified member. A fatal error will occur if
  88. // the member could not be deleted. This works best when used as a deferred
  89. // function.
  90. func DeleteMember(t *testing.T, client *gophercloud.ServiceClient, memberID string) {
  91. t.Logf("Attempting to delete member %s", memberID)
  92. if err := members.Delete(client, memberID).ExtractErr(); err != nil {
  93. t.Fatalf("Unable to delete member: %v", err)
  94. }
  95. t.Logf("Successfully deleted member %s", memberID)
  96. }
  97. // DeleteMonitor will delete a specified monitor. A fatal error will occur if
  98. // the monitor could not be deleted. This works best when used as a deferred
  99. // function.
  100. func DeleteMonitor(t *testing.T, client *gophercloud.ServiceClient, monitorID string) {
  101. t.Logf("Attempting to delete monitor %s", monitorID)
  102. if err := monitors.Delete(client, monitorID).ExtractErr(); err != nil {
  103. t.Fatalf("Unable to delete monitor: %v", err)
  104. }
  105. t.Logf("Successfully deleted monitor %s", monitorID)
  106. }
  107. // DeletePool will delete a specified pool. A fatal error will occur if the
  108. // pool could not be deleted. This works best when used as a deferred function.
  109. func DeletePool(t *testing.T, client *gophercloud.ServiceClient, poolID string) {
  110. t.Logf("Attempting to delete pool %s", poolID)
  111. if err := pools.Delete(client, poolID).ExtractErr(); err != nil {
  112. t.Fatalf("Unable to delete pool: %v", err)
  113. }
  114. t.Logf("Successfully deleted pool %s", poolID)
  115. }
  116. // DeleteVIP will delete a specified vip. A fatal error will occur if the vip
  117. // could not be deleted. This works best when used as a deferred function.
  118. func DeleteVIP(t *testing.T, client *gophercloud.ServiceClient, vipID string) {
  119. t.Logf("Attempting to delete vip %s", vipID)
  120. if err := vips.Delete(client, vipID).ExtractErr(); err != nil {
  121. t.Fatalf("Unable to delete vip: %v", err)
  122. }
  123. t.Logf("Successfully deleted vip %s", vipID)
  124. }