blob: ffdc74cea74743858c3db24bc8d92a8efd6bcdaf [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"
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010010 "github.com/rackspace/gophercloud/rackspace/lb/v1"
Jamie Hannaford2841a532014-11-04 11:05:42 +010011)
12
13// List is the operation responsible for returning a paginated collection of
14// load balancer virtual IP addresses.
15func 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 Hannafordf7e8e1a2014-11-04 11:48:46 +010021
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.
26type 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.
32type CreateOpts struct {
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010033 // Optional - the ID of an existing virtual IP. By doing this, you are
34 // allowing load balancers to share IPV6 addresses.
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010035 ID string
36
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010037 // Optional - the type of address.
38 Type Type
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010039
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010040 // Optional - the version of address.
41 Version Version
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010042}
43
44// ToVIPCreateMap casts a CreateOpts struct to a map.
45func (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 Hannaford491ea5d2014-11-04 11:59:37 +010061// 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 Hannafordf7e8e1a2014-11-04 11:48:46 +010064func 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 Hannaford491ea5d2014-11-04 11:59:37 +010082
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.
86func 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.
106func 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}