blob: 0ce8ca13517d760a40ab34d3b8b3ce63269fd9b1 [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
36}
37
38func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
39 type nodeMap map[string]interface{}
40 nodes := []nodeMap{}
41
42 for k, v := range opts {
43 if v.Address == "" {
44 return nodeMap{}, fmt.Errorf("ID is a required attribute, none provided for %d CreateOpt element", k)
45 }
46
47 node := make(map[string]interface{})
48 node["address"] = v.Address
49
50 if v.Port > 0 {
51 node["port"] = v.Port
52 }
53 if v.Condition != "" {
54 node["condition"] = v.Condition
55 }
56 if v.Type != "" {
57 node["type"] = v.Type
58 }
59
60 nodes = append(nodes, node)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010061 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010062
63 return nodeMap{"nodes": nodes}, nil
64}
65
66func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
67 var res CreateResult
68
69 reqBody, err := opts.ToNodeCreateMap()
70 if err != nil {
71 res.Err = err
72 return res
73 }
74
75 resp, err := perigee.Request("POST", rootURL(client, loadBalancerID), perigee.Options{
76 MoreHeaders: client.AuthenticatedHeaders(),
77 ReqBody: &reqBody,
78 Results: &res.Body,
79 OkCodes: []int{200},
80 })
81 if err != nil {
82 res.Err = err
83 return res
84 }
85
86 pr, err := pagination.PageResultFrom(resp.HttpResponse)
87 if err != nil {
88 res.Err = err
89 return res
90 }
91
92 return CreateResult{pagination.SinglePageBase(pr)}
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010093}
Jamie Hannaford16bebfc2014-11-03 12:52:30 +010094
95func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult {
96 var res DeleteResult
97
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +010098 if len(nodeIDs) > 10 || len(nodeIDs) == 0 {
99 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 node IDs")
100 return res
101 }
102
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100103 url := rootURL(c, loadBalancerID)
Jamie Hannaford940159d2014-11-03 13:04:08 +0100104 url += v1.IDSliceToQueryString("id", nodeIDs)
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100105
106 _, res.Err = perigee.Request("DELETE", url, perigee.Options{
107 MoreHeaders: c.AuthenticatedHeaders(),
108 OkCodes: []int{202},
109 })
110
111 return res
112}
Jamie Hannaford51175a02014-11-03 13:29:44 +0100113
114func Get(c *gophercloud.ServiceClient, lbID, nodeID int) GetResult {
115 var res GetResult
116
117 _, res.Err = perigee.Request("GET", resourceURL(c, lbID, nodeID), perigee.Options{
118 MoreHeaders: c.AuthenticatedHeaders(),
119 Results: &res.Body,
120 OkCodes: []int{200},
121 })
122
123 return res
124}