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 | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 6 | "github.com/racker/perigee" |
| 7 | |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
| 12 | // List is the operation responsible for returning a paginated collection of |
| 13 | // load balancer virtual IP addresses. |
| 14 | func List(client *gophercloud.ServiceClient, loadBalancerID int) pagination.Pager { |
| 15 | url := rootURL(client, loadBalancerID) |
| 16 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 17 | return VIPPage{pagination.SinglePageBase(r)} |
| 18 | }) |
| 19 | } |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 20 | |
| 21 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 22 | // to be used in the main Create operation in this package. Since many |
| 23 | // extensions decorate or modify the common logic, it is useful for them to |
| 24 | // satisfy a basic interface in order for them to be used. |
| 25 | type CreateOptsBuilder interface { |
| 26 | ToVIPCreateMap() (map[string]interface{}, error) |
| 27 | } |
| 28 | |
| 29 | // CreateOpts is the common options struct used in this package's Create |
| 30 | // operation. |
| 31 | type CreateOpts struct { |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 32 | // Optional - the ID of an existing virtual IP. By doing this, you are |
| 33 | // allowing load balancers to share IPV6 addresses. |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 34 | ID string |
| 35 | |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 36 | // Optional - the type of address. |
| 37 | Type Type |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 38 | |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 39 | // Optional - the version of address. |
| 40 | Version Version |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // ToVIPCreateMap casts a CreateOpts struct to a map. |
| 44 | func (opts CreateOpts) ToVIPCreateMap() (map[string]interface{}, error) { |
| 45 | lb := make(map[string]interface{}) |
| 46 | |
| 47 | if opts.ID != "" { |
| 48 | lb["id"] = opts.ID |
| 49 | } |
| 50 | if opts.Type != "" { |
| 51 | lb["type"] = opts.Type |
| 52 | } |
| 53 | if opts.Version != "" { |
| 54 | lb["ipVersion"] = opts.Version |
| 55 | } |
| 56 | |
| 57 | return lb, nil |
| 58 | } |
| 59 | |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 60 | // Create is the operation responsible for assigning a new Virtual IP to an |
| 61 | // existing load balancer resource. Currently, only version 6 IP addresses may |
| 62 | // be added. |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 63 | func Create(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) CreateResult { |
| 64 | var res CreateResult |
| 65 | |
| 66 | reqBody, err := opts.ToVIPCreateMap() |
| 67 | if err != nil { |
| 68 | res.Err = err |
| 69 | return res |
| 70 | } |
| 71 | |
| 72 | _, res.Err = perigee.Request("POST", rootURL(c, lbID), perigee.Options{ |
| 73 | MoreHeaders: c.AuthenticatedHeaders(), |
| 74 | ReqBody: &reqBody, |
| 75 | Results: &res.Body, |
| 76 | OkCodes: []int{202}, |
| 77 | }) |
| 78 | |
| 79 | return res |
| 80 | } |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 81 | |
| 82 | // BulkDelete is the operation responsible for batch deleting multiple VIPs in |
| 83 | // a single operation. It accepts a slice of integer IDs and will remove them |
| 84 | // from the load balancer. The maximum limit is 10 VIP removals at once. |
| 85 | func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, vipIDs []int) DeleteResult { |
| 86 | var res DeleteResult |
| 87 | |
| 88 | if len(vipIDs) > 10 || len(vipIDs) == 0 { |
| 89 | res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 VIP IDs") |
| 90 | return res |
| 91 | } |
| 92 | |
| 93 | url := rootURL(c, loadBalancerID) |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 94 | url += gophercloud.IDSliceToQueryString("id", vipIDs) |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 95 | |
| 96 | _, res.Err = perigee.Request("DELETE", url, perigee.Options{ |
| 97 | MoreHeaders: c.AuthenticatedHeaders(), |
| 98 | OkCodes: []int{202}, |
| 99 | }) |
| 100 | |
| 101 | return res |
| 102 | } |
| 103 | |
| 104 | // Delete is the operation responsible for permanently deleting a VIP. |
| 105 | func Delete(c *gophercloud.ServiceClient, lbID, vipID int) DeleteResult { |
| 106 | var res DeleteResult |
| 107 | _, res.Err = perigee.Request("DELETE", resourceURL(c, lbID, vipID), perigee.Options{ |
| 108 | MoreHeaders: c.AuthenticatedHeaders(), |
Jamie Hannaford | c9da4b4 | 2014-11-05 16:34:56 +0100 | [diff] [blame] | 109 | OkCodes: []int{202}, |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 110 | }) |
| 111 | return res |
| 112 | } |