Jamie Hannaford | 339394c | 2014-11-04 16:16:21 +0100 | [diff] [blame] | 1 | package acl |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
| 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. |
| 15 | func 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. |
| 25 | type 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). |
| 31 | type CreateOpts []CreateOpt |
| 32 | |
| 33 | // CreateOpt represents the options to create a single node. |
| 34 | type 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. |
| 44 | func (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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 66 | // 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 Hannaford | dfdf0a2 | 2014-11-12 11:06:45 +0100 | [diff] [blame^] | 69 | // unique and cannot be duplicated. |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 70 | func 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 88 | // BulkDelete will delete multiple network items from a load balancer's access |
| 89 | // list in a single operation. |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 90 | func 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 Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 103 | OkCodes: []int{202}, |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 104 | }) |
| 105 | |
| 106 | return res |
| 107 | } |
Jamie Hannaford | 43543b2 | 2014-11-04 16:47:40 +0100 | [diff] [blame] | 108 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 109 | // Delete will remove a single network item from a load balancer's access list. |
Jamie Hannaford | 43543b2 | 2014-11-04 16:47:40 +0100 | [diff] [blame] | 110 | func 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 Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 114 | OkCodes: []int{202}, |
Jamie Hannaford | 43543b2 | 2014-11-04 16:47:40 +0100 | [diff] [blame] | 115 | }) |
| 116 | return res |
| 117 | } |
Jamie Hannaford | ef2d9e1 | 2014-11-04 16:48:52 +0100 | [diff] [blame] | 118 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 119 | // DeleteAll will delete the entire contents of a load balancer's access list, |
| 120 | // effectively resetting it and allowing all traffic. |
Jamie Hannaford | ef2d9e1 | 2014-11-04 16:48:52 +0100 | [diff] [blame] | 121 | func 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 Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 125 | OkCodes: []int{202}, |
Jamie Hannaford | ef2d9e1 | 2014-11-04 16:48:52 +0100 | [diff] [blame] | 126 | }) |
| 127 | return res |
| 128 | } |