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