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