blob: 4b4a103afedb929f9000a4e354914e371157af5e [file] [log] [blame]
Jon Perritt9f8b0152015-03-17 19:28:18 -06001package lbpools
2
3import (
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.
9func 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.
19func 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.
30func 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.
40func 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.
57func 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.
67func 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.
78func 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.
88func 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 Perrittaa244992015-03-18 09:42:24 -060097// NodeOpts are options for bulk adding/deleting nodes to LB pools.
98type NodeOpts struct {
Jon Perritt9f8b0152015-03-17 19:28:18 -060099 ServerID string
100 PoolID string
101}
102
Jon Perrittaa244992015-03-18 09:42:24 -0600103// NodesOpts are a slice of NodeOpts, passed as options for bulk operations.
104type NodesOpts []NodeOpts
105
106// ToLBPoolCreateNodesMap serializes a NodesOpts into a map to send in the request.
107func (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 Perritt9f8b0152015-03-17 19:28:18 -0600111 "cloud_server": map[string]string{
Jon Perrittaa244992015-03-18 09:42:24 -0600112 "id": o[i].ServerID,
Jon Perritt9f8b0152015-03-17 19:28:18 -0600113 },
114 "load_balancer_pool": map[string]string{
Jon Perrittaa244992015-03-18 09:42:24 -0600115 "id": o[i].PoolID,
Jon Perritt9f8b0152015-03-17 19:28:18 -0600116 },
117 }
118 }
Jon Perrittaa244992015-03-18 09:42:24 -0600119 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.
124func 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 Perritt9f8b0152015-03-17 19:28:18 -0600132 _, 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 Perrittaa244992015-03-18 09:42:24 -0600142func DeleteNodes(c *gophercloud.ServiceClient, opts NodesOpts) DeleteNodesResult {
Jon Perritt9f8b0152015-03-17 19:28:18 -0600143 var res DeleteNodesResult
Jon Perrittaa244992015-03-18 09:42:24 -0600144 reqBody, err := opts.ToLBPoolCreateNodesMap()
145 if err != nil {
146 res.Err = err
147 return res
Jon Perritt9f8b0152015-03-17 19:28:18 -0600148 }
Jon Perrittaa244992015-03-18 09:42:24 -0600149
Jon Perritt9f8b0152015-03-17 19:28:18 -0600150 _, res.Err = c.Request("DELETE", createNodesURL(c), gophercloud.RequestOpts{
151 JSONBody: &reqBody,
152 OkCodes: []int{204},
153 })
154 return res
155}
156
Jon Perrittaa244992015-03-18 09:42:24 -0600157// ListNodesDetailsForServer is similar to ListNodesDetails but only returns nodes
158// for the given serverID.
Jon Perritt9f8b0152015-03-17 19:28:18 -0600159func 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}