blob: d4ce7c01f447a606e83cd7bd00acb39604090218 [file] [log] [blame]
Jamie Hannaford339394c2014-11-04 16:16:21 +01001package acl
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +01002
3import (
4 "errors"
5 "fmt"
6
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +01007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +01009)
10
11// List is the operation responsible for returning a paginated collection of
12// network items that define a load balancer's access list.
13func List(client *gophercloud.ServiceClient, lbID int) pagination.Pager {
14 url := rootURL(client, lbID)
15
16 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
17 return AccessListPage{pagination.SinglePageBase(r)}
18 })
19}
20
21// CreateOptsBuilder is the interface responsible for generating the JSON
22// for a Create operation.
23type CreateOptsBuilder interface {
24 ToAccessListCreateMap() (map[string]interface{}, error)
25}
26
27// CreateOpts is a slice of CreateOpt structs, that allow the user to create
28// multiple nodes in a single operation (one node per CreateOpt).
29type CreateOpts []CreateOpt
30
31// CreateOpt represents the options to create a single node.
32type CreateOpt struct {
33 // Required - the IP address or CIDR for item to add to access list.
34 Address string
35
36 // Required - the type of the node. Either ALLOW or DENY.
37 Type Type
38}
39
40// ToAccessListCreateMap converts a slice of options into a map that can be
41// used for the JSON.
42func (opts CreateOpts) ToAccessListCreateMap() (map[string]interface{}, error) {
43 type itemMap map[string]interface{}
44 items := []itemMap{}
45
46 for k, v := range opts {
47 if v.Address == "" {
48 return itemMap{}, fmt.Errorf("Address is a required attribute, none provided for %d CreateOpt element", k)
49 }
50 if v.Type != ALLOW && v.Type != DENY {
51 return itemMap{}, fmt.Errorf("Type must be ALLOW or DENY")
52 }
53
54 item := make(itemMap)
55 item["address"] = v.Address
56 item["type"] = v.Type
57
58 items = append(items, item)
59 }
60
61 return itemMap{"accessList": items}, nil
62}
63
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010064// Create is the operation responsible for adding network items to the access
65// rules for a particular load balancer. If network items already exist, the
66// new item will be appended. A single IP address or subnet range is considered
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +010067// unique and cannot be duplicated.
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010068func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
69 var res CreateResult
70
71 reqBody, err := opts.ToAccessListCreateMap()
72 if err != nil {
73 res.Err = err
74 return res
75 }
76
Jamie Hannaford5497f942015-03-25 11:55:51 +010077 _, res.Err = client.Post(rootURL(client, loadBalancerID), reqBody, nil, nil)
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010078 return res
79}
80
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010081// BulkDelete will delete multiple network items from a load balancer's access
82// list in a single operation.
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010083func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, itemIDs []int) DeleteResult {
84 var res DeleteResult
85
86 if len(itemIDs) > 10 || len(itemIDs) == 0 {
87 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 item IDs")
88 return res
89 }
90
91 url := rootURL(c, loadBalancerID)
Jamie Hannaford950561c2014-11-12 11:12:20 +010092 url += gophercloud.IDSliceToQueryString("id", itemIDs)
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010093
Jamie Hannaford5497f942015-03-25 11:55:51 +010094 _, res.Err = c.Delete(url, nil)
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010095 return res
96}
Jamie Hannaford43543b22014-11-04 16:47:40 +010097
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010098// Delete will remove a single network item from a load balancer's access list.
Jamie Hannaford43543b22014-11-04 16:47:40 +010099func Delete(c *gophercloud.ServiceClient, lbID, itemID int) DeleteResult {
100 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100101 _, res.Err = c.Delete(resourceURL(c, lbID, itemID), nil)
Jamie Hannaford43543b22014-11-04 16:47:40 +0100102 return res
103}
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100104
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100105// DeleteAll will delete the entire contents of a load balancer's access list,
106// effectively resetting it and allowing all traffic.
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100107func DeleteAll(c *gophercloud.ServiceClient, lbID int) DeleteResult {
108 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100109 _, res.Err = c.Delete(rootURL(c, lbID), nil)
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100110 return res
111}