blob: d52a73afd47605de4484a95e6609c0c338d38b2b [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 Hannaford2841a532014-11-04 11:05:42 +01006 "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.
12func 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 Hannafordf7e8e1a2014-11-04 11:48:46 +010018
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.
23type 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.
29type CreateOpts struct {
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010030 // Optional - the ID of an existing virtual IP. By doing this, you are
31 // allowing load balancers to share IPV6 addresses.
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010032 ID string
33
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010034 // Optional - the type of address.
35 Type Type
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010036
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010037 // Optional - the version of address.
38 Version Version
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010039}
40
41// ToVIPCreateMap casts a CreateOpts struct to a map.
42func (opts CreateOpts) ToVIPCreateMap() (map[string]interface{}, error) {
43 lb := make(map[string]interface{})
44
45 if opts.ID != "" {
46 lb["id"] = opts.ID
47 }
48 if opts.Type != "" {
49 lb["type"] = opts.Type
50 }
51 if opts.Version != "" {
52 lb["ipVersion"] = opts.Version
53 }
54
55 return lb, nil
56}
57
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010058// Create is the operation responsible for assigning a new Virtual IP to an
59// existing load balancer resource. Currently, only version 6 IP addresses may
60// be added.
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010061func Create(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) CreateResult {
62 var res CreateResult
63
64 reqBody, err := opts.ToVIPCreateMap()
65 if err != nil {
66 res.Err = err
67 return res
68 }
69
Ash Wilson59fb6c42015-02-12 16:21:13 -050070 _, res.Err = c.Request("POST", rootURL(c, lbID), gophercloud.RequestOpts{
71 JSONBody: &reqBody,
72 JSONResponse: &res.Body,
73 OkCodes: []int{202},
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010074 })
75
76 return res
77}
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010078
79// BulkDelete is the operation responsible for batch deleting multiple VIPs in
80// a single operation. It accepts a slice of integer IDs and will remove them
81// from the load balancer. The maximum limit is 10 VIP removals at once.
82func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, vipIDs []int) DeleteResult {
83 var res DeleteResult
84
85 if len(vipIDs) > 10 || len(vipIDs) == 0 {
86 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 VIP IDs")
87 return res
88 }
89
90 url := rootURL(c, loadBalancerID)
Jamie Hannaford950561c2014-11-12 11:12:20 +010091 url += gophercloud.IDSliceToQueryString("id", vipIDs)
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010092
Ash Wilson2199f102015-02-12 16:16:09 -050093 _, res.Err = c.Request("DELETE", url, gophercloud.RequestOpts{
94 OkCodes: []int{202},
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010095 })
96
97 return res
98}
99
100// Delete is the operation responsible for permanently deleting a VIP.
101func Delete(c *gophercloud.ServiceClient, lbID, vipID int) DeleteResult {
102 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500103 _, res.Err = c.Request("DELETE", resourceURL(c, lbID, vipID), gophercloud.RequestOpts{
104 OkCodes: []int{202},
Jamie Hannaford491ea5d2014-11-04 11:59:37 +0100105 })
106 return res
107}