Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 1 | package secgroups |
| 2 | |
| 3 | import ( |
| 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 Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 12 | const rootPath = "/os-security-groups" |
| 13 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 14 | const listGroupsJSON = ` |
| 15 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 16 | "security_groups": [ |
| 17 | { |
| 18 | "description": "default", |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 19 | "id": "{groupID}", |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 20 | "name": "default", |
| 21 | "rules": [], |
| 22 | "tenant_id": "openstack" |
| 23 | } |
| 24 | ] |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 25 | } |
| 26 | ` |
| 27 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 28 | func mockListGroupsResponse(t *testing.T) { |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 29 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 30 | 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 Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 36 | fmt.Fprintf(w, listGroupsJSON) |
| 37 | }) |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 38 | } |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 39 | |
| 40 | func mockListGroupsByServerResponse(t *testing.T, serverID string) { |
Jon Perritt | 13dd142 | 2015-02-10 17:35:54 -0700 | [diff] [blame] | 41 | url := fmt.Sprintf("/servers/%s%s", serverID, rootPath) |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 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 Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 50 | }) |
| 51 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 52 | |
| 53 | func 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 Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 74 | "id": "{groupID}", |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 75 | "name": "test", |
| 76 | "rules": [], |
| 77 | "tenant_id": "openstack" |
| 78 | } |
| 79 | } |
| 80 | `) |
| 81 | }) |
| 82 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 83 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 84 | func mockUpdateGroupResponse(t *testing.T, groupID string) { |
| 85 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 86 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 87 | th.TestMethod(t, r, "PUT") |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 88 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 89 | |
| 90 | th.TestJSONRequest(t, r, ` |
| 91 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 92 | "security_group": { |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 93 | "name": "new_name", |
| 94 | "description": "new_desc" |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 95 | } |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 96 | } |
| 97 | `) |
| 98 | |
| 99 | w.Header().Add("Content-Type", "application/json") |
| 100 | w.WriteHeader(http.StatusOK) |
| 101 | |
| 102 | fmt.Fprintf(w, ` |
| 103 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 104 | "security_group": { |
| 105 | "description": "something", |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 106 | "id": "{groupID}", |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 107 | "name": "new_name", |
| 108 | "rules": [], |
| 109 | "tenant_id": "openstack" |
| 110 | } |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 111 | } |
| 112 | `) |
| 113 | }) |
| 114 | } |
| 115 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 116 | func mockGetGroupsResponse(t *testing.T, groupID string) { |
| 117 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 118 | 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 Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 129 | "id": "{groupID}", |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 130 | "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 Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 140 | "parent_group_id": "{groupID}", |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 141 | "ip_range": { |
| 142 | "cidr": "0.0.0.0" |
| 143 | }, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 144 | "id": "{ruleID}" |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 145 | } |
| 146 | ], |
| 147 | "tenant_id": "openstack" |
| 148 | } |
| 149 | } |
| 150 | `) |
| 151 | }) |
| 152 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 153 | |
Jamie Hannaford | cb0c19a | 2014-11-25 11:57:35 +0100 | [diff] [blame] | 154 | func mockGetNumericIDGroupResponse(t *testing.T, groupID int) { |
| 155 | url := fmt.Sprintf("%s/%d", rootPath, groupID) |
| 156 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 157 | th.TestMethod(t, r, "GET") |
| 158 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 159 | |
| 160 | w.Header().Add("Content-Type", "application/json") |
| 161 | w.WriteHeader(http.StatusOK) |
| 162 | |
| 163 | fmt.Fprintf(w, ` |
| 164 | { |
| 165 | "security_group": { |
| 166 | "id": 12345 |
| 167 | } |
| 168 | } |
| 169 | `) |
| 170 | }) |
| 171 | } |
| 172 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 173 | func mockDeleteGroupResponse(t *testing.T, groupID string) { |
| 174 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 175 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 176 | th.TestMethod(t, r, "DELETE") |
| 177 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 178 | w.Header().Add("Content-Type", "application/json") |
| 179 | w.WriteHeader(http.StatusAccepted) |
| 180 | }) |
| 181 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 182 | |
| 183 | func mockAddRuleResponse(t *testing.T) { |
| 184 | th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) { |
| 185 | th.TestMethod(t, r, "POST") |
| 186 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 187 | |
| 188 | th.TestJSONRequest(t, r, ` |
| 189 | { |
| 190 | "security_group_rule": { |
| 191 | "from_port": 22, |
| 192 | "ip_protocol": "TCP", |
| 193 | "to_port": 22, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 194 | "parent_group_id": "{groupID}", |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 195 | "cidr": "0.0.0.0/0" |
| 196 | } |
| 197 | } `) |
| 198 | |
| 199 | w.Header().Add("Content-Type", "application/json") |
| 200 | w.WriteHeader(http.StatusOK) |
| 201 | |
| 202 | fmt.Fprintf(w, ` |
| 203 | { |
| 204 | "security_group_rule": { |
| 205 | "from_port": 22, |
| 206 | "group": {}, |
| 207 | "ip_protocol": "TCP", |
| 208 | "to_port": 22, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 209 | "parent_group_id": "{groupID}", |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 210 | "ip_range": { |
| 211 | "cidr": "0.0.0.0/0" |
| 212 | }, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 213 | "id": "{ruleID}" |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 214 | } |
| 215 | }`) |
| 216 | }) |
| 217 | } |
| 218 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 219 | func mockDeleteRuleResponse(t *testing.T, ruleID string) { |
| 220 | url := fmt.Sprintf("/os-security-group-rules/%s", ruleID) |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 221 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 222 | th.TestMethod(t, r, "DELETE") |
| 223 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 224 | w.Header().Add("Content-Type", "application/json") |
| 225 | w.WriteHeader(http.StatusAccepted) |
| 226 | }) |
| 227 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 228 | |
| 229 | func mockAddServerToGroupResponse(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 | { |
| 237 | "addSecurityGroup": { |
| 238 | "name": "test" |
| 239 | } |
| 240 | } |
| 241 | `) |
| 242 | |
| 243 | w.Header().Add("Content-Type", "application/json") |
Jamie Hannaford | 5b2feb5 | 2014-11-20 12:02:15 +0100 | [diff] [blame] | 244 | w.WriteHeader(http.StatusAccepted) |
Pratik Mallya | ee675fd | 2015-09-14 14:07:30 -0500 | [diff] [blame] | 245 | fmt.Fprintf(w, `{}`) |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 246 | }) |
| 247 | } |
| 248 | |
| 249 | func mockRemoveServerFromGroupResponse(t *testing.T, serverID string) { |
| 250 | url := fmt.Sprintf("/servers/%s/action", serverID) |
| 251 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 252 | th.TestMethod(t, r, "POST") |
| 253 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 254 | |
| 255 | th.TestJSONRequest(t, r, ` |
| 256 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 257 | "removeSecurityGroup": { |
| 258 | "name": "test" |
| 259 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 260 | } |
| 261 | `) |
| 262 | |
| 263 | w.Header().Add("Content-Type", "application/json") |
Jamie Hannaford | 5b2feb5 | 2014-11-20 12:02:15 +0100 | [diff] [blame] | 264 | w.WriteHeader(http.StatusAccepted) |
Pratik Mallya | ee675fd | 2015-09-14 14:07:30 -0500 | [diff] [blame] | 265 | fmt.Fprintf(w, `{}`) |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 266 | }) |
| 267 | } |