blob: 61da2b03d9bee230d1587574413d180748c421bf [file] [log] [blame]
Jon Perrittb55847b2015-03-17 20:43:48 -06001package publicips
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7 "time"
8
Jon Perrittb55847b2015-03-17 20:43:48 -06009 "github.com/rackspace/gophercloud/pagination"
10 th "github.com/rackspace/gophercloud/testhelper"
Jon Perrittac0190f2015-03-17 21:30:17 -060011 "github.com/rackspace/gophercloud/testhelper/client"
Jon Perrittb55847b2015-03-17 20:43:48 -060012 fake "github.com/rackspace/gophercloud/testhelper/client"
13)
14
15func TestListIPs(t *testing.T) {
16 th.SetupHTTP()
17 defer th.TeardownHTTP()
18 th.Mux.HandleFunc("/public_ips", func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "GET")
20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
21 th.TestHeader(t, r, "Accept", "application/json")
22
23 w.Header().Set("Content-Type", "application/json")
24 fmt.Fprintf(w, `[
25 {
26 "created": "2014-05-30T03:23:42Z",
27 "cloud_server": {
28 "cloud_network": {
29 "cidr": "192.168.100.0/24",
30 "created": "2014-05-25T01:23:42Z",
31 "id": "07426958-1ebf-4c38-b032-d456820ca21a",
32 "name": "RC-CLOUD",
33 "private_ip_v4": "192.168.100.5",
34 "updated": "2014-05-25T02:28:44Z"
35 },
36 "created": "2014-05-30T02:18:42Z",
37 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
38 "name": "RCv3TestServer1",
39 "updated": "2014-05-30T02:19:18Z"
40 },
41 "id": "2d0f586b-37a7-4ae0-adac-2743d5feb450",
42 "public_ip_v4": "203.0.113.110",
43 "status": "ACTIVE",
44 "status_detail": null,
45 "updated": "2014-05-30T03:24:18Z"
46 }
47 ]`)
48 })
49
50 expected := []PublicIP{
51 PublicIP{
52 ID: "2d0f586b-37a7-4ae0-adac-2743d5feb450",
53 PublicIPv4: "203.0.113.110",
54 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
55 CloudServer: struct {
56 ID string `mapstructure:"id"`
57 Name string `mapstructure:"name"`
58 CloudNetwork struct {
59 ID string `mapstructure:"id"`
60 Name string `mapstructure:"name"`
61 PrivateIPv4 string `mapstructure:"private_ip_v4"`
62 CIDR string `mapstructure:"cidr"`
63 CreatedAt time.Time `mapstructure:"-"`
64 UpdatedAt time.Time `mapstructure:"-"`
65 } `mapstructure:"cloud_network"`
66 CreatedAt time.Time `mapstructure:"-"`
67 UpdatedAt time.Time `mapstructure:"-"`
68 }{
69 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
70 CloudNetwork: struct {
71 ID string `mapstructure:"id"`
72 Name string `mapstructure:"name"`
73 PrivateIPv4 string `mapstructure:"private_ip_v4"`
74 CIDR string `mapstructure:"cidr"`
75 CreatedAt time.Time `mapstructure:"-"`
76 UpdatedAt time.Time `mapstructure:"-"`
77 }{
78 ID: "07426958-1ebf-4c38-b032-d456820ca21a",
79 CIDR: "192.168.100.0/24",
80 CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
81 Name: "RC-CLOUD",
82 PrivateIPv4: "192.168.100.5",
83 UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
84 },
85 CreatedAt: time.Date(2014, 5, 30, 2, 18, 42, 0, time.UTC),
86 Name: "RCv3TestServer1",
87 UpdatedAt: time.Date(2014, 5, 30, 2, 19, 18, 0, time.UTC),
88 },
89 Status: "ACTIVE",
90 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
91 },
92 }
93
94 count := 0
95 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
96 count++
97 actual, err := ExtractPublicIPs(page)
98 th.AssertNoErr(t, err)
99
100 th.CheckDeepEquals(t, expected, actual)
101
102 return true, nil
103 })
104 th.AssertNoErr(t, err)
105 th.CheckEquals(t, count, 1)
106}
107
108func TestCreateIP(t *testing.T) {
109 th.SetupHTTP()
110 defer th.TeardownHTTP()
111 th.Mux.HandleFunc("/public_ips", func(w http.ResponseWriter, r *http.Request) {
112 th.TestMethod(t, r, "POST")
113 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
114 th.TestHeader(t, r, "Accept", "application/json")
115 th.TestJSONRequest(t, r, `
116 {
117 "cloud_server": {
118 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2"
119 }
120 }
121 `)
122
123 w.Header().Set("Content-Type", "application/json")
124 w.WriteHeader(http.StatusCreated)
125 fmt.Fprintf(w, `
126 {
127 "created": "2014-05-30T03:23:42Z",
128 "cloud_server": {
129 "cloud_network": {
130 "cidr": "192.168.100.0/24",
131 "created": "2014-05-25T01:23:42Z",
132 "id": "07426958-1ebf-4c38-b032-d456820ca21a",
133 "name": "RC-CLOUD",
134 "private_ip_v4": "192.168.100.5",
135 "updated": "2014-05-25T02:28:44Z"
136 },
137 "created": "2014-05-30T02:18:42Z",
138 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
139 "name": "RCv3TestServer1",
140 "updated": "2014-05-30T02:19:18Z"
141 },
142 "id": "2d0f586b-37a7-4ae0-adac-2743d5feb450",
143 "status": "ADDING"
144 }`)
145 })
146
147 expected := &PublicIP{
148 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
149 CloudServer: struct {
150 ID string `mapstructure:"id"`
151 Name string `mapstructure:"name"`
152 CloudNetwork struct {
153 ID string `mapstructure:"id"`
154 Name string `mapstructure:"name"`
155 PrivateIPv4 string `mapstructure:"private_ip_v4"`
156 CIDR string `mapstructure:"cidr"`
157 CreatedAt time.Time `mapstructure:"-"`
158 UpdatedAt time.Time `mapstructure:"-"`
159 } `mapstructure:"cloud_network"`
160 CreatedAt time.Time `mapstructure:"-"`
161 UpdatedAt time.Time `mapstructure:"-"`
162 }{
163 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
164 CloudNetwork: struct {
165 ID string `mapstructure:"id"`
166 Name string `mapstructure:"name"`
167 PrivateIPv4 string `mapstructure:"private_ip_v4"`
168 CIDR string `mapstructure:"cidr"`
169 CreatedAt time.Time `mapstructure:"-"`
170 UpdatedAt time.Time `mapstructure:"-"`
171 }{
172 ID: "07426958-1ebf-4c38-b032-d456820ca21a",
173 CIDR: "192.168.100.0/24",
174 CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
175 Name: "RC-CLOUD",
176 PrivateIPv4: "192.168.100.5",
177 UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
178 },
179 CreatedAt: time.Date(2014, 5, 30, 2, 18, 42, 0, time.UTC),
180 Name: "RCv3TestServer1",
181 UpdatedAt: time.Date(2014, 5, 30, 2, 19, 18, 0, time.UTC),
182 },
183 ID: "2d0f586b-37a7-4ae0-adac-2743d5feb450",
184 Status: "ADDING",
185 }
186
187 actual, err := Create(fake.ServiceClient(), "d95ae0c4-6ab8-4873-b82f-f8433840cff2").Extract()
188 th.AssertNoErr(t, err)
189 th.AssertDeepEquals(t, expected, actual)
190}
191
192func TestGetIP(t *testing.T) {
193 th.SetupHTTP()
194 defer th.TeardownHTTP()
195 th.Mux.HandleFunc("/public_ips/2d0f586b-37a7-4ae0-adac-2743d5feb450", func(w http.ResponseWriter, r *http.Request) {
196 th.TestMethod(t, r, "GET")
197 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
198 th.TestHeader(t, r, "Accept", "application/json")
199
200 w.Header().Set("Content-Type", "application/json")
201 w.WriteHeader(http.StatusOK)
202 fmt.Fprintf(w, `
203 {
204 "created": "2014-05-30T03:23:42Z",
205 "cloud_server": {
206 "cloud_network": {
207 "cidr": "192.168.100.0/24",
208 "created": "2014-05-25T01:23:42Z",
209 "id": "07426958-1ebf-4c38-b032-d456820ca21a",
210 "name": "RC-CLOUD",
211 "private_ip_v4": "192.168.100.5",
212 "updated": "2014-05-25T02:28:44Z"
213 },
214 "created": "2014-05-30T02:18:42Z",
215 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
216 "name": "RCv3TestServer1",
217 "updated": "2014-05-30T02:19:18Z"
218 },
219 "id": "2d0f586b-37a7-4ae0-adac-2743d5feb450",
220 "public_ip_v4": "203.0.113.110",
221 "status": "ACTIVE",
222 "status_detail": null,
223 "updated": "2014-05-30T03:24:18Z"
224 }`)
225 })
226
227 expected := &PublicIP{
228 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
229 CloudServer: struct {
230 ID string `mapstructure:"id"`
231 Name string `mapstructure:"name"`
232 CloudNetwork struct {
233 ID string `mapstructure:"id"`
234 Name string `mapstructure:"name"`
235 PrivateIPv4 string `mapstructure:"private_ip_v4"`
236 CIDR string `mapstructure:"cidr"`
237 CreatedAt time.Time `mapstructure:"-"`
238 UpdatedAt time.Time `mapstructure:"-"`
239 } `mapstructure:"cloud_network"`
240 CreatedAt time.Time `mapstructure:"-"`
241 UpdatedAt time.Time `mapstructure:"-"`
242 }{
243 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
244 CloudNetwork: struct {
245 ID string `mapstructure:"id"`
246 Name string `mapstructure:"name"`
247 PrivateIPv4 string `mapstructure:"private_ip_v4"`
248 CIDR string `mapstructure:"cidr"`
249 CreatedAt time.Time `mapstructure:"-"`
250 UpdatedAt time.Time `mapstructure:"-"`
251 }{
252 ID: "07426958-1ebf-4c38-b032-d456820ca21a",
253 CIDR: "192.168.100.0/24",
254 CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
255 Name: "RC-CLOUD",
256 PrivateIPv4: "192.168.100.5",
257 UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
258 },
259 CreatedAt: time.Date(2014, 5, 30, 2, 18, 42, 0, time.UTC),
260 Name: "RCv3TestServer1",
261 UpdatedAt: time.Date(2014, 5, 30, 2, 19, 18, 0, time.UTC),
262 },
263 ID: "2d0f586b-37a7-4ae0-adac-2743d5feb450",
264 Status: "ACTIVE",
265 PublicIPv4: "203.0.113.110",
266 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
267 }
268
269 actual, err := Get(fake.ServiceClient(), "2d0f586b-37a7-4ae0-adac-2743d5feb450").Extract()
270 th.AssertNoErr(t, err)
271 th.AssertDeepEquals(t, expected, actual)
272}
273
274func TestDeleteIP(t *testing.T) {
275 th.SetupHTTP()
276 defer th.TeardownHTTP()
277 th.Mux.HandleFunc("/public_ips/2d0f586b-37a7-4ae0-adac-2743d5feb450", func(w http.ResponseWriter, r *http.Request) {
278 th.TestMethod(t, r, "DELETE")
279 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
280 th.TestHeader(t, r, "Accept", "application/json")
281
282 w.Header().Set("Content-Type", "application/json")
283 w.WriteHeader(http.StatusNoContent)
284 })
285
286 err := Delete(client.ServiceClient(), "2d0f586b-37a7-4ae0-adac-2743d5feb450").ExtractErr()
287 th.AssertNoErr(t, err)
288}
289
290func TestListForServer(t *testing.T) {
291 th.SetupHTTP()
292 defer th.TeardownHTTP()
293 th.Mux.HandleFunc("/public_ips", func(w http.ResponseWriter, r *http.Request) {
294 th.TestMethod(t, r, "GET")
295 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
296 th.TestHeader(t, r, "Accept", "application/json")
297
298 w.Header().Set("Content-Type", "application/json")
299 fmt.Fprintf(w, `
300 [
301 {
302 "created": "2014-05-30T03:23:42Z",
303 "cloud_server": {
304 "cloud_network": {
305 "cidr": "192.168.100.0/24",
306 "created": "2014-05-25T01:23:42Z",
307 "id": "07426958-1ebf-4c38-b032-d456820ca21a",
308 "name": "RC-CLOUD",
309 "private_ip_v4": "192.168.100.5",
310 "updated": "2014-05-25T02:28:44Z"
311 },
312 "created": "2014-05-30T02:18:42Z",
313 "id": "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
314 "name": "RCv3TestServer1",
315 "updated": "2014-05-30T02:19:18Z"
316 },
317 "id": "2d0f586b-37a7-4ae0-adac-2743d5feb450",
318 "public_ip_v4": "203.0.113.110",
319 "status": "ACTIVE",
320 "updated": "2014-05-30T03:24:18Z"
321 }
322 ]`)
323 })
324
325 expected := []PublicIP{
326 PublicIP{
327 CreatedAt: time.Date(2014, 5, 30, 3, 23, 42, 0, time.UTC),
328 CloudServer: struct {
329 ID string `mapstructure:"id"`
330 Name string `mapstructure:"name"`
331 CloudNetwork struct {
332 ID string `mapstructure:"id"`
333 Name string `mapstructure:"name"`
334 PrivateIPv4 string `mapstructure:"private_ip_v4"`
335 CIDR string `mapstructure:"cidr"`
336 CreatedAt time.Time `mapstructure:"-"`
337 UpdatedAt time.Time `mapstructure:"-"`
338 } `mapstructure:"cloud_network"`
339 CreatedAt time.Time `mapstructure:"-"`
340 UpdatedAt time.Time `mapstructure:"-"`
341 }{
342 ID: "d95ae0c4-6ab8-4873-b82f-f8433840cff2",
343 CloudNetwork: struct {
344 ID string `mapstructure:"id"`
345 Name string `mapstructure:"name"`
346 PrivateIPv4 string `mapstructure:"private_ip_v4"`
347 CIDR string `mapstructure:"cidr"`
348 CreatedAt time.Time `mapstructure:"-"`
349 UpdatedAt time.Time `mapstructure:"-"`
350 }{
351 ID: "07426958-1ebf-4c38-b032-d456820ca21a",
352 CIDR: "192.168.100.0/24",
353 CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
354 Name: "RC-CLOUD",
355 PrivateIPv4: "192.168.100.5",
356 UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
357 },
358 CreatedAt: time.Date(2014, 5, 30, 2, 18, 42, 0, time.UTC),
359 Name: "RCv3TestServer1",
360 UpdatedAt: time.Date(2014, 5, 30, 2, 19, 18, 0, time.UTC),
361 },
362 ID: "2d0f586b-37a7-4ae0-adac-2743d5feb450",
363 Status: "ACTIVE",
364 PublicIPv4: "203.0.113.110",
365 UpdatedAt: time.Date(2014, 5, 30, 3, 24, 18, 0, time.UTC),
366 },
367 }
368 count := 0
369 err := ListForServer(fake.ServiceClient(), "d95ae0c4-6ab8-4873-b82f-f8433840cff2").EachPage(func(page pagination.Page) (bool, error) {
370 count++
371 actual, err := ExtractPublicIPs(page)
372 th.AssertNoErr(t, err)
373 th.CheckDeepEquals(t, expected, actual)
374 return true, nil
375 })
376 th.AssertNoErr(t, err)
377 th.CheckEquals(t, count, 1)
378}