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