File: requests.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (485 lines) | stat: -rw-r--r-- 15,324 bytes parent folder | download | duplicates (2)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package pools

import (
	"fmt"
	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/pagination"
)

// AdminState gives users a solid type to work with for create and update
// operations. It is recommended that users use the `Up` and `Down` enums.
type AdminState *bool

type poolOpts struct {
	// Only required if the caller has an admin role and wants to create a pool
	// for another tenant.
	TenantID string

	// Optional. Name of the pool.
	Name string

	// Optional. Human-readable description for the pool.
	Description string

	// Required. The protocol used by the pool members, you can use either
	// ProtocolTCP, ProtocolHTTP, or ProtocolHTTPS.
	Protocol Protocol

	// The Loadbalancer on which the members of the pool will be associated with.
	// Note:  one of LoadbalancerID or ListenerID must be provided.
	LoadbalancerID string

	// The Listener on which the members of the pool will be associated with.
	// Note:  one of LoadbalancerID or ListenerID must be provided.
	ListenerID string

	// Required. The algorithm used to distribute load between the members of the pool. The
	// current specification supports LBMethodRoundRobin, LBMethodLeastConnections
	// and LBMethodSourceIp as valid values for this attribute.
	LBMethod LBMethod

	// Optional. Omit this field to prevent session persistence.
	Persistence *SessionPersistence

	// Optional. The administrative state of the Pool. A valid value is true (UP)
	// or false (DOWN).
	AdminStateUp *bool
}

// Convenience vars for AdminStateUp values.
var (
	iTrue  = true
	iFalse = false

	Up   AdminState = &iTrue
	Down AdminState = &iFalse
)

// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
	ToPoolListQuery() (string, error)
}

// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the Pool attributes you want to see returned. SortKey allows you to
// sort by a particular Pool attribute. SortDir sets the direction, and is
// either `asc' or `desc'. Marker and Limit are used for pagination.
type ListOpts struct {
	LBMethod       string `q:"lb_algorithm"`
	Protocol       string `q:"protocol"`
	TenantID       string `q:"tenant_id"`
	AdminStateUp   *bool  `q:"admin_state_up"`
	Name           string `q:"name"`
	ID             string `q:"id"`
	LoadbalancerID string `q:"loadbalancer_id"`
	ListenerID     string `q:"listener_id"`
	Limit          int    `q:"limit"`
	Marker         string `q:"marker"`
	SortKey        string `q:"sort_key"`
	SortDir        string `q:"sort_dir"`
}

// ToPoolListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToPoolListQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	if err != nil {
		return "", err
	}
	return q.String(), nil
}

// List returns a Pager which allows you to iterate over a collection of
// pools. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
//
// Default policy settings return only those pools that are owned by the
// tenant who submits the request, unless an admin user submits the request.
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
	url := rootURL(c)
	if opts != nil {
		query, err := opts.ToPoolListQuery()
		if err != nil {
			return pagination.Pager{Err: err}
		}
		url += query
	}

	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
		return PoolPage{pagination.LinkedPageBase{PageResult: r}}
	})
}

type LBMethod string
type Protocol string

// Supported attributes for create/update operations.
const (
	LBMethodRoundRobin       LBMethod = "ROUND_ROBIN"
	LBMethodLeastConnections LBMethod = "LEAST_CONNECTIONS"
	LBMethodSourceIp         LBMethod = "SOURCE_IP"

	ProtocolTCP   Protocol = "TCP"
	ProtocolHTTP  Protocol = "HTTP"
	ProtocolHTTPS Protocol = "HTTPS"
)

var (
	errLoadbalancerOrListenerRequired = fmt.Errorf("A ListenerID or LoadbalancerID is required")
	errValidLBMethodRequired          = fmt.Errorf("A valid LBMethod is required. Supported values are ROUND_ROBIN, LEAST_CONNECTIONS, SOURCE_IP")
	errValidProtocolRequired          = fmt.Errorf("A valid Protocol is required. Supported values are TCP, HTTP, HTTPS")
)

// CreateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Create operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type CreateOptsBuilder interface {
	ToPoolCreateMap() (map[string]interface{}, error)
}

// CreateOpts is the common options struct used in this package's Create
// operation.
type CreateOpts poolOpts

// ToPoolCreateMap casts a CreateOpts struct to a map.
func (opts CreateOpts) ToPoolCreateMap() (map[string]interface{}, error) {
	l := make(map[string]interface{})
	allowedLBMethod := map[LBMethod]bool{LBMethodRoundRobin: true, LBMethodLeastConnections: true, LBMethodSourceIp: true}
	allowedProtocol := map[Protocol]bool{ProtocolTCP: true, ProtocolHTTP: true, ProtocolHTTPS: true}

	if allowedLBMethod[opts.LBMethod] {
		l["lb_algorithm"] = opts.LBMethod
	} else {
		return nil, errValidLBMethodRequired
	}
	if allowedProtocol[opts.Protocol] {
		l["protocol"] = opts.Protocol
	} else {
		return nil, errValidProtocolRequired
	}
	if opts.LoadbalancerID == "" && opts.ListenerID == "" {
		return nil, errLoadbalancerOrListenerRequired
	} else {
		if opts.LoadbalancerID != "" {
			l["loadbalancer_id"] = opts.LoadbalancerID
		}
		if opts.ListenerID != "" {
			l["listener_id"] = opts.ListenerID
		}
	}
	if opts.AdminStateUp != nil {
		l["admin_state_up"] = &opts.AdminStateUp
	}
	if opts.Name != "" {
		l["name"] = opts.Name
	}
	if opts.TenantID != "" {
		l["tenant_id"] = opts.TenantID
	}
	if opts.Persistence != nil {
		l["session_persistence"] = &opts.Persistence
	}

	return map[string]interface{}{"pool": l}, nil
}

// Create accepts a CreateOpts struct and uses the values to create a new
// load balancer pool.
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	var res CreateResult

	reqBody, err := opts.ToPoolCreateMap()
	if err != nil {
		res.Err = err
		return res
	}

	// Send request to API
	_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
	return res
}

// Get retrieves a particular pool based on its unique ID.
func Get(c *gophercloud.ServiceClient, id string) GetResult {
	var res GetResult
	_, res.Err = c.Get(resourceURL(c, id), &res.Body, nil)
	return res
}

// UpdateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Update operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type UpdateOptsBuilder interface {
	ToPoolUpdateMap() (map[string]interface{}, error)
}

// UpdateOpts is the common options struct used in this package's Update
// operation.
type UpdateOpts poolOpts

// ToPoolUpdateMap casts a UpdateOpts struct to a map.
func (opts UpdateOpts) ToPoolUpdateMap() (map[string]interface{}, error) {
	l := make(map[string]interface{})
	allowedLBMethod := map[LBMethod]bool{LBMethodRoundRobin: true, LBMethodLeastConnections: true, LBMethodSourceIp: true}

	if opts.LBMethod != "" {
		if allowedLBMethod[opts.LBMethod] {
			l["lb_algorithm"] = opts.LBMethod
		} else {
			return nil, errValidLBMethodRequired
		}
	}
	if opts.Name != "" {
		l["name"] = opts.Name
	}
	if opts.Description != "" {
		l["description"] = opts.Description
	}
	if opts.AdminStateUp != nil {
		l["admin_state_up"] = &opts.AdminStateUp
	}

	return map[string]interface{}{"pool": l}, nil
}

// Update allows pools to be updated.
func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
	var res UpdateResult

	reqBody, err := opts.ToPoolUpdateMap()
	if err != nil {
		res.Err = err
		return res
	}

	// Send request to API
	_, res.Err = c.Put(resourceURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	return res
}

// Delete will permanently delete a particular pool based on its unique ID.
func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
	var res DeleteResult
	_, res.Err = c.Delete(resourceURL(c, id), nil)
	return res
}

// CreateOpts contains all the values needed to create a new Member for a Pool.
type memberOpts struct {
	// Optional. Name of the Member.
	Name string

	// Only required if the caller has an admin role and wants to create a Member
	// for another tenant.
	TenantID string

	// Required. The IP address of the member to receive traffic from the load balancer.
	Address string

	// Required. The port on which to listen for client traffic.
	ProtocolPort int

	// Optional. A positive integer value that indicates the relative portion of
	// traffic that this member should receive from the pool. For example, a
	// member with a weight of 10 receives five times as much traffic as a member
	// with a weight of 2.
	Weight int

	// Optional.  If you omit this parameter, LBaaS uses the vip_subnet_id
	// parameter value for the subnet UUID.
	SubnetID string

	// Optional. The administrative state of the Pool. A valid value is true (UP)
	// or false (DOWN).
	AdminStateUp *bool
}

// MemberListOptsBuilder allows extensions to add additional parameters to the
// Member List request.
type MemberListOptsBuilder interface {
	ToMemberListQuery() (string, error)
}

// MemberListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the Member attributes you want to see returned. SortKey allows you to
// sort by a particular Member attribute. SortDir sets the direction, and is
// either `asc' or `desc'. Marker and Limit are used for pagination.
type MemberListOpts struct {
	Name         string `q:"name"`
	Weight       int    `q:"weight"`
	AdminStateUp *bool  `q:"admin_state_up"`
	TenantID     string `q:"tenant_id"`
	Address      string `q:"address"`
	ProtocolPort int    `q:"protocol_port"`
	ID           string `q:"id"`
	Limit        int    `q:"limit"`
	Marker       string `q:"marker"`
	SortKey      string `q:"sort_key"`
	SortDir      string `q:"sort_dir"`
}

// ToMemberListQuery formats a ListOpts into a query string.
func (opts MemberListOpts) ToMemberListQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	if err != nil {
		return "", err
	}
	return q.String(), nil
}

// List returns a Pager which allows you to iterate over a collection of
// members. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
//
// Default policy settings return only those members that are owned by the
// tenant who submits the request, unless an admin user submits the request.
func ListAssociateMembers(c *gophercloud.ServiceClient, poolID string, opts MemberListOptsBuilder) pagination.Pager {
	url := memberRootURL(c, poolID)
	if opts != nil {
		query, err := opts.ToMemberListQuery()
		if err != nil {
			return pagination.Pager{Err: err}
		}
		url += query
	}

	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
		return MemberPage{pagination.LinkedPageBase{PageResult: r}}
	})
}

var (
	errPoolIdRequired       = fmt.Errorf("PoolID is required")
	errAddressRequired      = fmt.Errorf("Address is required")
	errProtocolPortRequired = fmt.Errorf("ProtocolPort is required")
)

// MemberCreateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Create operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type MemberCreateOptsBuilder interface {
	ToMemberCreateMap() (map[string]interface{}, error)
}

// MemberCreateOpts is the common options struct used in this package's Create
// operation.
type MemberCreateOpts memberOpts

// ToMemberCreateMap casts a CreateOpts struct to a map.
func (opts MemberCreateOpts) ToMemberCreateMap() (map[string]interface{}, error) {
	l := make(map[string]interface{})

	if opts.Address != "" {
		l["address"] = opts.Address
	} else {
		return nil, errAddressRequired
	}
	if opts.ProtocolPort != 0 {
		l["protocol_port"] = opts.ProtocolPort
	} else {
		return nil, errProtocolPortRequired
	}
	if opts.AdminStateUp != nil {
		l["admin_state_up"] = &opts.AdminStateUp
	}
	if opts.Name != "" {
		l["name"] = opts.Name
	}
	if opts.TenantID != "" {
		l["tenant_id"] = opts.TenantID
	}
	if opts.SubnetID != "" {
		l["subnet_id"] = opts.SubnetID
	}
	if opts.Weight != 0 {
		l["weight"] = opts.Weight
	}

	return map[string]interface{}{"member": l}, nil
}

// CreateAssociateMember will create and associate a Member with a particular Pool.
func CreateAssociateMember(c *gophercloud.ServiceClient, poolID string, opts MemberCreateOpts) AssociateResult {
	var res AssociateResult

	if poolID == "" {
		res.Err = errPoolIdRequired
		return res
	}

	reqBody, err := opts.ToMemberCreateMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = c.Post(memberRootURL(c, poolID), reqBody, &res.Body, nil)
	return res
}

// Get retrieves a particular Pool Member based on its unique ID.
func GetAssociateMember(c *gophercloud.ServiceClient, poolID string, memberID string) GetResult {
	var res GetResult
	_, res.Err = c.Get(memberResourceURL(c, poolID, memberID), &res.Body, nil)
	return res
}

// MemberUpdateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Update operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type MemberUpdateOptsBuilder interface {
	ToMemberUpdateMap() (map[string]interface{}, error)
}

// UpdateOpts is the common options struct used in this package's Update
// operation.
type MemberUpdateOpts memberOpts

// ToMemberUpdateMap casts a UpdateOpts struct to a map.
func (opts MemberUpdateOpts) ToMemberUpdateMap() (map[string]interface{}, error) {
	l := make(map[string]interface{})

	if opts.AdminStateUp != nil {
		l["admin_state_up"] = &opts.AdminStateUp
	}
	if opts.Name != "" {
		l["name"] = opts.Name
	}
	if opts.Weight != 0 {
		l["weight"] = opts.Weight
	}

	return map[string]interface{}{"member": l}, nil
}

// Update allows Member to be updated.
func UpdateAssociateMember(c *gophercloud.ServiceClient, poolID string, memberID string, opts MemberUpdateOpts) UpdateResult {
	var res UpdateResult

	reqBody, err := opts.ToMemberUpdateMap()
	if err != nil {
		res.Err = err
		return res
	}

	// Send request to API
	_, res.Err = c.Put(memberResourceURL(c, poolID, memberID), reqBody, &res.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200, 201, 202},
	})
	return res
}

// DisassociateMember will remove and disassociate a Member from a particular Pool.
func DeleteMember(c *gophercloud.ServiceClient, poolID string, memberID string) DeleteResult {
	var res DeleteResult
	_, res.Err = c.Delete(memberResourceURL(c, poolID, memberID), nil)
	return res
}