blob: e1e92ac631915e49a8c5eb9a5638cf75b3f231f5 [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
7 "github.com/racker/perigee"
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010010)
11
12// List is the operation responsible for returning a paginated collection of
13// network items that define a load balancer's access list.
14func List(client *gophercloud.ServiceClient, lbID int) pagination.Pager {
15 url := rootURL(client, lbID)
16
17 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
18 return AccessListPage{pagination.SinglePageBase(r)}
19 })
20}
21
22// CreateOptsBuilder is the interface responsible for generating the JSON
23// for a Create operation.
24type CreateOptsBuilder interface {
25 ToAccessListCreateMap() (map[string]interface{}, error)
26}
27
28// CreateOpts is a slice of CreateOpt structs, that allow the user to create
29// multiple nodes in a single operation (one node per CreateOpt).
30type CreateOpts []CreateOpt
31
32// CreateOpt represents the options to create a single node.
33type CreateOpt struct {
34 // Required - the IP address or CIDR for item to add to access list.
35 Address string
36
37 // Required - the type of the node. Either ALLOW or DENY.
38 Type Type
39}
40
41// ToAccessListCreateMap converts a slice of options into a map that can be
42// used for the JSON.
43func (opts CreateOpts) ToAccessListCreateMap() (map[string]interface{}, error) {
44 type itemMap map[string]interface{}
45 items := []itemMap{}
46
47 for k, v := range opts {
48 if v.Address == "" {
49 return itemMap{}, fmt.Errorf("Address is a required attribute, none provided for %d CreateOpt element", k)
50 }
51 if v.Type != ALLOW && v.Type != DENY {
52 return itemMap{}, fmt.Errorf("Type must be ALLOW or DENY")
53 }
54
55 item := make(itemMap)
56 item["address"] = v.Address
57 item["type"] = v.Type
58
59 items = append(items, item)
60 }
61
62 return itemMap{"accessList": items}, nil
63}
64
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010065// Create is the operation responsible for adding network items to the access
66// rules for a particular load balancer. If network items already exist, the
67// new item will be appended. A single IP address or subnet range is considered
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +010068// unique and cannot be duplicated.
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010069func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
70 var res CreateResult
71
72 reqBody, err := opts.ToAccessListCreateMap()
73 if err != nil {
74 res.Err = err
75 return res
76 }
77
78 _, res.Err = perigee.Request("POST", rootURL(client, loadBalancerID), perigee.Options{
79 MoreHeaders: client.AuthenticatedHeaders(),
80 ReqBody: &reqBody,
81 OkCodes: []int{202},
82 })
83
84 return res
85}
86
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010087// BulkDelete will delete multiple network items from a load balancer's access
88// list in a single operation.
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010089func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, itemIDs []int) DeleteResult {
90 var res DeleteResult
91
92 if len(itemIDs) > 10 || len(itemIDs) == 0 {
93 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 item IDs")
94 return res
95 }
96
97 url := rootURL(c, loadBalancerID)
Jamie Hannaford950561c2014-11-12 11:12:20 +010098 url += gophercloud.IDSliceToQueryString("id", itemIDs)
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010099
100 _, res.Err = perigee.Request("DELETE", url, perigee.Options{
101 MoreHeaders: c.AuthenticatedHeaders(),
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +0100102 OkCodes: []int{202},
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +0100103 })
104
105 return res
106}
Jamie Hannaford43543b22014-11-04 16:47:40 +0100107
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100108// Delete will remove a single network item from a load balancer's access list.
Jamie Hannaford43543b22014-11-04 16:47:40 +0100109func Delete(c *gophercloud.ServiceClient, lbID, itemID int) DeleteResult {
110 var res DeleteResult
111 _, res.Err = perigee.Request("DELETE", resourceURL(c, lbID, itemID), perigee.Options{
112 MoreHeaders: c.AuthenticatedHeaders(),
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +0100113 OkCodes: []int{202},
Jamie Hannaford43543b22014-11-04 16:47:40 +0100114 })
115 return res
116}
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100117
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100118// DeleteAll will delete the entire contents of a load balancer's access list,
119// effectively resetting it and allowing all traffic.
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100120func DeleteAll(c *gophercloud.ServiceClient, lbID int) DeleteResult {
121 var res DeleteResult
122 _, res.Err = perigee.Request("DELETE", rootURL(c, lbID), perigee.Options{
123 MoreHeaders: c.AuthenticatedHeaders(),
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +0100124 OkCodes: []int{202},
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100125 })
126 return res
127}