blob: 42f0c1d071a9fef2d1d03a2a4d2b6e7f87032f79 [file] [log] [blame]
Jamie Hannafordf0cd1652014-11-04 10:54:11 +01001package vips
Jamie Hannaford2841a532014-11-04 11:05:42 +01002
3import (
Jamie Hannaford491ea5d2014-11-04 11:59:37 +01004 "errors"
5
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +01006 "github.com/racker/perigee"
7
Jamie Hannaford2841a532014-11-04 11:05:42 +01008 "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.
14func 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 Hannafordf7e8e1a2014-11-04 11:48:46 +010020
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.
25type 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.
31type CreateOpts struct {
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010032 // Optional - the ID of an existing virtual IP. By doing this, you are
33 // allowing load balancers to share IPV6 addresses.
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010034 ID string
35
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010036 // Optional - the type of address.
37 Type Type
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010038
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010039 // Optional - the version of address.
40 Version Version
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010041}
42
43// ToVIPCreateMap casts a CreateOpts struct to a map.
44func (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 Hannaford491ea5d2014-11-04 11:59:37 +010060// 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 Hannafordf7e8e1a2014-11-04 11:48:46 +010063func 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 Hannaford491ea5d2014-11-04 11:59:37 +010081
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.
85func 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 Hannaford950561c2014-11-12 11:12:20 +010094 url += gophercloud.IDSliceToQueryString("id", vipIDs)
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010095
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.
105func 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 Hannafordc9da4b42014-11-05 16:34:56 +0100109 OkCodes: []int{202},
Jamie Hannaford491ea5d2014-11-04 11:59:37 +0100110 })
111 return res
112}