blob: a93d766cd92215c6acd11f154727e10fab8fd601 [file] [log] [blame]
Jamie Hannaforde5145412014-11-06 12:35:59 +01001package sessions
2
3import (
4 "errors"
5
Jamie Hannaforde5145412014-11-06 12:35:59 +01006 "github.com/rackspace/gophercloud"
7)
8
9// CreateOptsBuilder is the interface options structs have to satisfy in order
10// to be used in the main Create operation in this package.
11type CreateOptsBuilder interface {
12 ToSPCreateMap() (map[string]interface{}, error)
13}
14
15// CreateOpts is the common options struct used in this package's Create
16// operation.
17type CreateOpts struct {
18 // Required - can either be HTTPCOOKIE or SOURCEIP
19 Type Type
20}
21
22// ToSPCreateMap casts a CreateOpts struct to a map.
23func (opts CreateOpts) ToSPCreateMap() (map[string]interface{}, error) {
24 sp := make(map[string]interface{})
25
26 if opts.Type == "" {
27 return sp, errors.New("Type is a required field")
28 }
29
30 sp["persistenceType"] = opts.Type
31 return map[string]interface{}{"sessionPersistence": sp}, nil
32}
33
34// Enable is the operation responsible for enabling session persistence for a
35// particular load balancer.
36func Enable(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) EnableResult {
37 var res EnableResult
38
39 reqBody, err := opts.ToSPCreateMap()
40 if err != nil {
41 res.Err = err
42 return res
43 }
44
Jamie Hannaford5497f942015-03-25 11:55:51 +010045 _, res.Err = c.Put(rootURL(c, lbID), reqBody, &res.Body, nil)
Jamie Hannaforde5145412014-11-06 12:35:59 +010046 return res
47}
48
49// Get is the operation responsible for showing details of the session
50// persistence configuration for a particular load balancer.
51func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
52 var res GetResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010053 _, res.Err = c.Get(rootURL(c, lbID), &res.Body, nil)
Jamie Hannaforde5145412014-11-06 12:35:59 +010054 return res
55}
56
57// Disable is the operation responsible for disabling session persistence for a
58// particular load balancer.
59func Disable(c *gophercloud.ServiceClient, lbID int) DisableResult {
60 var res DisableResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010061 _, res.Err = c.Delete(rootURL(c, lbID), nil)
Jamie Hannaforde5145412014-11-06 12:35:59 +010062 return res
63}