blob: abf7037806fa182b36dae2a4740b8993c0046941 [file] [log] [blame]
Jamie Hannafordb6927c12014-11-03 10:31:26 +01001package nodes
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01002
3import (
4 "fmt"
Jamie Hannaford16bebfc2014-11-03 12:52:30 +01005 "strconv"
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01006
Jamie Hannaforded8b89a2014-11-03 12:24:19 +01007 "github.com/racker/perigee"
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
12func List(client *gophercloud.ServiceClient, loadBalancerID int, limit *int) pagination.Pager {
13 url := rootURL(client, loadBalancerID)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010014 if limit != nil {
15 url += fmt.Sprintf("?limit=%d", limit)
16 }
17
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010018 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010019 return NodePage{pagination.SinglePageBase(r)}
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010020 })
21}
22
23type CreateOptsBuilder interface {
24 ToNodeCreateMap() (map[string]interface{}, error)
25}
26
27type CreateOpts []CreateOpt
28
29type CreateOpt struct {
30 // Required
31 Address string
32 Port int
33 Condition Condition
34 Type Type
35}
36
37func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
38 type nodeMap map[string]interface{}
39 nodes := []nodeMap{}
40
41 for k, v := range opts {
42 if v.Address == "" {
43 return nodeMap{}, fmt.Errorf("ID is a required attribute, none provided for %d CreateOpt element", k)
44 }
45
46 node := make(map[string]interface{})
47 node["address"] = v.Address
48
49 if v.Port > 0 {
50 node["port"] = v.Port
51 }
52 if v.Condition != "" {
53 node["condition"] = v.Condition
54 }
55 if v.Type != "" {
56 node["type"] = v.Type
57 }
58
59 nodes = append(nodes, node)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010060 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010061
62 return nodeMap{"nodes": nodes}, nil
63}
64
65func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
66 var res CreateResult
67
68 reqBody, err := opts.ToNodeCreateMap()
69 if err != nil {
70 res.Err = err
71 return res
72 }
73
74 resp, err := perigee.Request("POST", rootURL(client, loadBalancerID), perigee.Options{
75 MoreHeaders: client.AuthenticatedHeaders(),
76 ReqBody: &reqBody,
77 Results: &res.Body,
78 OkCodes: []int{200},
79 })
80 if err != nil {
81 res.Err = err
82 return res
83 }
84
85 pr, err := pagination.PageResultFrom(resp.HttpResponse)
86 if err != nil {
87 res.Err = err
88 return res
89 }
90
91 return CreateResult{pagination.SinglePageBase(pr)}
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010092}
Jamie Hannaford16bebfc2014-11-03 12:52:30 +010093
94func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult {
95 var res DeleteResult
96
97 url := rootURL(c, loadBalancerID)
98 for k, v := range nodeIDs {
99 if k == 0 {
100 url += "?"
101 } else {
102 url += "&"
103 }
104 url += "id=" + strconv.Itoa(v)
105 }
106
107 _, res.Err = perigee.Request("DELETE", url, perigee.Options{
108 MoreHeaders: c.AuthenticatedHeaders(),
109 OkCodes: []int{202},
110 })
111
112 return res
113}