/acceptance/openstack/networking/v2/extensions/extensions.go

https://github.com/gophercloud/gophercloud · Go · 164 lines · 113 code · 36 blank · 15 comment · 12 complexity · 021d0c31a79c9f16fd4c79635984b8a6 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. th "github.com/gophercloud/gophercloud/testhelper"
  12. )
  13. // CreateExternalNetwork will create an external network. An error will be
  14. // returned if the creation failed.
  15. func CreateExternalNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) {
  16. networkName := tools.RandomString("TESTACC-", 8)
  17. networkDescription := tools.RandomString("TESTACC-DESC-", 8)
  18. t.Logf("Attempting to create external network: %s", networkName)
  19. adminStateUp := true
  20. isExternal := true
  21. networkCreateOpts := networks.CreateOpts{
  22. Name: networkName,
  23. Description: networkDescription,
  24. AdminStateUp: &adminStateUp,
  25. }
  26. createOpts := external.CreateOptsExt{
  27. CreateOptsBuilder: networkCreateOpts,
  28. External: &isExternal,
  29. }
  30. network, err := networks.Create(client, createOpts).Extract()
  31. if err != nil {
  32. return network, err
  33. }
  34. t.Logf("Created external network: %s", networkName)
  35. th.AssertEquals(t, network.Name, networkName)
  36. th.AssertEquals(t, network.Description, networkDescription)
  37. return network, nil
  38. }
  39. // CreatePortWithSecurityGroup will create a port with a security group
  40. // attached. An error will be returned if the port could not be created.
  41. func CreatePortWithSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, secGroupID string) (*ports.Port, error) {
  42. portName := tools.RandomString("TESTACC-", 8)
  43. portDescription := tools.RandomString("TESTACC-DESC-", 8)
  44. iFalse := false
  45. t.Logf("Attempting to create port: %s", portName)
  46. createOpts := ports.CreateOpts{
  47. NetworkID: networkID,
  48. Name: portName,
  49. Description: portDescription,
  50. AdminStateUp: &iFalse,
  51. FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}},
  52. SecurityGroups: &[]string{secGroupID},
  53. }
  54. port, err := ports.Create(client, createOpts).Extract()
  55. if err != nil {
  56. return port, err
  57. }
  58. t.Logf("Successfully created port: %s", portName)
  59. th.AssertEquals(t, port.Name, portName)
  60. th.AssertEquals(t, port.Description, portDescription)
  61. th.AssertEquals(t, port.NetworkID, networkID)
  62. return port, nil
  63. }
  64. // CreateSecurityGroup will create a security group with a random name.
  65. // An error will be returned if one was failed to be created.
  66. func CreateSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (*groups.SecGroup, error) {
  67. secGroupName := tools.RandomString("TESTACC-", 8)
  68. secGroupDescription := tools.RandomString("TESTACC-DESC-", 8)
  69. t.Logf("Attempting to create security group: %s", secGroupName)
  70. createOpts := groups.CreateOpts{
  71. Name: secGroupName,
  72. Description: secGroupDescription,
  73. }
  74. secGroup, err := groups.Create(client, createOpts).Extract()
  75. if err != nil {
  76. return secGroup, err
  77. }
  78. t.Logf("Created security group: %s", secGroup.ID)
  79. th.AssertEquals(t, secGroup.Name, secGroupName)
  80. th.AssertEquals(t, secGroup.Description, secGroupDescription)
  81. return secGroup, nil
  82. }
  83. // CreateSecurityGroupRule will create a security group rule with a random name
  84. // and random port between 80 and 99.
  85. // An error will be returned if one was failed to be created.
  86. func CreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) (*rules.SecGroupRule, error) {
  87. t.Logf("Attempting to create security group rule in group: %s", secGroupID)
  88. description := "Rule description"
  89. fromPort := tools.RandomInt(80, 89)
  90. toPort := tools.RandomInt(90, 99)
  91. createOpts := rules.CreateOpts{
  92. Description: description,
  93. Direction: "ingress",
  94. EtherType: "IPv4",
  95. SecGroupID: secGroupID,
  96. PortRangeMin: fromPort,
  97. PortRangeMax: toPort,
  98. Protocol: rules.ProtocolTCP,
  99. }
  100. rule, err := rules.Create(client, createOpts).Extract()
  101. if err != nil {
  102. return rule, err
  103. }
  104. t.Logf("Created security group rule: %s", rule.ID)
  105. th.AssertEquals(t, rule.SecGroupID, secGroupID)
  106. th.AssertEquals(t, rule.Description, description)
  107. return rule, nil
  108. }
  109. // DeleteSecurityGroup will delete a security group of a specified ID.
  110. // A fatal error will occur if the deletion failed. This works best as a
  111. // deferred function
  112. func DeleteSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) {
  113. t.Logf("Attempting to delete security group: %s", secGroupID)
  114. err := groups.Delete(client, secGroupID).ExtractErr()
  115. if err != nil {
  116. t.Fatalf("Unable to delete security group: %v", err)
  117. }
  118. }
  119. // DeleteSecurityGroupRule will delete a security group rule of a specified ID.
  120. // A fatal error will occur if the deletion failed. This works best as a
  121. // deferred function
  122. func DeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, ruleID string) {
  123. t.Logf("Attempting to delete security group rule: %s", ruleID)
  124. err := rules.Delete(client, ruleID).ExtractErr()
  125. if err != nil {
  126. t.Fatalf("Unable to delete security group rule: %v", err)
  127. }
  128. }