blob: c300c56c1e9a9cd4038275755c650c179ac729bc [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
Jamie Hannaford5497f942015-03-25 11:55:51 +010021 _, res.Err = c.Get(getURL(c, id), &res.Body, nil)
Jon Perritt9f8b0152015-03-17 19:28:18 -060022 return res
23}
24
25// ListNodes returns all load balancer pool nodes that are associated with RackConnect
26// for the given LB pool ID.
27func 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.
37func 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 Hannaford5497f942015-03-25 11:55:51 +010044 _, res.Err = c.Post(createNodeURL(c, poolID), reqBody, &res.Body, nil)
Jon Perritt9f8b0152015-03-17 19:28:18 -060045 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.
50func 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.
60func GetNode(c *gophercloud.ServiceClient, poolID, nodeID string) GetNodeResult {
61 var res GetNodeResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010062 _, res.Err = c.Get(nodeURL(c, poolID, nodeID), &res.Body, nil)
Jon Perritt9f8b0152015-03-17 19:28:18 -060063 return res
64}
65
66// DeleteNode removes the node with the given nodeID from the LB pool with the
67// given poolID.
68func DeleteNode(c *gophercloud.ServiceClient, poolID, nodeID string) DeleteNodeResult {
69 var res DeleteNodeResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010070 _, res.Err = c.Delete(deleteNodeURL(c, poolID, nodeID), nil)
Jon Perritt9f8b0152015-03-17 19:28:18 -060071 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.
76func GetNodeDetails(c *gophercloud.ServiceClient, poolID, nodeID string) GetNodeDetailsResult {
77 var res GetNodeDetailsResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010078 _, res.Err = c.Get(nodeDetailsURL(c, poolID, nodeID), &res.Body, nil)
Jon Perritt9f8b0152015-03-17 19:28:18 -060079 return res
80}
81
Jon Perrittaa244992015-03-18 09:42:24 -060082// NodeOpts are options for bulk adding/deleting nodes to LB pools.
83type NodeOpts struct {
Jon Perritt9f8b0152015-03-17 19:28:18 -060084 ServerID string
85 PoolID string
86}
87
Jon Perrittaa244992015-03-18 09:42:24 -060088// NodesOpts are a slice of NodeOpts, passed as options for bulk operations.
89type NodesOpts []NodeOpts
90
91// ToLBPoolCreateNodesMap serializes a NodesOpts into a map to send in the request.
92func (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 Perritt9f8b0152015-03-17 19:28:18 -060096 "cloud_server": map[string]string{
Jon Perrittaa244992015-03-18 09:42:24 -060097 "id": o[i].ServerID,
Jon Perritt9f8b0152015-03-17 19:28:18 -060098 },
99 "load_balancer_pool": map[string]string{
Jon Perrittaa244992015-03-18 09:42:24 -0600100 "id": o[i].PoolID,
Jon Perritt9f8b0152015-03-17 19:28:18 -0600101 },
102 }
103 }
Jon Perrittaa244992015-03-18 09:42:24 -0600104 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.
109func 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 Hannaford5497f942015-03-25 11:55:51 +0100117 _, res.Err = c.Post(createNodesURL(c), reqBody, &res.Body, nil)
Jon Perritt9f8b0152015-03-17 19:28:18 -0600118 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 Perrittaa244992015-03-18 09:42:24 -0600123func DeleteNodes(c *gophercloud.ServiceClient, opts NodesOpts) DeleteNodesResult {
Jon Perritt9f8b0152015-03-17 19:28:18 -0600124 var res DeleteNodesResult
Jon Perrittaa244992015-03-18 09:42:24 -0600125 reqBody, err := opts.ToLBPoolCreateNodesMap()
126 if err != nil {
127 res.Err = err
128 return res
Jon Perritt9f8b0152015-03-17 19:28:18 -0600129 }
Jon Perrittaa244992015-03-18 09:42:24 -0600130
Jon Perritt9f8b0152015-03-17 19:28:18 -0600131 _, res.Err = c.Request("DELETE", createNodesURL(c), gophercloud.RequestOpts{
132 JSONBody: &reqBody,
133 OkCodes: []int{204},
134 })
135 return res
136}
137
Jon Perrittaa244992015-03-18 09:42:24 -0600138// ListNodesDetailsForServer is similar to ListNodesDetails but only returns nodes
139// for the given serverID.
Jon Perritt9f8b0152015-03-17 19:28:18 -0600140func 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}