PageRenderTime 94ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 142 lines | 95 code | 32 blank | 15 comment | 12 complexity | 1769eb5b0edcb47c5e50d46d8f86d34d MD5 | raw file
  1. package extensions
  2. import (
  3. "testing"
  4. "github.com/gophercloud/gophercloud"
  5. "github.com/gophercloud/gophercloud/acceptance/tools"
  6. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external"
  7. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
  8. "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
  9. "github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
  10. "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
  11. )
  12. // CreateExternalNetwork will create an external network. An error will be
  13. // returned if the creation failed.
  14. func CreateExternalNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) {
  15. networkName := tools.RandomString("TESTACC-", 8)
  16. t.Logf("Attempting to create external network: %s", networkName)
  17. adminStateUp := true
  18. isExternal := true
  19. networkCreateOpts := networks.CreateOpts{
  20. Name: networkName,
  21. AdminStateUp: &adminStateUp,
  22. }
  23. createOpts := external.CreateOptsExt{
  24. CreateOptsBuilder: networkCreateOpts,
  25. External: &isExternal,
  26. }
  27. network, err := networks.Create(client, createOpts).Extract()
  28. if err != nil {
  29. return network, err
  30. }
  31. t.Logf("Created external network: %s", networkName)
  32. return network, nil
  33. }
  34. // CreatePortWithSecurityGroup will create a port with a security group
  35. // attached. An error will be returned if the port could not be created.
  36. func CreatePortWithSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, secGroupID string) (*ports.Port, error) {
  37. portName := tools.RandomString("TESTACC-", 8)
  38. iFalse := false
  39. t.Logf("Attempting to create port: %s", portName)
  40. createOpts := ports.CreateOpts{
  41. NetworkID: networkID,
  42. Name: portName,
  43. AdminStateUp: &iFalse,
  44. FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}},
  45. SecurityGroups: &[]string{secGroupID},
  46. }
  47. port, err := ports.Create(client, createOpts).Extract()
  48. if err != nil {
  49. return port, err
  50. }
  51. t.Logf("Successfully created port: %s", portName)
  52. return port, nil
  53. }
  54. // CreateSecurityGroup will create a security group with a random name.
  55. // An error will be returned if one was failed to be created.
  56. func CreateSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (*groups.SecGroup, error) {
  57. secGroupName := tools.RandomString("TESTACC-", 8)
  58. t.Logf("Attempting to create security group: %s", secGroupName)
  59. createOpts := groups.CreateOpts{
  60. Name: secGroupName,
  61. }
  62. secGroup, err := groups.Create(client, createOpts).Extract()
  63. if err != nil {
  64. return secGroup, err
  65. }
  66. t.Logf("Created security group: %s", secGroup.ID)
  67. return secGroup, nil
  68. }
  69. // CreateSecurityGroupRule will create a security group rule with a random name
  70. // and random port between 80 and 99.
  71. // An error will be returned if one was failed to be created.
  72. func CreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) (*rules.SecGroupRule, error) {
  73. t.Logf("Attempting to create security group rule in group: %s", secGroupID)
  74. fromPort := tools.RandomInt(80, 89)
  75. toPort := tools.RandomInt(90, 99)
  76. createOpts := rules.CreateOpts{
  77. Direction: "ingress",
  78. EtherType: "IPv4",
  79. SecGroupID: secGroupID,
  80. PortRangeMin: fromPort,
  81. PortRangeMax: toPort,
  82. Protocol: rules.ProtocolTCP,
  83. }
  84. rule, err := rules.Create(client, createOpts).Extract()
  85. if err != nil {
  86. return rule, err
  87. }
  88. t.Logf("Created security group rule: %s", rule.ID)
  89. return rule, nil
  90. }
  91. // DeleteSecurityGroup will delete a security group of a specified ID.
  92. // A fatal error will occur if the deletion failed. This works best as a
  93. // deferred function
  94. func DeleteSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) {
  95. t.Logf("Attempting to delete security group: %s", secGroupID)
  96. err := groups.Delete(client, secGroupID).ExtractErr()
  97. if err != nil {
  98. t.Fatalf("Unable to delete security group: %v", err)
  99. }
  100. }
  101. // DeleteSecurityGroupRule will delete a security group rule of a specified ID.
  102. // A fatal error will occur if the deletion failed. This works best as a
  103. // deferred function
  104. func DeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, ruleID string) {
  105. t.Logf("Attempting to delete security group rule: %s", ruleID)
  106. err := rules.Delete(client, ruleID).ExtractErr()
  107. if err != nil {
  108. t.Fatalf("Unable to delete security group rule: %v", err)
  109. }
  110. }