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 | |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // List is the operation responsible for returning a paginated collection of |
| 12 | // network items that define a load balancer's access list. |
| 13 | func 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. |
| 23 | type 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). |
| 29 | type CreateOpts []CreateOpt |
| 30 | |
| 31 | // CreateOpt represents the options to create a single node. |
| 32 | type 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. |
| 42 | func (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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 64 | // 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 Hannaford | dfdf0a2 | 2014-11-12 11:06:45 +0100 | [diff] [blame] | 67 | // unique and cannot be duplicated. |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 68 | func 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 Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 77 | _, res.Err = client.Post(rootURL(client, loadBalancerID), reqBody, nil, nil) |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 78 | return res |
| 79 | } |
| 80 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 81 | // BulkDelete will delete multiple network items from a load balancer's access |
| 82 | // list in a single operation. |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 83 | func 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 Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 92 | url += gophercloud.IDSliceToQueryString("id", itemIDs) |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 93 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 94 | _, res.Err = c.Delete(url, nil) |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 95 | return res |
| 96 | } |
Jamie Hannaford | 43543b2 | 2014-11-04 16:47:40 +0100 | [diff] [blame] | 97 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 98 | // 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] | 99 | func Delete(c *gophercloud.ServiceClient, lbID, itemID int) DeleteResult { |
| 100 | var res DeleteResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 101 | _, res.Err = c.Delete(resourceURL(c, lbID, itemID), nil) |
Jamie Hannaford | 43543b2 | 2014-11-04 16:47:40 +0100 | [diff] [blame] | 102 | return res |
| 103 | } |
Jamie Hannaford | ef2d9e1 | 2014-11-04 16:48:52 +0100 | [diff] [blame] | 104 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 105 | // DeleteAll will delete the entire contents of a load balancer's access list, |
| 106 | // effectively resetting it and allowing all traffic. |
Jamie Hannaford | ef2d9e1 | 2014-11-04 16:48:52 +0100 | [diff] [blame] | 107 | func DeleteAll(c *gophercloud.ServiceClient, lbID int) DeleteResult { |
| 108 | var res DeleteResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 109 | _, res.Err = c.Delete(rootURL(c, lbID), nil) |
Jamie Hannaford | ef2d9e1 | 2014-11-04 16:48:52 +0100 | [diff] [blame] | 110 | return res |
| 111 | } |