Jamie Hannaford | b6927c1 | 2014-11-03 10:31:26 +0100 | [diff] [blame] | 1 | package nodes |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 4 | "errors" |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 5 | "fmt" |
| 6 | |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 7 | "github.com/racker/perigee" |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 940159d | 2014-11-03 13:04:08 +0100 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/rackspace/lb/v1" |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | func List(client *gophercloud.ServiceClient, loadBalancerID int, limit *int) pagination.Pager { |
| 14 | url := rootURL(client, loadBalancerID) |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 15 | if limit != nil { |
| 16 | url += fmt.Sprintf("?limit=%d", limit) |
| 17 | } |
| 18 | |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 19 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 20 | return NodePage{pagination.SinglePageBase(r)} |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 21 | }) |
| 22 | } |
| 23 | |
| 24 | type CreateOptsBuilder interface { |
| 25 | ToNodeCreateMap() (map[string]interface{}, error) |
| 26 | } |
| 27 | |
| 28 | type CreateOpts []CreateOpt |
| 29 | |
| 30 | type CreateOpt struct { |
| 31 | // Required |
| 32 | Address string |
| 33 | Port int |
| 34 | Condition Condition |
| 35 | Type Type |
Jamie Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 36 | Weight *int |
| 37 | } |
| 38 | |
| 39 | func 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 Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func (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 Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 54 | if weightErr := validateWeight(v.Weight); weightErr != nil { |
| 55 | return nodeMap{}, weightErr |
| 56 | } |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 57 | |
| 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 Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 70 | if v.Weight != nil { |
| 71 | node["weight"] = &v.Weight |
| 72 | } |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 73 | |
| 74 | nodes = append(nodes, node) |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 75 | } |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 76 | |
| 77 | return nodeMap{"nodes": nodes}, nil |
| 78 | } |
| 79 | |
| 80 | func 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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 107 | } |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 108 | |
| 109 | func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult { |
| 110 | var res DeleteResult |
| 111 | |
Jamie Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 112 | 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 Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 117 | url := rootURL(c, loadBalancerID) |
Jamie Hannaford | 940159d | 2014-11-03 13:04:08 +0100 | [diff] [blame] | 118 | url += v1.IDSliceToQueryString("id", nodeIDs) |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 119 | |
| 120 | _, res.Err = perigee.Request("DELETE", url, perigee.Options{ |
| 121 | MoreHeaders: c.AuthenticatedHeaders(), |
| 122 | OkCodes: []int{202}, |
| 123 | }) |
| 124 | |
| 125 | return res |
| 126 | } |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 127 | |
| 128 | func 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 Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 139 | |
| 140 | func IntToPointer(i int) *int { |
| 141 | return &i |
| 142 | } |
| 143 | |
| 144 | type UpdateOptsBuilder interface { |
| 145 | ToNodeUpdateMap() (map[string]interface{}, error) |
| 146 | } |
| 147 | |
| 148 | type UpdateOpts struct { |
| 149 | Address string |
| 150 | Condition Condition |
| 151 | Weight *int |
| 152 | Type Type |
| 153 | } |
| 154 | |
| 155 | func (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 | |
| 177 | func 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 Hannaford | 9f4870f | 2014-11-03 14:03:16 +0100 | [diff] [blame] | 194 | |
| 195 | func Delete(c *gophercloud.ServiceClient, lbID, nodeID int) DeleteResult { |
| 196 | var res DeleteResult |
Jamie Hannaford | 9f4870f | 2014-11-03 14:03:16 +0100 | [diff] [blame] | 197 | _, res.Err = perigee.Request("DELETE", resourceURL(c, lbID, nodeID), perigee.Options{ |
| 198 | MoreHeaders: c.AuthenticatedHeaders(), |
| 199 | OkCodes: []int{200}, |
| 200 | }) |
Jamie Hannaford | 9f4870f | 2014-11-03 14:03:16 +0100 | [diff] [blame] | 201 | return res |
| 202 | } |
Jamie Hannaford | 1fac9dd | 2014-11-03 14:22:40 +0100 | [diff] [blame^] | 203 | |
| 204 | func 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 | } |