30
30
31
31
from quantumclient .common import exceptions
32
32
from quantumclient .common .serializer import Serializer
33
-
33
+ from quantumclient .common import utils
34
+
35
+ net_filters_v11_opt = []
36
+ net_filters_v11_opt .append ({'--name' :
37
+ {'help' : _ ('name filter' ), }, })
38
+ net_filters_v11_opt .append ({'--op-status' :
39
+ {'help' : _ ('op-status filter' ), }, })
40
+ net_filters_v11_opt .append ({'--port-op-status' :
41
+ {'help' : _ ('port-op-status filter' ), }, })
42
+ net_filters_v11_opt .append ({'--port-state' :
43
+ {'help' : _ ('port-state filter' ), }, })
44
+ net_filters_v11_opt .append ({'--has-attachment' :
45
+ {'help' : _ ('has-attachment filter' ), }, })
46
+ net_filters_v11_opt .append ({'--attachment' :
47
+ {'help' : _ ('attachment filter' ), }, })
48
+ net_filters_v11_opt .append ({'--port' :
49
+ {'help' : _ ('port filter' ), }, })
50
+
51
+ port_filters_v11_opt = []
52
+ port_filters_v11_opt .append ({'--name' :
53
+ {'help' : _ ('name filter' ), }, })
54
+ port_filters_v11_opt .append ({'--op-status' :
55
+ {'help' : _ ('op-status filter' ), }, })
56
+ port_filters_v11_opt .append ({'--has-attachment' :
57
+ {'help' : _ ('has-attachment filter' ), }, })
58
+ port_filters_v11_opt .append ({'--attachement' :
59
+ {'help' : _ ('attachment filter' ), }, })
60
+
61
+ net_filters_v11 = []
62
+ for arg in net_filters_v11_opt :
63
+ for key in arg .iterkeys ():
64
+ net_filters_v11 .append (key .split ('--' , 2 )[1 ])
65
+
66
+ port_filters_v11 = []
67
+ for arg in port_filters_v11_opt :
68
+ for key in arg .iterkeys ():
69
+ port_filters_v11 .append (key .split ('--' , 2 )[1 ])
34
70
35
71
LOG = logging .getLogger ('quantumclient' )
36
72
37
-
38
73
AUTH_TOKEN_HEADER = "X-Auth-Token"
39
74
40
75
@@ -55,21 +90,19 @@ def exception_handler_v10(status_code, error_content):
55
90
430 : 'portNotFound' ,
56
91
431 : 'requestedStateInvalid' ,
57
92
432 : 'portInUse' ,
58
- 440 : 'alreadyAttached' ,
59
- }
93
+ 440 : 'alreadyAttached' , }
60
94
61
95
quantum_errors = {
62
96
400 : exceptions .BadInputError ,
63
- 401 : exceptions .NotAuthorized ,
97
+ 401 : exceptions .Unauthorized ,
64
98
404 : exceptions .NotFound ,
65
99
420 : exceptions .NetworkNotFoundClient ,
66
100
421 : exceptions .NetworkInUseClient ,
67
101
430 : exceptions .PortNotFoundClient ,
68
102
431 : exceptions .StateInvalidClient ,
69
103
432 : exceptions .PortInUseClient ,
70
104
440 : exceptions .AlreadyAttachedClient ,
71
- 501 : NotImplementedError ,
72
- }
105
+ 501 : NotImplementedError , }
73
106
74
107
# Find real error type
75
108
error_type = None
@@ -105,7 +138,7 @@ def exception_handler_v11(status_code, error_content):
105
138
'RequestedStateInvalid' : exceptions .StateInvalidClient ,
106
139
'PortInUse' : exceptions .PortInUseClient ,
107
140
'AlreadyAttached' : exceptions .AlreadyAttachedClient ,
108
- }
141
+ 'QuantumServiceFault' : exceptions . QuantumClientException , }
109
142
110
143
error_dict = None
111
144
if isinstance (error_content , dict ):
@@ -156,6 +189,33 @@ def with_params(*args, **kwargs):
156
189
return with_params
157
190
158
191
192
+ class APIFilterCall (object ):
193
+
194
+ def __init__ (self , filters ):
195
+ self .filters = filters
196
+
197
+ def __call__ (self , f ):
198
+ def wrapped_f (* args , ** kwargs ):
199
+ """
200
+ Temporarily sets the format and tenant for this request
201
+ """
202
+ instance = args [0 ]
203
+ (format , tenant ) = (instance .format , instance .tenant )
204
+
205
+ if 'format' in kwargs :
206
+ instance .format = kwargs ['format' ]
207
+ if 'format' not in self .filters :
208
+ del kwargs ['format' ]
209
+ if 'tenant' in kwargs :
210
+ instance .tenant = kwargs ['tenant' ]
211
+ if 'tenant' not in self .filters :
212
+ del kwargs ['tenant' ]
213
+ ret = f (* args , ** kwargs )
214
+ (instance .format , instance .tenant ) = (format , tenant )
215
+ return ret
216
+ return wrapped_f
217
+
218
+
159
219
class Client (object ):
160
220
161
221
"""A base client class - derived from Glance.BaseClient"""
@@ -166,13 +226,10 @@ class Client(object):
166
226
"attributes" : {
167
227
"network" : ["id" , "name" ],
168
228
"port" : ["id" , "state" ],
169
- "attachment" : ["id" ]},
229
+ "attachment" : ["id" ], },
170
230
"plurals" : {
171
231
"networks" : "network" ,
172
- "ports" : "port" ,
173
- },
174
- },
175
- }
232
+ "ports" : "port" , }, }, }
176
233
177
234
# Action query strings
178
235
networks_path = "/networks"
@@ -298,19 +355,18 @@ def do_request(self, method, action, body=None,
298
355
headers [AUTH_TOKEN_HEADER ] = self .auth_token
299
356
# Open connection and send request, handling SSL certs
300
357
certs = {'key_file' : self .key_file , 'cert_file' : self .cert_file }
301
- certs = dict ((x , certs [x ]) for x in certs if certs [x ] != None )
358
+ certs = dict ((x , certs [x ]) for x in certs if certs [x ] is not None )
302
359
if self .use_ssl and len (certs ):
303
360
conn = connection_type (self .host , self .port , ** certs )
304
361
else :
305
362
conn = connection_type (self .host , self .port )
306
- # besides HTTP(s)Connection, we still have testConnection
307
- if (LOG .isEnabledFor (logging .DEBUG ) and
308
- isinstance (conn , httplib .HTTPConnection )):
309
- conn .set_debuglevel (1 )
310
-
311
363
res = self ._send_request (conn , method , action , body , headers )
312
364
status_code = self .get_status_code (res )
313
365
data = res .read ()
366
+ utils .http_log (LOG , [method , action ],
367
+ {'body' : body ,
368
+ 'headers' : headers ,
369
+ }, res , data )
314
370
if self .logger :
315
371
self .logger .debug ("Quantum Client Reply (code = %s) :\n %s" %
316
372
(str (status_code ), data ))
@@ -531,30 +587,30 @@ class ClientV11(Client):
531
587
features specific to API v1.1 such as filters
532
588
"""
533
589
534
- @ApiCall
590
+ @APIFilterCall ( net_filters_v11 )
535
591
def list_networks (self , ** filters ):
536
592
"""
537
593
Fetches a list of all networks for a tenant
538
594
"""
539
595
# Pass filters in "params" argument to do_request
540
596
return self .get (self .networks_path , params = filters )
541
597
542
- @ApiCall
598
+ @APIFilterCall ( net_filters_v11 )
543
599
def list_networks_details (self , ** filters ):
544
600
"""
545
601
Fetches a detailed list of all networks for a tenant
546
602
"""
547
603
return self .get (self .networks_path + self .detail_path , params = filters )
548
604
549
- @ApiCall
605
+ @APIFilterCall ( port_filters_v11 )
550
606
def list_ports (self , network , ** filters ):
551
607
"""
552
608
Fetches a list of ports on a given network
553
609
"""
554
610
# Pass filters in "params" argument to do_request
555
611
return self .get (self .ports_path % (network ), params = filters )
556
612
557
- @ApiCall
613
+ @APIFilterCall ( port_filters_v11 )
558
614
def list_ports_details (self , network , ** filters ):
559
615
"""
560
616
Fetches a detailed list of ports on a given network
0 commit comments