blob: 3c7c8b2f95dda785b4403aea1fa420737b490e89 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
2
Jamie Hannaford0708c002014-09-17 16:08:49 +02003import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
10 th "github.com/rackspace/gophercloud/testhelper"
11)
12
13const TokenID = "123"
14
15func ServiceClient() *gophercloud.ServiceClient {
16 return &gophercloud.ServiceClient{
17 Provider: &gophercloud.ProviderClient{
18 TokenID: TokenID,
19 },
20 Endpoint: th.Endpoint(),
21 }
22}
Jamie Hannaford89f9af22014-09-17 12:21:48 +020023
24func TestList(t *testing.T) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020025 th.SetupHTTP()
26 defer th.TeardownHTTP()
Jamie Hannaford89f9af22014-09-17 12:21:48 +020027
Jamie Hannaford0708c002014-09-17 16:08:49 +020028 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
29 th.TestMethod(t, r, "GET")
30 th.TestHeader(t, r, "X-Auth-Token", TokenID)
31
32 w.Header().Add("Content-Type", "application/json")
33 w.WriteHeader(http.StatusOK)
34
35 fmt.Fprintf(w, `
36{
37 "subnets": [
38 {
39 "name": "private-subnet",
40 "enable_dhcp": true,
41 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
42 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
43 "dns_nameservers": [],
44 "allocation_pools": [
45 {
46 "start": "10.0.0.2",
47 "end": "10.0.0.254"
48 }
49 ],
50 "host_routes": [],
51 "ip_version": 4,
52 "gateway_ip": "10.0.0.1",
53 "cidr": "10.0.0.0/24",
54 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
55 },
56 {
57 "name": "my_subnet",
58 "enable_dhcp": true,
59 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
60 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
61 "dns_nameservers": [],
62 "allocation_pools": [
63 {
64 "start": "192.0.0.2",
65 "end": "192.255.255.254"
66 }
67 ],
68 "host_routes": [],
69 "ip_version": 4,
70 "gateway_ip": "192.0.0.1",
71 "cidr": "192.0.0.0/8",
72 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
73 }
74 ]
75}
76 `)
77 })
78
79 count := 0
80
81 List(ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
82 count++
83 actual, err := ExtractSubnets(page)
84 if err != nil {
85 t.Errorf("Failed to extract subnets: %v", err)
86 return false, nil
87 }
88
89 expected := []Subnet{
90 Subnet{
91 Name: "private-subnet",
92 EnableDHCP: true,
93 NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
94 TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Jamie Hannaford965ae702014-09-22 14:58:19 +020095 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +020096 AllocationPools: []AllocationPool{
97 AllocationPool{
98 Start: "10.0.0.2",
99 End: "10.0.0.254",
100 },
101 },
Jamie Hannafordf2835402014-09-23 11:01:21 +0200102 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200103 IPVersion: 4,
104 GatewayIP: "10.0.0.1",
105 CIDR: "10.0.0.0/24",
106 ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
107 },
108 Subnet{
109 Name: "my_subnet",
110 EnableDHCP: true,
111 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
112 TenantID: "4fd44f30292945e481c7b8a0c8908869",
Jamie Hannaford965ae702014-09-22 14:58:19 +0200113 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200114 AllocationPools: []AllocationPool{
115 AllocationPool{
116 Start: "192.0.0.2",
117 End: "192.255.255.254",
118 },
119 },
Jamie Hannafordf2835402014-09-23 11:01:21 +0200120 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200121 IPVersion: 4,
122 GatewayIP: "192.0.0.1",
123 CIDR: "192.0.0.0/8",
124 ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
125 },
126 }
127
128 th.CheckDeepEquals(t, expected, actual)
129
130 return true, nil
131 })
132
133 if count != 1 {
134 t.Errorf("Expected 1 page, got %d", count)
135 }
136}
137
138func TestGet(t *testing.T) {
139 th.SetupHTTP()
140 defer th.TeardownHTTP()
141
142 th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) {
143 th.TestMethod(t, r, "GET")
144 th.TestHeader(t, r, "X-Auth-Token", TokenID)
145
146 w.Header().Add("Content-Type", "application/json")
147 w.WriteHeader(http.StatusOK)
148
149 fmt.Fprintf(w, `
150{
151 "subnet": {
152 "name": "my_subnet",
153 "enable_dhcp": true,
154 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
155 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
156 "dns_nameservers": [],
157 "allocation_pools": [
158 {
159 "start": "192.0.0.2",
160 "end": "192.255.255.254"
161 }
162 ],
163 "host_routes": [],
164 "ip_version": 4,
165 "gateway_ip": "192.0.0.1",
166 "cidr": "192.0.0.0/8",
167 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
168 }
169}
170 `)
171 })
172
Jamie Hannafordd9036422014-09-23 17:50:24 +0200173 s, err := Get(ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
Jamie Hannaford0708c002014-09-17 16:08:49 +0200174 th.AssertNoErr(t, err)
175
176 th.AssertEquals(t, s.Name, "my_subnet")
177 th.AssertEquals(t, s.EnableDHCP, true)
178 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
179 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200180 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200181 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
182 AllocationPool{
183 Start: "192.0.0.2",
184 End: "192.255.255.254",
185 },
186 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200187 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200188 th.AssertEquals(t, s.IPVersion, 4)
189 th.AssertEquals(t, s.GatewayIP, "192.0.0.1")
190 th.AssertEquals(t, s.CIDR, "192.0.0.0/8")
191 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
Jamie Hannaford89f9af22014-09-17 12:21:48 +0200192}
Jamie Hannaford63631432014-09-18 11:40:09 +0200193
194func TestCreate(t *testing.T) {
195 th.SetupHTTP()
196 defer th.TeardownHTTP()
197
198 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
199 th.TestMethod(t, r, "POST")
200 th.TestHeader(t, r, "X-Auth-Token", TokenID)
201 th.TestHeader(t, r, "Content-Type", "application/json")
202 th.TestHeader(t, r, "Accept", "application/json")
203 th.TestJSONRequest(t, r, `
204{
205 "subnet": {
206 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
207 "ip_version": 4,
208 "cidr": "192.168.199.0/24"
209 }
210}
211 `)
212
213 w.Header().Add("Content-Type", "application/json")
214 w.WriteHeader(http.StatusCreated)
215
216 fmt.Fprintf(w, `
217{
218 "subnet": {
219 "name": "",
220 "enable_dhcp": true,
221 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
222 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
223 "dns_nameservers": [],
224 "allocation_pools": [
225 {
226 "start": "192.168.199.2",
227 "end": "192.168.199.254"
228 }
229 ],
230 "host_routes": [],
231 "ip_version": 4,
232 "gateway_ip": "192.168.199.1",
233 "cidr": "192.168.199.0/24",
234 "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
235 }
236}
237 `)
238 })
239
Jamie Hannaford965ae702014-09-22 14:58:19 +0200240 opts := CreateOpts{NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", IPVersion: 4, CIDR: "192.168.199.0/24"}
Jamie Hannafordd9036422014-09-23 17:50:24 +0200241 s, err := Create(ServiceClient(), opts).Extract()
Jamie Hannaford63631432014-09-18 11:40:09 +0200242 th.AssertNoErr(t, err)
243
244 th.AssertEquals(t, s.Name, "")
245 th.AssertEquals(t, s.EnableDHCP, true)
246 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
247 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200248 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200249 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
250 AllocationPool{
251 Start: "192.168.199.2",
252 End: "192.168.199.254",
253 },
254 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200255 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200256 th.AssertEquals(t, s.IPVersion, 4)
257 th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
258 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
259 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
260}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200261
262func TestUpdate(t *testing.T) {
263 th.SetupHTTP()
264 defer th.TeardownHTTP()
265
266 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
267 th.TestMethod(t, r, "PUT")
268 th.TestHeader(t, r, "X-Auth-Token", TokenID)
269 th.TestHeader(t, r, "Content-Type", "application/json")
270 th.TestHeader(t, r, "Accept", "application/json")
271 th.TestJSONRequest(t, r, `
272{
273 "subnet": {
274 "name": "my_new_subnet"
275 }
276}
277 `)
278
279 w.Header().Add("Content-Type", "application/json")
280 w.WriteHeader(http.StatusCreated)
281
282 fmt.Fprintf(w, `
283{
284 "subnet": {
285 "name": "my_new_subnet",
286 "enable_dhcp": true,
287 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
288 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
289 "dns_nameservers": [],
290 "allocation_pools": [
291 {
292 "start": "10.0.0.2",
293 "end": "10.0.0.254"
294 }
295 ],
296 "host_routes": [],
297 "ip_version": 4,
298 "gateway_ip": "10.0.0.1",
299 "cidr": "10.0.0.0/24",
300 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
301 }
302}
303 `)
304 })
305
Jamie Hannaford965ae702014-09-22 14:58:19 +0200306 opts := UpdateOpts{Name: "my_new_subnet"}
Jamie Hannafordd9036422014-09-23 17:50:24 +0200307 s, err := Update(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200308 th.AssertNoErr(t, err)
309
310 th.AssertEquals(t, s.Name, "my_new_subnet")
311 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
312}
313
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200314func TestDelete(t *testing.T) {
315 th.SetupHTTP()
316 defer th.TeardownHTTP()
317
318 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
319 th.TestMethod(t, r, "DELETE")
320 th.TestHeader(t, r, "X-Auth-Token", TokenID)
321 w.WriteHeader(http.StatusNoContent)
322 })
323
Jamie Hannafordd9036422014-09-23 17:50:24 +0200324 res := Delete(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
325 th.AssertNoErr(t, res.Err)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200326}