blob: 2d9dbe48b4052753d73f03159bcc5b16904a727c [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
Jamie Hannaforda493e642014-11-19 12:40:30 +01004 "github.com/racker/perigee"
5
Jamie Hannaford924c09d2014-11-19 12:05:38 +01006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannaford19151792014-11-19 12:46:47 +010010func commonList(client *gophercloud.ServiceClient, url string) pagination.Pager {
Jamie Hannaford924c09d2014-11-19 12:05:38 +010011 createPage := func(r pagination.PageResult) pagination.Page {
12 return SecurityGroupPage{pagination.SinglePageBase(r)}
13 }
14
Jamie Hannaford19151792014-11-19 12:46:47 +010015 return pagination.NewPager(client, url, createPage)
16}
17
18func List(client *gophercloud.ServiceClient) pagination.Pager {
19 return commonList(client, rootURL(client))
20}
21
22func ListByServer(client *gophercloud.ServiceClient, serverID string) pagination.Pager {
23 return commonList(client, listByServerURL(client, serverID))
Jamie Hannaford924c09d2014-11-19 12:05:38 +010024}
Jamie Hannaforda493e642014-11-19 12:40:30 +010025
26type 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
36func 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 Hannafordb38dd312014-11-19 13:02:11 +010052
53func 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 Hannafordd276e612014-11-19 13:56:28 +010064
65func 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}