Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 1 | package lbpools |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | ) |
| 7 | |
| 8 | // List returns all load balancer pools that are associated with RackConnect. |
| 9 | func List(c *gophercloud.ServiceClient) pagination.Pager { |
| 10 | url := listURL(c) |
| 11 | createPage := func(r pagination.PageResult) pagination.Page { |
| 12 | return PoolPage{pagination.SinglePageBase(r)} |
| 13 | } |
| 14 | return pagination.NewPager(c, url, createPage) |
| 15 | } |
| 16 | |
| 17 | // Get retrieves a specific load balancer pool (that is associated with RackConnect) |
| 18 | // based on its unique ID. |
| 19 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 20 | var res GetResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 21 | _, res.Err = c.Get(getURL(c, id), &res.Body, nil) |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 22 | return res |
| 23 | } |
| 24 | |
| 25 | // ListNodes returns all load balancer pool nodes that are associated with RackConnect |
| 26 | // for the given LB pool ID. |
| 27 | func ListNodes(c *gophercloud.ServiceClient, id string) pagination.Pager { |
| 28 | url := listNodesURL(c, id) |
| 29 | createPage := func(r pagination.PageResult) pagination.Page { |
| 30 | return NodePage{pagination.SinglePageBase(r)} |
| 31 | } |
| 32 | return pagination.NewPager(c, url, createPage) |
| 33 | } |
| 34 | |
| 35 | // CreateNode adds the cloud server with the given serverID to the load balancer |
| 36 | // pool with the given poolID. |
| 37 | func CreateNode(c *gophercloud.ServiceClient, poolID, serverID string) CreateNodeResult { |
| 38 | var res CreateNodeResult |
| 39 | reqBody := map[string]interface{}{ |
| 40 | "cloud_server": map[string]string{ |
| 41 | "id": serverID, |
| 42 | }, |
| 43 | } |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 44 | _, res.Err = c.Post(createNodeURL(c, poolID), reqBody, &res.Body, nil) |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 45 | return res |
| 46 | } |
| 47 | |
| 48 | // ListNodesDetails returns all load balancer pool nodes that are associated with RackConnect |
| 49 | // for the given LB pool ID with all their details. |
| 50 | func ListNodesDetails(c *gophercloud.ServiceClient, id string) pagination.Pager { |
| 51 | url := listNodesDetailsURL(c, id) |
| 52 | createPage := func(r pagination.PageResult) pagination.Page { |
| 53 | return NodeDetailsPage{pagination.SinglePageBase(r)} |
| 54 | } |
| 55 | return pagination.NewPager(c, url, createPage) |
| 56 | } |
| 57 | |
| 58 | // GetNode retrieves a specific LB pool node (that is associated with RackConnect) |
| 59 | // based on its unique ID and the LB pool's unique ID. |
| 60 | func GetNode(c *gophercloud.ServiceClient, poolID, nodeID string) GetNodeResult { |
| 61 | var res GetNodeResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 62 | _, res.Err = c.Get(nodeURL(c, poolID, nodeID), &res.Body, nil) |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 63 | return res |
| 64 | } |
| 65 | |
| 66 | // DeleteNode removes the node with the given nodeID from the LB pool with the |
| 67 | // given poolID. |
| 68 | func DeleteNode(c *gophercloud.ServiceClient, poolID, nodeID string) DeleteNodeResult { |
| 69 | var res DeleteNodeResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 70 | _, res.Err = c.Delete(deleteNodeURL(c, poolID, nodeID), nil) |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 71 | return res |
| 72 | } |
| 73 | |
| 74 | // GetNodeDetails retrieves a specific LB pool node's details based on its unique |
| 75 | // ID and the LB pool's unique ID. |
| 76 | func GetNodeDetails(c *gophercloud.ServiceClient, poolID, nodeID string) GetNodeDetailsResult { |
| 77 | var res GetNodeDetailsResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 78 | _, res.Err = c.Get(nodeDetailsURL(c, poolID, nodeID), &res.Body, nil) |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 79 | return res |
| 80 | } |
| 81 | |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 82 | // NodeOpts are options for bulk adding/deleting nodes to LB pools. |
| 83 | type NodeOpts struct { |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 84 | ServerID string |
| 85 | PoolID string |
| 86 | } |
| 87 | |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 88 | // NodesOpts are a slice of NodeOpts, passed as options for bulk operations. |
| 89 | type NodesOpts []NodeOpts |
| 90 | |
| 91 | // ToLBPoolCreateNodesMap serializes a NodesOpts into a map to send in the request. |
| 92 | func (o NodesOpts) ToLBPoolCreateNodesMap() ([]map[string]interface{}, error) { |
| 93 | m := make([]map[string]interface{}, len(o)) |
| 94 | for i := range o { |
| 95 | m[i] = map[string]interface{}{ |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 96 | "cloud_server": map[string]string{ |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 97 | "id": o[i].ServerID, |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 98 | }, |
| 99 | "load_balancer_pool": map[string]string{ |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 100 | "id": o[i].PoolID, |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 101 | }, |
| 102 | } |
| 103 | } |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 104 | return m, nil |
| 105 | } |
| 106 | |
| 107 | // CreateNodes adds the cloud servers with the given serverIDs to the corresponding |
| 108 | // load balancer pools with the given poolIDs. |
| 109 | func CreateNodes(c *gophercloud.ServiceClient, opts NodesOpts) CreateNodesResult { |
| 110 | var res CreateNodesResult |
| 111 | reqBody, err := opts.ToLBPoolCreateNodesMap() |
| 112 | if err != nil { |
| 113 | res.Err = err |
| 114 | return res |
| 115 | } |
| 116 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 117 | _, res.Err = c.Post(createNodesURL(c), reqBody, &res.Body, nil) |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 118 | return res |
| 119 | } |
| 120 | |
| 121 | // DeleteNodes removes the cloud servers with the given serverIDs to the corresponding |
| 122 | // load balancer pools with the given poolIDs. |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 123 | func DeleteNodes(c *gophercloud.ServiceClient, opts NodesOpts) DeleteNodesResult { |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 124 | var res DeleteNodesResult |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 125 | reqBody, err := opts.ToLBPoolCreateNodesMap() |
| 126 | if err != nil { |
| 127 | res.Err = err |
| 128 | return res |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 129 | } |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 130 | |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 131 | _, res.Err = c.Request("DELETE", createNodesURL(c), gophercloud.RequestOpts{ |
| 132 | JSONBody: &reqBody, |
| 133 | OkCodes: []int{204}, |
| 134 | }) |
| 135 | return res |
| 136 | } |
| 137 | |
Jon Perritt | aa24499 | 2015-03-18 09:42:24 -0600 | [diff] [blame] | 138 | // ListNodesDetailsForServer is similar to ListNodesDetails but only returns nodes |
| 139 | // for the given serverID. |
Jon Perritt | 9f8b015 | 2015-03-17 19:28:18 -0600 | [diff] [blame] | 140 | func ListNodesDetailsForServer(c *gophercloud.ServiceClient, serverID string) pagination.Pager { |
| 141 | url := listNodesForServerURL(c, serverID) |
| 142 | createPage := func(r pagination.PageResult) pagination.Page { |
| 143 | return NodeDetailsForServerPage{pagination.SinglePageBase(r)} |
| 144 | } |
| 145 | return pagination.NewPager(c, url, createPage) |
| 146 | } |