Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 1 | package sessions |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 6 | "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. |
| 11 | type 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. |
| 17 | type CreateOpts struct { |
| 18 | // Required - can either be HTTPCOOKIE or SOURCEIP |
| 19 | Type Type |
| 20 | } |
| 21 | |
| 22 | // ToSPCreateMap casts a CreateOpts struct to a map. |
| 23 | func (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. |
| 36 | func 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 Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 45 | _, res.Err = c.Put(rootURL(c, lbID), reqBody, &res.Body, nil) |
Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 46 | return res |
| 47 | } |
| 48 | |
| 49 | // Get is the operation responsible for showing details of the session |
| 50 | // persistence configuration for a particular load balancer. |
| 51 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 52 | var res GetResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 53 | _, res.Err = c.Get(rootURL(c, lbID), &res.Body, nil) |
Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 54 | return res |
| 55 | } |
| 56 | |
| 57 | // Disable is the operation responsible for disabling session persistence for a |
| 58 | // particular load balancer. |
| 59 | func Disable(c *gophercloud.ServiceClient, lbID int) DisableResult { |
| 60 | var res DisableResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 61 | _, res.Err = c.Delete(rootURL(c, lbID), nil) |
Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 62 | return res |
| 63 | } |