blob: 397492360653479001a85458e961c2563d643a74 [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
66func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
67 var res CreateResult
68
69 reqBody, err := opts.ToAccessListCreateMap()
70 if err != nil {
71 res.Err = err
72 return res
73 }
74
75 _, res.Err = perigee.Request("POST", rootURL(client, loadBalancerID), perigee.Options{
76 MoreHeaders: client.AuthenticatedHeaders(),
77 ReqBody: &reqBody,
78 OkCodes: []int{202},
79 })
80
81 return res
82}
83
84func BulkDelete(c *gophercloud.ServiceClient, loadBalancerID int, itemIDs []int) DeleteResult {
85 var res DeleteResult
86
87 if len(itemIDs) > 10 || len(itemIDs) == 0 {
88 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 item IDs")
89 return res
90 }
91
92 url := rootURL(c, loadBalancerID)
93 url += v1.IDSliceToQueryString("id", itemIDs)
94
95 _, res.Err = perigee.Request("DELETE", url, perigee.Options{
96 MoreHeaders: c.AuthenticatedHeaders(),
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010097 OkCodes: []int{202},
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010098 })
99
100 return res
101}
Jamie Hannaford43543b22014-11-04 16:47:40 +0100102
103func Delete(c *gophercloud.ServiceClient, lbID, itemID int) DeleteResult {
104 var res DeleteResult
105 _, res.Err = perigee.Request("DELETE", resourceURL(c, lbID, itemID), perigee.Options{
106 MoreHeaders: c.AuthenticatedHeaders(),
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +0100107 OkCodes: []int{202},
Jamie Hannaford43543b22014-11-04 16:47:40 +0100108 })
109 return res
110}
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100111
112func DeleteAll(c *gophercloud.ServiceClient, lbID int) DeleteResult {
113 var res DeleteResult
114 _, res.Err = perigee.Request("DELETE", rootURL(c, lbID), perigee.Options{
115 MoreHeaders: c.AuthenticatedHeaders(),
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +0100116 OkCodes: []int{202},
Jamie Hannafordef2d9e12014-11-04 16:48:52 +0100117 })
118 return res
119}