Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 1 | package secgroups |
| 2 | |
| 3 | import ( |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 4 | "github.com/racker/perigee" |
| 5 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 10 | func commonList(client *gophercloud.ServiceClient, url string) pagination.Pager { |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 11 | createPage := func(r pagination.PageResult) pagination.Page { |
| 12 | return SecurityGroupPage{pagination.SinglePageBase(r)} |
| 13 | } |
| 14 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 15 | return pagination.NewPager(client, url, createPage) |
| 16 | } |
| 17 | |
| 18 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 19 | return commonList(client, rootURL(client)) |
| 20 | } |
| 21 | |
| 22 | func ListByServer(client *gophercloud.ServiceClient, serverID string) pagination.Pager { |
| 23 | return commonList(client, listByServerURL(client, serverID)) |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 24 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 25 | |
| 26 | type CreateOpts struct { |
| 27 | // Optional - the name of your security group. If no value provided, null |
| 28 | // will be set. |
| 29 | Name string `json:"name,omitempty"` |
| 30 | |
| 31 | // Optional - the description of your security group. If no value provided, |
| 32 | // null will be set. |
| 33 | Description string `json:"description,omitempty"` |
| 34 | } |
| 35 | |
| 36 | func Create(client *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 37 | var result CreateResult |
| 38 | |
| 39 | reqBody := struct { |
| 40 | CreateOpts `json:"security_group"` |
| 41 | }{opts} |
| 42 | |
| 43 | _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 44 | Results: &result.Body, |
| 45 | ReqBody: &reqBody, |
| 46 | MoreHeaders: client.AuthenticatedHeaders(), |
| 47 | OkCodes: []int{200}, |
| 48 | }) |
| 49 | |
| 50 | return result |
| 51 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 52 | |
| 53 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 54 | var result GetResult |
| 55 | |
| 56 | _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{ |
| 57 | Results: &result.Body, |
| 58 | MoreHeaders: client.AuthenticatedHeaders(), |
| 59 | OkCodes: []int{200}, |
| 60 | }) |
| 61 | |
| 62 | return result |
| 63 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame^] | 64 | |
| 65 | func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult { |
| 66 | var result gophercloud.ErrResult |
| 67 | |
| 68 | _, result.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{ |
| 69 | MoreHeaders: client.AuthenticatedHeaders(), |
| 70 | OkCodes: []int{202}, |
| 71 | }) |
| 72 | |
| 73 | return result |
| 74 | } |