blob: 5572407eddaf980f765dc983ef47c070030398c0 [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
Ash Wilson59fb6c42015-02-12 16:21:13 -050045 _, res.Err = c.Request("PUT", rootURL(c, lbID), gophercloud.RequestOpts{
46 JSONBody: &reqBody,
47 JSONResponse: &res.Body,
48 OkCodes: []int{202},
Jamie Hannaforde5145412014-11-06 12:35:59 +010049 })
50
51 return res
52}
53
54// Get is the operation responsible for showing details of the session
55// persistence configuration for a particular load balancer.
56func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
57 var res GetResult
58
Ash Wilson59fb6c42015-02-12 16:21:13 -050059 _, res.Err = c.Request("GET", rootURL(c, lbID), gophercloud.RequestOpts{
60 JSONResponse: &res.Body,
61 OkCodes: []int{200},
Jamie Hannaforde5145412014-11-06 12:35:59 +010062 })
63
64 return res
65}
66
67// Disable is the operation responsible for disabling session persistence for a
68// particular load balancer.
69func Disable(c *gophercloud.ServiceClient, lbID int) DisableResult {
70 var res DisableResult
71
Ash Wilson59fb6c42015-02-12 16:21:13 -050072 _, res.Err = c.Request("DELETE", rootURL(c, lbID), gophercloud.RequestOpts{
73 OkCodes: []int{202},
Jamie Hannaforde5145412014-11-06 12:35:59 +010074 })
75
76 return res
77}