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", |
| 19 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 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) { |
| 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 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", |
| 74 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 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 | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 84 | func mockUpdateGroupResponse(t *testing.T, groupID string) { |
| 85 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
| 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": { |
| 93 | "name": "new_name" |
| 94 | } |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 95 | } |
| 96 | `) |
| 97 | |
| 98 | w.Header().Add("Content-Type", "application/json") |
| 99 | w.WriteHeader(http.StatusOK) |
| 100 | |
| 101 | fmt.Fprintf(w, ` |
| 102 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame^] | 103 | "security_group": { |
| 104 | "description": "something", |
| 105 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 106 | "name": "new_name", |
| 107 | "rules": [], |
| 108 | "tenant_id": "openstack" |
| 109 | } |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 110 | } |
| 111 | `) |
| 112 | }) |
| 113 | } |
| 114 | |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 115 | func mockGetGroupsResponse(t *testing.T, groupID string) { |
| 116 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
| 117 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 118 | th.TestMethod(t, r, "GET") |
| 119 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 120 | |
| 121 | w.Header().Add("Content-Type", "application/json") |
| 122 | w.WriteHeader(http.StatusOK) |
| 123 | |
| 124 | fmt.Fprintf(w, ` |
| 125 | { |
| 126 | "security_group": { |
| 127 | "description": "default", |
| 128 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 129 | "name": "default", |
| 130 | "rules": [ |
| 131 | { |
| 132 | "from_port": 80, |
| 133 | "group": { |
| 134 | "tenant_id": "openstack", |
| 135 | "name": "default" |
| 136 | }, |
| 137 | "ip_protocol": "TCP", |
| 138 | "to_port": 85, |
| 139 | "parent_group_id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 140 | "ip_range": { |
| 141 | "cidr": "0.0.0.0" |
| 142 | }, |
| 143 | "id": "ebe599e2-6b8c-457c-b1ff-a75e48f10923" |
| 144 | } |
| 145 | ], |
| 146 | "tenant_id": "openstack" |
| 147 | } |
| 148 | } |
| 149 | `) |
| 150 | }) |
| 151 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 152 | |
| 153 | func mockDeleteGroupResponse(t *testing.T, groupID string) { |
| 154 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
| 155 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 156 | th.TestMethod(t, r, "DELETE") |
| 157 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 158 | w.Header().Add("Content-Type", "application/json") |
| 159 | w.WriteHeader(http.StatusAccepted) |
| 160 | }) |
| 161 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 162 | |
| 163 | func mockAddRuleResponse(t *testing.T) { |
| 164 | th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) { |
| 165 | th.TestMethod(t, r, "POST") |
| 166 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 167 | |
| 168 | th.TestJSONRequest(t, r, ` |
| 169 | { |
| 170 | "security_group_rule": { |
| 171 | "from_port": 22, |
| 172 | "ip_protocol": "TCP", |
| 173 | "to_port": 22, |
| 174 | "parent_group_id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 175 | "cidr": "0.0.0.0/0" |
| 176 | } |
| 177 | } `) |
| 178 | |
| 179 | w.Header().Add("Content-Type", "application/json") |
| 180 | w.WriteHeader(http.StatusOK) |
| 181 | |
| 182 | fmt.Fprintf(w, ` |
| 183 | { |
| 184 | "security_group_rule": { |
| 185 | "from_port": 22, |
| 186 | "group": {}, |
| 187 | "ip_protocol": "TCP", |
| 188 | "to_port": 22, |
| 189 | "parent_group_id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 190 | "ip_range": { |
| 191 | "cidr": "0.0.0.0/0" |
| 192 | }, |
| 193 | "id": "f9a97fcf-3a97-47b0-b76f-919136afb7ed" |
| 194 | } |
| 195 | }`) |
| 196 | }) |
| 197 | } |
| 198 | |
| 199 | func mockDeleteRuleResponse(t *testing.T, ruleID string) { |
| 200 | url := fmt.Sprintf("/os-security-group-rules/%s", ruleID) |
| 201 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 202 | th.TestMethod(t, r, "DELETE") |
| 203 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 204 | w.Header().Add("Content-Type", "application/json") |
| 205 | w.WriteHeader(http.StatusAccepted) |
| 206 | }) |
| 207 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 208 | |
| 209 | func mockAddServerToGroupResponse(t *testing.T, serverID string) { |
| 210 | url := fmt.Sprintf("/servers/%s/action", serverID) |
| 211 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 212 | th.TestMethod(t, r, "POST") |
| 213 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 214 | |
| 215 | th.TestJSONRequest(t, r, ` |
| 216 | { |
| 217 | "addSecurityGroup": { |
| 218 | "name": "test" |
| 219 | } |
| 220 | } |
| 221 | `) |
| 222 | |
| 223 | w.Header().Add("Content-Type", "application/json") |
Jamie Hannaford | 5b2feb5 | 2014-11-20 12:02:15 +0100 | [diff] [blame] | 224 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 225 | }) |
| 226 | } |
| 227 | |
| 228 | func mockRemoveServerFromGroupResponse(t *testing.T, serverID string) { |
| 229 | url := fmt.Sprintf("/servers/%s/action", serverID) |
| 230 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 231 | th.TestMethod(t, r, "POST") |
| 232 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 233 | |
| 234 | th.TestJSONRequest(t, r, ` |
| 235 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame^] | 236 | "removeSecurityGroup": { |
| 237 | "name": "test" |
| 238 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 239 | } |
| 240 | `) |
| 241 | |
| 242 | w.Header().Add("Content-Type", "application/json") |
Jamie Hannaford | 5b2feb5 | 2014-11-20 12:02:15 +0100 | [diff] [blame] | 243 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 244 | }) |
| 245 | } |