File: requests.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (97 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package vips

import (
	"errors"

	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/pagination"
)

// List is the operation responsible for returning a paginated collection of
// load balancer virtual IP addresses.
func List(client *gophercloud.ServiceClient, loadBalancerID int) pagination.Pager {
	url := rootURL(client, loadBalancerID)
	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
		return VIPPage{pagination.SinglePageBase(r)}
	})
}

// CreateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Create operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type CreateOptsBuilder interface {
	ToVIPCreateMap() (map[string]interface{}, error)
}

// CreateOpts is the common options struct used in this package's Create
// operation.
type CreateOpts struct {
	// Optional - the ID of an existing virtual IP. By doing this, you are
	// allowing load balancers to share IPV6 addresses.
	ID string

	// Optional - the type of address.
	Type Type

	// Optional - the version of address.
	Version Version
}

// ToVIPCreateMap casts a CreateOpts struct to a map.
func (opts CreateOpts) ToVIPCreateMap() (map[string]interface{}, error) {
	lb := make(map[string]interface{})

	if opts.ID != "" {
		lb["id"] = opts.ID
	}
	if opts.Type != "" {
		lb["type"] = opts.Type
	}
	if opts.Version != "" {
		lb["ipVersion"] = opts.Version
	}

	return lb, nil
}

// Create is the operation responsible for assigning a new Virtual IP to an
// existing load balancer resource. Currently, only version 6 IP addresses may
// be added.
func Create(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) CreateResult {
	var res CreateResult

	reqBody, err := opts.ToVIPCreateMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = c.Post(rootURL(c, lbID), reqBody, &res.Body, nil)
	return res
}

// BulkDelete is the operation responsible for batch deleting multiple VIPs in
// a single operation. It accepts a slice of integer IDs and will remove them
// from the load balancer. The maximum limit is 10 VIP removals at once.
func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, vipIDs []int) DeleteResult {
	var res DeleteResult

	if len(vipIDs) > 10 || len(vipIDs) == 0 {
		res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 VIP IDs")
		return res
	}

	url := rootURL(c, loadBalancerID)
	url += gophercloud.IDSliceToQueryString("id", vipIDs)

	_, res.Err = c.Delete(url, nil)
	return res
}

// Delete is the operation responsible for permanently deleting a VIP.
func Delete(c *gophercloud.ServiceClient, lbID, vipID int) DeleteResult {
	var res DeleteResult
	_, res.Err = c.Delete(resourceURL(c, lbID, vipID), nil)
	return res
}