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