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 | { |
| 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 | ] |
| 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 | |
| 84 | func mockGetGroupsResponse(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) { |
| 87 | th.TestMethod(t, r, "GET") |
| 88 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 89 | |
| 90 | w.Header().Add("Content-Type", "application/json") |
| 91 | w.WriteHeader(http.StatusOK) |
| 92 | |
| 93 | fmt.Fprintf(w, ` |
| 94 | { |
| 95 | "security_group": { |
| 96 | "description": "default", |
| 97 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 98 | "name": "default", |
| 99 | "rules": [ |
| 100 | { |
| 101 | "from_port": 80, |
| 102 | "group": { |
| 103 | "tenant_id": "openstack", |
| 104 | "name": "default" |
| 105 | }, |
| 106 | "ip_protocol": "TCP", |
| 107 | "to_port": 85, |
| 108 | "parent_group_id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 109 | "ip_range": { |
| 110 | "cidr": "0.0.0.0" |
| 111 | }, |
| 112 | "id": "ebe599e2-6b8c-457c-b1ff-a75e48f10923" |
| 113 | } |
| 114 | ], |
| 115 | "tenant_id": "openstack" |
| 116 | } |
| 117 | } |
| 118 | `) |
| 119 | }) |
| 120 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame^] | 121 | |
| 122 | func mockDeleteGroupResponse(t *testing.T, groupID string) { |
| 123 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
| 124 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 125 | th.TestMethod(t, r, "DELETE") |
| 126 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 127 | w.Header().Add("Content-Type", "application/json") |
| 128 | w.WriteHeader(http.StatusAccepted) |
| 129 | }) |
| 130 | } |