blob: 9d1dac147ae9487d02ab50cf71c97713b602c324 [file] [log] [blame]
Joe Topjian7c8dd022016-09-01 12:02:04 -06001package v2
2
3import (
4 "fmt"
5 "testing"
6
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02008 "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 Topjian7c8dd022016-09-01 12:02:04 -060012)
13
14// CreateNetwork will create basic network. An error will be returned if the
15// network could not be created.
16func 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.
36func 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 Topjianc5d17b82016-09-26 12:39:57 -040044 AdminStateUp: gophercloud.Enabled,
Joe Topjian7c8dd022016-09-01 12:02:04 -060045 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 Topjianc5d17b82016-09-26 12:39:57 -040053 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 Topjian7c8dd022016-09-01 12:02:04 -060062 t.Logf("Successfully created port: %s", portName)
63
Joe Topjianc5d17b82016-09-26 12:39:57 -040064 return newPort, nil
Joe Topjian7c8dd022016-09-01 12:02:04 -060065}
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.
69func 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 Topjian7c8dd022016-09-01 12:02:04 -060074 createOpts := subnets.CreateOpts{
75 NetworkID: networkID,
76 CIDR: subnetCIDR,
77 IPVersion: 4,
78 Name: subnetName,
Joe Topjian19e713b2016-10-06 10:10:24 -060079 EnableDHCP: gophercloud.Disabled,
Joe Topjian7c8dd022016-09-01 12:02:04 -060080 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 Topjian19e713b2016-10-06 10:10:24 -060094// 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.
97func 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.
123func 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 Topjian7c8dd022016-09-01 12:02:04 -0600156// 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.
159func 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.
173func 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.
187func 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 Topjianc5d17b82016-09-26 12:39:57 -0400198func 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}