-
Notifications
You must be signed in to change notification settings - Fork 39
/
service_http.go
809 lines (768 loc) · 21.8 KB
/
service_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Neutron double testing service - HTTP API implementation
package neutronservice
import (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"path"
"strconv"
"strings"
"github.com/go-goose/goose/v3/neutron"
"github.com/go-goose/goose/v3/testservices"
"github.com/go-goose/goose/v3/testservices/identityservice"
)
const (
authToken = "X-Auth-Token"
apiFloatingIPsV2 = "/v2.0/" + neutron.ApiFloatingIPsV2
apiNetworksV2 = "/v2.0/" + neutron.ApiNetworksV2
apiPortsV2 = "/v2.0/" + neutron.ApiPortsV2
apiSubnetsV2 = "/v2.0/" + neutron.ApiSubnetsV2
apiSecurityGroupsV2 = "/v2.0/" + neutron.ApiSecurityGroupsV2
apiSecurityGroupRulesV2 = "/v2.0/" + neutron.ApiSecurityGroupRulesV2
)
// errorResponse defines a single HTTP error response.
type errorResponse struct {
code int
body string
contentType string
errorText string
headers map[string]string
neutron *Neutron
}
// verbatim real Neutron responses (as errors).
var (
errUnauthorized = &errorResponse{
http.StatusUnauthorized,
`401 Unauthorized
This server could not verify that you are authorized to access the ` +
`document you requested. Either you supplied the wrong ` +
`credentials (e.g., bad password), or your browser does ` +
`not understand how to supply the credentials required.
Authentication required
`,
"text/plain; charset=UTF-8",
"unauthorized request",
nil,
nil,
}
errBadRequestMalformedURL = &errorResponse{
http.StatusBadRequest,
`{"badRequest": {"message": "Malformed request url", "code": 400}}`,
"application/json; charset=UTF-8",
"bad request base path or URL",
nil,
nil,
}
errBadRequestIncorrect = &errorResponse{
http.StatusBadRequest,
`{"badRequest": {"message": "The server could not comply with the ` +
`request since it is either malformed or otherwise incorrect.", "code": 400}}`,
"application/json; charset=UTF-8",
"bad request URL",
nil,
nil,
}
errNotFound = &errorResponse{
http.StatusNotFound,
`404 Not Found
The resource could not be found.
`,
"text/plain; charset=UTF-8",
"resource not found",
nil,
nil,
}
errNotFoundJSON = &errorResponse{
http.StatusNotFound,
`{"itemNotFound": {"message": "The resource could not be found.", "code": 404}}`,
"application/json; charset=UTF-8",
"resource not found",
nil,
nil,
}
errNotFoundJSONSG = &errorResponse{
http.StatusNotFound,
`{"itemNotFound": {"message": "Security group $ID$ not found.", "code": 404}}`,
"application/json; charset=UTF-8",
"",
nil,
nil,
}
errNotFoundJSONSGR = &errorResponse{
http.StatusNotFound,
`{"itemNotFound": {"message": "Rule ($ID$) not found.", "code": 404}}`,
"application/json; charset=UTF-8",
"security rule not found",
nil,
nil,
}
errNotFoundJSONP = &errorResponse{
http.StatusNotFound,
`{"itemNotFound": {"message": "Port $ID$ not found.", "code": 404}}`,
"application/json; charset=UTF-8",
"",
nil,
nil,
}
errMultipleChoices = &errorResponse{
http.StatusMultipleChoices,
`{"choices": [{"status": "CURRENT", "media-types": [{"base": ` +
`"application/xml", "type": "application/vnd.openstack.compute+` +
`xml;version=2"}, {"base": "application/json", "type": "application/` +
`vnd.openstack.compute+json;version=2"}], "id": "v2.0", "links": ` +
`[{"href": "$ENDPOINT$$URL$", "rel": "self"}]}]}`,
"application/json",
"multiple URL redirection choices",
nil,
nil,
}
errNoVersion = &errorResponse{
http.StatusOK,
`{"versions": [{"status": "CURRENT", "id": "v2.0", "links": [{"href": "v2.0", "rel": "self"}]}]}`,
"application/json",
"no version specified in URL",
nil,
nil,
}
errNoGroupId = &errorResponse{
errorText: "no security group id given",
}
errNoPortId = &errorResponse{
errorText: "no port id given",
}
errRateLimitExceeded = &errorResponse{
http.StatusRequestEntityTooLarge,
"",
"text/plain; charset=UTF-8",
"too many requests",
// RFC says that Retry-After should be an int, but we don't want to wait an entire second during the test suite.
map[string]string{"Retry-After": "0.001"},
nil,
}
errNoMoreFloatingIPs = &errorResponse{
http.StatusNotFound,
"Zero floating ips available.",
"text/plain; charset=UTF-8",
"zero floating ips available",
nil,
nil,
}
errIPLimitExceeded = &errorResponse{
http.StatusRequestEntityTooLarge,
"Maximum number of floating ips exceeded.",
"text/plain; charset=UTF-8",
"maximum number of floating ips exceeded",
nil,
nil,
}
)
func (e *errorResponse) Error() string {
return e.errorText
}
// requestBody returns the body for the error response, replacing
// $ENDPOINT$, $URL$, $ID$, and $ERROR$ in e.body with the values from
// the request.
func (e *errorResponse) requestBody(r *http.Request) []byte {
url := strings.TrimLeft(r.URL.Path, "/")
body := e.body
if body != "" {
if e.neutron != nil {
body = strings.Replace(body, "$ENDPOINT$", e.neutron.endpointURL(true, "/"), -1)
}
body = strings.Replace(body, "$URL$", url, -1)
body = strings.Replace(body, "$ERROR$", e.Error(), -1)
if slash := strings.LastIndex(url, "/"); slash != -1 {
body = strings.Replace(body, "$ID$", url[slash+1:], -1)
}
}
return []byte(body)
}
func (e *errorResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e.contentType != "" {
w.Header().Set("Content-Type", e.contentType)
}
body := e.requestBody(r)
if e.headers != nil {
for h, v := range e.headers {
w.Header().Set(h, v)
}
}
// workaround for https://code.google.com/p/go/issues/detail?id=4454
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
if e.code != 0 {
w.WriteHeader(e.code)
}
if len(body) > 0 {
w.Write(body)
}
}
type neutronHandler struct {
n *Neutron
method func(n *Neutron, w http.ResponseWriter, r *http.Request) error
}
func userInfo(i identityservice.IdentityService, r *http.Request) (*identityservice.UserInfo, error) {
return i.FindUser(r.Header.Get(authToken))
}
func (h *neutronHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
// handle invalid X-Auth-Token header
_, err := userInfo(h.n.IdentityService, r)
if err != nil {
errUnauthorized.ServeHTTP(w, r)
return
}
// handle trailing slash in the path
if strings.HasSuffix(path, "/") && path != "/" {
errNotFound.ServeHTTP(w, r)
return
}
err = h.method(h.n, w, r)
if err == nil {
return
}
var resp http.Handler
if err == testservices.RateLimitExceededError {
resp = errRateLimitExceeded
} else if err == testservices.NoMoreFloatingIPs {
resp = errNoMoreFloatingIPs
} else if err == testservices.IPLimitExceeded {
resp = errIPLimitExceeded
} else {
resp, _ = err.(http.Handler)
if resp == nil {
code, encodedErr := errorJSONEncode(err)
resp = &errorResponse{
code,
encodedErr,
"application/json",
err.Error(),
nil,
h.n,
}
}
}
resp.ServeHTTP(w, r)
}
func writeResponse(w http.ResponseWriter, code int, body []byte) {
// workaround for https://code.google.com/p/go/issues/detail?id=4454
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(code)
w.Write(body)
}
// sendJSON sends the specified response serialized as JSON.
func sendJSON(code int, resp interface{}, w http.ResponseWriter, r *http.Request) error {
data, err := json.Marshal(resp)
if err != nil {
return err
}
writeResponse(w, code, data)
return nil
}
func (n *Neutron) handler(method func(n *Neutron, w http.ResponseWriter, r *http.Request) error) http.Handler {
return &neutronHandler{n, method}
}
func (n *Neutron) handleRoot(w http.ResponseWriter, r *http.Request) error {
if r.URL.Path == "/" {
return errNoVersion
}
return errMultipleChoices
}
func (n *Neutron) HandleRoot(w http.ResponseWriter, r *http.Request) {
n.handler((*Neutron).handleRoot).ServeHTTP(w, r)
}
// newUUID generates a random UUID conforming to RFC 4122.
func newUUID() (string, error) {
uuid := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, uuid); err != nil {
return "", err
}
uuid[8] = uuid[8]&^0xc0 | 0x80 // variant bits; see section 4.1.1.
uuid[6] = uuid[6]&^0xf0 | 0x40 // version 4; see section 4.1.3.
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}
// processGroupId returns the group if one is specified in the given request,
// either by id or by ?name. If there was no group id specified in the path,
// it returns errNoGroupId
func (n *Neutron) processGroupId(w http.ResponseWriter, r *http.Request) (*neutron.SecurityGroupV2, error) {
groupId := path.Base(r.URL.Path)
apiFunc := path.Base(apiSecurityGroupsV2)
if groupId != apiFunc {
group, err := n.securityGroup(groupId)
if err != nil {
return nil, errNotFoundJSONSG
}
return group, nil
}
return nil, errNoGroupId
}
// handleSecurityGroups handles the /v2.0/security-groups HTTP API.
func (n *Neutron) handleSecurityGroups(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
group, err := n.processGroupId(w, r)
if err == errNoGroupId {
var groups []neutron.SecurityGroupV2
query := r.URL.Query()
if len(query) == 1 {
secGroupName := query["name"][0]
groups, err = n.securityGroupByName(secGroupName)
if err != nil {
return err
}
} else {
groups = n.allSecurityGroups()
}
resp := struct {
Groups []neutron.SecurityGroupV2 `json:"security_groups"`
}{groups}
return sendJSON(http.StatusOK, resp, w, r)
}
if err != nil {
return err
}
resp := struct {
Group neutron.SecurityGroupV2 `json:"security_group"`
}{*group}
return sendJSON(http.StatusOK, resp, w, r)
case "POST":
_, err := n.processGroupId(w, r)
if err != errNoGroupId {
return errNotFound
}
body, err := ioutil.ReadAll(r.Body)
if err != nil || len(body) == 0 {
return errBadRequestIncorrect
}
var req struct {
Group struct {
Name string
Description string
} `json:"security_group"`
}
if err := json.Unmarshal(body, &req); err != nil {
return err
} else {
n.nextGroupId++
nextId := strconv.Itoa(n.nextGroupId)
err = n.addSecurityGroup(neutron.SecurityGroupV2{
Id: nextId,
Name: req.Group.Name,
Description: req.Group.Description,
TenantId: n.TenantId,
})
if err != nil {
return err
}
group, err := n.securityGroup(nextId)
if err != nil {
return err
}
var resp struct {
Group neutron.SecurityGroupV2 `json:"security_group"`
}
resp.Group = *group
return sendJSON(http.StatusCreated, resp, w, r)
}
case "PUT":
group, err := n.processGroupId(w, r)
if err == errNoGroupId {
return errNotFound
}
var req struct {
Group struct {
Name string
Description string
} `json:"security_group"`
}
body, err := ioutil.ReadAll(r.Body)
if err != nil || len(body) == 0 {
return errBadRequestIncorrect
}
if err := json.Unmarshal(body, &req); err != nil {
return err
}
err = n.updateSecurityGroup(neutron.SecurityGroupV2{
Id: group.Id,
Name: req.Group.Name,
Description: req.Group.Description,
TenantId: group.TenantId,
})
if err != nil {
return err
}
group, err = n.securityGroup(group.Id)
if err != nil {
return err
}
var resp struct {
Group neutron.SecurityGroupV2 `json:"security_group"`
}
resp.Group = *group
return sendJSON(http.StatusOK, resp, w, r)
case "DELETE":
if group, err := n.processGroupId(w, r); group != nil {
if err := n.removeSecurityGroup(group.Id); err != nil {
return err
}
writeResponse(w, http.StatusNoContent, nil)
return nil
} else if err == errNoGroupId {
return errNotFound
} else {
return err
}
}
return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path)
}
// handleSecurityGroupRules handles the /v2.0/security-group-rules HTTP API.
func (n *Neutron) handleSecurityGroupRules(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
return errNotFoundJSON
case "POST":
ruleId := path.Base(r.URL.Path)
apiFunc := path.Base(apiSecurityGroupRulesV2)
if ruleId != apiFunc {
return errNotFound
}
body, err := ioutil.ReadAll(r.Body)
if err != nil || len(body) == 0 {
return errBadRequestIncorrect
}
var req struct {
Rule neutron.RuleInfoV2 `json:"security_group_rule"`
}
if err = json.Unmarshal(body, &req); err != nil {
return err
}
inrule := req.Rule
// set default EthernetType for correct comparison
// TODO: we should probably have a neutronmodel func to parse and set default values
// and/or move the duplicate check there
if inrule.EthernetType == "" {
inrule.EthernetType = "IPv4"
}
group, err := n.securityGroup(inrule.ParentGroupId)
if err != nil {
return err // TODO: should be a 4XX error with details
}
for _, r := range group.Rules {
// TODO: this logic is actually wrong, not what neutron does at all
// why are we reimplementing half of neutron/api/openstack in go again?
if r.IPProtocol != nil && *r.IPProtocol == inrule.IPProtocol &&
r.PortRangeMax != nil && *r.PortRangeMax == inrule.PortRangeMax &&
r.PortRangeMin != nil && *r.PortRangeMin == inrule.PortRangeMin &&
r.RemoteIPPrefix != "" && r.RemoteIPPrefix == inrule.RemoteIPPrefix &&
r.EthernetType != "" && r.EthernetType == inrule.EthernetType {
// TODO: Use a proper helper and sane error type
return &errorResponse{
http.StatusBadRequest,
fmt.Sprintf(`{"badRequest": {"message": "This rule already exists in group %s", "code": 400}}`, group.Id),
"application/json; charset=UTF-8",
"rule already exists",
nil,
nil,
}
}
}
n.nextRuleId++
nextId := strconv.Itoa(n.nextRuleId)
err = n.addSecurityGroupRule(nextId, req.Rule)
if err != nil {
return err
}
rule, err := n.securityGroupRule(nextId)
if err != nil {
return err
}
var resp struct {
Rule neutron.SecurityGroupRuleV2 `json:"security_group_rule"`
}
resp.Rule = *rule
return sendJSON(http.StatusCreated, resp, w, r)
case "PUT":
return errNotFound
case "DELETE":
ruleId := path.Base(r.URL.Path)
apiFunc := path.Base(apiSecurityGroupRulesV2)
if ruleId != apiFunc {
if _, err := n.securityGroupRule(ruleId); err != nil {
return errNotFoundJSONSGR
}
if err := n.removeSecurityGroupRule(ruleId); err != nil {
return err
}
writeResponse(w, http.StatusNoContent, nil)
return nil
}
return errNotFound
}
return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path)
}
// processPortId returns the group if one is specified in the given request,
// either by id or by ?name. If there was no group id specified in the path,
// it returns errNoPortId
func (n *Neutron) processPortId(w http.ResponseWriter, r *http.Request) (*neutron.PortV2, error) {
portId := path.Base(r.URL.Path)
apiFunc := path.Base(apiPortsV2)
if portId != apiFunc {
port, err := n.port(portId)
if err != nil {
return nil, errNotFoundJSONP
}
return port, nil
}
return nil, errNoPortId
}
// handlePorts handles the /v2.0/ports HTTP API.
func (n *Neutron) handlePorts(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
port, err := n.processPortId(w, r)
if err == errNoPortId {
resp := struct {
Ports []neutron.PortV2 `json:"ports"`
}{n.allPorts()}
return sendJSON(http.StatusOK, resp, w, r)
}
if err != nil {
return err
}
resp := struct {
Port neutron.PortV2 `json:"port"`
}{*port}
return sendJSON(http.StatusOK, resp, w, r)
case "POST":
_, err := n.processPortId(w, r)
if err != errNoPortId {
return errNotFound
}
body, err := ioutil.ReadAll(r.Body)
if err != nil || len(body) == 0 {
return errBadRequestIncorrect
}
var req struct {
Port neutron.PortV2 `json:"port"`
}
if err := json.Unmarshal(body, &req); err != nil {
return err
} else {
n.nextPortId++
nextId := strconv.Itoa(n.nextPortId)
err = n.addPort(neutron.PortV2{
Id: nextId,
Name: req.Port.Name,
Description: req.Port.Description,
FixedIPs: req.Port.FixedIPs,
NetworkId: req.Port.NetworkId,
Status: req.Port.Status,
Tags: req.Port.Tags,
TenantId: n.TenantId,
})
if err != nil {
return err
}
port, err := n.port(nextId)
if err != nil {
return err
}
var resp struct {
Port neutron.PortV2 `json:"port"`
}
resp.Port = *port
return sendJSON(http.StatusCreated, resp, w, r)
}
case "DELETE":
if port, err := n.processPortId(w, r); port != nil {
if err := n.removePort(port.Id); err != nil {
return err
}
writeResponse(w, http.StatusNoContent, nil)
return nil
} else if err == errNoPortId {
return errNotFound
} else {
return err
}
}
return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path)
}
// handleFloatingIPs handles the v2/floatingips HTTP API.
func (n *Neutron) handleFloatingIPs(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
ipId := path.Base(r.URL.Path)
apiFunc := path.Base(apiFloatingIPsV2)
if ipId != apiFunc {
fip, err := n.floatingIP(ipId)
if err != nil {
return errNotFoundJSON
}
resp := struct {
IP neutron.FloatingIPV2 `json:"floatingip"`
}{*fip}
return sendJSON(http.StatusOK, resp, w, r)
}
f := make(filter)
if err := r.ParseForm(); err == nil && len(r.Form) > 0 {
for filterKey, filterValues := range r.Form {
for _, value := range filterValues {
f[filterKey] = value
}
}
}
fips := n.allFloatingIPs(f)
if len(fips) == 0 {
fips = []neutron.FloatingIPV2{}
}
resp := struct {
IPs []neutron.FloatingIPV2 `json:"floatingips"`
}{fips}
return sendJSON(http.StatusOK, resp, w, r)
case "POST":
ipId := path.Base(r.URL.Path)
apiFunc := path.Base(apiFloatingIPsV2)
if ipId != apiFunc {
return errNotFound
}
body, err := ioutil.ReadAll(r.Body)
if err != nil || len(body) == 0 {
return errBadRequestIncorrect
}
var req struct {
FIP neutron.FloatingIPV2 `json:"floatingip"`
}
if err := json.Unmarshal(body, &req); err != nil {
return err
}
extNetwork, err := n.network(req.FIP.FloatingNetworkId)
if err != nil {
return err
}
if !extNetwork.External {
return errNotFound
}
n.nextIPId++
addr := fmt.Sprintf("10.0.0.%d", n.nextIPId)
nextId := strconv.Itoa(n.nextIPId)
fip := neutron.FloatingIPV2{FixedIP: "", Id: nextId, IP: addr, FloatingNetworkId: extNetwork.Id}
err = n.addFloatingIP(fip)
if err != nil {
return err
}
resp := struct {
FloatingIPV2 neutron.FloatingIPV2 `json:"floatingip"`
}{fip}
return sendJSON(http.StatusCreated, resp, w, r)
case "PUT":
return errNotFound
case "DELETE":
ipId := path.Base(r.URL.Path)
apiFunc := path.Base(apiFloatingIPsV2)
if ipId != apiFunc {
if err := n.removeFloatingIP(ipId); err == nil {
writeResponse(w, http.StatusNoContent, nil)
return nil
}
return errNotFoundJSON
}
return errNotFound
}
return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path)
}
// handleNetworks handles the v2/networks HTTP API.
func (n *Neutron) handleNetworks(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
networkId := path.Base(r.URL.Path)
apiFunc := path.Base(apiNetworksV2)
if networkId != apiFunc {
network, err := n.network(networkId)
if err != nil {
return errNotFoundJSON
}
resp := struct {
Network neutron.NetworkV2 `json:"network"`
}{*network}
return sendJSON(http.StatusOK, resp, w, r)
}
f := make(filter)
if err := r.ParseForm(); err == nil && len(r.Form) > 0 {
for filterKey, filterValues := range r.Form {
for _, value := range filterValues {
f[filterKey] = value
}
}
}
nets, err := n.allNetworks(f)
if err != nil {
return err
}
if len(nets) == 0 {
nets = []neutron.NetworkV2{}
}
resp := struct {
Network []neutron.NetworkV2 `json:"networks"`
}{nets}
return sendJSON(http.StatusOK, resp, w, r)
default:
return errNotFound
}
}
// handleNetworks handles the v2/subnets HTTP API.
func (n *Neutron) handleSubnets(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
subnetId := path.Base(r.URL.Path)
apiFunc := path.Base(apiSubnetsV2)
if subnetId != apiFunc {
subnet, err := n.subnet(subnetId)
if err != nil {
return errNotFoundJSON
}
resp := struct {
Subnet neutron.SubnetV2 `json:"subnet"`
}{*subnet}
return sendJSON(http.StatusOK, resp, w, r)
}
subnets := n.allSubnets()
if len(subnets) == 0 {
subnets = []neutron.SubnetV2{}
}
resp := struct {
Subnets []neutron.SubnetV2 `json:"subnets"`
}{subnets}
return sendJSON(http.StatusOK, resp, w, r)
default:
return errNotFound
}
}
// SetupHTTP attaches all the needed handlers to provide the HTTP API.
func (n *Neutron) SetupHTTP(mux *http.ServeMux) {
handlers := map[string]http.Handler{
"/$v": errNoVersion,
"/$v/": errBadRequestMalformedURL,
"/$v/security-groups": n.handler((*Neutron).handleSecurityGroups),
"/$v/security-groups/": n.handler((*Neutron).handleSecurityGroups),
"/$v/security-group-rules": n.handler((*Neutron).handleSecurityGroupRules),
"/$v/security-group-rules/": n.handler((*Neutron).handleSecurityGroupRules),
"/$v/ports": n.handler((*Neutron).handlePorts),
"/$v/ports/": n.handler((*Neutron).handlePorts),
"/$v/floatingips": n.handler((*Neutron).handleFloatingIPs),
"/$v/floatingips/": n.handler((*Neutron).handleFloatingIPs),
"/$v/networks": n.handler((*Neutron).handleNetworks),
"/$v/networks/": n.handler((*Neutron).handleNetworks),
"/$v/subnets": n.handler((*Neutron).handleSubnets),
"/$v/subnets/": n.handler((*Neutron).handleSubnets),
}
for path, h := range handlers {
path = strings.Replace(path, "$v", n.VersionPath, 1)
mux.Handle(path, h)
}
}
func (n *Neutron) SetupRootHandler(mux *http.ServeMux) {
mux.Handle("/", n.handler((*Neutron).handleRoot))
}