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 |
| 36 | } |
| 37 | |
| 38 | func (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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 61 | } |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 62 | |
| 63 | return nodeMap{"nodes": nodes}, nil |
| 64 | } |
| 65 | |
| 66 | func 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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 93 | } |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 94 | |
| 95 | func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult { |
| 96 | var res DeleteResult |
| 97 | |
Jamie Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 98 | 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 Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 103 | url := rootURL(c, loadBalancerID) |
Jamie Hannaford | 940159d | 2014-11-03 13:04:08 +0100 | [diff] [blame] | 104 | url += v1.IDSliceToQueryString("id", nodeIDs) |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 105 | |
| 106 | _, res.Err = perigee.Request("DELETE", url, perigee.Options{ |
| 107 | MoreHeaders: c.AuthenticatedHeaders(), |
| 108 | OkCodes: []int{202}, |
| 109 | }) |
| 110 | |
| 111 | return res |
| 112 | } |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame^] | 113 | |
| 114 | func 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 | } |