blob: b33d33949a48a319b3a0b58335834c3d6209c1ca [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 Wilsonfe80dae2015-06-24 10:12:01 -0400122 pr := pagination.PageResult{
123 Result: gophercloud.Result{
124 Body: res.Body,
125 Header: resp.Header,
126 },
127 URL: *resp.Request.URL,
Jamie Hannaforded8b89a2014-11-03 12:24:19 +0100128 }
129
130 return CreateResult{pagination.SinglePageBase(pr)}
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +0100131}
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100132
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100133// BulkDelete is the operation responsible for batch deleting multiple nodes in
134// a single operation. It accepts a slice of integer IDs and will remove them
135// from the load balancer. The maximum limit is 10 node removals at once.
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100136func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult {
137 var res DeleteResult
138
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +0100139 if len(nodeIDs) > 10 || len(nodeIDs) == 0 {
140 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 node IDs")
141 return res
142 }
143
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100144 url := rootURL(c, loadBalancerID)
Jamie Hannaford950561c2014-11-12 11:12:20 +0100145 url += gophercloud.IDSliceToQueryString("id", nodeIDs)
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100146
Jamie Hannaford5497f942015-03-25 11:55:51 +0100147 _, res.Err = c.Delete(url, nil)
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100148 return res
149}
Jamie Hannaford51175a02014-11-03 13:29:44 +0100150
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100151// Get is the operation responsible for showing details for a single node.
Jamie Hannaford51175a02014-11-03 13:29:44 +0100152func Get(c *gophercloud.ServiceClient, lbID, nodeID int) GetResult {
153 var res GetResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100154 _, res.Err = c.Get(resourceURL(c, lbID, nodeID), &res.Body, nil)
Jamie Hannaford51175a02014-11-03 13:29:44 +0100155 return res
156}
Jamie Hannaford00222d72014-11-03 13:58:52 +0100157
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100158// UpdateOptsBuilder represents a type that can be converted into a JSON-like
159// map structure.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100160type UpdateOptsBuilder interface {
161 ToNodeUpdateMap() (map[string]interface{}, error)
162}
163
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100164// UpdateOpts represent the options for updating an existing node.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100165type UpdateOpts struct {
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100166 // Optional - the condition of the node. See the consts in Results.go.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100167 Condition Condition
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100168
169 // Optional - the type of the node. See the consts in Results.go.
170 Type Type
171
172 // Optional - a pointer to an integer between 0 and 100.
173 Weight *int
Jamie Hannaford00222d72014-11-03 13:58:52 +0100174}
175
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100176// ToNodeUpdateMap converts an options struct into a JSON-like map.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100177func (opts UpdateOpts) ToNodeUpdateMap() (map[string]interface{}, error) {
178 node := make(map[string]interface{})
179
Jamie Hannaford00222d72014-11-03 13:58:52 +0100180 if opts.Condition != "" {
181 node["condition"] = opts.Condition
182 }
183 if opts.Weight != nil {
184 if weightErr := validateWeight(opts.Weight); weightErr != nil {
185 return node, weightErr
186 }
187 node["weight"] = &opts.Weight
188 }
189 if opts.Type != "" {
190 node["type"] = opts.Type
191 }
192
193 return map[string]interface{}{"node": node}, nil
194}
195
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100196// Update is the operation responsible for updating an existing node. A node's
197// IP, port, and status are immutable attributes and cannot be modified.
Jamie Hannaford00222d72014-11-03 13:58:52 +0100198func Update(c *gophercloud.ServiceClient, lbID, nodeID int, opts UpdateOptsBuilder) UpdateResult {
199 var res UpdateResult
200
201 reqBody, err := opts.ToNodeUpdateMap()
202 if err != nil {
203 res.Err = err
204 return res
205 }
206
Jamie Hannaford5497f942015-03-25 11:55:51 +0100207 _, res.Err = c.Put(resourceURL(c, lbID, nodeID), reqBody, nil, nil)
Jamie Hannaford00222d72014-11-03 13:58:52 +0100208 return res
209}
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100210
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100211// Delete is the operation responsible for permanently deleting a node.
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100212func Delete(c *gophercloud.ServiceClient, lbID, nodeID int) DeleteResult {
213 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100214 _, res.Err = c.Delete(resourceURL(c, lbID, nodeID), nil)
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100215 return res
216}
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100217
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100218// ListEventsOptsBuilder allows extensions to add additional parameters to the
219// List request.
220type ListEventsOptsBuilder interface {
221 ToEventsListQuery() (string, error)
222}
223
224// ListEventsOpts allows the filtering and sorting of paginated collections through
225// the API.
226type ListEventsOpts struct {
227 Marker string `q:"marker"`
228 Limit int `q:"limit"`
229}
230
231// ToEventsListQuery formats a ListOpts into a query string.
232func (opts ListEventsOpts) ToEventsListQuery() (string, error) {
233 q, err := gophercloud.BuildQueryString(opts)
234 if err != nil {
235 return "", err
236 }
237 return q.String(), nil
238}
239
240// ListEvents is the operation responsible for listing all the events
241// associated with the activity between the node and the load balancer. The
242// events report errors found with the node. The detailedMessage provides the
243// detailed reason for the error.
Jamie Hannaford703527e2014-11-05 12:38:15 +0100244func ListEvents(client *gophercloud.ServiceClient, loadBalancerID int, opts ListEventsOptsBuilder) pagination.Pager {
245 url := eventsURL(client, loadBalancerID)
Jamie Hannaford8cdaa802014-11-03 15:11:39 +0100246
247 if opts != nil {
248 query, err := opts.ToEventsListQuery()
249 if err != nil {
250 return pagination.Pager{Err: err}
251 }
252 url += query
253 }
254
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100255 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
256 return NodeEventPage{pagination.SinglePageBase(r)}
257 })
258}