blob: 9853ad1320caada63bd8d49ad9028d5ccc83edc5 [file] [log] [blame]
Jamie Hannaforde5145412014-11-06 12:35:59 +01001package sessions
2
3import (
4 "errors"
5
6 "github.com/racker/perigee"
7
8 "github.com/rackspace/gophercloud"
9)
10
11// CreateOptsBuilder is the interface options structs have to satisfy in order
12// to be used in the main Create operation in this package.
13type CreateOptsBuilder interface {
14 ToSPCreateMap() (map[string]interface{}, error)
15}
16
17// CreateOpts is the common options struct used in this package's Create
18// operation.
19type CreateOpts struct {
20 // Required - can either be HTTPCOOKIE or SOURCEIP
21 Type Type
22}
23
24// ToSPCreateMap casts a CreateOpts struct to a map.
25func (opts CreateOpts) ToSPCreateMap() (map[string]interface{}, error) {
26 sp := make(map[string]interface{})
27
28 if opts.Type == "" {
29 return sp, errors.New("Type is a required field")
30 }
31
32 sp["persistenceType"] = opts.Type
33 return map[string]interface{}{"sessionPersistence": sp}, nil
34}
35
36// Enable is the operation responsible for enabling session persistence for a
37// particular load balancer.
38func Enable(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) EnableResult {
39 var res EnableResult
40
41 reqBody, err := opts.ToSPCreateMap()
42 if err != nil {
43 res.Err = err
44 return res
45 }
46
47 _, res.Err = perigee.Request("PUT", rootURL(c, lbID), perigee.Options{
48 MoreHeaders: c.AuthenticatedHeaders(),
49 ReqBody: &reqBody,
50 Results: &res.Body,
Jamie Hannaford3da65282014-11-10 11:36:16 +010051 OkCodes: []int{202},
Jamie Hannaforde5145412014-11-06 12:35:59 +010052 })
53
54 return res
55}
56
57// Get is the operation responsible for showing details of the session
58// persistence configuration for a particular load balancer.
59func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
60 var res GetResult
61
62 _, res.Err = perigee.Request("GET", rootURL(c, lbID), perigee.Options{
63 MoreHeaders: c.AuthenticatedHeaders(),
64 Results: &res.Body,
65 OkCodes: []int{200},
66 })
67
68 return res
69}
70
71// Disable is the operation responsible for disabling session persistence for a
72// particular load balancer.
73func Disable(c *gophercloud.ServiceClient, lbID int) DisableResult {
74 var res DisableResult
75
76 _, res.Err = perigee.Request("DELETE", rootURL(c, lbID), perigee.Options{
77 MoreHeaders: c.AuthenticatedHeaders(),
Jamie Hannaford3da65282014-11-10 11:36:16 +010078 OkCodes: []int{202},
Jamie Hannaforde5145412014-11-06 12:35:59 +010079 })
80
81 return res
82}