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 ( |
| 4 | "fmt" |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame^] | 5 | "strconv" |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 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" |
| 10 | ) |
| 11 | |
| 12 | func List(client *gophercloud.ServiceClient, loadBalancerID int, limit *int) pagination.Pager { |
| 13 | url := rootURL(client, loadBalancerID) |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 14 | if limit != nil { |
| 15 | url += fmt.Sprintf("?limit=%d", limit) |
| 16 | } |
| 17 | |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 18 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 19 | return NodePage{pagination.SinglePageBase(r)} |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 20 | }) |
| 21 | } |
| 22 | |
| 23 | type CreateOptsBuilder interface { |
| 24 | ToNodeCreateMap() (map[string]interface{}, error) |
| 25 | } |
| 26 | |
| 27 | type CreateOpts []CreateOpt |
| 28 | |
| 29 | type CreateOpt struct { |
| 30 | // Required |
| 31 | Address string |
| 32 | Port int |
| 33 | Condition Condition |
| 34 | Type Type |
| 35 | } |
| 36 | |
| 37 | func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) { |
| 38 | type nodeMap map[string]interface{} |
| 39 | nodes := []nodeMap{} |
| 40 | |
| 41 | for k, v := range opts { |
| 42 | if v.Address == "" { |
| 43 | return nodeMap{}, fmt.Errorf("ID is a required attribute, none provided for %d CreateOpt element", k) |
| 44 | } |
| 45 | |
| 46 | node := make(map[string]interface{}) |
| 47 | node["address"] = v.Address |
| 48 | |
| 49 | if v.Port > 0 { |
| 50 | node["port"] = v.Port |
| 51 | } |
| 52 | if v.Condition != "" { |
| 53 | node["condition"] = v.Condition |
| 54 | } |
| 55 | if v.Type != "" { |
| 56 | node["type"] = v.Type |
| 57 | } |
| 58 | |
| 59 | nodes = append(nodes, node) |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 60 | } |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 61 | |
| 62 | return nodeMap{"nodes": nodes}, nil |
| 63 | } |
| 64 | |
| 65 | func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult { |
| 66 | var res CreateResult |
| 67 | |
| 68 | reqBody, err := opts.ToNodeCreateMap() |
| 69 | if err != nil { |
| 70 | res.Err = err |
| 71 | return res |
| 72 | } |
| 73 | |
| 74 | resp, err := perigee.Request("POST", rootURL(client, loadBalancerID), perigee.Options{ |
| 75 | MoreHeaders: client.AuthenticatedHeaders(), |
| 76 | ReqBody: &reqBody, |
| 77 | Results: &res.Body, |
| 78 | OkCodes: []int{200}, |
| 79 | }) |
| 80 | if err != nil { |
| 81 | res.Err = err |
| 82 | return res |
| 83 | } |
| 84 | |
| 85 | pr, err := pagination.PageResultFrom(resp.HttpResponse) |
| 86 | if err != nil { |
| 87 | res.Err = err |
| 88 | return res |
| 89 | } |
| 90 | |
| 91 | return CreateResult{pagination.SinglePageBase(pr)} |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 92 | } |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame^] | 93 | |
| 94 | func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, nodeIDs []int) DeleteResult { |
| 95 | var res DeleteResult |
| 96 | |
| 97 | url := rootURL(c, loadBalancerID) |
| 98 | for k, v := range nodeIDs { |
| 99 | if k == 0 { |
| 100 | url += "?" |
| 101 | } else { |
| 102 | url += "&" |
| 103 | } |
| 104 | url += "id=" + strconv.Itoa(v) |
| 105 | } |
| 106 | |
| 107 | _, res.Err = perigee.Request("DELETE", url, perigee.Options{ |
| 108 | MoreHeaders: c.AuthenticatedHeaders(), |
| 109 | OkCodes: []int{202}, |
| 110 | }) |
| 111 | |
| 112 | return res |
| 113 | } |