blob: d58d908943f581decc3a4d62c82fd3e6a2020509 [file] [log] [blame]
Keith Byrnebda48592016-03-23 11:37:08 +00001// +build fixtures
2
Jamie Hannaford924c09d2014-11-19 12:05:38 +01003package secgroups
4
5import (
6 "fmt"
7 "net/http"
8 "testing"
9
10 th "github.com/rackspace/gophercloud/testhelper"
11 fake "github.com/rackspace/gophercloud/testhelper/client"
12)
13
Jamie Hannaforda493e642014-11-19 12:40:30 +010014const rootPath = "/os-security-groups"
15
Jamie Hannaford19151792014-11-19 12:46:47 +010016const listGroupsJSON = `
17{
Jamie Hannaford334c8752014-11-20 12:05:09 +010018 "security_groups": [
19 {
20 "description": "default",
Jamie Hannaford2f226172014-11-25 11:52:25 +010021 "id": "{groupID}",
Jamie Hannaford334c8752014-11-20 12:05:09 +010022 "name": "default",
23 "rules": [],
24 "tenant_id": "openstack"
25 }
26 ]
Jamie Hannaford19151792014-11-19 12:46:47 +010027}
28`
29
Jamie Hannaford924c09d2014-11-19 12:05:38 +010030func mockListGroupsResponse(t *testing.T) {
Jamie Hannaforda493e642014-11-19 12:40:30 +010031 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford924c09d2014-11-19 12:05:38 +010032 th.TestMethod(t, r, "GET")
33 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
34
35 w.Header().Add("Content-Type", "application/json")
36 w.WriteHeader(http.StatusOK)
37
Jamie Hannaford19151792014-11-19 12:46:47 +010038 fmt.Fprintf(w, listGroupsJSON)
39 })
Jamie Hannaford924c09d2014-11-19 12:05:38 +010040}
Jamie Hannaford19151792014-11-19 12:46:47 +010041
42func mockListGroupsByServerResponse(t *testing.T, serverID string) {
Jon Perritt13dd1422015-02-10 17:35:54 -070043 url := fmt.Sprintf("/servers/%s%s", serverID, rootPath)
Jamie Hannaford19151792014-11-19 12:46:47 +010044 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
45 th.TestMethod(t, r, "GET")
46 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
47
48 w.Header().Add("Content-Type", "application/json")
49 w.WriteHeader(http.StatusOK)
50
51 fmt.Fprintf(w, listGroupsJSON)
Jamie Hannaford924c09d2014-11-19 12:05:38 +010052 })
53}
Jamie Hannaforda493e642014-11-19 12:40:30 +010054
55func mockCreateGroupResponse(t *testing.T) {
56 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
57 th.TestMethod(t, r, "POST")
58 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
59
60 th.TestJSONRequest(t, r, `
61{
62 "security_group": {
63 "name": "test",
64 "description": "something"
65 }
66}
67 `)
68
69 w.Header().Add("Content-Type", "application/json")
70 w.WriteHeader(http.StatusOK)
71
72 fmt.Fprintf(w, `
73{
74 "security_group": {
75 "description": "something",
Jamie Hannaford2f226172014-11-25 11:52:25 +010076 "id": "{groupID}",
Jamie Hannaforda493e642014-11-19 12:40:30 +010077 "name": "test",
78 "rules": [],
79 "tenant_id": "openstack"
80 }
81}
82`)
83 })
84}
Jamie Hannafordb38dd312014-11-19 13:02:11 +010085
Jamie Hannaford2f226172014-11-25 11:52:25 +010086func mockUpdateGroupResponse(t *testing.T, groupID string) {
87 url := fmt.Sprintf("%s/%s", rootPath, groupID)
Jamie Hannaford30c74662014-11-19 15:37:34 +010088 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford740e4a32014-11-19 16:13:30 +010089 th.TestMethod(t, r, "PUT")
Jamie Hannaford30c74662014-11-19 15:37:34 +010090 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
91
92 th.TestJSONRequest(t, r, `
93{
Jamie Hannaford334c8752014-11-20 12:05:09 +010094 "security_group": {
Jamie Hannaford0e750962014-11-24 16:04:38 +010095 "name": "new_name",
96 "description": "new_desc"
Jamie Hannaford334c8752014-11-20 12:05:09 +010097 }
Jamie Hannaford30c74662014-11-19 15:37:34 +010098}
99 `)
100
101 w.Header().Add("Content-Type", "application/json")
102 w.WriteHeader(http.StatusOK)
103
104 fmt.Fprintf(w, `
105{
Jamie Hannaford334c8752014-11-20 12:05:09 +0100106 "security_group": {
107 "description": "something",
Jamie Hannaford2f226172014-11-25 11:52:25 +0100108 "id": "{groupID}",
Jamie Hannaford334c8752014-11-20 12:05:09 +0100109 "name": "new_name",
110 "rules": [],
111 "tenant_id": "openstack"
112 }
Jamie Hannaford30c74662014-11-19 15:37:34 +0100113}
114`)
115 })
116}
117
Jamie Hannaford2f226172014-11-25 11:52:25 +0100118func mockGetGroupsResponse(t *testing.T, groupID string) {
119 url := fmt.Sprintf("%s/%s", rootPath, groupID)
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100120 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
121 th.TestMethod(t, r, "GET")
122 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
123
124 w.Header().Add("Content-Type", "application/json")
125 w.WriteHeader(http.StatusOK)
126
127 fmt.Fprintf(w, `
128{
129 "security_group": {
130 "description": "default",
Jamie Hannaford2f226172014-11-25 11:52:25 +0100131 "id": "{groupID}",
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100132 "name": "default",
133 "rules": [
134 {
135 "from_port": 80,
136 "group": {
137 "tenant_id": "openstack",
138 "name": "default"
139 },
140 "ip_protocol": "TCP",
141 "to_port": 85,
Jamie Hannaford2f226172014-11-25 11:52:25 +0100142 "parent_group_id": "{groupID}",
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100143 "ip_range": {
144 "cidr": "0.0.0.0"
145 },
Jamie Hannaford2f226172014-11-25 11:52:25 +0100146 "id": "{ruleID}"
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100147 }
148 ],
149 "tenant_id": "openstack"
150 }
151}
152 `)
153 })
154}
Jamie Hannafordd276e612014-11-19 13:56:28 +0100155
Jamie Hannafordcb0c19a2014-11-25 11:57:35 +0100156func mockGetNumericIDGroupResponse(t *testing.T, groupID int) {
157 url := fmt.Sprintf("%s/%d", rootPath, groupID)
158 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
159 th.TestMethod(t, r, "GET")
160 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
161
162 w.Header().Add("Content-Type", "application/json")
163 w.WriteHeader(http.StatusOK)
164
165 fmt.Fprintf(w, `
166{
167 "security_group": {
168 "id": 12345
169 }
170}
171 `)
172 })
173}
174
Jamie Hannaford2f226172014-11-25 11:52:25 +0100175func mockDeleteGroupResponse(t *testing.T, groupID string) {
176 url := fmt.Sprintf("%s/%s", rootPath, groupID)
Jamie Hannafordd276e612014-11-19 13:56:28 +0100177 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
178 th.TestMethod(t, r, "DELETE")
179 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
180 w.Header().Add("Content-Type", "application/json")
181 w.WriteHeader(http.StatusAccepted)
182 })
183}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100184
185func mockAddRuleResponse(t *testing.T) {
186 th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) {
187 th.TestMethod(t, r, "POST")
188 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
189
190 th.TestJSONRequest(t, r, `
191{
192 "security_group_rule": {
193 "from_port": 22,
194 "ip_protocol": "TCP",
195 "to_port": 22,
Jamie Hannaford2f226172014-11-25 11:52:25 +0100196 "parent_group_id": "{groupID}",
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100197 "cidr": "0.0.0.0/0"
198 }
199} `)
200
201 w.Header().Add("Content-Type", "application/json")
202 w.WriteHeader(http.StatusOK)
203
204 fmt.Fprintf(w, `
205{
206 "security_group_rule": {
207 "from_port": 22,
208 "group": {},
209 "ip_protocol": "TCP",
210 "to_port": 22,
Jamie Hannaford2f226172014-11-25 11:52:25 +0100211 "parent_group_id": "{groupID}",
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100212 "ip_range": {
213 "cidr": "0.0.0.0/0"
214 },
Jamie Hannaford2f226172014-11-25 11:52:25 +0100215 "id": "{ruleID}"
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100216 }
217}`)
218 })
219}
220
Joe Topjian4f9dce22016-02-28 00:03:37 +0000221func mockAddRuleResponseICMPZero(t *testing.T) {
222 th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) {
223 th.TestMethod(t, r, "POST")
224 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
225
226 th.TestJSONRequest(t, r, `
227{
228 "security_group_rule": {
229 "from_port": 0,
230 "ip_protocol": "ICMP",
231 "to_port": 0,
232 "parent_group_id": "{groupID}",
233 "cidr": "0.0.0.0/0"
234 }
235} `)
236
237 w.Header().Add("Content-Type", "application/json")
238 w.WriteHeader(http.StatusOK)
239
240 fmt.Fprintf(w, `
241{
242 "security_group_rule": {
243 "from_port": 0,
244 "group": {},
245 "ip_protocol": "ICMP",
246 "to_port": 0,
247 "parent_group_id": "{groupID}",
248 "ip_range": {
249 "cidr": "0.0.0.0/0"
250 },
251 "id": "{ruleID}"
252 }
253}`)
254 })
255}
256
Jamie Hannaford2f226172014-11-25 11:52:25 +0100257func mockDeleteRuleResponse(t *testing.T, ruleID string) {
258 url := fmt.Sprintf("/os-security-group-rules/%s", ruleID)
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100259 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
260 th.TestMethod(t, r, "DELETE")
261 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
262 w.Header().Add("Content-Type", "application/json")
263 w.WriteHeader(http.StatusAccepted)
264 })
265}
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100266
267func mockAddServerToGroupResponse(t *testing.T, serverID string) {
268 url := fmt.Sprintf("/servers/%s/action", serverID)
269 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
270 th.TestMethod(t, r, "POST")
271 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
272
273 th.TestJSONRequest(t, r, `
274{
275 "addSecurityGroup": {
276 "name": "test"
277 }
278}
279 `)
280
281 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford5b2feb52014-11-20 12:02:15 +0100282 w.WriteHeader(http.StatusAccepted)
Pratik Mallyaee675fd2015-09-14 14:07:30 -0500283 fmt.Fprintf(w, `{}`)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100284 })
285}
286
287func mockRemoveServerFromGroupResponse(t *testing.T, serverID string) {
288 url := fmt.Sprintf("/servers/%s/action", serverID)
289 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
290 th.TestMethod(t, r, "POST")
291 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
292
293 th.TestJSONRequest(t, r, `
294{
Jamie Hannaford334c8752014-11-20 12:05:09 +0100295 "removeSecurityGroup": {
296 "name": "test"
297 }
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100298}
299 `)
300
301 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford5b2feb52014-11-20 12:02:15 +0100302 w.WriteHeader(http.StatusAccepted)
Pratik Mallyaee675fd2015-09-14 14:07:30 -0500303 fmt.Fprintf(w, `{}`)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100304 })
305}