blob: 82451f756eba75d605a693709846edb9de9db7c0 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
2
Jamie Hannaford0708c002014-09-17 16:08:49 +02003import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jamie Hannaford0708c002014-09-17 16:08:49 +02008 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
10)
11
Jamie Hannaford89f9af22014-09-17 12:21:48 +020012func TestList(t *testing.T) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020013 th.SetupHTTP()
14 defer th.TeardownHTTP()
Jamie Hannaford89f9af22014-09-17 12:21:48 +020015
Jamie Hannaford0708c002014-09-17 16:08:49 +020016 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
17 th.TestMethod(t, r, "GET")
Jamie Hannaford09cc0a72014-10-02 17:28:25 +020018 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannaford0708c002014-09-17 16:08:49 +020019
20 w.Header().Add("Content-Type", "application/json")
21 w.WriteHeader(http.StatusOK)
22
23 fmt.Fprintf(w, `
24{
25 "subnets": [
26 {
27 "name": "private-subnet",
28 "enable_dhcp": true,
29 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
30 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
31 "dns_nameservers": [],
32 "allocation_pools": [
33 {
34 "start": "10.0.0.2",
35 "end": "10.0.0.254"
36 }
37 ],
38 "host_routes": [],
39 "ip_version": 4,
40 "gateway_ip": "10.0.0.1",
41 "cidr": "10.0.0.0/24",
42 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
43 },
44 {
45 "name": "my_subnet",
46 "enable_dhcp": true,
47 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
48 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
49 "dns_nameservers": [],
50 "allocation_pools": [
51 {
52 "start": "192.0.0.2",
53 "end": "192.255.255.254"
54 }
55 ],
56 "host_routes": [],
57 "ip_version": 4,
58 "gateway_ip": "192.0.0.1",
59 "cidr": "192.0.0.0/8",
60 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
61 }
62 ]
63}
64 `)
65 })
66
67 count := 0
68
Jamie Hannaford09cc0a72014-10-02 17:28:25 +020069 List(th.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020070 count++
71 actual, err := ExtractSubnets(page)
72 if err != nil {
73 t.Errorf("Failed to extract subnets: %v", err)
74 return false, nil
75 }
76
77 expected := []Subnet{
78 Subnet{
79 Name: "private-subnet",
80 EnableDHCP: true,
81 NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
82 TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Jamie Hannaford965ae702014-09-22 14:58:19 +020083 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +020084 AllocationPools: []AllocationPool{
85 AllocationPool{
86 Start: "10.0.0.2",
87 End: "10.0.0.254",
88 },
89 },
Jamie Hannafordf2835402014-09-23 11:01:21 +020090 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +020091 IPVersion: 4,
92 GatewayIP: "10.0.0.1",
93 CIDR: "10.0.0.0/24",
94 ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
95 },
96 Subnet{
97 Name: "my_subnet",
98 EnableDHCP: true,
99 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
100 TenantID: "4fd44f30292945e481c7b8a0c8908869",
Jamie Hannaford965ae702014-09-22 14:58:19 +0200101 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200102 AllocationPools: []AllocationPool{
103 AllocationPool{
104 Start: "192.0.0.2",
105 End: "192.255.255.254",
106 },
107 },
Jamie Hannafordf2835402014-09-23 11:01:21 +0200108 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200109 IPVersion: 4,
110 GatewayIP: "192.0.0.1",
111 CIDR: "192.0.0.0/8",
112 ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
113 },
114 }
115
116 th.CheckDeepEquals(t, expected, actual)
117
118 return true, nil
119 })
120
121 if count != 1 {
122 t.Errorf("Expected 1 page, got %d", count)
123 }
124}
125
126func TestGet(t *testing.T) {
127 th.SetupHTTP()
128 defer th.TeardownHTTP()
129
130 th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) {
131 th.TestMethod(t, r, "GET")
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200132 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200133
134 w.Header().Add("Content-Type", "application/json")
135 w.WriteHeader(http.StatusOK)
136
137 fmt.Fprintf(w, `
138{
139 "subnet": {
140 "name": "my_subnet",
141 "enable_dhcp": true,
142 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
143 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
144 "dns_nameservers": [],
145 "allocation_pools": [
146 {
147 "start": "192.0.0.2",
148 "end": "192.255.255.254"
149 }
150 ],
151 "host_routes": [],
152 "ip_version": 4,
153 "gateway_ip": "192.0.0.1",
154 "cidr": "192.0.0.0/8",
155 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
156 }
157}
158 `)
159 })
160
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200161 s, err := Get(th.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
Jamie Hannaford0708c002014-09-17 16:08:49 +0200162 th.AssertNoErr(t, err)
163
164 th.AssertEquals(t, s.Name, "my_subnet")
165 th.AssertEquals(t, s.EnableDHCP, true)
166 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
167 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200168 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200169 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
170 AllocationPool{
171 Start: "192.0.0.2",
172 End: "192.255.255.254",
173 },
174 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200175 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200176 th.AssertEquals(t, s.IPVersion, 4)
177 th.AssertEquals(t, s.GatewayIP, "192.0.0.1")
178 th.AssertEquals(t, s.CIDR, "192.0.0.0/8")
179 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
Jamie Hannaford89f9af22014-09-17 12:21:48 +0200180}
Jamie Hannaford63631432014-09-18 11:40:09 +0200181
182func TestCreate(t *testing.T) {
183 th.SetupHTTP()
184 defer th.TeardownHTTP()
185
186 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
187 th.TestMethod(t, r, "POST")
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200188 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannaford63631432014-09-18 11:40:09 +0200189 th.TestHeader(t, r, "Content-Type", "application/json")
190 th.TestHeader(t, r, "Accept", "application/json")
191 th.TestJSONRequest(t, r, `
192{
193 "subnet": {
194 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
195 "ip_version": 4,
196 "cidr": "192.168.199.0/24"
197 }
198}
199 `)
200
201 w.Header().Add("Content-Type", "application/json")
202 w.WriteHeader(http.StatusCreated)
203
204 fmt.Fprintf(w, `
205{
206 "subnet": {
207 "name": "",
208 "enable_dhcp": true,
209 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
210 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
211 "dns_nameservers": [],
212 "allocation_pools": [
213 {
214 "start": "192.168.199.2",
215 "end": "192.168.199.254"
216 }
217 ],
218 "host_routes": [],
219 "ip_version": 4,
220 "gateway_ip": "192.168.199.1",
221 "cidr": "192.168.199.0/24",
222 "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
223 }
224}
225 `)
226 })
227
Jamie Hannaford965ae702014-09-22 14:58:19 +0200228 opts := CreateOpts{NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", IPVersion: 4, CIDR: "192.168.199.0/24"}
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200229 s, err := Create(th.ServiceClient(), opts).Extract()
Jamie Hannaford63631432014-09-18 11:40:09 +0200230 th.AssertNoErr(t, err)
231
232 th.AssertEquals(t, s.Name, "")
233 th.AssertEquals(t, s.EnableDHCP, true)
234 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
235 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200236 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200237 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
238 AllocationPool{
239 Start: "192.168.199.2",
240 End: "192.168.199.254",
241 },
242 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200243 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200244 th.AssertEquals(t, s.IPVersion, 4)
245 th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
246 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
247 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
248}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200249
250func TestUpdate(t *testing.T) {
251 th.SetupHTTP()
252 defer th.TeardownHTTP()
253
254 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
255 th.TestMethod(t, r, "PUT")
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200256 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200257 th.TestHeader(t, r, "Content-Type", "application/json")
258 th.TestHeader(t, r, "Accept", "application/json")
259 th.TestJSONRequest(t, r, `
260{
261 "subnet": {
262 "name": "my_new_subnet"
263 }
264}
265 `)
266
267 w.Header().Add("Content-Type", "application/json")
268 w.WriteHeader(http.StatusCreated)
269
270 fmt.Fprintf(w, `
271{
272 "subnet": {
273 "name": "my_new_subnet",
274 "enable_dhcp": true,
275 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
276 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
277 "dns_nameservers": [],
278 "allocation_pools": [
279 {
280 "start": "10.0.0.2",
281 "end": "10.0.0.254"
282 }
283 ],
284 "host_routes": [],
285 "ip_version": 4,
286 "gateway_ip": "10.0.0.1",
287 "cidr": "10.0.0.0/24",
288 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
289 }
290}
291 `)
292 })
293
Jamie Hannaford965ae702014-09-22 14:58:19 +0200294 opts := UpdateOpts{Name: "my_new_subnet"}
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200295 s, err := Update(th.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200296 th.AssertNoErr(t, err)
297
298 th.AssertEquals(t, s.Name, "my_new_subnet")
299 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
300}
301
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200302func TestDelete(t *testing.T) {
303 th.SetupHTTP()
304 defer th.TeardownHTTP()
305
306 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
307 th.TestMethod(t, r, "DELETE")
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200308 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200309 w.WriteHeader(http.StatusNoContent)
310 })
311
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200312 res := Delete(th.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
Jamie Hannafordd9036422014-09-23 17:50:24 +0200313 th.AssertNoErr(t, res.Err)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200314}