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