blob: f3f2850ab3f5dc20beb3654d8388645fe8af9938 [file] [log] [blame]
Jamie Hannafordb6927c12014-11-03 10:31:26 +01001package nodes
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01002
3import (
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +01004 "errors"
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01005 "fmt"
6
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"
Jamie Hannaford940159d2014-11-03 13:04:08 +010010 "github.com/rackspace/gophercloud/rackspace/lb/v1"
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010011)
12
13func List(client *gophercloud.ServiceClient, loadBalancerID int, limit *int) pagination.Pager {
14 url := rootURL(client, loadBalancerID)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010015 if limit != nil {
16 url += fmt.Sprintf("?limit=%d", limit)
17 }
18
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010019 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010020 return NodePage{pagination.SinglePageBase(r)}
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010021 })
22}
23
24type CreateOptsBuilder interface {
25 ToNodeCreateMap() (map[string]interface{}, error)
26}
27
28type CreateOpts []CreateOpt
29
30type CreateOpt struct {
31 // Required
32 Address string
33 Port int
34 Condition Condition
35 Type Type
Jamie Hannaford00222d72014-11-03 13:58:52 +010036 Weight *int
37}
38
39func validateWeight(weight *int) error {
40 if weight != nil && (*weight > 100 || *weight < 0) {
41 return errors.New("Weight must be a valid int between 0 and 100")
42 }
43 return nil
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010044}
45
46func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
47 type nodeMap map[string]interface{}
48 nodes := []nodeMap{}
49
50 for k, v := range opts {
51 if v.Address == "" {
52 return nodeMap{}, fmt.Errorf("ID is a required attribute, none provided for %d CreateOpt element", k)
53 }
Jamie Hannaford00222d72014-11-03 13:58:52 +010054 if weightErr := validateWeight(v.Weight); weightErr != nil {
55 return nodeMap{}, weightErr
56 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010057
58 node := make(map[string]interface{})
59 node["address"] = v.Address
60
61 if v.Port > 0 {
62 node["port"] = v.Port
63 }
64 if v.Condition != "" {
65 node["condition"] = v.Condition
66 }
67 if v.Type != "" {
68 node["type"] = v.Type
69 }
Jamie Hannaford00222d72014-11-03 13:58:52 +010070 if v.Weight != nil {
71 node["weight"] = &v.Weight
72 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010073
74 nodes = append(nodes, node)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010075 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010076
77 return nodeMap{"nodes": nodes}, nil
78}
79
80func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
81 var res CreateResult
82
83 reqBody, err := opts.ToNodeCreateMap()
84 if err != nil {
85 res.Err = err
86 return res
87 }
88
89 resp, err := perigee.Request("POST", rootURL(client, loadBalancerID), perigee.Options{
90 MoreHeaders: client.AuthenticatedHeaders(),
91 ReqBody: &reqBody,
92 Results: &res.Body,
93 OkCodes: []int{200},
94 })
95 if err != nil {
96 res.Err = err
97 return res
98 }
99
100 pr, err := pagination.PageResultFrom(resp.HttpResponse)
101 if err != nil {
102 res.Err = err
103 return res
104 }
105
106 return CreateResult{pagination.SinglePageBase(pr)}
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +0100107}
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100108
109func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult {
110 var res DeleteResult
111
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +0100112 if len(nodeIDs) > 10 || len(nodeIDs) == 0 {
113 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 node IDs")
114 return res
115 }
116
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100117 url := rootURL(c, loadBalancerID)
Jamie Hannaford940159d2014-11-03 13:04:08 +0100118 url += v1.IDSliceToQueryString("id", nodeIDs)
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100119
120 _, res.Err = perigee.Request("DELETE", url, perigee.Options{
121 MoreHeaders: c.AuthenticatedHeaders(),
122 OkCodes: []int{202},
123 })
124
125 return res
126}
Jamie Hannaford51175a02014-11-03 13:29:44 +0100127
128func Get(c *gophercloud.ServiceClient, lbID, nodeID int) GetResult {
129 var res GetResult
130
131 _, res.Err = perigee.Request("GET", resourceURL(c, lbID, nodeID), perigee.Options{
132 MoreHeaders: c.AuthenticatedHeaders(),
133 Results: &res.Body,
134 OkCodes: []int{200},
135 })
136
137 return res
138}
Jamie Hannaford00222d72014-11-03 13:58:52 +0100139
140func IntToPointer(i int) *int {
141 return &i
142}
143
144type UpdateOptsBuilder interface {
145 ToNodeUpdateMap() (map[string]interface{}, error)
146}
147
148type UpdateOpts struct {
149 Address string
150 Condition Condition
151 Weight *int
152 Type Type
153}
154
155func (opts UpdateOpts) ToNodeUpdateMap() (map[string]interface{}, error) {
156 node := make(map[string]interface{})
157
158 if opts.Address != "" {
159 node["address"] = opts.Address
160 }
161 if opts.Condition != "" {
162 node["condition"] = opts.Condition
163 }
164 if opts.Weight != nil {
165 if weightErr := validateWeight(opts.Weight); weightErr != nil {
166 return node, weightErr
167 }
168 node["weight"] = &opts.Weight
169 }
170 if opts.Type != "" {
171 node["type"] = opts.Type
172 }
173
174 return map[string]interface{}{"node": node}, nil
175}
176
177func Update(c *gophercloud.ServiceClient, lbID, nodeID int, opts UpdateOptsBuilder) UpdateResult {
178 var res UpdateResult
179
180 reqBody, err := opts.ToNodeUpdateMap()
181 if err != nil {
182 res.Err = err
183 return res
184 }
185
186 _, res.Err = perigee.Request("PUT", resourceURL(c, lbID, nodeID), perigee.Options{
187 MoreHeaders: c.AuthenticatedHeaders(),
188 ReqBody: &reqBody,
189 OkCodes: []int{202},
190 })
191
192 return res
193}
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100194
195func Delete(c *gophercloud.ServiceClient, lbID, nodeID int) DeleteResult {
196 var res DeleteResult
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100197 _, res.Err = perigee.Request("DELETE", resourceURL(c, lbID, nodeID), perigee.Options{
198 MoreHeaders: c.AuthenticatedHeaders(),
199 OkCodes: []int{200},
200 })
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100201 return res
202}
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100203
204func ListEvents(client *gophercloud.ServiceClient, loadBalancerID, nodeID int) pagination.Pager {
205 url := eventsURL(client, loadBalancerID, nodeID)
206 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
207 return NodeEventPage{pagination.SinglePageBase(r)}
208 })
209}