blob: 338c2508a762fdf0dc3fbc25c245082bedc7a356 [file] [log] [blame]
Jamie Hannafordb6927c12014-11-03 10:31:26 +01001package nodes
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01002
3import (
4 "fmt"
5
Jamie Hannaforded8b89a2014-11-03 12:24:19 +01006 "github.com/racker/perigee"
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11func List(client *gophercloud.ServiceClient, loadBalancerID int, limit *int) pagination.Pager {
12 url := rootURL(client, loadBalancerID)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010013 if limit != nil {
14 url += fmt.Sprintf("?limit=%d", limit)
15 }
16
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010017 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010018 return NodePage{pagination.SinglePageBase(r)}
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010019 })
20}
21
22type CreateOptsBuilder interface {
23 ToNodeCreateMap() (map[string]interface{}, error)
24}
25
26type CreateOpts []CreateOpt
27
28type CreateOpt struct {
29 // Required
30 Address string
31 Port int
32 Condition Condition
33 Type Type
34}
35
36func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
37 type nodeMap map[string]interface{}
38 nodes := []nodeMap{}
39
40 for k, v := range opts {
41 if v.Address == "" {
42 return nodeMap{}, fmt.Errorf("ID is a required attribute, none provided for %d CreateOpt element", k)
43 }
44
45 node := make(map[string]interface{})
46 node["address"] = v.Address
47
48 if v.Port > 0 {
49 node["port"] = v.Port
50 }
51 if v.Condition != "" {
52 node["condition"] = v.Condition
53 }
54 if v.Type != "" {
55 node["type"] = v.Type
56 }
57
58 nodes = append(nodes, node)
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010059 }
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010060
61 return nodeMap{"nodes": nodes}, nil
62}
63
64func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
65 var res CreateResult
66
67 reqBody, err := opts.ToNodeCreateMap()
68 if err != nil {
69 res.Err = err
70 return res
71 }
72
73 resp, err := perigee.Request("POST", rootURL(client, loadBalancerID), perigee.Options{
74 MoreHeaders: client.AuthenticatedHeaders(),
75 ReqBody: &reqBody,
76 Results: &res.Body,
77 OkCodes: []int{200},
78 })
79 if err != nil {
80 res.Err = err
81 return res
82 }
83
84 pr, err := pagination.PageResultFrom(resp.HttpResponse)
85 if err != nil {
86 res.Err = err
87 return res
88 }
89
90 return CreateResult{pagination.SinglePageBase(pr)}
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010091}