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 | |
| 66 | func 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 | |
| 84 | func 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(), |
| 97 | OkCodes: []int{200}, |
| 98 | }) |
| 99 | |
| 100 | return res |
| 101 | } |