blob: cc5befbcc4a5fab76234f1ec813446e105289dbc [file] [log] [blame]
Joe Topjian7c8dd022016-09-01 12:02:04 -06001package v2
2
3import (
4 "fmt"
5 "testing"
6
7 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/acceptance/tools"
9 "github.com/gophercloud/gophercloud/openstack/networking/v2/apiversions"
10 "github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
11 "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
12 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
13)
14
15// CreateNetwork will create basic network. An error will be returned if the
16// network could not be created.
17func CreateNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) {
18 networkName := tools.RandomString("TESTACC-", 8)
19 createOpts := networks.CreateOpts{
20 Name: networkName,
21 AdminStateUp: gophercloud.Enabled,
22 }
23
24 t.Logf("Attempting to create network: %s", networkName)
25
26 network, err := networks.Create(client, createOpts).Extract()
27 if err != nil {
28 return network, err
29 }
30
31 t.Logf("Successfully created network.")
32 return network, nil
33}
34
35// CreatePort will create a port on the specified subnet. An error will be
36// returned if the port could not be created.
37func CreatePort(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID string) (*ports.Port, error) {
38 portName := tools.RandomString("TESTACC-", 8)
39
40 t.Logf("Attempting to create port: %s", portName)
41
42 createOpts := ports.CreateOpts{
43 NetworkID: networkID,
44 Name: portName,
Joe Topjianc5d17b82016-09-26 12:39:57 -040045 AdminStateUp: gophercloud.Enabled,
Joe Topjian7c8dd022016-09-01 12:02:04 -060046 FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}},
47 }
48
49 port, err := ports.Create(client, createOpts).Extract()
50 if err != nil {
51 return port, err
52 }
53
Joe Topjianc5d17b82016-09-26 12:39:57 -040054 if err := WaitForPortToCreate(client, port.ID, 60); err != nil {
55 return port, err
56 }
57
58 newPort, err := ports.Get(client, port.ID).Extract()
59 if err != nil {
60 return newPort, err
61 }
62
Joe Topjian7c8dd022016-09-01 12:02:04 -060063 t.Logf("Successfully created port: %s", portName)
64
Joe Topjianc5d17b82016-09-26 12:39:57 -040065 return newPort, nil
Joe Topjian7c8dd022016-09-01 12:02:04 -060066}
67
68// CreateSubnet will create a subnet on the specified Network ID. An error
69// will be returned if the subnet could not be created.
70func CreateSubnet(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) {
71 subnetName := tools.RandomString("TESTACC-", 8)
72 subnetOctet := tools.RandomInt(1, 250)
73 subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet)
74 subnetGateway := fmt.Sprintf("192.168.%d.1", subnetOctet)
Joe Topjian7c8dd022016-09-01 12:02:04 -060075 createOpts := subnets.CreateOpts{
76 NetworkID: networkID,
77 CIDR: subnetCIDR,
78 IPVersion: 4,
79 Name: subnetName,
Joe Topjian19e713b2016-10-06 10:10:24 -060080 EnableDHCP: gophercloud.Disabled,
Joe Topjian7c8dd022016-09-01 12:02:04 -060081 GatewayIP: &subnetGateway,
82 }
83
84 t.Logf("Attempting to create subnet: %s", subnetName)
85
86 subnet, err := subnets.Create(client, createOpts).Extract()
87 if err != nil {
88 return subnet, err
89 }
90
91 t.Logf("Successfully created subnet.")
92 return subnet, nil
93}
94
Joe Topjian19e713b2016-10-06 10:10:24 -060095// CreateSubnetWithDefaultGateway will create a subnet on the specified Network
96// ID and have Neutron set the gateway by default An error will be returned if
97// the subnet could not be created.
98func CreateSubnetWithDefaultGateway(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) {
99 subnetName := tools.RandomString("TESTACC-", 8)
100 subnetOctet := tools.RandomInt(1, 250)
101 subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet)
102 createOpts := subnets.CreateOpts{
103 NetworkID: networkID,
104 CIDR: subnetCIDR,
105 IPVersion: 4,
106 Name: subnetName,
107 EnableDHCP: gophercloud.Disabled,
108 }
109
110 t.Logf("Attempting to create subnet: %s", subnetName)
111
112 subnet, err := subnets.Create(client, createOpts).Extract()
113 if err != nil {
114 return subnet, err
115 }
116
117 t.Logf("Successfully created subnet.")
118 return subnet, nil
119}
120
121// CreateSubnetWithNoGateway will create a subnet with no gateway on the
122// specified Network ID. An error will be returned if the subnet could not be
123// created.
124func CreateSubnetWithNoGateway(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) {
125 var noGateway = ""
126 subnetName := tools.RandomString("TESTACC-", 8)
127 subnetOctet := tools.RandomInt(1, 250)
128 subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet)
129 dhcpStart := fmt.Sprintf("192.168.%d.10", subnetOctet)
130 dhcpEnd := fmt.Sprintf("192.168.%d.200", subnetOctet)
131 createOpts := subnets.CreateOpts{
132 NetworkID: networkID,
133 CIDR: subnetCIDR,
134 IPVersion: 4,
135 Name: subnetName,
136 EnableDHCP: gophercloud.Disabled,
137 GatewayIP: &noGateway,
138 AllocationPools: []subnets.AllocationPool{
139 {
140 Start: dhcpStart,
141 End: dhcpEnd,
142 },
143 },
144 }
145
146 t.Logf("Attempting to create subnet: %s", subnetName)
147
148 subnet, err := subnets.Create(client, createOpts).Extract()
149 if err != nil {
150 return subnet, err
151 }
152
153 t.Logf("Successfully created subnet.")
154 return subnet, nil
155}
156
Joe Topjian7c8dd022016-09-01 12:02:04 -0600157// DeleteNetwork will delete a network with a specified ID. A fatal error will
158// occur if the delete was not successful. This works best when used as a
159// deferred function.
160func DeleteNetwork(t *testing.T, client *gophercloud.ServiceClient, networkID string) {
161 t.Logf("Attempting to delete network: %s", networkID)
162
163 err := networks.Delete(client, networkID).ExtractErr()
164 if err != nil {
165 t.Fatalf("Unable to delete network %s: %v", networkID, err)
166 }
167
168 t.Logf("Deleted network: %s", networkID)
169}
170
171// DeletePort will delete a port with a specified ID. A fatal error will
172// occur if the delete was not successful. This works best when used as a
173// deferred function.
174func DeletePort(t *testing.T, client *gophercloud.ServiceClient, portID string) {
175 t.Logf("Attempting to delete port: %s", portID)
176
177 err := ports.Delete(client, portID).ExtractErr()
178 if err != nil {
179 t.Fatalf("Unable to delete port %s: %v", portID, err)
180 }
181
182 t.Logf("Deleted port: %s", portID)
183}
184
185// DeleteSubnet will delete a subnet with a specified ID. A fatal error will
186// occur if the delete was not successful. This works best when used as a
187// deferred function.
188func DeleteSubnet(t *testing.T, client *gophercloud.ServiceClient, subnetID string) {
189 t.Logf("Attempting to delete subnet: %s", subnetID)
190
191 err := subnets.Delete(client, subnetID).ExtractErr()
192 if err != nil {
193 t.Fatalf("Unable to delete subnet %s: %v", subnetID, err)
194 }
195
196 t.Logf("Deleted subnet: %s", subnetID)
197}
198
199// PrintAPIVersion will print an API version and all of its attributes.
200func PrintAPIVersion(t *testing.T, apiVersion *apiversions.APIVersion) {
201 t.Logf("ID: %s", apiVersion.ID)
202 t.Logf("Status: %s", apiVersion.Status)
203}
204
205// PrintNetwork will print a network and all of its attributes.
206func PrintNetwork(t *testing.T, network *networks.Network) {
207 t.Logf("ID: %s", network.ID)
208 t.Logf("Name: %s", network.Name)
209 t.Logf("AdminStateUp: %t", network.AdminStateUp)
210 t.Logf("Status: %s", network.Status)
211 t.Logf("TenantID: %s", network.TenantID)
212 t.Logf("Shared: %t", network.Shared)
213 t.Logf("Subnets: %s", network.Subnets)
214}
215
216// PrintPort will print a port and all of its attributes.
217func PrintPort(t *testing.T, port *ports.Port) {
218 t.Logf("ID: %s", port.ID)
219 t.Logf("NetworkID: %s", port.NetworkID)
220 t.Logf("Name: %s", port.Name)
221 t.Logf("AdminStateUp: %t", port.AdminStateUp)
222 t.Logf("Status: %s", port.Status)
223 t.Logf("MACAddress: %s", port.MACAddress)
224 t.Logf("FixedIPs: %s", port.FixedIPs)
225 t.Logf("TenantID: %s", port.TenantID)
226 t.Logf("DeviceOwner: %s", port.DeviceOwner)
227 t.Logf("SecurityGroups: %s", port.SecurityGroups)
228 t.Logf("DeviceID: %s", port.DeviceID)
229 t.Logf("DeviceOwner: %s", port.DeviceOwner)
230 t.Logf("AllowedAddressPairs: %s", port.AllowedAddressPairs)
231}
232
233// PrintSubnet will print a subnet and all of its attributes.
234func PrintSubnet(t *testing.T, subnet *subnets.Subnet) {
235 t.Logf("ID: %s", subnet.ID)
236 t.Logf("NetworkID: %s", subnet.NetworkID)
237 t.Logf("Name: %s", subnet.Name)
238 t.Logf("IPVersion: %d", subnet.IPVersion)
239 t.Logf("CIDR: %s", subnet.CIDR)
240 t.Logf("GatewayIP: %s", subnet.GatewayIP)
241 t.Logf("DNSNameservers: %s", subnet.DNSNameservers)
242 t.Logf("AllocationPools: %s", subnet.AllocationPools)
243 t.Logf("HostRoutes: %s", subnet.HostRoutes)
244 t.Logf("EnableDHCP: %t", subnet.EnableDHCP)
245 t.Logf("TenantID: %s", subnet.TenantID)
246}
247
248// PrintVersionResource will print an API version resource and all of its attributes.
249func PrintVersionResource(t *testing.T, versionResource *apiversions.APIVersionResource) {
250 t.Logf("Name: %s", versionResource.Name)
251 t.Logf("Collection: %s", versionResource.Collection)
252}
Joe Topjianc5d17b82016-09-26 12:39:57 -0400253
254func WaitForPortToCreate(client *gophercloud.ServiceClient, portID string, secs int) error {
255 return gophercloud.WaitFor(secs, func() (bool, error) {
256 p, err := ports.Get(client, portID).Extract()
257 if err != nil {
258 return false, err
259 }
260
261 if p.Status == "ACTIVE" || p.Status == "DOWN" {
262 return true, nil
263 }
264
265 return false, nil
266 })
267}