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" |
| 5 | |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame^] | 6 | "github.com/racker/perigee" |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | func List(client *gophercloud.ServiceClient, loadBalancerID int, limit *int) pagination.Pager { |
| 12 | url := rootURL(client, loadBalancerID) |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 13 | if limit != nil { |
| 14 | url += fmt.Sprintf("?limit=%d", limit) |
| 15 | } |
| 16 | |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame^] | 17 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 18 | return NodePage{pagination.SinglePageBase(r)} |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame^] | 19 | }) |
| 20 | } |
| 21 | |
| 22 | type CreateOptsBuilder interface { |
| 23 | ToNodeCreateMap() (map[string]interface{}, error) |
| 24 | } |
| 25 | |
| 26 | type CreateOpts []CreateOpt |
| 27 | |
| 28 | type CreateOpt struct { |
| 29 | // Required |
| 30 | Address string |
| 31 | Port int |
| 32 | Condition Condition |
| 33 | Type Type |
| 34 | } |
| 35 | |
| 36 | func (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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 59 | } |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame^] | 60 | |
| 61 | return nodeMap{"nodes": nodes}, nil |
| 62 | } |
| 63 | |
| 64 | func 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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 91 | } |