blob: d45609c3f9609b09393ee324c9611dac85220077 [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
Jamie Hannaforda493e642014-11-19 12:40:30 +010012const rootPath = "/os-security-groups"
13
Jamie Hannaford19151792014-11-19 12:46:47 +010014const listGroupsJSON = `
15{
Jamie Hannaford334c8752014-11-20 12:05:09 +010016 "security_groups": [
17 {
18 "description": "default",
Jamie Hannaford558572f2014-11-24 14:31:57 +010019 "id": 1,
Jamie Hannaford334c8752014-11-20 12:05:09 +010020 "name": "default",
21 "rules": [],
22 "tenant_id": "openstack"
23 }
24 ]
Jamie Hannaford19151792014-11-19 12:46:47 +010025}
26`
27
Jamie Hannaford924c09d2014-11-19 12:05:38 +010028func mockListGroupsResponse(t *testing.T) {
Jamie Hannaforda493e642014-11-19 12:40:30 +010029 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford924c09d2014-11-19 12:05:38 +010030 th.TestMethod(t, r, "GET")
31 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
32
33 w.Header().Add("Content-Type", "application/json")
34 w.WriteHeader(http.StatusOK)
35
Jamie Hannaford19151792014-11-19 12:46:47 +010036 fmt.Fprintf(w, listGroupsJSON)
37 })
Jamie Hannaford924c09d2014-11-19 12:05:38 +010038}
Jamie Hannaford19151792014-11-19 12:46:47 +010039
40func mockListGroupsByServerResponse(t *testing.T, serverID string) {
41 url := fmt.Sprintf("%s/servers/%s%s", rootPath, serverID, rootPath)
42 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
43 th.TestMethod(t, r, "GET")
44 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
45
46 w.Header().Add("Content-Type", "application/json")
47 w.WriteHeader(http.StatusOK)
48
49 fmt.Fprintf(w, listGroupsJSON)
Jamie Hannaford924c09d2014-11-19 12:05:38 +010050 })
51}
Jamie Hannaforda493e642014-11-19 12:40:30 +010052
53func mockCreateGroupResponse(t *testing.T) {
54 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
55 th.TestMethod(t, r, "POST")
56 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
57
58 th.TestJSONRequest(t, r, `
59{
60 "security_group": {
61 "name": "test",
62 "description": "something"
63 }
64}
65 `)
66
67 w.Header().Add("Content-Type", "application/json")
68 w.WriteHeader(http.StatusOK)
69
70 fmt.Fprintf(w, `
71{
72 "security_group": {
73 "description": "something",
Jamie Hannaford558572f2014-11-24 14:31:57 +010074 "id": 1,
Jamie Hannaforda493e642014-11-19 12:40:30 +010075 "name": "test",
76 "rules": [],
77 "tenant_id": "openstack"
78 }
79}
80`)
81 })
82}
Jamie Hannafordb38dd312014-11-19 13:02:11 +010083
Jamie Hannaford558572f2014-11-24 14:31:57 +010084func mockUpdateGroupResponse(t *testing.T, groupID int) {
85 url := fmt.Sprintf("%s/%d", rootPath, groupID)
Jamie Hannaford30c74662014-11-19 15:37:34 +010086 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford740e4a32014-11-19 16:13:30 +010087 th.TestMethod(t, r, "PUT")
Jamie Hannaford30c74662014-11-19 15:37:34 +010088 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
89
90 th.TestJSONRequest(t, r, `
91{
Jamie Hannaford334c8752014-11-20 12:05:09 +010092 "security_group": {
Jamie Hannaford0e750962014-11-24 16:04:38 +010093 "name": "new_name",
94 "description": "new_desc"
Jamie Hannaford334c8752014-11-20 12:05:09 +010095 }
Jamie Hannaford30c74662014-11-19 15:37:34 +010096}
97 `)
98
99 w.Header().Add("Content-Type", "application/json")
100 w.WriteHeader(http.StatusOK)
101
102 fmt.Fprintf(w, `
103{
Jamie Hannaford334c8752014-11-20 12:05:09 +0100104 "security_group": {
105 "description": "something",
Jamie Hannaford558572f2014-11-24 14:31:57 +0100106 "id": 1,
Jamie Hannaford334c8752014-11-20 12:05:09 +0100107 "name": "new_name",
108 "rules": [],
109 "tenant_id": "openstack"
110 }
Jamie Hannaford30c74662014-11-19 15:37:34 +0100111}
112`)
113 })
114}
115
Jamie Hannaford558572f2014-11-24 14:31:57 +0100116func mockGetGroupsResponse(t *testing.T, groupID int) {
117 url := fmt.Sprintf("%s/%d", rootPath, groupID)
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100118 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
119 th.TestMethod(t, r, "GET")
120 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
121
122 w.Header().Add("Content-Type", "application/json")
123 w.WriteHeader(http.StatusOK)
124
125 fmt.Fprintf(w, `
126{
127 "security_group": {
128 "description": "default",
Jamie Hannaford558572f2014-11-24 14:31:57 +0100129 "id": 1,
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100130 "name": "default",
131 "rules": [
132 {
133 "from_port": 80,
134 "group": {
135 "tenant_id": "openstack",
136 "name": "default"
137 },
138 "ip_protocol": "TCP",
139 "to_port": 85,
Jamie Hannaford558572f2014-11-24 14:31:57 +0100140 "parent_group_id": 1,
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100141 "ip_range": {
142 "cidr": "0.0.0.0"
143 },
Jamie Hannaford558572f2014-11-24 14:31:57 +0100144 "id": 2
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100145 }
146 ],
147 "tenant_id": "openstack"
148 }
149}
150 `)
151 })
152}
Jamie Hannafordd276e612014-11-19 13:56:28 +0100153
Jamie Hannaford558572f2014-11-24 14:31:57 +0100154func mockDeleteGroupResponse(t *testing.T, groupID int) {
155 url := fmt.Sprintf("%s/%d", rootPath, groupID)
Jamie Hannafordd276e612014-11-19 13:56:28 +0100156 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
157 th.TestMethod(t, r, "DELETE")
158 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
159 w.Header().Add("Content-Type", "application/json")
160 w.WriteHeader(http.StatusAccepted)
161 })
162}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100163
164func mockAddRuleResponse(t *testing.T) {
165 th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) {
166 th.TestMethod(t, r, "POST")
167 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
168
169 th.TestJSONRequest(t, r, `
170{
171 "security_group_rule": {
172 "from_port": 22,
173 "ip_protocol": "TCP",
174 "to_port": 22,
Jamie Hannaford558572f2014-11-24 14:31:57 +0100175 "parent_group_id": 1,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100176 "cidr": "0.0.0.0/0"
177 }
178} `)
179
180 w.Header().Add("Content-Type", "application/json")
181 w.WriteHeader(http.StatusOK)
182
183 fmt.Fprintf(w, `
184{
185 "security_group_rule": {
186 "from_port": 22,
187 "group": {},
188 "ip_protocol": "TCP",
189 "to_port": 22,
Jamie Hannaford558572f2014-11-24 14:31:57 +0100190 "parent_group_id": 1,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100191 "ip_range": {
192 "cidr": "0.0.0.0/0"
193 },
Jamie Hannaford558572f2014-11-24 14:31:57 +0100194 "id": 2
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100195 }
196}`)
197 })
198}
199
Jamie Hannaford558572f2014-11-24 14:31:57 +0100200func mockDeleteRuleResponse(t *testing.T, ruleID int) {
201 url := fmt.Sprintf("/os-security-group-rules/%d", ruleID)
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100202 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
203 th.TestMethod(t, r, "DELETE")
204 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
205 w.Header().Add("Content-Type", "application/json")
206 w.WriteHeader(http.StatusAccepted)
207 })
208}
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100209
210func mockAddServerToGroupResponse(t *testing.T, serverID string) {
211 url := fmt.Sprintf("/servers/%s/action", serverID)
212 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
213 th.TestMethod(t, r, "POST")
214 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
215
216 th.TestJSONRequest(t, r, `
217{
218 "addSecurityGroup": {
219 "name": "test"
220 }
221}
222 `)
223
224 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford5b2feb52014-11-20 12:02:15 +0100225 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100226 })
227}
228
229func mockRemoveServerFromGroupResponse(t *testing.T, serverID string) {
230 url := fmt.Sprintf("/servers/%s/action", serverID)
231 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
232 th.TestMethod(t, r, "POST")
233 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
234
235 th.TestJSONRequest(t, r, `
236{
Jamie Hannaford334c8752014-11-20 12:05:09 +0100237 "removeSecurityGroup": {
238 "name": "test"
239 }
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100240}
241 `)
242
243 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford5b2feb52014-11-20 12:02:15 +0100244 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100245 })
246}