blob: 02af86b5c1e21db268e87739833567dc8bf3e205 [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
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010011// List is the operation responsible for returning a paginated collection of
12// load balancer nodes. It requires the node ID, its parent load balancer ID,
13// and optional limit integer (passed in either as a pointer or a nil poitner).
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010014func List(client *gophercloud.ServiceClient, loadBalancerID int, limit *int) pagination.Pager {
15 url := rootURL(client, loadBalancerID)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010016 if limit != nil {
17 url += fmt.Sprintf("?limit=%d", limit)
18 }
19
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010020 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010021 return NodePage{pagination.SinglePageBase(r)}
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010022 })
23}
24
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010025// CreateOptsBuilder is the interface responsible for generating the JSON
26// for a Create operation.
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010027type CreateOptsBuilder interface {
28 ToNodeCreateMap() (map[string]interface{}, error)
29}
30
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010031// CreateOpts is a slice of CreateOpt structs, that allow the user to create
32// multiple nodes in a single operation (one node per CreateOpt).
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010033type CreateOpts []CreateOpt
34
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010035// CreateOpt represents the options to create a single node.
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010036type CreateOpt struct {
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010037 // Required - the IP address or CIDR for this back-end node. It can either be
38 // a private IP (ServiceNet) or a public IP.
39 Address string
40
41 // Optional - the port on which traffic is sent and received.
42 Port int
43
44 // Optional - the condition of the node. See the consts in Results.go.
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010045 Condition Condition
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010046
47 // Optional - the type of the node. See the consts in Results.go.
48 Type Type
49
50 // Optional - a pointer to an integer between 0 and 100.
51 Weight *int
Jamie Hannaford00222d72014-11-03 13:58:52 +010052}
53
54func validateWeight(weight *int) error {
55 if weight != nil && (*weight > 100 || *weight < 0) {
56 return errors.New("Weight must be a valid int between 0 and 100")
57 }
58 return nil
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010059}
60
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010061// ToNodeCreateMap converts a slice of options into a map that can be used for
62// the JSON.
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010063func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
64 type nodeMap map[string]interface{}
65 nodes := []nodeMap{}
66
67 for k, v := range opts {
68 if v.Address == "" {
69 return nodeMap{}, fmt.Errorf("ID is a required attribute, none provided for %d CreateOpt element", k)
70 }
Jamie Hannaford00222d72014-11-03 13:58:52 +010071 if weightErr := validateWeight(v.Weight); weightErr != nil {
72 return nodeMap{}, weightErr
73 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010074
75 node := make(map[string]interface{})
76 node["address"] = v.Address
77
78 if v.Port > 0 {
79 node["port"] = v.Port
80 }
81 if v.Condition != "" {
82 node["condition"] = v.Condition
83 }
84 if v.Type != "" {
85 node["type"] = v.Type
86 }
Jamie Hannaford00222d72014-11-03 13:58:52 +010087 if v.Weight != nil {
88 node["weight"] = &v.Weight
89 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010090
91 nodes = append(nodes, node)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010092 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010093
94 return nodeMap{"nodes": nodes}, nil
95}
96
Jamie Hannaford8cdaa802014-11-03 15:11:39 +010097// Create is the operation responsible for creating a new node on a load
98// balancer. Since every load balancer exists in both ServiceNet and the public
99// Internet, both private and public IP addresses can be used for nodes.
100//
101// If nodes need time to boot up services before they become operational, you
102// can temporarily prevent traffic from being sent to that node by setting the
103// Condition field to DRAINING. Health checks will still be performed; but once
104// your node is ready, you can update its condition to ENABLED and have it
105// handle traffic.
Jamie Hannaforded8b89a2014-11-03 12:24:19 +0100106func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
107 var res CreateResult
108
109 reqBody, err := opts.ToNodeCreateMap()
110 if err != nil {
111 res.Err = err
112 return res
113 }
114
Jamie Hannaford5497f942015-03-25 11:55:51 +0100115 resp, err := client.Post(rootURL(client, loadBalancerID), reqBody, &res.Body, nil)
116
Jamie Hannaforded8b89a2014-11-03 12:24:19 +0100117 if err != nil {
118 res.Err = err
119 return res
120 }
121
Ash Wilson59fb6c42015-02-12 16:21:13 -0500122 pr, err := pagination.PageResultFrom(resp)
Jamie Hannaforded8b89a2014-11-03 12:24:19 +0100123 if err != nil {
124 res.Err = err
125 return res
126 }
127
128 return CreateResult{pagination.SinglePageBase(pr)}
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +0100129}
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100130
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100131// BulkDelete is the operation responsible for batch deleting multiple nodes in
132// a single operation. It accepts a slice of integer IDs and will remove them
133// from the load balancer. The maximum limit is 10 node removals at once.
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100134func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult {
135 var res DeleteResult
136
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +0100137 if len(nodeIDs) > 10 || len(nodeIDs) == 0 {
138 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 node IDs")
139 return res
140 }
141
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100142 url := rootURL(c, loadBalancerID)
Jamie Hannaford950561c2014-11-12 11:12:20 +0100143 url += gophercloud.IDSliceToQueryString("id", nodeIDs)
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100144
Jamie Hannaford5497f942015-03-25 11:55:51 +0100145 _, res.Err = c.Delete(url, nil)
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100146 return res
147}
Jamie Hannaford51175a02014-11-03 13:29:44 +0100148
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100149// Get is the operation responsible for showing details for a single node.
Jamie Hannaford51175a02014-11-03 13:29:44 +0100150func Get(c *gophercloud.ServiceClient, lbID, nodeID int) GetResult {
151 var res GetResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100152 _, res.Err = c.Get(resourceURL(c, lbID, nodeID), &res.Body, nil)
Jamie Hannaford51175a02014-11-03 13:29:44 +0100153 return res
154}
Jamie Hannaford00222d72014-11-03 13:58:52 +0100155
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100156// UpdateOptsBuilder represents a type that can be converted into a JSON-like
157// map structure.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100158type UpdateOptsBuilder interface {
159 ToNodeUpdateMap() (map[string]interface{}, error)
160}
161
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100162// UpdateOpts represent the options for updating an existing node.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100163type UpdateOpts struct {
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100164 // Optional - the condition of the node. See the consts in Results.go.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100165 Condition Condition
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100166
167 // Optional - the type of the node. See the consts in Results.go.
168 Type Type
169
170 // Optional - a pointer to an integer between 0 and 100.
171 Weight *int
Jamie Hannaford00222d72014-11-03 13:58:52 +0100172}
173
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100174// ToNodeUpdateMap converts an options struct into a JSON-like map.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100175func (opts UpdateOpts) ToNodeUpdateMap() (map[string]interface{}, error) {
176 node := make(map[string]interface{})
177
Jamie Hannaford00222d72014-11-03 13:58:52 +0100178 if opts.Condition != "" {
179 node["condition"] = opts.Condition
180 }
181 if opts.Weight != nil {
182 if weightErr := validateWeight(opts.Weight); weightErr != nil {
183 return node, weightErr
184 }
185 node["weight"] = &opts.Weight
186 }
187 if opts.Type != "" {
188 node["type"] = opts.Type
189 }
190
191 return map[string]interface{}{"node": node}, nil
192}
193
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100194// Update is the operation responsible for updating an existing node. A node's
195// IP, port, and status are immutable attributes and cannot be modified.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100196func Update(c *gophercloud.ServiceClient, lbID, nodeID int, opts UpdateOptsBuilder) UpdateResult {
197 var res UpdateResult
198
199 reqBody, err := opts.ToNodeUpdateMap()
200 if err != nil {
201 res.Err = err
202 return res
203 }
204
Jamie Hannaford5497f942015-03-25 11:55:51 +0100205 _, res.Err = c.Put(resourceURL(c, lbID, nodeID), reqBody, nil, nil)
Jamie Hannaford00222d72014-11-03 13:58:52 +0100206 return res
207}
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100208
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100209// Delete is the operation responsible for permanently deleting a node.
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100210func Delete(c *gophercloud.ServiceClient, lbID, nodeID int) DeleteResult {
211 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100212 _, res.Err = c.Delete(resourceURL(c, lbID, nodeID), nil)
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100213 return res
214}
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100215
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100216// ListEventsOptsBuilder allows extensions to add additional parameters to the
217// List request.
218type ListEventsOptsBuilder interface {
219 ToEventsListQuery() (string, error)
220}
221
222// ListEventsOpts allows the filtering and sorting of paginated collections through
223// the API.
224type ListEventsOpts struct {
225 Marker string `q:"marker"`
226 Limit int `q:"limit"`
227}
228
229// ToEventsListQuery formats a ListOpts into a query string.
230func (opts ListEventsOpts) ToEventsListQuery() (string, error) {
231 q, err := gophercloud.BuildQueryString(opts)
232 if err != nil {
233 return "", err
234 }
235 return q.String(), nil
236}
237
238// ListEvents is the operation responsible for listing all the events
239// associated with the activity between the node and the load balancer. The
240// events report errors found with the node. The detailedMessage provides the
241// detailed reason for the error.
Jamie Hannaford703527e2014-11-05 12:38:15 +0100242func ListEvents(client *gophercloud.ServiceClient, loadBalancerID int, opts ListEventsOptsBuilder) pagination.Pager {
243 url := eventsURL(client, loadBalancerID)
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100244
245 if opts != nil {
246 query, err := opts.ToEventsListQuery()
247 if err != nil {
248 return pagination.Pager{Err: err}
249 }
250 url += query
251 }
252
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100253 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
254 return NodeEventPage{pagination.SinglePageBase(r)}
255 })
256}