blob: 48ebcece13156da887438f642f63e5f594be5ee0 [file] [log] [blame]
Jon Perritt9f8b0152015-03-17 19:28:18 -06001package lbpools
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7 "time"
8
Jon Perritt9f8b0152015-03-17 19:28:18 -06009 "github.com/rackspace/gophercloud/pagination"
10 th "github.com/rackspace/gophercloud/testhelper"
Jon Perrittac0190f2015-03-17 21:30:17 -060011 "github.com/rackspace/gophercloud/testhelper/client"
Jon Perritt9f8b0152015-03-17 19:28:18 -060012 fake "github.com/rackspace/gophercloud/testhelper/client"
13)
14
15func TestListPools(t *testing.T) {
16 th.SetupHTTP()
17 defer th.TeardownHTTP()
18 th.Mux.HandleFunc("/load_balancer_pools", func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "GET")
20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
21 th.TestHeader(t, r, "Accept", "application/json")
22
23 w.Header().Set("Content-Type", "application/json")
24 fmt.Fprintf(w, `[
25 {
26 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
27 "name": "RCv3Test",
28 "node_counts": {
29 "cloud_servers": 3,
30 "external": 4,
31 "total": 7
32 },
33 "port": 80,
34 "status": "ACTIVE",
35 "status_detail": null,
36 "virtual_ip": "203.0.113.5"
37 },
38 {
39 "id": "33021100-4abf-4836-9080-465a6d87ab68",
40 "name": "RCv3Test2",
41 "node_counts": {
42 "cloud_servers": 1,
43 "external": 0,
44 "total": 1
45 },
46 "port": 80,
47 "status": "ACTIVE",
48 "status_detail": null,
49 "virtual_ip": "203.0.113.7"
50 },
51 {
52 "id": "b644350a-301b-47b5-a411-c6e0f933c347",
53 "name": "RCv3Test3",
54 "node_counts": {
55 "cloud_servers": 2,
56 "external": 3,
57 "total": 5
58 },
59 "port": 443,
60 "status": "ACTIVE",
61 "status_detail": null,
62 "virtual_ip": "203.0.113.15"
63 }
64 ]`)
65 })
66
67 expected := []Pool{
68 Pool{
69 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
70 Name: "RCv3Test",
71 NodeCounts: struct {
72 CloudServers int `mapstructure:"cloud_servers"`
73 External int `mapstructure:"external"`
74 Total int `mapstructure:"total"`
75 }{
76 CloudServers: 3,
77 External: 4,
78 Total: 7,
79 },
80 Port: 80,
81 Status: "ACTIVE",
82 VirtualIP: "203.0.113.5",
83 },
84 Pool{
85 ID: "33021100-4abf-4836-9080-465a6d87ab68",
86 Name: "RCv3Test2",
87 NodeCounts: struct {
88 CloudServers int `mapstructure:"cloud_servers"`
89 External int `mapstructure:"external"`
90 Total int `mapstructure:"total"`
91 }{
92 CloudServers: 1,
93 External: 0,
94 Total: 1,
95 },
96 Port: 80,
97 Status: "ACTIVE",
98 VirtualIP: "203.0.113.7",
99 },
100 Pool{
101 ID: "b644350a-301b-47b5-a411-c6e0f933c347",
102 Name: "RCv3Test3",
103 NodeCounts: struct {
104 CloudServers int `mapstructure:"cloud_servers"`
105 External int `mapstructure:"external"`
106 Total int `mapstructure:"total"`
107 }{
108 CloudServers: 2,
109 External: 3,
110 Total: 5,
111 },
112 Port: 443,
113 Status: "ACTIVE",
114 VirtualIP: "203.0.113.15",
115 },
116 }
117
118 count := 0
119 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
120 count++
121 actual, err := ExtractPools(page)
122 th.AssertNoErr(t, err)
123
124 th.CheckDeepEquals(t, expected, actual)
125
126 return true, nil
127 })
128 th.AssertNoErr(t, err)
129 th.CheckEquals(t, count, 1)
130}
131
132func TestGetLBPool(t *testing.T) {
133 th.SetupHTTP()
134 defer th.TeardownHTTP()
135 th.Mux.HandleFunc("/load_balancer_pools/d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2", func(w http.ResponseWriter, r *http.Request) {
136 th.TestMethod(t, r, "GET")
137 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
138 th.TestHeader(t, r, "Accept", "application/json")
139
140 w.Header().Set("Content-Type", "application/json")
141 w.WriteHeader(http.StatusOK)
142 fmt.Fprintf(w, `{
143 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
144 "name": "RCv3Test",
145 "node_counts": {
146 "cloud_servers": 3,
147 "external": 4,
148 "total": 7
149 },
150 "port": 80,
151 "status": "ACTIVE",
152 "status_detail": null,
153 "virtual_ip": "203.0.113.5"
154 }`)
155 })
156
157 expected := &Pool{
158 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
159 Name: "RCv3Test",
160 NodeCounts: struct {
161 CloudServers int `mapstructure:"cloud_servers"`
162 External int `mapstructure:"external"`
163 Total int `mapstructure:"total"`
164 }{
165 CloudServers: 3,
166 External: 4,
167 Total: 7,
168 },
169 Port: 80,
170 Status: "ACTIVE",
171 VirtualIP: "203.0.113.5",
172 }
173
174 actual, err := Get(fake.ServiceClient(), "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2").Extract()
175 th.AssertNoErr(t, err)
176
177 th.AssertDeepEquals(t, expected, actual)
178}
179
180func TestListNodes(t *testing.T) {
181 th.SetupHTTP()
182 defer th.TeardownHTTP()
183 th.Mux.HandleFunc("/load_balancer_pools/d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2/nodes", func(w http.ResponseWriter, r *http.Request) {
184 th.TestMethod(t, r, "GET")
185 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
186 th.TestHeader(t, r, "Accept", "application/json")
187
188 w.Header().Set("Content-Type", "application/json")
189 fmt.Fprintf(w, `[
190 {
191 "created": "2014-05-30T03:23:42Z",
192 "cloud_server": {
193 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
194 },
195 "id": "1860451d-fb89-45b8-b54e-151afceb50e5",
196 "load_balancer_pool": {
197 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
198 },
199 "status": "ACTIVE",
200 "updated": "2014-05-30T03:24:18Z"
201 },
202 {
203 "created": "2014-05-31T08:23:12Z",
204 "cloud_server": {
205 "id": "f28b870f-a063-498a-8b12-7025e5b1caa6"
206 },
207 "id": "b70481dd-7edf-4dbb-a44b-41cc7679d4fb",
208 "load_balancer_pool": {
209 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
210 },
211 "status": "ADDING",
212 "updated": "2014-05-31T08:23:26Z"
213 },
214 {
215 "created": "2014-05-31T08:23:18Z",
216 "cloud_server": {
217 "id": "a3d3a6b3-e4e4-496f-9a3d-5c987163e458"
218 },
219 "id": "ced9ddc8-6fae-4e72-9457-16ead52b5515",
220 "load_balancer_pool": {
221 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
222 },
223 "status": "ADD_FAILED",
224 "status_detail": "Unable to communicate with network device",
225 "updated": "2014-05-31T08:24:36Z"
226 }
227 ]`)
228 })
229
230 expected := []Node{
231 Node{
232 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
233 CloudServer: struct {
234 ID string `mapstructure:"id"`
235 }{
236 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
237 },
238 ID: "1860451d-fb89-45b8-b54e-151afceb50e5",
239 LoadBalancerPool: struct {
240 ID string `mapstructure:"id"`
241 }{
242 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
243 },
244 Status: "ACTIVE",
245 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
246 },
247 Node{
248 CreatedAt: time.Date(2014, 5, 31, 8, 23, 12, 0, time.UTC),
249 CloudServer: struct {
250 ID string `mapstructure:"id"`
251 }{
252 ID: "f28b870f-a063-498a-8b12-7025e5b1caa6",
253 },
254 ID: "b70481dd-7edf-4dbb-a44b-41cc7679d4fb",
255 LoadBalancerPool: struct {
256 ID string `mapstructure:"id"`
257 }{
258 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
259 },
260 Status: "ADDING",
261 UpdatedAt: time.Date(2014, 5, 31, 8, 23, 26, 0, time.UTC),
262 },
263 Node{
264 CreatedAt: time.Date(2014, 5, 31, 8, 23, 18, 0, time.UTC),
265 CloudServer: struct {
266 ID string `mapstructure:"id"`
267 }{
268 ID: "a3d3a6b3-e4e4-496f-9a3d-5c987163e458",
269 },
270 ID: "ced9ddc8-6fae-4e72-9457-16ead52b5515",
271 LoadBalancerPool: struct {
272 ID string `mapstructure:"id"`
273 }{
274 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
275 },
276 Status: "ADD_FAILED",
277 StatusDetail: "Unable to communicate with network device",
278 UpdatedAt: time.Date(2014, 5, 31, 8, 24, 36, 0, time.UTC),
279 },
280 }
281
282 count := 0
283 err := ListNodes(fake.ServiceClient(), "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2").EachPage(func(page pagination.Page) (bool, error) {
284 count++
285 actual, err := ExtractNodes(page)
286 th.AssertNoErr(t, err)
287
288 th.CheckDeepEquals(t, expected, actual)
289
290 return true, nil
291 })
292 th.AssertNoErr(t, err)
293 th.CheckEquals(t, count, 1)
294}
295
296func TestCreateNode(t *testing.T) {
297 th.SetupHTTP()
298 defer th.TeardownHTTP()
299 th.Mux.HandleFunc("/load_balancer_pools/d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2/nodes", func(w http.ResponseWriter, r *http.Request) {
300 th.TestMethod(t, r, "POST")
301 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
302 th.TestHeader(t, r, "Accept", "application/json")
303 th.TestJSONRequest(t, r, `
304 {
305 "cloud_server": {
306 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
307 }
308 }
309 `)
310
311 w.Header().Set("Content-Type", "application/json")
312 w.WriteHeader(http.StatusCreated)
313 fmt.Fprintf(w, `
314 {
315 "created": "2014-05-30T03:23:42Z",
316 "cloud_server": {
317 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
318 },
319 "id": "1860451d-fb89-45b8-b54e-151afceb50e5",
320 "load_balancer_pool": {
321 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
322 },
323 "status": "ACTIVE",
324 "status_detail": null,
325 "updated": "2014-05-30T03:24:18Z"
326 }
327 `)
328 })
329
330 expected := &Node{
331 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
332 CloudServer: struct {
333 ID string `mapstructure:"id"`
334 }{
335 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
336 },
337 ID: "1860451d-fb89-45b8-b54e-151afceb50e5",
338 LoadBalancerPool: struct {
339 ID string `mapstructure:"id"`
340 }{
341 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
342 },
343 Status: "ACTIVE",
344 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
345 }
346
347 actual, err := CreateNode(fake.ServiceClient(), "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2", "d95ae0c4-6ab8-4873-b82f-f8433840cff2").Extract()
348 th.AssertNoErr(t, err)
349
350 th.AssertDeepEquals(t, expected, actual)
351}
352
353func TestListNodesDetails(t *testing.T) {
354 th.SetupHTTP()
355 defer th.TeardownHTTP()
356 th.Mux.HandleFunc("/load_balancer_pools/d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2/nodes/details", func(w http.ResponseWriter, r *http.Request) {
357 th.TestMethod(t, r, "GET")
358 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
359 th.TestHeader(t, r, "Accept", "application/json")
360
361 w.Header().Set("Content-Type", "application/json")
362 fmt.Fprintf(w, `
363 [
364 {
365 "created": "2014-05-30T03:23:42Z",
366 "cloud_server": {
367 "cloud_network": {
368 "cidr": "192.168.100.0/24",
369 "created": "2014-05-25T01:23:42Z",
370 "id": "07426958-1ebf-4c38-b032-d456820ca21a",
371 "name": "RC-CLOUD",
372 "private_ip_v4": "192.168.100.5",
373 "updated": "2014-05-25T02:28:44Z"
374 },
375 "created": "2014-05-30T02:18:42Z",
376 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
377 "name": "RCv3TestServer1",
378 "updated": "2014-05-30T02:19:18Z"
379 },
380 "id": "1860451d-fb89-45b8-b54e-151afceb50e5",
381 "load_balancer_pool": {
382 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
383 "name": "RCv3Test",
384 "node_counts": {
385 "cloud_servers": 3,
386 "external": 4,
387 "total": 7
388 },
389 "port": 80,
390 "status": "ACTIVE",
391 "status_detail": null,
392 "virtual_ip": "203.0.113.5"
393 },
394 "status": "ACTIVE",
395 "status_detail": null,
396 "updated": "2014-05-30T03:24:18Z"
397 }
398 ]
399 `)
400 })
401
402 expected := []NodeDetails{
403 NodeDetails{
404 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
405 CloudServer: struct {
406 ID string `mapstructure:"id"`
407 Name string `mapstructure:"name"`
408 CloudNetwork struct {
409 ID string `mapstructure:"id"`
410 Name string `mapstructure:"name"`
411 PrivateIPv4 string `mapstructure:"private_ip_v4"`
412 CIDR string `mapstructure:"cidr"`
413 CreatedAt time.Time `mapstructure:"-"`
414 UpdatedAt time.Time `mapstructure:"-"`
415 } `mapstructure:"cloud_network"`
416 CreatedAt time.Time `mapstructure:"-"`
417 UpdatedAt time.Time `mapstructure:"-"`
418 }{
419 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
420 CloudNetwork: struct {
421 ID string `mapstructure:"id"`
422 Name string `mapstructure:"name"`
423 PrivateIPv4 string `mapstructure:"private_ip_v4"`
424 CIDR string `mapstructure:"cidr"`
425 CreatedAt time.Time `mapstructure:"-"`
426 UpdatedAt time.Time `mapstructure:"-"`
427 }{
428 ID: "07426958-1ebf-4c38-b032-d456820ca21a",
429 CIDR: "192.168.100.0/24",
430 CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
431 Name: "RC-CLOUD",
432 PrivateIPv4: "192.168.100.5",
433 UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
434 },
435 CreatedAt: time.Date(2014, 5, 30, 2, 18, 42, 0, time.UTC),
436 Name: "RCv3TestServer1",
437 UpdatedAt: time.Date(2014, 5, 30, 2, 19, 18, 0, time.UTC),
438 },
439 ID: "1860451d-fb89-45b8-b54e-151afceb50e5",
440 LoadBalancerPool: Pool{
441 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
442 Name: "RCv3Test",
443 NodeCounts: struct {
444 CloudServers int `mapstructure:"cloud_servers"`
445 External int `mapstructure:"external"`
446 Total int `mapstructure:"total"`
447 }{
448 CloudServers: 3,
449 External: 4,
450 Total: 7,
451 },
452 Port: 80,
453 Status: "ACTIVE",
454 VirtualIP: "203.0.113.5",
455 },
456 Status: "ACTIVE",
457 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
458 },
459 }
460 count := 0
461 err := ListNodesDetails(fake.ServiceClient(), "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2").EachPage(func(page pagination.Page) (bool, error) {
462 count++
463 actual, err := ExtractNodesDetails(page)
464 th.AssertNoErr(t, err)
465
466 th.CheckDeepEquals(t, expected, actual)
467
468 return true, nil
469 })
470 th.AssertNoErr(t, err)
471 th.CheckEquals(t, count, 1)
472}
473
474func TestGetNode(t *testing.T) {
475 th.SetupHTTP()
476 defer th.TeardownHTTP()
477 th.Mux.HandleFunc("/load_balancer_pools/d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2/nodes/1860451d-fb89-45b8-b54e-151afceb50e5", func(w http.ResponseWriter, r *http.Request) {
478 th.TestMethod(t, r, "GET")
479 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
480 th.TestHeader(t, r, "Accept", "application/json")
481
482 w.Header().Set("Content-Type", "application/json")
483 w.WriteHeader(http.StatusOK)
484 fmt.Fprintf(w, `
485 {
486 "created": "2014-05-30T03:23:42Z",
487 "cloud_server": {
488 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
489 },
490 "id": "1860451d-fb89-45b8-b54e-151afceb50e5",
491 "load_balancer_pool": {
492 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
493 },
494 "status": "ACTIVE",
495 "status_detail": null,
496 "updated": "2014-05-30T03:24:18Z"
497 }
498 `)
499 })
500
501 expected := &Node{
502 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
503 CloudServer: struct {
504 ID string `mapstructure:"id"`
505 }{
506 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
507 },
508 ID: "1860451d-fb89-45b8-b54e-151afceb50e5",
509 LoadBalancerPool: struct {
510 ID string `mapstructure:"id"`
511 }{
512 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
513 },
514 Status: "ACTIVE",
515 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
516 }
517
518 actual, err := GetNode(fake.ServiceClient(), "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2", "1860451d-fb89-45b8-b54e-151afceb50e5").Extract()
519 th.AssertNoErr(t, err)
520
521 th.AssertDeepEquals(t, expected, actual)
522}
523
524func TestDeleteNode(t *testing.T) {
525 th.SetupHTTP()
526 defer th.TeardownHTTP()
527 th.Mux.HandleFunc("/load_balancer_pools/d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2/nodes/1860451d-fb89-45b8-b54e-151afceb50e5", func(w http.ResponseWriter, r *http.Request) {
528 th.TestMethod(t, r, "DELETE")
529 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
530 th.TestHeader(t, r, "Accept", "application/json")
531
532 w.Header().Set("Content-Type", "application/json")
533 w.WriteHeader(http.StatusNoContent)
534 })
535
536 err := DeleteNode(client.ServiceClient(), "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2", "1860451d-fb89-45b8-b54e-151afceb50e5").ExtractErr()
537 th.AssertNoErr(t, err)
538}
539
540func TestGetNodeDetails(t *testing.T) {
541 th.SetupHTTP()
542 defer th.TeardownHTTP()
543 th.Mux.HandleFunc("/load_balancer_pools/d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2/nodes/d95ae0c4-6ab8-4873-b82f-f8433840cff2/details", func(w http.ResponseWriter, r *http.Request) {
544 th.TestMethod(t, r, "GET")
545 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
546 th.TestHeader(t, r, "Accept", "application/json")
547
548 w.Header().Set("Content-Type", "application/json")
549 fmt.Fprintf(w, `
550 {
551 "created": "2014-05-30T03:23:42Z",
552 "cloud_server": {
553 "cloud_network": {
554 "cidr": "192.168.100.0/24",
555 "created": "2014-05-25T01:23:42Z",
556 "id": "07426958-1ebf-4c38-b032-d456820ca21a",
557 "name": "RC-CLOUD",
558 "private_ip_v4": "192.168.100.5",
559 "updated": "2014-05-25T02:28:44Z"
560 },
561 "created": "2014-05-30T02:18:42Z",
562 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
563 "name": "RCv3TestServer1",
564 "updated": "2014-05-30T02:19:18Z"
565 },
566 "id": "1860451d-fb89-45b8-b54e-151afceb50e5",
567 "load_balancer_pool": {
568 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
569 "name": "RCv3Test",
570 "node_counts": {
571 "cloud_servers": 3,
572 "external": 4,
573 "total": 7
574 },
575 "port": 80,
576 "status": "ACTIVE",
577 "status_detail": null,
578 "virtual_ip": "203.0.113.5"
579 },
580 "status": "ACTIVE",
581 "status_detail": null,
582 "updated": "2014-05-30T03:24:18Z"
583 }
584 `)
585 })
586
587 expected := &NodeDetails{
588 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
589 CloudServer: struct {
590 ID string `mapstructure:"id"`
591 Name string `mapstructure:"name"`
592 CloudNetwork struct {
593 ID string `mapstructure:"id"`
594 Name string `mapstructure:"name"`
595 PrivateIPv4 string `mapstructure:"private_ip_v4"`
596 CIDR string `mapstructure:"cidr"`
597 CreatedAt time.Time `mapstructure:"-"`
598 UpdatedAt time.Time `mapstructure:"-"`
599 } `mapstructure:"cloud_network"`
600 CreatedAt time.Time `mapstructure:"-"`
601 UpdatedAt time.Time `mapstructure:"-"`
602 }{
603 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
604 CloudNetwork: struct {
605 ID string `mapstructure:"id"`
606 Name string `mapstructure:"name"`
607 PrivateIPv4 string `mapstructure:"private_ip_v4"`
608 CIDR string `mapstructure:"cidr"`
609 CreatedAt time.Time `mapstructure:"-"`
610 UpdatedAt time.Time `mapstructure:"-"`
611 }{
612 ID: "07426958-1ebf-4c38-b032-d456820ca21a",
613 CIDR: "192.168.100.0/24",
614 CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
615 Name: "RC-CLOUD",
616 PrivateIPv4: "192.168.100.5",
617 UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
618 },
619 CreatedAt: time.Date(2014, 5, 30, 2, 18, 42, 0, time.UTC),
620 Name: "RCv3TestServer1",
621 UpdatedAt: time.Date(2014, 5, 30, 2, 19, 18, 0, time.UTC),
622 },
623 ID: "1860451d-fb89-45b8-b54e-151afceb50e5",
624 LoadBalancerPool: Pool{
625 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
626 Name: "RCv3Test",
627 NodeCounts: struct {
628 CloudServers int `mapstructure:"cloud_servers"`
629 External int `mapstructure:"external"`
630 Total int `mapstructure:"total"`
631 }{
632 CloudServers: 3,
633 External: 4,
634 Total: 7,
635 },
636 Port: 80,
637 Status: "ACTIVE",
638 VirtualIP: "203.0.113.5",
639 },
640 Status: "ACTIVE",
641 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
642 }
643
644 actual, err := GetNodeDetails(fake.ServiceClient(), "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2", "d95ae0c4-6ab8-4873-b82f-f8433840cff2").Extract()
645 th.AssertNoErr(t, err)
646 th.CheckDeepEquals(t, expected, actual)
647}
648
649func TestCreateNodes(t *testing.T) {
650 th.SetupHTTP()
651 defer th.TeardownHTTP()
652 th.Mux.HandleFunc("/load_balancer_pools/nodes", func(w http.ResponseWriter, r *http.Request) {
653 th.TestMethod(t, r, "POST")
654 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
655 th.TestHeader(t, r, "Accept", "application/json")
656 th.TestJSONRequest(t, r, `
657 [
658 {
659 "cloud_server": {
660 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
661 },
662 "load_balancer_pool": {
663 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
664 }
665 },
666 {
667 "cloud_server": {
668 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
669 },
670 "load_balancer_pool": {
671 "id": "33021100-4abf-4836-9080-465a6d87ab68"
672 }
673 }
674 ]
675 `)
676
677 w.Header().Set("Content-Type", "application/json")
678 w.WriteHeader(http.StatusCreated)
679 fmt.Fprintf(w, `
680 [
681 {
682 "created": "2014-05-30T03:23:42Z",
683 "cloud_server": {
684 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
685 },
686 "id": "1860451d-fb89-45b8-b54e-151afceb50e5",
687 "load_balancer_pool": {
688 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
689 },
690 "status": "ADDING",
691 "status_detail": null,
692 "updated": null
693 },
694 {
695 "created": "2014-05-31T08:23:12Z",
696 "cloud_server": {
697 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
698 },
699 "id": "b70481dd-7edf-4dbb-a44b-41cc7679d4fb",
700 "load_balancer_pool": {
701 "id": "33021100-4abf-4836-9080-465a6d87ab68"
702 },
703 "status": "ADDING",
704 "status_detail": null,
705 "updated": null
706 }
707 ]
708 `)
709 })
710
711 expected := []Node{
712 Node{
713 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
714 CloudServer: struct {
715 ID string `mapstructure:"id"`
716 }{
717 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
718 },
719 ID: "1860451d-fb89-45b8-b54e-151afceb50e5",
720 LoadBalancerPool: struct {
721 ID string `mapstructure:"id"`
722 }{
723 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
724 },
725 Status: "ADDING",
726 },
727 Node{
728 CreatedAt: time.Date(2014, 5, 31, 8, 23, 12, 0, time.UTC),
729 CloudServer: struct {
730 ID string `mapstructure:"id"`
731 }{
732 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
733 },
734 ID: "b70481dd-7edf-4dbb-a44b-41cc7679d4fb",
735 LoadBalancerPool: struct {
736 ID string `mapstructure:"id"`
737 }{
738 ID: "33021100-4abf-4836-9080-465a6d87ab68",
739 },
740 Status: "ADDING",
741 },
742 }
743
Jon Perrittaa244992015-03-18 09:42:24 -0600744 opts := NodesOpts{
745 NodeOpts{
Jon Perritt9f8b0152015-03-17 19:28:18 -0600746 ServerID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
747 PoolID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
748 },
Jon Perrittaa244992015-03-18 09:42:24 -0600749 NodeOpts{
Jon Perritt9f8b0152015-03-17 19:28:18 -0600750 ServerID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
751 PoolID: "33021100-4abf-4836-9080-465a6d87ab68",
752 },
753 }
754 actual, err := CreateNodes(fake.ServiceClient(), opts).Extract()
755 th.AssertNoErr(t, err)
756 th.AssertDeepEquals(t, expected, actual)
757}
758
759func TestDeleteNodes(t *testing.T) {
760 th.SetupHTTP()
761 defer th.TeardownHTTP()
762 th.Mux.HandleFunc("/load_balancer_pools/nodes", func(w http.ResponseWriter, r *http.Request) {
763 th.TestMethod(t, r, "DELETE")
764 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
765 th.TestHeader(t, r, "Accept", "application/json")
766 th.TestJSONRequest(t, r, `
767 [
768 {
769 "cloud_server": {
770 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
771 },
772 "load_balancer_pool": {
773 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2"
774 }
775 },
776 {
777 "cloud_server": {
778 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
779 },
780 "load_balancer_pool": {
781 "id": "33021100-4abf-4836-9080-465a6d87ab68"
782 }
783 }
784 ]
785 `)
786
787 w.Header().Set("Content-Type", "application/json")
788 w.WriteHeader(http.StatusNoContent)
789 })
790
Jon Perrittaa244992015-03-18 09:42:24 -0600791 opts := NodesOpts{
792 NodeOpts{
Jon Perritt9f8b0152015-03-17 19:28:18 -0600793 ServerID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
794 PoolID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
795 },
Jon Perrittaa244992015-03-18 09:42:24 -0600796 NodeOpts{
Jon Perritt9f8b0152015-03-17 19:28:18 -0600797 ServerID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
798 PoolID: "33021100-4abf-4836-9080-465a6d87ab68",
799 },
800 }
801 err := DeleteNodes(client.ServiceClient(), opts).ExtractErr()
802 th.AssertNoErr(t, err)
803}
804
805func TestListNodesForServerDetails(t *testing.T) {
806 th.SetupHTTP()
807 defer th.TeardownHTTP()
808 th.Mux.HandleFunc("/load_balancer_pools/nodes/details", func(w http.ResponseWriter, r *http.Request) {
809 th.TestMethod(t, r, "GET")
810 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
811 th.TestHeader(t, r, "Accept", "application/json")
812
813 w.Header().Set("Content-Type", "application/json")
814 fmt.Fprintf(w, `
815 [
816 {
817 "created": "2014-05-30T03:23:42Z",
818 "id": "1860451d-fb89-45b8-b54e-151afceb50e5",
819 "load_balancer_pool": {
820 "id": "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
821 "name": "RCv3Test",
822 "node_counts": {
823 "cloud_servers": 3,
824 "external": 4,
825 "total": 7
826 },
827 "port": 80,
828 "status": "ACTIVE",
829 "status_detail": null,
830 "virtual_ip": "203.0.113.5"
831 },
832 "status": "ACTIVE",
833 "status_detail": null,
834 "updated": "2014-05-30T03:24:18Z"
835 }
836 ]
837 `)
838 })
839
840 expected := []NodeDetailsForServer{
841 NodeDetailsForServer{
842 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
843 ID: "1860451d-fb89-45b8-b54e-151afceb50e5",
844 LoadBalancerPool: Pool{
845 ID: "d6d3aa7c-dfa5-4e61-96ee-1d54ac1075d2",
846 Name: "RCv3Test",
847 NodeCounts: struct {
848 CloudServers int `mapstructure:"cloud_servers"`
849 External int `mapstructure:"external"`
850 Total int `mapstructure:"total"`
851 }{
852 CloudServers: 3,
853 External: 4,
854 Total: 7,
855 },
856 Port: 80,
857 Status: "ACTIVE",
858 VirtualIP: "203.0.113.5",
859 },
860 Status: "ACTIVE",
861 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
862 },
863 }
864 count := 0
865 err := ListNodesDetailsForServer(fake.ServiceClient(), "07426958-1ebf-4c38-b032-d456820ca21a").EachPage(func(page pagination.Page) (bool, error) {
866 count++
867 actual, err := ExtractNodesDetailsForServer(page)
868 th.AssertNoErr(t, err)
869
870 th.CheckDeepEquals(t, expected, actual)
871
872 return true, nil
873 })
874 th.AssertNoErr(t, err)
875 th.CheckEquals(t, count, 1)
876}