blob: 9432ddac05f16782eb9883b5e9d1d195ef631fc9 [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)
75 iFalse := false
76 createOpts := subnets.CreateOpts{
77 NetworkID: networkID,
78 CIDR: subnetCIDR,
79 IPVersion: 4,
80 Name: subnetName,
81 EnableDHCP: &iFalse,
82 GatewayIP: &subnetGateway,
83 }
84
85 t.Logf("Attempting to create subnet: %s", subnetName)
86
87 subnet, err := subnets.Create(client, createOpts).Extract()
88 if err != nil {
89 return subnet, err
90 }
91
92 t.Logf("Successfully created subnet.")
93 return subnet, nil
94}
95
96// DeleteNetwork will delete a network with a specified ID. A fatal error will
97// occur if the delete was not successful. This works best when used as a
98// deferred function.
99func DeleteNetwork(t *testing.T, client *gophercloud.ServiceClient, networkID string) {
100 t.Logf("Attempting to delete network: %s", networkID)
101
102 err := networks.Delete(client, networkID).ExtractErr()
103 if err != nil {
104 t.Fatalf("Unable to delete network %s: %v", networkID, err)
105 }
106
107 t.Logf("Deleted network: %s", networkID)
108}
109
110// DeletePort will delete a port with a specified ID. A fatal error will
111// occur if the delete was not successful. This works best when used as a
112// deferred function.
113func DeletePort(t *testing.T, client *gophercloud.ServiceClient, portID string) {
114 t.Logf("Attempting to delete port: %s", portID)
115
116 err := ports.Delete(client, portID).ExtractErr()
117 if err != nil {
118 t.Fatalf("Unable to delete port %s: %v", portID, err)
119 }
120
121 t.Logf("Deleted port: %s", portID)
122}
123
124// DeleteSubnet will delete a subnet with a specified ID. A fatal error will
125// occur if the delete was not successful. This works best when used as a
126// deferred function.
127func DeleteSubnet(t *testing.T, client *gophercloud.ServiceClient, subnetID string) {
128 t.Logf("Attempting to delete subnet: %s", subnetID)
129
130 err := subnets.Delete(client, subnetID).ExtractErr()
131 if err != nil {
132 t.Fatalf("Unable to delete subnet %s: %v", subnetID, err)
133 }
134
135 t.Logf("Deleted subnet: %s", subnetID)
136}
137
138// PrintAPIVersion will print an API version and all of its attributes.
139func PrintAPIVersion(t *testing.T, apiVersion *apiversions.APIVersion) {
140 t.Logf("ID: %s", apiVersion.ID)
141 t.Logf("Status: %s", apiVersion.Status)
142}
143
144// PrintNetwork will print a network and all of its attributes.
145func PrintNetwork(t *testing.T, network *networks.Network) {
146 t.Logf("ID: %s", network.ID)
147 t.Logf("Name: %s", network.Name)
148 t.Logf("AdminStateUp: %t", network.AdminStateUp)
149 t.Logf("Status: %s", network.Status)
150 t.Logf("TenantID: %s", network.TenantID)
151 t.Logf("Shared: %t", network.Shared)
152 t.Logf("Subnets: %s", network.Subnets)
153}
154
155// PrintPort will print a port and all of its attributes.
156func PrintPort(t *testing.T, port *ports.Port) {
157 t.Logf("ID: %s", port.ID)
158 t.Logf("NetworkID: %s", port.NetworkID)
159 t.Logf("Name: %s", port.Name)
160 t.Logf("AdminStateUp: %t", port.AdminStateUp)
161 t.Logf("Status: %s", port.Status)
162 t.Logf("MACAddress: %s", port.MACAddress)
163 t.Logf("FixedIPs: %s", port.FixedIPs)
164 t.Logf("TenantID: %s", port.TenantID)
165 t.Logf("DeviceOwner: %s", port.DeviceOwner)
166 t.Logf("SecurityGroups: %s", port.SecurityGroups)
167 t.Logf("DeviceID: %s", port.DeviceID)
168 t.Logf("DeviceOwner: %s", port.DeviceOwner)
169 t.Logf("AllowedAddressPairs: %s", port.AllowedAddressPairs)
170}
171
172// PrintSubnet will print a subnet and all of its attributes.
173func PrintSubnet(t *testing.T, subnet *subnets.Subnet) {
174 t.Logf("ID: %s", subnet.ID)
175 t.Logf("NetworkID: %s", subnet.NetworkID)
176 t.Logf("Name: %s", subnet.Name)
177 t.Logf("IPVersion: %d", subnet.IPVersion)
178 t.Logf("CIDR: %s", subnet.CIDR)
179 t.Logf("GatewayIP: %s", subnet.GatewayIP)
180 t.Logf("DNSNameservers: %s", subnet.DNSNameservers)
181 t.Logf("AllocationPools: %s", subnet.AllocationPools)
182 t.Logf("HostRoutes: %s", subnet.HostRoutes)
183 t.Logf("EnableDHCP: %t", subnet.EnableDHCP)
184 t.Logf("TenantID: %s", subnet.TenantID)
185}
186
187// PrintVersionResource will print an API version resource and all of its attributes.
188func PrintVersionResource(t *testing.T, versionResource *apiversions.APIVersionResource) {
189 t.Logf("Name: %s", versionResource.Name)
190 t.Logf("Collection: %s", versionResource.Collection)
191}
Joe Topjianc5d17b82016-09-26 12:39:57 -0400192
193func WaitForPortToCreate(client *gophercloud.ServiceClient, portID string, secs int) error {
194 return gophercloud.WaitFor(secs, func() (bool, error) {
195 p, err := ports.Get(client, portID).Extract()
196 if err != nil {
197 return false, err
198 }
199
200 if p.Status == "ACTIVE" || p.Status == "DOWN" {
201 return true, nil
202 }
203
204 return false, nil
205 })
206}