Jamie Hannaford | f0cd165 | 2014-11-04 10:54:11 +0100 | [diff] [blame] | 1 | package vips |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame^] | 4 | "github.com/racker/perigee" |
| 5 | |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // List is the operation responsible for returning a paginated collection of |
| 11 | // load balancer virtual IP addresses. |
| 12 | func List(client *gophercloud.ServiceClient, loadBalancerID int) pagination.Pager { |
| 13 | url := rootURL(client, loadBalancerID) |
| 14 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 15 | return VIPPage{pagination.SinglePageBase(r)} |
| 16 | }) |
| 17 | } |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame^] | 18 | |
| 19 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 20 | // to be used in the main Create operation in this package. Since many |
| 21 | // extensions decorate or modify the common logic, it is useful for them to |
| 22 | // satisfy a basic interface in order for them to be used. |
| 23 | type CreateOptsBuilder interface { |
| 24 | ToVIPCreateMap() (map[string]interface{}, error) |
| 25 | } |
| 26 | |
| 27 | // CreateOpts is the common options struct used in this package's Create |
| 28 | // operation. |
| 29 | type CreateOpts struct { |
| 30 | ID string |
| 31 | |
| 32 | Type string |
| 33 | |
| 34 | Version string |
| 35 | } |
| 36 | |
| 37 | // ToVIPCreateMap casts a CreateOpts struct to a map. |
| 38 | func (opts CreateOpts) ToVIPCreateMap() (map[string]interface{}, error) { |
| 39 | lb := make(map[string]interface{}) |
| 40 | |
| 41 | if opts.ID != "" { |
| 42 | lb["id"] = opts.ID |
| 43 | } |
| 44 | if opts.Type != "" { |
| 45 | lb["type"] = opts.Type |
| 46 | } |
| 47 | if opts.Version != "" { |
| 48 | lb["ipVersion"] = opts.Version |
| 49 | } |
| 50 | |
| 51 | return lb, nil |
| 52 | } |
| 53 | |
| 54 | func Create(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) CreateResult { |
| 55 | var res CreateResult |
| 56 | |
| 57 | reqBody, err := opts.ToVIPCreateMap() |
| 58 | if err != nil { |
| 59 | res.Err = err |
| 60 | return res |
| 61 | } |
| 62 | |
| 63 | _, res.Err = perigee.Request("POST", rootURL(c, lbID), perigee.Options{ |
| 64 | MoreHeaders: c.AuthenticatedHeaders(), |
| 65 | ReqBody: &reqBody, |
| 66 | Results: &res.Body, |
| 67 | OkCodes: []int{202}, |
| 68 | }) |
| 69 | |
| 70 | return res |
| 71 | } |