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 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 45 | _, res.Err = c.Request("PUT", rootURL(c, lbID), gophercloud.RequestOpts{ |
| 46 | JSONBody: &reqBody, |
| 47 | JSONResponse: &res.Body, |
| 48 | OkCodes: []int{202}, |
Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 49 | }) |
| 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. |
| 56 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 57 | var res GetResult |
| 58 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 59 | _, res.Err = c.Request("GET", rootURL(c, lbID), gophercloud.RequestOpts{ |
| 60 | JSONResponse: &res.Body, |
| 61 | OkCodes: []int{200}, |
Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 62 | }) |
| 63 | |
| 64 | return res |
| 65 | } |
| 66 | |
| 67 | // Disable is the operation responsible for disabling session persistence for a |
| 68 | // particular load balancer. |
| 69 | func Disable(c *gophercloud.ServiceClient, lbID int) DisableResult { |
| 70 | var res DisableResult |
| 71 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 72 | _, res.Err = c.Request("DELETE", rootURL(c, lbID), gophercloud.RequestOpts{ |
| 73 | OkCodes: []int{202}, |
Jamie Hannaford | e514541 | 2014-11-06 12:35:59 +0100 | [diff] [blame] | 74 | }) |
| 75 | |
| 76 | return res |
| 77 | } |