blob: acd43f2a340e3554a3e395ae484a5289e265a7c2 [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
Jamie Hannaford8badf1e2014-11-19 14:39:26 +01004 "errors"
5
Jamie Hannaforda493e642014-11-19 12:40:30 +01006 "github.com/racker/perigee"
7
Jamie Hannaford924c09d2014-11-19 12:05:38 +01008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
Jamie Hannaford19151792014-11-19 12:46:47 +010012func commonList(client *gophercloud.ServiceClient, url string) pagination.Pager {
Jamie Hannaford924c09d2014-11-19 12:05:38 +010013 createPage := func(r pagination.PageResult) pagination.Page {
14 return SecurityGroupPage{pagination.SinglePageBase(r)}
15 }
16
Jamie Hannaford19151792014-11-19 12:46:47 +010017 return pagination.NewPager(client, url, createPage)
18}
19
20func List(client *gophercloud.ServiceClient) pagination.Pager {
21 return commonList(client, rootURL(client))
22}
23
24func ListByServer(client *gophercloud.ServiceClient, serverID string) pagination.Pager {
25 return commonList(client, listByServerURL(client, serverID))
Jamie Hannaford924c09d2014-11-19 12:05:38 +010026}
Jamie Hannaforda493e642014-11-19 12:40:30 +010027
28type CreateOpts struct {
29 // Optional - the name of your security group. If no value provided, null
30 // will be set.
31 Name string `json:"name,omitempty"`
32
33 // Optional - the description of your security group. If no value provided,
34 // null will be set.
35 Description string `json:"description,omitempty"`
36}
37
38func Create(client *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
39 var result CreateResult
40
41 reqBody := struct {
42 CreateOpts `json:"security_group"`
43 }{opts}
44
45 _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{
46 Results: &result.Body,
47 ReqBody: &reqBody,
48 MoreHeaders: client.AuthenticatedHeaders(),
49 OkCodes: []int{200},
50 })
51
52 return result
53}
Jamie Hannafordb38dd312014-11-19 13:02:11 +010054
55func Get(client *gophercloud.ServiceClient, id string) GetResult {
56 var result GetResult
57
58 _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{
59 Results: &result.Body,
60 MoreHeaders: client.AuthenticatedHeaders(),
61 OkCodes: []int{200},
62 })
63
64 return result
65}
Jamie Hannafordd276e612014-11-19 13:56:28 +010066
67func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
68 var result gophercloud.ErrResult
69
70 _, result.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{
71 MoreHeaders: client.AuthenticatedHeaders(),
72 OkCodes: []int{202},
73 })
74
75 return result
76}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +010077
78type AddRuleOpts struct {
79 // Required - the ID of the group that this rule will be added to.
80 ParentGroupID string `json:"parent_group_id"`
81
82 // Required - the lower bound of the port range that will be opened.
83 FromPort int `json:"from_port"`
84
85 // Required - the upper bound of the port range that will be opened.
86 ToPort int `json:"to_port"`
87
88 // Required - the protocol type that will be allowed, e.g. TCP.
89 IPProtocol string `json:"ip_protocol"`
90
91 // ONLY required if FromGroupID is blank. This represents the IP range that
92 // will be the source of network traffic to your security group. Use
93 // 0.0.0.0/0 to allow all IP addresses.
94 CIDR string `json:"cidr,omitempty"`
95
96 // ONLY required if CIDR is blank. This value represents the ID of a group
97 // that forwards traffic to the parent group. So, instead of accepting
98 // network traffic from an entire IP range, you can instead refine the
99 // inbound source by an existing security group.
100 FromGroupID string `json:"group_id,omitempty"`
101}
102
103func AddRule(client *gophercloud.ServiceClient, opts AddRuleOpts) AddRuleResult {
104 var result AddRuleResult
105
106 if opts.ParentGroupID == "" {
107 result.Err = errors.New("A ParentGroupID must be set")
108 return result
109 }
110 if opts.FromPort == 0 {
111 result.Err = errors.New("A FromPort must be set")
112 return result
113 }
114 if opts.ToPort == 0 {
115 result.Err = errors.New("A ToPort must be set")
116 return result
117 }
118 if opts.IPProtocol == "" {
119 result.Err = errors.New("A IPProtocol must be set")
120 return result
121 }
122 if opts.CIDR == "" && opts.FromGroupID == "" {
123 result.Err = errors.New("A CIDR or FromGroupID must be set")
124 return result
125 }
126
127 reqBody := struct {
128 AddRuleOpts `json:"security_group_rule"`
129 }{opts}
130
131 _, result.Err = perigee.Request("POST", rootRuleURL(client), perigee.Options{
132 Results: &result.Body,
133 ReqBody: &reqBody,
134 MoreHeaders: client.AuthenticatedHeaders(),
135 OkCodes: []int{200},
136 })
137
138 return result
139}
Jamie Hannaford61f81ca2014-11-19 14:44:33 +0100140
141func DeleteRule(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
142 var result gophercloud.ErrResult
143
144 _, result.Err = perigee.Request("DELETE", resourceRuleURL(client, id), perigee.Options{
145 MoreHeaders: client.AuthenticatedHeaders(),
146 OkCodes: []int{202},
147 })
148
149 return result
150}