Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit e940a16

Browse files
committed
Adding Support for LBaaS v2 - Pools and Members
1 parent 57e2801 commit e940a16

File tree

4 files changed

+1146
-0
lines changed

4 files changed

+1146
-0
lines changed
Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
package pools
2+
3+
import (
4+
"github.com/rackspace/gophercloud"
5+
"github.com/rackspace/gophercloud/pagination"
6+
)
7+
8+
// AdminState gives users a solid type to work with for create and update
9+
// operations. It is recommended that users use the `Up` and `Down` enums.
10+
type AdminState *bool
11+
12+
// Convenience vars for AdminStateUp values.
13+
var (
14+
iTrue = true
15+
iFalse = false
16+
17+
Up AdminState = &iTrue
18+
Down AdminState = &iFalse
19+
)
20+
21+
// ListOpts allows the filtering and sorting of paginated collections through
22+
// the API. Filtering is achieved by passing in struct field values that map to
23+
// the Pool attributes you want to see returned. SortKey allows you to
24+
// sort by a particular Pool attribute. SortDir sets the direction, and is
25+
// either `asc' or `desc'. Marker and Limit are used for pagination.
26+
type ListOpts struct {
27+
LBMethod string `q:"lb_algorithm"`
28+
Protocol string `q:"protocol"`
29+
SubnetID string `q:"subnet_id"`
30+
TenantID string `q:"tenant_id"`
31+
AdminStateUp *bool `q:"admin_state_up"`
32+
Name string `q:"name"`
33+
ID string `q:"id"`
34+
LoadbalancerID string `q:"loadbalancer_id"`
35+
Limit int `q:"limit"`
36+
Marker string `q:"marker"`
37+
SortKey string `q:"sort_key"`
38+
SortDir string `q:"sort_dir"`
39+
}
40+
41+
// List returns a Pager which allows you to iterate over a collection of
42+
// pools. It accepts a ListOpts struct, which allows you to filter and sort
43+
// the returned collection for greater efficiency.
44+
//
45+
// Default policy settings return only those pools that are owned by the
46+
// tenant who submits the request, unless an admin user submits the request.
47+
func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
48+
q, err := gophercloud.BuildQueryString(&opts)
49+
if err != nil {
50+
return pagination.Pager{Err: err}
51+
}
52+
u := rootURL(c) + q.String()
53+
return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
54+
return PoolPage{pagination.LinkedPageBase{PageResult: r}}
55+
})
56+
}
57+
58+
// Supported attributes for create/update operations.
59+
const (
60+
LBMethodRoundRobin = "ROUND_ROBIN"
61+
LBMethodLeastConnections = "LEAST_CONNECTIONS"
62+
LBMethodSourceIp = "SOURCE_IP"
63+
64+
ProtocolTCP = "TCP"
65+
ProtocolHTTP = "HTTP"
66+
ProtocolHTTPS = "HTTPS"
67+
)
68+
69+
// CreateOpts contains all the values needed to create a new pool.
70+
type CreateOpts struct {
71+
// Only required if the caller has an admin role and wants to create a pool
72+
// for another tenant.
73+
TenantID string
74+
75+
// Optional. The network on which the members of the pool will be located.
76+
// Only members that are on this network can be added to the pool.
77+
SubnetID string
78+
79+
// Optional. Name of the pool.
80+
Name string
81+
82+
// Optional. Human-readable description for the pool.
83+
Description string
84+
85+
// Required. The protocol used by the pool members, you can use either
86+
// ProtocolTCP, ProtocolHTTP, or ProtocolHTTPS.
87+
Protocol string
88+
89+
// The Loadbalancer on which the members of the pool will be associated with.
90+
// Note: one of LoadbalancerID or ListenerID must be provided.
91+
LoadbalancerID string
92+
93+
// The Listener on which the members of the pool will be associated with.
94+
// Note: one of LoadbalancerID or ListenerID must be provided.
95+
ListenerID string
96+
97+
// The algorithm used to distribute load between the members of the pool. The
98+
// current specification supports LBMethodRoundRobin, LBMethodLeastConnections
99+
// and LBMethodSourceIp as valid values for this attribute.
100+
LBMethod string
101+
102+
// Optional. Omit this field to prevent session persistence.
103+
Persistence *SessionPersistence
104+
105+
// Optional. The administrative state of the Listener. A valid value is true (UP)
106+
// or false (DOWN).
107+
AdminStateUp *bool
108+
}
109+
110+
// Create accepts a CreateOpts struct and uses the values to create a new
111+
// load balancer pool.
112+
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
113+
type pool struct {
114+
Name string `json:"name,omitempty"`
115+
Description string `json:"description,omitempty"`
116+
TenantID string `json:"tenant_id,omitempty"`
117+
SubnetID string `json:"subnet_id,omitempty"`
118+
Protocol string `json:"protocol"`
119+
LoadbalancerID string `json:"loadbalancer_id,omitempty"`
120+
ListenerID string `json:"listener_id,omitempty"`
121+
LBMethod string `json:"lb_algorithm"`
122+
Persistence *SessionPersistence `json:"session_persistence,omitempty"`
123+
AdminStateUp *bool `json:"admin_state_up,omitempty"`
124+
}
125+
126+
type request struct {
127+
Pool pool `json:"pool"`
128+
}
129+
130+
reqBody := request{Pool: pool{
131+
Name: opts.Name,
132+
Description: opts.Description,
133+
TenantID: opts.TenantID,
134+
SubnetID: opts.SubnetID,
135+
Protocol: opts.Protocol,
136+
LoadbalancerID: opts.LoadbalancerID,
137+
ListenerID: opts.ListenerID,
138+
LBMethod: opts.LBMethod,
139+
AdminStateUp: opts.AdminStateUp,
140+
}}
141+
142+
if opts.Persistence != nil {
143+
reqBody.Pool.Persistence = opts.Persistence
144+
}
145+
146+
var res CreateResult
147+
_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
148+
return res
149+
}
150+
151+
// Get retrieves a particular pool based on its unique ID.
152+
func Get(c *gophercloud.ServiceClient, id string) GetResult {
153+
var res GetResult
154+
_, res.Err = c.Get(resourceURL(c, id), &res.Body, nil)
155+
return res
156+
}
157+
158+
// UpdateOpts contains the values used when updating a pool.
159+
type UpdateOpts struct {
160+
// Optional. Name of the pool.
161+
Name string
162+
163+
// Optional. Human-readable description for the pool.
164+
Description string
165+
166+
// The algorithm used to distribute load between the members of the pool. The
167+
// current specification supports LBMethodRoundRobin, LBMethodLeastConnections
168+
// and LBMethodSourceIp as valid values for this attribute.
169+
LBMethod string
170+
171+
// Optional. The administrative state of the Listener. A valid value is true (UP)
172+
// or false (DOWN).
173+
AdminStateUp *bool
174+
}
175+
176+
// Update allows pools to be updated.
177+
func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
178+
type pool struct {
179+
Name string `json:"name,omitempty"`
180+
Description string `json:"description,omitempty"`
181+
LBMethod string `json:"lb_algorithm,omitempty"`
182+
AdminStateUp *bool `json:"admin_state_up,omitempty"`
183+
}
184+
type request struct {
185+
Pool pool `json:"pool"`
186+
}
187+
188+
reqBody := request{Pool: pool{
189+
Name: opts.Name,
190+
Description: opts.Description,
191+
LBMethod: opts.LBMethod,
192+
AdminStateUp: opts.AdminStateUp,
193+
}}
194+
195+
// Send request to API
196+
var res UpdateResult
197+
_, res.Err = c.Put(resourceURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{
198+
OkCodes: []int{200},
199+
})
200+
return res
201+
}
202+
203+
// Delete will permanently delete a particular pool based on its unique ID.
204+
func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
205+
var res DeleteResult
206+
_, res.Err = c.Delete(resourceURL(c, id), nil)
207+
return res
208+
}
209+
210+
// MemberListOpts allows the filtering and sorting of paginated collections through
211+
// the API. Filtering is achieved by passing in struct field values that map to
212+
// the Member attributes you want to see returned. SortKey allows you to
213+
// sort by a particular Member attribute. SortDir sets the direction, and is
214+
// either `asc' or `desc'. Marker and Limit are used for pagination.
215+
type MemberListOpts struct {
216+
Name string `q:"name"`
217+
Weight int `q:"weight"`
218+
AdminStateUp *bool `q:"admin_state_up"`
219+
TenantID string `q:"tenant_id"`
220+
SubnetID string `q:"subnet_id"`
221+
Address string `q:"address"`
222+
ProtocolPort int `q:"protocol_port"`
223+
ID string `q:"id"`
224+
Limit int `q:"limit"`
225+
Marker string `q:"marker"`
226+
SortKey string `q:"sort_key"`
227+
SortDir string `q:"sort_dir"`
228+
}
229+
230+
// List returns a Pager which allows you to iterate over a collection of
231+
// members. It accepts a ListOpts struct, which allows you to filter and sort
232+
// the returned collection for greater efficiency.
233+
//
234+
// Default policy settings return only those members that are owned by the
235+
// tenant who submits the request, unless an admin user submits the request.
236+
func ListAssociateMembers(c *gophercloud.ServiceClient, poolID string, opts MemberListOpts) pagination.Pager {
237+
q, err := gophercloud.BuildQueryString(&opts)
238+
if err != nil {
239+
return pagination.Pager{Err: err}
240+
}
241+
u := memberRootURL(c, poolID) + q.String()
242+
return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
243+
return MemberPage{pagination.LinkedPageBase{PageResult: r}}
244+
})
245+
}
246+
247+
// CreateOpts contains all the values needed to create a new Member for a Pool.
248+
type MemberCreateOpts struct {
249+
// Optional. Name of the Member.
250+
Name string
251+
252+
// Only required if the caller has an admin role and wants to create a Member
253+
// for another tenant.
254+
TenantID string
255+
256+
// Required. The IP address of the member to receive traffic from the load balancer.
257+
Address string
258+
259+
// Required. The port on which to listen for client traffic.
260+
ProtocolPort int
261+
262+
// Optional. A positive integer value that indicates the relative portion of
263+
// traffic that this member should receive from the pool. For example, a
264+
// member with a weight of 10 receives five times as much traffic as a member
265+
// with a weight of 2.
266+
Weight int
267+
268+
// Optional. If you omit this parameter, LBaaS uses the vip_subnet_id
269+
// parameter value for the subnet UUID.
270+
SubnetID string
271+
272+
// Optional. The administrative state of the Listener. A valid value is true (UP)
273+
// or false (DOWN).
274+
AdminStateUp *bool
275+
}
276+
277+
// CreateAssociateMember will create and associate a Member with a particular Pool.
278+
func CreateAssociateMember(c *gophercloud.ServiceClient, poolID string, opts MemberCreateOpts) AssociateResult {
279+
type member struct {
280+
Name string `json:"name,omitempty"`
281+
TenantID string `json:"tenant_id,omitempty"`
282+
Address string `json:"address,omitempty"`
283+
ProtocolPort int `json:"protocol_port,omitempty"`
284+
Weight int `json:"weight,omitempty"`
285+
SubnetID string `json:"subnet_id,omitempty"`
286+
AdminStateUp *bool `json:"admin_state_up,omitempty"`
287+
}
288+
type request struct {
289+
Member member `json:"member"`
290+
}
291+
292+
reqBody := request{Member: member{
293+
Name: opts.Name,
294+
TenantID: opts.TenantID,
295+
Address: opts.Address,
296+
ProtocolPort: opts.ProtocolPort,
297+
Weight: opts.Weight,
298+
SubnetID: opts.SubnetID,
299+
AdminStateUp: opts.AdminStateUp,
300+
}}
301+
302+
var res AssociateResult
303+
_, res.Err = c.Post(memberRootURL(c, poolID), reqBody, &res.Body, nil)
304+
return res
305+
}
306+
307+
// Get retrieves a particular Pool Member based on its unique ID.
308+
func GetAssociateMember(c *gophercloud.ServiceClient, poolID string, memberID string) GetResult {
309+
var res GetResult
310+
_, res.Err = c.Get(memberResourceURL(c, poolID, memberID), &res.Body, nil)
311+
return res
312+
}
313+
314+
// UpdateOpts contains the values used when updating a Pool Member.
315+
type MemberUpdateOpts struct {
316+
// Name of the Member.
317+
Name string
318+
319+
// A positive integer value that indicates the relative portion of
320+
// traffic that this member should receive from the pool. For example, a
321+
// member with a weight of 10 receives five times as much traffic as a member
322+
// with a weight of 2.
323+
Weight int
324+
325+
// The administrative state of the member, which is up (true) or down (false).
326+
AdminStateUp *bool
327+
}
328+
329+
// Update allows Member to be updated.
330+
func UpdateAssociateMember(c *gophercloud.ServiceClient, poolID string, memberID string, opts MemberUpdateOpts) UpdateResult {
331+
type member struct {
332+
Name string `json:"name,omitempty"`
333+
Weight int `json:"weight,omitempty"`
334+
AdminStateUp *bool `json:"admin_state_up,omitempty"`
335+
}
336+
type request struct {
337+
Member member `json:"member"`
338+
}
339+
340+
reqBody := request{Member: member{
341+
Name: opts.Name,
342+
Weight: opts.Weight,
343+
AdminStateUp: opts.AdminStateUp,
344+
}}
345+
346+
// Send request to API
347+
var res UpdateResult
348+
_, res.Err = c.Put(memberResourceURL(c, poolID, memberID), reqBody, &res.Body, &gophercloud.RequestOpts{
349+
OkCodes: []int{200, 201, 202},
350+
})
351+
return res
352+
}
353+
354+
// DisassociateMember will remove and disassociate a Member from a particular Pool.
355+
func DeleteMember(c *gophercloud.ServiceClient, poolID string, memberID string) AssociateResult {
356+
var res AssociateResult
357+
_, res.Err = c.Delete(memberResourceURL(c, poolID, memberID), nil)
358+
return res
359+
}

0 commit comments

Comments
 (0)