PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 246 lines | 177 code | 46 blank | 23 comment | 32 complexity | 4905f7bcab04094f65b570d48c127dcc MD5 | raw file
  1. package v2
  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/networks"
  8. "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
  9. "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
  10. )
  11. // CreateNetwork will create basic network. An error will be returned if the
  12. // network could not be created.
  13. func CreateNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) {
  14. networkName := tools.RandomString("TESTACC-", 8)
  15. createOpts := networks.CreateOpts{
  16. Name: networkName,
  17. AdminStateUp: gophercloud.Enabled,
  18. }
  19. t.Logf("Attempting to create network: %s", networkName)
  20. network, err := networks.Create(client, createOpts).Extract()
  21. if err != nil {
  22. return network, err
  23. }
  24. t.Logf("Successfully created network.")
  25. return network, nil
  26. }
  27. // CreatePort will create a port on the specified subnet. An error will be
  28. // returned if the port could not be created.
  29. func CreatePort(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID string) (*ports.Port, error) {
  30. portName := tools.RandomString("TESTACC-", 8)
  31. t.Logf("Attempting to create port: %s", portName)
  32. createOpts := ports.CreateOpts{
  33. NetworkID: networkID,
  34. Name: portName,
  35. AdminStateUp: gophercloud.Enabled,
  36. FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}},
  37. }
  38. port, err := ports.Create(client, createOpts).Extract()
  39. if err != nil {
  40. return port, err
  41. }
  42. if err := WaitForPortToCreate(client, port.ID, 60); err != nil {
  43. return port, err
  44. }
  45. newPort, err := ports.Get(client, port.ID).Extract()
  46. if err != nil {
  47. return newPort, err
  48. }
  49. t.Logf("Successfully created port: %s", portName)
  50. return newPort, nil
  51. }
  52. // CreatePortWithNoSecurityGroup will create a port with no security group
  53. // attached. An error will be returned if the port could not be created.
  54. func CreatePortWithNoSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID string) (*ports.Port, error) {
  55. portName := tools.RandomString("TESTACC-", 8)
  56. iFalse := false
  57. t.Logf("Attempting to create port: %s", portName)
  58. createOpts := ports.CreateOpts{
  59. NetworkID: networkID,
  60. Name: portName,
  61. AdminStateUp: &iFalse,
  62. FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}},
  63. SecurityGroups: &[]string{},
  64. }
  65. port, err := ports.Create(client, createOpts).Extract()
  66. if err != nil {
  67. return port, err
  68. }
  69. if err := WaitForPortToCreate(client, port.ID, 60); err != nil {
  70. return port, err
  71. }
  72. newPort, err := ports.Get(client, port.ID).Extract()
  73. if err != nil {
  74. return newPort, err
  75. }
  76. t.Logf("Successfully created port: %s", portName)
  77. return newPort, nil
  78. }
  79. // CreateSubnet will create a subnet on the specified Network ID. An error
  80. // will be returned if the subnet could not be created.
  81. func CreateSubnet(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) {
  82. subnetName := tools.RandomString("TESTACC-", 8)
  83. subnetOctet := tools.RandomInt(1, 250)
  84. subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet)
  85. subnetGateway := fmt.Sprintf("192.168.%d.1", subnetOctet)
  86. createOpts := subnets.CreateOpts{
  87. NetworkID: networkID,
  88. CIDR: subnetCIDR,
  89. IPVersion: 4,
  90. Name: subnetName,
  91. EnableDHCP: gophercloud.Disabled,
  92. GatewayIP: &subnetGateway,
  93. }
  94. t.Logf("Attempting to create subnet: %s", subnetName)
  95. subnet, err := subnets.Create(client, createOpts).Extract()
  96. if err != nil {
  97. return subnet, err
  98. }
  99. t.Logf("Successfully created subnet.")
  100. return subnet, nil
  101. }
  102. // CreateSubnetWithDefaultGateway will create a subnet on the specified Network
  103. // ID and have Neutron set the gateway by default An error will be returned if
  104. // the subnet could not be created.
  105. func CreateSubnetWithDefaultGateway(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) {
  106. subnetName := tools.RandomString("TESTACC-", 8)
  107. subnetOctet := tools.RandomInt(1, 250)
  108. subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet)
  109. createOpts := subnets.CreateOpts{
  110. NetworkID: networkID,
  111. CIDR: subnetCIDR,
  112. IPVersion: 4,
  113. Name: subnetName,
  114. EnableDHCP: gophercloud.Disabled,
  115. }
  116. t.Logf("Attempting to create subnet: %s", subnetName)
  117. subnet, err := subnets.Create(client, createOpts).Extract()
  118. if err != nil {
  119. return subnet, err
  120. }
  121. t.Logf("Successfully created subnet.")
  122. return subnet, nil
  123. }
  124. // CreateSubnetWithNoGateway will create a subnet with no gateway on the
  125. // specified Network ID. An error will be returned if the subnet could not be
  126. // created.
  127. func CreateSubnetWithNoGateway(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) {
  128. var noGateway = ""
  129. subnetName := tools.RandomString("TESTACC-", 8)
  130. subnetOctet := tools.RandomInt(1, 250)
  131. subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet)
  132. dhcpStart := fmt.Sprintf("192.168.%d.10", subnetOctet)
  133. dhcpEnd := fmt.Sprintf("192.168.%d.200", subnetOctet)
  134. createOpts := subnets.CreateOpts{
  135. NetworkID: networkID,
  136. CIDR: subnetCIDR,
  137. IPVersion: 4,
  138. Name: subnetName,
  139. EnableDHCP: gophercloud.Disabled,
  140. GatewayIP: &noGateway,
  141. AllocationPools: []subnets.AllocationPool{
  142. {
  143. Start: dhcpStart,
  144. End: dhcpEnd,
  145. },
  146. },
  147. }
  148. t.Logf("Attempting to create subnet: %s", subnetName)
  149. subnet, err := subnets.Create(client, createOpts).Extract()
  150. if err != nil {
  151. return subnet, err
  152. }
  153. t.Logf("Successfully created subnet.")
  154. return subnet, nil
  155. }
  156. // DeleteNetwork will delete a network with a specified ID. A fatal error will
  157. // occur if the delete was not successful. This works best when used as a
  158. // deferred function.
  159. func DeleteNetwork(t *testing.T, client *gophercloud.ServiceClient, networkID string) {
  160. t.Logf("Attempting to delete network: %s", networkID)
  161. err := networks.Delete(client, networkID).ExtractErr()
  162. if err != nil {
  163. t.Fatalf("Unable to delete network %s: %v", networkID, err)
  164. }
  165. t.Logf("Deleted network: %s", networkID)
  166. }
  167. // DeletePort will delete a port with a specified ID. A fatal error will
  168. // occur if the delete was not successful. This works best when used as a
  169. // deferred function.
  170. func DeletePort(t *testing.T, client *gophercloud.ServiceClient, portID string) {
  171. t.Logf("Attempting to delete port: %s", portID)
  172. err := ports.Delete(client, portID).ExtractErr()
  173. if err != nil {
  174. t.Fatalf("Unable to delete port %s: %v", portID, err)
  175. }
  176. t.Logf("Deleted port: %s", portID)
  177. }
  178. // DeleteSubnet will delete a subnet with a specified ID. A fatal error will
  179. // occur if the delete was not successful. This works best when used as a
  180. // deferred function.
  181. func DeleteSubnet(t *testing.T, client *gophercloud.ServiceClient, subnetID string) {
  182. t.Logf("Attempting to delete subnet: %s", subnetID)
  183. err := subnets.Delete(client, subnetID).ExtractErr()
  184. if err != nil {
  185. t.Fatalf("Unable to delete subnet %s: %v", subnetID, err)
  186. }
  187. t.Logf("Deleted subnet: %s", subnetID)
  188. }
  189. func WaitForPortToCreate(client *gophercloud.ServiceClient, portID string, secs int) error {
  190. return gophercloud.WaitFor(secs, func() (bool, error) {
  191. p, err := ports.Get(client, portID).Extract()
  192. if err != nil {
  193. return false, err
  194. }
  195. if p.Status == "ACTIVE" || p.Status == "DOWN" {
  196. return true, nil
  197. }
  198. return false, nil
  199. })
  200. }