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 | |
| 97 | // NodesOpts are options for bulk adding/deleting nodes to LB pools. |
| 98 | type NodesOpts struct { |
| 99 | ServerID string |
| 100 | PoolID string |
| 101 | } |
| 102 | |
| 103 | // CreateNodes adds the cloud servers with the given serverIDs to the corresponding |
| 104 | // load balancer pools with the given poolIDs. |
| 105 | func CreateNodes(c *gophercloud.ServiceClient, opts []NodesOpts) CreateNodesResult { |
| 106 | var res CreateNodesResult |
| 107 | reqBody := make([]map[string]interface{}, len(opts)) |
| 108 | for i := range opts { |
| 109 | reqBody[i] = map[string]interface{}{ |
| 110 | "cloud_server": map[string]string{ |
| 111 | "id": opts[i].ServerID, |
| 112 | }, |
| 113 | "load_balancer_pool": map[string]string{ |
| 114 | "id": opts[i].PoolID, |
| 115 | }, |
| 116 | } |
| 117 | } |
| 118 | _, res.Err = c.Request("POST", createNodesURL(c), gophercloud.RequestOpts{ |
| 119 | JSONBody: &reqBody, |
| 120 | JSONResponse: &res.Body, |
| 121 | OkCodes: []int{201}, |
| 122 | }) |
| 123 | return res |
| 124 | } |
| 125 | |
| 126 | // DeleteNodes removes the cloud servers with the given serverIDs to the corresponding |
| 127 | // load balancer pools with the given poolIDs. |
| 128 | func DeleteNodes(c *gophercloud.ServiceClient, opts []NodesOpts) DeleteNodesResult { |
| 129 | var res DeleteNodesResult |
| 130 | reqBody := make([]map[string]interface{}, len(opts)) |
| 131 | for i := range opts { |
| 132 | reqBody[i] = map[string]interface{}{ |
| 133 | "cloud_server": map[string]string{ |
| 134 | "id": opts[i].ServerID, |
| 135 | }, |
| 136 | "load_balancer_pool": map[string]string{ |
| 137 | "id": opts[i].PoolID, |
| 138 | }, |
| 139 | } |
| 140 | } |
| 141 | _, res.Err = c.Request("DELETE", createNodesURL(c), gophercloud.RequestOpts{ |
| 142 | JSONBody: &reqBody, |
| 143 | OkCodes: []int{204}, |
| 144 | }) |
| 145 | return res |
| 146 | } |
| 147 | |
| 148 | // ListNodesDetailsForServer returns all load balancer pool nodes that are associated with RackConnect |
| 149 | // for the given LB pool ID with all their details for the server with the given serverID. |
| 150 | func ListNodesDetailsForServer(c *gophercloud.ServiceClient, serverID string) pagination.Pager { |
| 151 | url := listNodesForServerURL(c, serverID) |
| 152 | createPage := func(r pagination.PageResult) pagination.Page { |
| 153 | return NodeDetailsForServerPage{pagination.SinglePageBase(r)} |
| 154 | } |
| 155 | return pagination.NewPager(c, url, createPage) |
| 156 | } |