Merge lp:~eday/nova/pep8-fixes-db into lp:~hudson-openstack/nova/trunk
- pep8-fixes-db
- Merge into trunk
Proposed by
Eric Day
Status: | Merged |
---|---|
Approved by: | Eric Day |
Approved revision: | 378 |
Merged at revision: | 379 |
Proposed branch: | lp:~eday/nova/pep8-fixes-db |
Merge into: | lp:~hudson-openstack/nova/trunk |
Prerequisite: | lp:~eday/nova/pep8-fixes-api |
Diff against target: |
1438 lines (+408/-386) 4 files modified
nova/db/api.py (+9/-3) nova/db/sqlalchemy/api.py (+372/-363) nova/db/sqlalchemy/models.py (+25/-19) nova/db/sqlalchemy/session.py (+2/-1) |
To merge this branch: | bzr merge lp:~eday/nova/pep8-fixes-db |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Vish Ishaya (community) | Approve | ||
Jay Pipes (community) | Approve | ||
Review via email: mp+39110@code.launchpad.net |
Commit message
Description of the change
Another pep8 cleanup branch for nova/db, should be merged after lp:~eday/nova/pep8-fixes-api.
To post a comment you must log in.
Revision history for this message
Eric Day (eday) wrote : | # |
I like this way better personally, and as far as what is 'pythonic', pep8 complains about the extra whitespace before the ')' in the old way. There might be a way to suppress it for this file if we really want it the old way, but I hate having exceptions to well-known standards. :)
Revision history for this message
Jay Pipes (jaypipes) wrote : | # |
lgtm. I don't really mind either style, frankly. :) But if this style doesn't piss of PEP8, then let's just go with this.
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'nova/db/api.py' |
2 | --- nova/db/api.py 2010-10-14 06:17:40 +0000 |
3 | +++ nova/db/api.py 2010-10-21 23:19:44 +0000 |
4 | @@ -256,10 +256,12 @@ |
5 | """Get all instances.""" |
6 | return IMPL.instance_get_all(context) |
7 | |
8 | + |
9 | def instance_get_all_by_user(context, user_id): |
10 | """Get all instances.""" |
11 | return IMPL.instance_get_all_by_user(context, user_id) |
12 | |
13 | + |
14 | def instance_get_all_by_project(context, project_id): |
15 | """Get all instance belonging to a project.""" |
16 | return IMPL.instance_get_all_by_project(context, project_id) |
17 | @@ -306,7 +308,8 @@ |
18 | |
19 | def instance_add_security_group(context, instance_id, security_group_id): |
20 | """Associate the given security group with the given instance""" |
21 | - return IMPL.instance_add_security_group(context, instance_id, security_group_id) |
22 | + return IMPL.instance_add_security_group(context, instance_id, |
23 | + security_group_id) |
24 | |
25 | |
26 | ################### |
27 | @@ -482,10 +485,12 @@ |
28 | """Destroy an auth token""" |
29 | return IMPL.auth_destroy_token(context, token) |
30 | |
31 | + |
32 | def auth_get_token(context, token_hash): |
33 | """Retrieves a token given the hash representing it""" |
34 | return IMPL.auth_get_token(context, token_hash) |
35 | |
36 | + |
37 | def auth_create_token(context, token): |
38 | """Creates a new token""" |
39 | return IMPL.auth_create_token(context, token) |
40 | @@ -644,7 +649,9 @@ |
41 | |
42 | def security_group_rule_get_by_security_group(context, security_group_id): |
43 | """Get all rules for a a given security group""" |
44 | - return IMPL.security_group_rule_get_by_security_group(context, security_group_id) |
45 | + return IMPL.security_group_rule_get_by_security_group(context, |
46 | + security_group_id) |
47 | + |
48 | |
49 | def security_group_rule_destroy(context, security_group_rule_id): |
50 | """Deletes a security group rule""" |
51 | @@ -767,4 +774,3 @@ |
52 | network host |
53 | """ |
54 | return IMPL.host_get_networks(context, host) |
55 | - |
56 | |
57 | === modified file 'nova/db/sqlalchemy/api.py' |
58 | --- nova/db/sqlalchemy/api.py 2010-10-14 06:26:58 +0000 |
59 | +++ nova/db/sqlalchemy/api.py 2010-10-21 23:19:44 +0000 |
60 | @@ -124,10 +124,10 @@ |
61 | if not session: |
62 | session = get_session() |
63 | |
64 | - result = session.query(models.Service |
65 | - ).filter_by(id=service_id |
66 | - ).filter_by(deleted=can_read_deleted(context) |
67 | - ).first() |
68 | + result = session.query(models.Service).\ |
69 | + filter_by(id=service_id).\ |
70 | + filter_by(deleted=can_read_deleted(context)).\ |
71 | + first() |
72 | |
73 | if not result: |
74 | raise exception.NotFound('No service for id %s' % service_id) |
75 | @@ -138,23 +138,23 @@ |
76 | @require_admin_context |
77 | def service_get_all_by_topic(context, topic): |
78 | session = get_session() |
79 | - return session.query(models.Service |
80 | - ).filter_by(deleted=False |
81 | - ).filter_by(disabled=False |
82 | - ).filter_by(topic=topic |
83 | - ).all() |
84 | + return session.query(models.Service).\ |
85 | + filter_by(deleted=False).\ |
86 | + filter_by(disabled=False).\ |
87 | + filter_by(topic=topic).\ |
88 | + all() |
89 | |
90 | |
91 | @require_admin_context |
92 | def _service_get_all_topic_subquery(context, session, topic, subq, label): |
93 | sort_value = getattr(subq.c, label) |
94 | - return session.query(models.Service, func.coalesce(sort_value, 0) |
95 | - ).filter_by(topic=topic |
96 | - ).filter_by(deleted=False |
97 | - ).filter_by(disabled=False |
98 | - ).outerjoin((subq, models.Service.host == subq.c.host) |
99 | - ).order_by(sort_value |
100 | - ).all() |
101 | + return session.query(models.Service, func.coalesce(sort_value, 0)).\ |
102 | + filter_by(topic=topic).\ |
103 | + filter_by(deleted=False).\ |
104 | + filter_by(disabled=False).\ |
105 | + outerjoin((subq, models.Service.host == subq.c.host)).\ |
106 | + order_by(sort_value).\ |
107 | + all() |
108 | |
109 | |
110 | @require_admin_context |
111 | @@ -171,10 +171,10 @@ |
112 | topic = 'compute' |
113 | label = 'instance_cores' |
114 | subq = session.query(models.Instance.host, |
115 | - func.sum(models.Instance.vcpus).label(label) |
116 | - ).filter_by(deleted=False |
117 | - ).group_by(models.Instance.host |
118 | - ).subquery() |
119 | + func.sum(models.Instance.vcpus).label(label)).\ |
120 | + filter_by(deleted=False).\ |
121 | + group_by(models.Instance.host).\ |
122 | + subquery() |
123 | return _service_get_all_topic_subquery(context, |
124 | session, |
125 | topic, |
126 | @@ -189,10 +189,10 @@ |
127 | topic = 'network' |
128 | label = 'network_count' |
129 | subq = session.query(models.Network.host, |
130 | - func.count(models.Network.id).label(label) |
131 | - ).filter_by(deleted=False |
132 | - ).group_by(models.Network.host |
133 | - ).subquery() |
134 | + func.count(models.Network.id).label(label)).\ |
135 | + filter_by(deleted=False).\ |
136 | + group_by(models.Network.host).\ |
137 | + subquery() |
138 | return _service_get_all_topic_subquery(context, |
139 | session, |
140 | topic, |
141 | @@ -207,10 +207,10 @@ |
142 | topic = 'volume' |
143 | label = 'volume_gigabytes' |
144 | subq = session.query(models.Volume.host, |
145 | - func.sum(models.Volume.size).label(label) |
146 | - ).filter_by(deleted=False |
147 | - ).group_by(models.Volume.host |
148 | - ).subquery() |
149 | + func.sum(models.Volume.size).label(label)).\ |
150 | + filter_by(deleted=False).\ |
151 | + group_by(models.Volume.host).\ |
152 | + subquery() |
153 | return _service_get_all_topic_subquery(context, |
154 | session, |
155 | topic, |
156 | @@ -221,11 +221,11 @@ |
157 | @require_admin_context |
158 | def service_get_by_args(context, host, binary): |
159 | session = get_session() |
160 | - result = session.query(models.Service |
161 | - ).filter_by(host=host |
162 | - ).filter_by(binary=binary |
163 | - ).filter_by(deleted=can_read_deleted(context) |
164 | - ).first() |
165 | + result = session.query(models.Service).\ |
166 | + filter_by(host=host).\ |
167 | + filter_by(binary=binary).\ |
168 | + filter_by(deleted=can_read_deleted(context)).\ |
169 | + first() |
170 | if not result: |
171 | raise exception.NotFound('No service for %s, %s' % (host, binary)) |
172 | |
173 | @@ -259,13 +259,13 @@ |
174 | authorize_project_context(context, project_id) |
175 | session = get_session() |
176 | with session.begin(): |
177 | - floating_ip_ref = session.query(models.FloatingIp |
178 | - ).filter_by(host=host |
179 | - ).filter_by(fixed_ip_id=None |
180 | - ).filter_by(project_id=None |
181 | - ).filter_by(deleted=False |
182 | - ).with_lockmode('update' |
183 | - ).first() |
184 | + floating_ip_ref = session.query(models.FloatingIp).\ |
185 | + filter_by(host=host).\ |
186 | + filter_by(fixed_ip_id=None).\ |
187 | + filter_by(project_id=None).\ |
188 | + filter_by(deleted=False).\ |
189 | + with_lockmode('update').\ |
190 | + first() |
191 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
192 | # then this has concurrency issues |
193 | if not floating_ip_ref: |
194 | @@ -288,10 +288,10 @@ |
195 | def floating_ip_count_by_project(context, project_id): |
196 | authorize_project_context(context, project_id) |
197 | session = get_session() |
198 | - return session.query(models.FloatingIp |
199 | - ).filter_by(project_id=project_id |
200 | - ).filter_by(deleted=False |
201 | - ).count() |
202 | + return session.query(models.FloatingIp).\ |
203 | + filter_by(project_id=project_id).\ |
204 | + filter_by(deleted=False).\ |
205 | + count() |
206 | |
207 | |
208 | @require_context |
209 | @@ -354,31 +354,31 @@ |
210 | @require_admin_context |
211 | def floating_ip_get_all(context): |
212 | session = get_session() |
213 | - return session.query(models.FloatingIp |
214 | - ).options(joinedload_all('fixed_ip.instance') |
215 | - ).filter_by(deleted=False |
216 | - ).all() |
217 | + return session.query(models.FloatingIp).\ |
218 | + options(joinedload_all('fixed_ip.instance')).\ |
219 | + filter_by(deleted=False).\ |
220 | + all() |
221 | |
222 | |
223 | @require_admin_context |
224 | def floating_ip_get_all_by_host(context, host): |
225 | session = get_session() |
226 | - return session.query(models.FloatingIp |
227 | - ).options(joinedload_all('fixed_ip.instance') |
228 | - ).filter_by(host=host |
229 | - ).filter_by(deleted=False |
230 | - ).all() |
231 | + return session.query(models.FloatingIp).\ |
232 | + options(joinedload_all('fixed_ip.instance')).\ |
233 | + filter_by(host=host).\ |
234 | + filter_by(deleted=False).\ |
235 | + all() |
236 | |
237 | |
238 | @require_context |
239 | def floating_ip_get_all_by_project(context, project_id): |
240 | authorize_project_context(context, project_id) |
241 | session = get_session() |
242 | - return session.query(models.FloatingIp |
243 | - ).options(joinedload_all('fixed_ip.instance') |
244 | - ).filter_by(project_id=project_id |
245 | - ).filter_by(deleted=False |
246 | - ).all() |
247 | + return session.query(models.FloatingIp).\ |
248 | + options(joinedload_all('fixed_ip.instance')).\ |
249 | + filter_by(project_id=project_id).\ |
250 | + filter_by(deleted=False).\ |
251 | + all() |
252 | |
253 | |
254 | @require_context |
255 | @@ -387,10 +387,10 @@ |
256 | if not session: |
257 | session = get_session() |
258 | |
259 | - result = session.query(models.FloatingIp |
260 | - ).filter_by(address=address |
261 | - ).filter_by(deleted=can_read_deleted(context) |
262 | - ).first() |
263 | + result = session.query(models.FloatingIp).\ |
264 | + filter_by(address=address).\ |
265 | + filter_by(deleted=can_read_deleted(context)).\ |
266 | + first() |
267 | if not result: |
268 | raise exception.NotFound('No fixed ip for address %s' % address) |
269 | |
270 | @@ -405,12 +405,12 @@ |
271 | session = get_session() |
272 | with session.begin(): |
273 | instance = instance_get(context, instance_id, session=session) |
274 | - fixed_ip_ref = session.query(models.FixedIp |
275 | - ).filter_by(address=address |
276 | - ).filter_by(deleted=False |
277 | - ).filter_by(instance=None |
278 | - ).with_lockmode('update' |
279 | - ).first() |
280 | + fixed_ip_ref = session.query(models.FixedIp).\ |
281 | + filter_by(address=address).\ |
282 | + filter_by(deleted=False).\ |
283 | + filter_by(instance=None).\ |
284 | + with_lockmode('update').\ |
285 | + first() |
286 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
287 | # then this has concurrency issues |
288 | if not fixed_ip_ref: |
289 | @@ -425,13 +425,13 @@ |
290 | with session.begin(): |
291 | network_or_none = or_(models.FixedIp.network_id == network_id, |
292 | models.FixedIp.network_id == None) |
293 | - fixed_ip_ref = session.query(models.FixedIp |
294 | - ).filter(network_or_none |
295 | - ).filter_by(reserved=False |
296 | - ).filter_by(deleted=False |
297 | - ).filter_by(instance=None |
298 | - ).with_lockmode('update' |
299 | - ).first() |
300 | + fixed_ip_ref = session.query(models.FixedIp).\ |
301 | + filter(network_or_none).\ |
302 | + filter_by(reserved=False).\ |
303 | + filter_by(deleted=False).\ |
304 | + filter_by(instance=None).\ |
305 | + with_lockmode('update').\ |
306 | + first() |
307 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
308 | # then this has concurrency issues |
309 | if not fixed_ip_ref: |
310 | @@ -455,6 +455,7 @@ |
311 | fixed_ip_ref.save() |
312 | return fixed_ip_ref['address'] |
313 | |
314 | + |
315 | @require_context |
316 | def fixed_ip_disassociate(context, address): |
317 | session = get_session() |
318 | @@ -465,6 +466,7 @@ |
319 | fixed_ip_ref.instance = None |
320 | fixed_ip_ref.save(session=session) |
321 | |
322 | + |
323 | @require_admin_context |
324 | def fixed_ip_disassociate_all_by_timeout(_context, host, time): |
325 | session = get_session() |
326 | @@ -486,12 +488,12 @@ |
327 | def fixed_ip_get_by_address(context, address, session=None): |
328 | if not session: |
329 | session = get_session() |
330 | - result = session.query(models.FixedIp |
331 | - ).filter_by(address=address |
332 | - ).filter_by(deleted=can_read_deleted(context) |
333 | - ).options(joinedload('network') |
334 | - ).options(joinedload('instance') |
335 | - ).first() |
336 | + result = session.query(models.FixedIp).\ |
337 | + filter_by(address=address).\ |
338 | + filter_by(deleted=can_read_deleted(context)).\ |
339 | + options(joinedload('network')).\ |
340 | + options(joinedload('instance')).\ |
341 | + first() |
342 | if not result: |
343 | raise exception.NotFound('No floating ip for address %s' % address) |
344 | |
345 | @@ -552,10 +554,10 @@ |
346 | def instance_data_get_for_project(context, project_id): |
347 | session = get_session() |
348 | result = session.query(func.count(models.Instance.id), |
349 | - func.sum(models.Instance.vcpus) |
350 | - ).filter_by(project_id=project_id |
351 | - ).filter_by(deleted=False |
352 | - ).first() |
353 | + func.sum(models.Instance.vcpus)).\ |
354 | + filter_by(project_id=project_id).\ |
355 | + filter_by(deleted=False).\ |
356 | + first() |
357 | # NOTE(vish): convert None to 0 |
358 | return (result[0] or 0, result[1] or 0) |
359 | |
360 | @@ -575,18 +577,18 @@ |
361 | result = None |
362 | |
363 | if is_admin_context(context): |
364 | - result = session.query(models.Instance |
365 | - ).options(joinedload('security_groups') |
366 | - ).filter_by(id=instance_id |
367 | - ).filter_by(deleted=can_read_deleted(context) |
368 | - ).first() |
369 | + result = session.query(models.Instance).\ |
370 | + options(joinedload('security_groups')).\ |
371 | + filter_by(id=instance_id).\ |
372 | + filter_by(deleted=can_read_deleted(context)).\ |
373 | + first() |
374 | elif is_user_context(context): |
375 | - result = session.query(models.Instance |
376 | - ).options(joinedload('security_groups') |
377 | - ).filter_by(project_id=context.project_id |
378 | - ).filter_by(id=instance_id |
379 | - ).filter_by(deleted=False |
380 | - ).first() |
381 | + result = session.query(models.Instance).\ |
382 | + options(joinedload('security_groups')).\ |
383 | + filter_by(project_id=context.project_id).\ |
384 | + filter_by(id=instance_id).\ |
385 | + filter_by(deleted=False).\ |
386 | + first() |
387 | if not result: |
388 | raise exception.NotFound('No instance for id %s' % instance_id) |
389 | |
390 | @@ -596,22 +598,22 @@ |
391 | @require_admin_context |
392 | def instance_get_all(context): |
393 | session = get_session() |
394 | - return session.query(models.Instance |
395 | - ).options(joinedload_all('fixed_ip.floating_ips') |
396 | - ).options(joinedload('security_groups') |
397 | - ).filter_by(deleted=can_read_deleted(context) |
398 | - ).all() |
399 | + return session.query(models.Instance).\ |
400 | + options(joinedload_all('fixed_ip.floating_ips')).\ |
401 | + options(joinedload('security_groups')).\ |
402 | + filter_by(deleted=can_read_deleted(context)).\ |
403 | + all() |
404 | |
405 | |
406 | @require_admin_context |
407 | def instance_get_all_by_user(context, user_id): |
408 | session = get_session() |
409 | - return session.query(models.Instance |
410 | - ).options(joinedload_all('fixed_ip.floating_ips') |
411 | - ).options(joinedload('security_groups') |
412 | - ).filter_by(deleted=can_read_deleted(context) |
413 | - ).filter_by(user_id=user_id |
414 | - ).all() |
415 | + return session.query(models.Instance).\ |
416 | + options(joinedload_all('fixed_ip.floating_ips')).\ |
417 | + options(joinedload('security_groups')).\ |
418 | + filter_by(deleted=can_read_deleted(context)).\ |
419 | + filter_by(user_id=user_id).\ |
420 | + all() |
421 | |
422 | |
423 | @require_context |
424 | @@ -619,12 +621,12 @@ |
425 | authorize_project_context(context, project_id) |
426 | |
427 | session = get_session() |
428 | - return session.query(models.Instance |
429 | - ).options(joinedload_all('fixed_ip.floating_ips') |
430 | - ).options(joinedload('security_groups') |
431 | - ).filter_by(project_id=project_id |
432 | - ).filter_by(deleted=can_read_deleted(context) |
433 | - ).all() |
434 | + return session.query(models.Instance).\ |
435 | + options(joinedload_all('fixed_ip.floating_ips')).\ |
436 | + options(joinedload('security_groups')).\ |
437 | + filter_by(project_id=project_id).\ |
438 | + filter_by(deleted=can_read_deleted(context)).\ |
439 | + all() |
440 | |
441 | |
442 | @require_context |
443 | @@ -632,20 +634,20 @@ |
444 | session = get_session() |
445 | |
446 | if is_admin_context(context): |
447 | - return session.query(models.Instance |
448 | - ).options(joinedload_all('fixed_ip.floating_ips') |
449 | - ).options(joinedload('security_groups') |
450 | - ).filter_by(reservation_id=reservation_id |
451 | - ).filter_by(deleted=can_read_deleted(context) |
452 | - ).all() |
453 | + return session.query(models.Instance).\ |
454 | + options(joinedload_all('fixed_ip.floating_ips')).\ |
455 | + options(joinedload('security_groups')).\ |
456 | + filter_by(reservation_id=reservation_id).\ |
457 | + filter_by(deleted=can_read_deleted(context)).\ |
458 | + all() |
459 | elif is_user_context(context): |
460 | - return session.query(models.Instance |
461 | - ).options(joinedload_all('fixed_ip.floating_ips') |
462 | - ).options(joinedload('security_groups') |
463 | - ).filter_by(project_id=context.project_id |
464 | - ).filter_by(reservation_id=reservation_id |
465 | - ).filter_by(deleted=False |
466 | - ).all() |
467 | + return session.query(models.Instance).\ |
468 | + options(joinedload_all('fixed_ip.floating_ips')).\ |
469 | + options(joinedload('security_groups')).\ |
470 | + filter_by(project_id=context.project_id).\ |
471 | + filter_by(reservation_id=reservation_id).\ |
472 | + filter_by(deleted=False).\ |
473 | + all() |
474 | |
475 | |
476 | @require_context |
477 | @@ -653,18 +655,18 @@ |
478 | session = get_session() |
479 | |
480 | if is_admin_context(context): |
481 | - result = session.query(models.Instance |
482 | - ).options(joinedload('security_groups') |
483 | - ).filter_by(internal_id=internal_id |
484 | - ).filter_by(deleted=can_read_deleted(context) |
485 | - ).first() |
486 | + result = session.query(models.Instance).\ |
487 | + options(joinedload('security_groups')).\ |
488 | + filter_by(internal_id=internal_id).\ |
489 | + filter_by(deleted=can_read_deleted(context)).\ |
490 | + first() |
491 | elif is_user_context(context): |
492 | - result = session.query(models.Instance |
493 | - ).options(joinedload('security_groups') |
494 | - ).filter_by(project_id=context.project_id |
495 | - ).filter_by(internal_id=internal_id |
496 | - ).filter_by(deleted=False |
497 | - ).first() |
498 | + result = session.query(models.Instance).\ |
499 | + options(joinedload('security_groups')).\ |
500 | + filter_by(project_id=context.project_id).\ |
501 | + filter_by(internal_id=internal_id).\ |
502 | + filter_by(deleted=False).\ |
503 | + first() |
504 | if not result: |
505 | raise exception.NotFound('Instance %s not found' % (internal_id)) |
506 | |
507 | @@ -675,9 +677,9 @@ |
508 | def instance_internal_id_exists(context, internal_id, session=None): |
509 | if not session: |
510 | session = get_session() |
511 | - return session.query( |
512 | - exists().where(models.Instance.internal_id==internal_id) |
513 | - ).one()[0] |
514 | + return session.query(exists().\ |
515 | + where(models.Instance.internal_id == internal_id)).\ |
516 | + one()[0] |
517 | |
518 | |
519 | @require_context |
520 | @@ -782,11 +784,11 @@ |
521 | if not session: |
522 | session = get_session() |
523 | |
524 | - result = session.query(models.KeyPair |
525 | - ).filter_by(user_id=user_id |
526 | - ).filter_by(name=name |
527 | - ).filter_by(deleted=can_read_deleted(context) |
528 | - ).first() |
529 | + result = session.query(models.KeyPair).\ |
530 | + filter_by(user_id=user_id).\ |
531 | + filter_by(name=name).\ |
532 | + filter_by(deleted=can_read_deleted(context)).\ |
533 | + first() |
534 | if not result: |
535 | raise exception.NotFound('no keypair for user %s, name %s' % |
536 | (user_id, name)) |
537 | @@ -797,10 +799,10 @@ |
538 | def key_pair_get_all_by_user(context, user_id): |
539 | authorize_user_context(context, user_id) |
540 | session = get_session() |
541 | - return session.query(models.KeyPair |
542 | - ).filter_by(user_id=user_id |
543 | - ).filter_by(deleted=False |
544 | - ).all() |
545 | + return session.query(models.KeyPair).\ |
546 | + filter_by(user_id=user_id).\ |
547 | + filter_by(deleted=False).\ |
548 | + all() |
549 | |
550 | |
551 | ################### |
552 | @@ -810,11 +812,11 @@ |
553 | def network_associate(context, project_id): |
554 | session = get_session() |
555 | with session.begin(): |
556 | - network_ref = session.query(models.Network |
557 | - ).filter_by(deleted=False |
558 | - ).filter_by(project_id=None |
559 | - ).with_lockmode('update' |
560 | - ).first() |
561 | + network_ref = session.query(models.Network).\ |
562 | + filter_by(deleted=False).\ |
563 | + filter_by(project_id=None).\ |
564 | + with_lockmode('update').\ |
565 | + first() |
566 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
567 | # then this has concurrency issues |
568 | if not network_ref: |
569 | @@ -827,40 +829,40 @@ |
570 | @require_admin_context |
571 | def network_count(context): |
572 | session = get_session() |
573 | - return session.query(models.Network |
574 | - ).filter_by(deleted=can_read_deleted(context) |
575 | - ).count() |
576 | + return session.query(models.Network).\ |
577 | + filter_by(deleted=can_read_deleted(context)).\ |
578 | + count() |
579 | |
580 | |
581 | @require_admin_context |
582 | def network_count_allocated_ips(context, network_id): |
583 | session = get_session() |
584 | - return session.query(models.FixedIp |
585 | - ).filter_by(network_id=network_id |
586 | - ).filter_by(allocated=True |
587 | - ).filter_by(deleted=False |
588 | - ).count() |
589 | + return session.query(models.FixedIp).\ |
590 | + filter_by(network_id=network_id).\ |
591 | + filter_by(allocated=True).\ |
592 | + filter_by(deleted=False).\ |
593 | + count() |
594 | |
595 | |
596 | @require_admin_context |
597 | def network_count_available_ips(context, network_id): |
598 | session = get_session() |
599 | - return session.query(models.FixedIp |
600 | - ).filter_by(network_id=network_id |
601 | - ).filter_by(allocated=False |
602 | - ).filter_by(reserved=False |
603 | - ).filter_by(deleted=False |
604 | - ).count() |
605 | + return session.query(models.FixedIp).\ |
606 | + filter_by(network_id=network_id).\ |
607 | + filter_by(allocated=False).\ |
608 | + filter_by(reserved=False).\ |
609 | + filter_by(deleted=False).\ |
610 | + count() |
611 | |
612 | |
613 | @require_admin_context |
614 | def network_count_reserved_ips(context, network_id): |
615 | session = get_session() |
616 | - return session.query(models.FixedIp |
617 | - ).filter_by(network_id=network_id |
618 | - ).filter_by(reserved=True |
619 | - ).filter_by(deleted=False |
620 | - ).count() |
621 | + return session.query(models.FixedIp).\ |
622 | + filter_by(network_id=network_id).\ |
623 | + filter_by(reserved=True).\ |
624 | + filter_by(deleted=False).\ |
625 | + count() |
626 | |
627 | |
628 | @require_admin_context |
629 | @@ -893,16 +895,16 @@ |
630 | result = None |
631 | |
632 | if is_admin_context(context): |
633 | - result = session.query(models.Network |
634 | - ).filter_by(id=network_id |
635 | - ).filter_by(deleted=can_read_deleted(context) |
636 | - ).first() |
637 | + result = session.query(models.Network).\ |
638 | + filter_by(id=network_id).\ |
639 | + filter_by(deleted=can_read_deleted(context)).\ |
640 | + first() |
641 | elif is_user_context(context): |
642 | - result = session.query(models.Network |
643 | - ).filter_by(project_id=context.project_id |
644 | - ).filter_by(id=network_id |
645 | - ).filter_by(deleted=False |
646 | - ).first() |
647 | + result = session.query(models.Network).\ |
648 | + filter_by(project_id=context.project_id).\ |
649 | + filter_by(id=network_id).\ |
650 | + filter_by(deleted=False).\ |
651 | + first() |
652 | if not result: |
653 | raise exception.NotFound('No network for id %s' % network_id) |
654 | |
655 | @@ -915,21 +917,21 @@ |
656 | @require_admin_context |
657 | def network_get_associated_fixed_ips(context, network_id): |
658 | session = get_session() |
659 | - return session.query(models.FixedIp |
660 | - ).options(joinedload_all('instance') |
661 | - ).filter_by(network_id=network_id |
662 | - ).filter(models.FixedIp.instance_id != None |
663 | - ).filter_by(deleted=False |
664 | - ).all() |
665 | + return session.query(models.FixedIp).\ |
666 | + options(joinedload_all('instance')).\ |
667 | + filter_by(network_id=network_id).\ |
668 | + filter(models.FixedIp.instance_id != None).\ |
669 | + filter_by(deleted=False).\ |
670 | + all() |
671 | |
672 | |
673 | @require_admin_context |
674 | def network_get_by_bridge(context, bridge): |
675 | session = get_session() |
676 | - result = session.query(models.Network |
677 | - ).filter_by(bridge=bridge |
678 | - ).filter_by(deleted=False |
679 | - ).first() |
680 | + result = session.query(models.Network).\ |
681 | + filter_by(bridge=bridge).\ |
682 | + filter_by(deleted=False).\ |
683 | + first() |
684 | |
685 | if not result: |
686 | raise exception.NotFound('No network for bridge %s' % bridge) |
687 | @@ -939,12 +941,12 @@ |
688 | @require_admin_context |
689 | def network_get_by_instance(_context, instance_id): |
690 | session = get_session() |
691 | - rv = session.query(models.Network |
692 | - ).filter_by(deleted=False |
693 | - ).join(models.Network.fixed_ips |
694 | - ).filter_by(instance_id=instance_id |
695 | - ).filter_by(deleted=False |
696 | - ).first() |
697 | + rv = session.query(models.Network).\ |
698 | + filter_by(deleted=False).\ |
699 | + join(models.Network.fixed_ips).\ |
700 | + filter_by(instance_id=instance_id).\ |
701 | + filter_by(deleted=False).\ |
702 | + first() |
703 | if not rv: |
704 | raise exception.NotFound('No network for instance %s' % instance_id) |
705 | return rv |
706 | @@ -954,11 +956,11 @@ |
707 | def network_set_host(context, network_id, host_id): |
708 | session = get_session() |
709 | with session.begin(): |
710 | - network_ref = session.query(models.Network |
711 | - ).filter_by(id=network_id |
712 | - ).filter_by(deleted=False |
713 | - ).with_lockmode('update' |
714 | - ).first() |
715 | + network_ref = session.query(models.Network).\ |
716 | + filter_by(id=network_id).\ |
717 | + filter_by(deleted=False).\ |
718 | + with_lockmode('update').\ |
719 | + first() |
720 | if not network_ref: |
721 | raise exception.NotFound('No network for id %s' % network_id) |
722 | |
723 | @@ -987,10 +989,10 @@ |
724 | @require_context |
725 | def project_get_network(context, project_id): |
726 | session = get_session() |
727 | - rv = session.query(models.Network |
728 | - ).filter_by(project_id=project_id |
729 | - ).filter_by(deleted=False |
730 | - ).first() |
731 | + rv = session.query(models.Network).\ |
732 | + filter_by(project_id=project_id).\ |
733 | + filter_by(deleted=False).\ |
734 | + first() |
735 | if not rv: |
736 | try: |
737 | return network_associate(context, project_id) |
738 | @@ -998,10 +1000,10 @@ |
739 | # NOTE(vish): We hit this if there is a race and two |
740 | # processes are attempting to allocate the |
741 | # network at the same time |
742 | - rv = session.query(models.Network |
743 | - ).filter_by(project_id=project_id |
744 | - ).filter_by(deleted=False |
745 | - ).first() |
746 | + rv = session.query(models.Network).\ |
747 | + filter_by(project_id=project_id).\ |
748 | + filter_by(deleted=False).\ |
749 | + first() |
750 | return rv |
751 | |
752 | |
753 | @@ -1019,9 +1021,9 @@ |
754 | @require_admin_context |
755 | def export_device_count(context): |
756 | session = get_session() |
757 | - return session.query(models.ExportDevice |
758 | - ).filter_by(deleted=can_read_deleted(context) |
759 | - ).count() |
760 | + return session.query(models.ExportDevice).\ |
761 | + filter_by(deleted=can_read_deleted(context)).\ |
762 | + count() |
763 | |
764 | |
765 | @require_admin_context |
766 | @@ -1043,18 +1045,20 @@ |
767 | session = get_session() |
768 | session.delete(token) |
769 | |
770 | + |
771 | def auth_get_token(_context, token_hash): |
772 | session = get_session() |
773 | - tk = session.query(models.AuthToken |
774 | - ).filter_by(token_hash=token_hash |
775 | - ).first() |
776 | + tk = session.query(models.AuthToken).\ |
777 | + filter_by(token_hash=token_hash).\ |
778 | + first() |
779 | if not tk: |
780 | raise exception.NotFound('Token %s does not exist' % token_hash) |
781 | return tk |
782 | |
783 | + |
784 | def auth_create_token(_context, token): |
785 | tk = models.AuthToken() |
786 | - for k,v in token.iteritems(): |
787 | + for k, v in token.iteritems(): |
788 | tk[k] = v |
789 | tk.save() |
790 | return tk |
791 | @@ -1068,10 +1072,10 @@ |
792 | if not session: |
793 | session = get_session() |
794 | |
795 | - result = session.query(models.Quota |
796 | - ).filter_by(project_id=project_id |
797 | - ).filter_by(deleted=can_read_deleted(context) |
798 | - ).first() |
799 | + result = session.query(models.Quota).\ |
800 | + filter_by(project_id=project_id).\ |
801 | + filter_by(deleted=can_read_deleted(context)).\ |
802 | + first() |
803 | if not result: |
804 | raise exception.NotFound('No quota for project_id %s' % project_id) |
805 | |
806 | @@ -1112,11 +1116,11 @@ |
807 | def volume_allocate_shelf_and_blade(context, volume_id): |
808 | session = get_session() |
809 | with session.begin(): |
810 | - export_device = session.query(models.ExportDevice |
811 | - ).filter_by(volume=None |
812 | - ).filter_by(deleted=False |
813 | - ).with_lockmode('update' |
814 | - ).first() |
815 | + export_device = session.query(models.ExportDevice).\ |
816 | + filter_by(volume=None).\ |
817 | + filter_by(deleted=False).\ |
818 | + with_lockmode('update').\ |
819 | + first() |
820 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
821 | # then this has concurrency issues |
822 | if not export_device: |
823 | @@ -1134,7 +1138,8 @@ |
824 | volume_ref['status'] = 'in-use' |
825 | volume_ref['mountpoint'] = mountpoint |
826 | volume_ref['attach_status'] = 'attached' |
827 | - volume_ref.instance = instance_get(context, instance_id, session=session) |
828 | + volume_ref.instance = instance_get(context, instance_id, |
829 | + session=session) |
830 | volume_ref.save(session=session) |
831 | |
832 | |
833 | @@ -1158,10 +1163,10 @@ |
834 | def volume_data_get_for_project(context, project_id): |
835 | session = get_session() |
836 | result = session.query(func.count(models.Volume.id), |
837 | - func.sum(models.Volume.size) |
838 | - ).filter_by(project_id=project_id |
839 | - ).filter_by(deleted=False |
840 | - ).first() |
841 | + func.sum(models.Volume.size)).\ |
842 | + filter_by(project_id=project_id).\ |
843 | + filter_by(deleted=False).\ |
844 | + first() |
845 | # NOTE(vish): convert None to 0 |
846 | return (result[0] or 0, result[1] or 0) |
847 | |
848 | @@ -1197,16 +1202,16 @@ |
849 | result = None |
850 | |
851 | if is_admin_context(context): |
852 | - result = session.query(models.Volume |
853 | - ).filter_by(id=volume_id |
854 | - ).filter_by(deleted=can_read_deleted(context) |
855 | - ).first() |
856 | + result = session.query(models.Volume).\ |
857 | + filter_by(id=volume_id).\ |
858 | + filter_by(deleted=can_read_deleted(context)).\ |
859 | + first() |
860 | elif is_user_context(context): |
861 | - result = session.query(models.Volume |
862 | - ).filter_by(project_id=context.project_id |
863 | - ).filter_by(id=volume_id |
864 | - ).filter_by(deleted=False |
865 | - ).first() |
866 | + result = session.query(models.Volume).\ |
867 | + filter_by(project_id=context.project_id).\ |
868 | + filter_by(id=volume_id).\ |
869 | + filter_by(deleted=False).\ |
870 | + first() |
871 | if not result: |
872 | raise exception.NotFound('No volume for id %s' % volume_id) |
873 | |
874 | @@ -1216,19 +1221,20 @@ |
875 | @require_admin_context |
876 | def volume_get_all(context): |
877 | session = get_session() |
878 | - return session.query(models.Volume |
879 | - ).filter_by(deleted=can_read_deleted(context) |
880 | - ).all() |
881 | + return session.query(models.Volume).\ |
882 | + filter_by(deleted=can_read_deleted(context)).\ |
883 | + all() |
884 | + |
885 | |
886 | @require_context |
887 | def volume_get_all_by_project(context, project_id): |
888 | authorize_project_context(context, project_id) |
889 | |
890 | session = get_session() |
891 | - return session.query(models.Volume |
892 | - ).filter_by(project_id=project_id |
893 | - ).filter_by(deleted=can_read_deleted(context) |
894 | - ).all() |
895 | + return session.query(models.Volume).\ |
896 | + filter_by(project_id=project_id).\ |
897 | + filter_by(deleted=can_read_deleted(context)).\ |
898 | + all() |
899 | |
900 | |
901 | @require_context |
902 | @@ -1237,16 +1243,16 @@ |
903 | result = None |
904 | |
905 | if is_admin_context(context): |
906 | - result = session.query(models.Volume |
907 | - ).filter_by(ec2_id=ec2_id |
908 | - ).filter_by(deleted=can_read_deleted(context) |
909 | - ).first() |
910 | + result = session.query(models.Volume).\ |
911 | + filter_by(ec2_id=ec2_id).\ |
912 | + filter_by(deleted=can_read_deleted(context)).\ |
913 | + first() |
914 | elif is_user_context(context): |
915 | - result = session.query(models.Volume |
916 | - ).filter_by(project_id=context.project_id |
917 | - ).filter_by(ec2_id=ec2_id |
918 | - ).filter_by(deleted=False |
919 | - ).first() |
920 | + result = session.query(models.Volume).\ |
921 | + filter_by(project_id=context.project_id).\ |
922 | + filter_by(ec2_id=ec2_id).\ |
923 | + filter_by(deleted=False).\ |
924 | + first() |
925 | else: |
926 | raise exception.NotAuthorized() |
927 | |
928 | @@ -1261,19 +1267,19 @@ |
929 | if not session: |
930 | session = get_session() |
931 | |
932 | - return session.query(exists( |
933 | - ).where(models.Volume.id==ec2_id) |
934 | - ).one()[0] |
935 | + return session.query(exists().\ |
936 | + where(models.Volume.id == ec2_id)).\ |
937 | + one()[0] |
938 | |
939 | |
940 | @require_admin_context |
941 | def volume_get_instance(context, volume_id): |
942 | session = get_session() |
943 | - result = session.query(models.Volume |
944 | - ).filter_by(id=volume_id |
945 | - ).filter_by(deleted=can_read_deleted(context) |
946 | - ).options(joinedload('instance') |
947 | - ).first() |
948 | + result = session.query(models.Volume).\ |
949 | + filter_by(id=volume_id).\ |
950 | + filter_by(deleted=can_read_deleted(context)).\ |
951 | + options(joinedload('instance')).\ |
952 | + first() |
953 | if not result: |
954 | raise exception.NotFound('Volume %s not found' % ec2_id) |
955 | |
956 | @@ -1283,9 +1289,9 @@ |
957 | @require_admin_context |
958 | def volume_get_shelf_and_blade(context, volume_id): |
959 | session = get_session() |
960 | - result = session.query(models.ExportDevice |
961 | - ).filter_by(volume_id=volume_id |
962 | - ).first() |
963 | + result = session.query(models.ExportDevice).\ |
964 | + filter_by(volume_id=volume_id).\ |
965 | + first() |
966 | if not result: |
967 | raise exception.NotFound('No export device found for volume %s' % |
968 | volume_id) |
969 | @@ -1309,10 +1315,10 @@ |
970 | @require_context |
971 | def security_group_get_all(context): |
972 | session = get_session() |
973 | - return session.query(models.SecurityGroup |
974 | - ).filter_by(deleted=can_read_deleted(context) |
975 | - ).options(joinedload_all('rules') |
976 | - ).all() |
977 | + return session.query(models.SecurityGroup).\ |
978 | + filter_by(deleted=can_read_deleted(context)).\ |
979 | + options(joinedload_all('rules')).\ |
980 | + all() |
981 | |
982 | |
983 | @require_context |
984 | @@ -1320,18 +1326,18 @@ |
985 | if not session: |
986 | session = get_session() |
987 | if is_admin_context(context): |
988 | - result = session.query(models.SecurityGroup |
989 | - ).filter_by(deleted=can_read_deleted(context), |
990 | - ).filter_by(id=security_group_id |
991 | - ).options(joinedload_all('rules') |
992 | - ).first() |
993 | + result = session.query(models.SecurityGroup).\ |
994 | + filter_by(deleted=can_read_deleted(context),).\ |
995 | + filter_by(id=security_group_id).\ |
996 | + options(joinedload_all('rules')).\ |
997 | + first() |
998 | else: |
999 | - result = session.query(models.SecurityGroup |
1000 | - ).filter_by(deleted=False |
1001 | - ).filter_by(id=security_group_id |
1002 | - ).filter_by(project_id=context.project_id |
1003 | - ).options(joinedload_all('rules') |
1004 | - ).first() |
1005 | + result = session.query(models.SecurityGroup).\ |
1006 | + filter_by(deleted=False).\ |
1007 | + filter_by(id=security_group_id).\ |
1008 | + filter_by(project_id=context.project_id).\ |
1009 | + options(joinedload_all('rules')).\ |
1010 | + first() |
1011 | if not result: |
1012 | raise exception.NotFound("No secuity group with id %s" % |
1013 | security_group_id) |
1014 | @@ -1341,13 +1347,13 @@ |
1015 | @require_context |
1016 | def security_group_get_by_name(context, project_id, group_name): |
1017 | session = get_session() |
1018 | - result = session.query(models.SecurityGroup |
1019 | - ).filter_by(project_id=project_id |
1020 | - ).filter_by(name=group_name |
1021 | - ).filter_by(deleted=False |
1022 | - ).options(joinedload_all('rules') |
1023 | - ).options(joinedload_all('instances') |
1024 | - ).first() |
1025 | + result = session.query(models.SecurityGroup).\ |
1026 | + filter_by(project_id=project_id).\ |
1027 | + filter_by(name=group_name).\ |
1028 | + filter_by(deleted=False).\ |
1029 | + options(joinedload_all('rules')).\ |
1030 | + options(joinedload_all('instances')).\ |
1031 | + first() |
1032 | if not result: |
1033 | raise exception.NotFound( |
1034 | 'No security group named %s for project: %s' \ |
1035 | @@ -1358,23 +1364,23 @@ |
1036 | @require_context |
1037 | def security_group_get_by_project(context, project_id): |
1038 | session = get_session() |
1039 | - return session.query(models.SecurityGroup |
1040 | - ).filter_by(project_id=project_id |
1041 | - ).filter_by(deleted=False |
1042 | - ).options(joinedload_all('rules') |
1043 | - ).all() |
1044 | + return session.query(models.SecurityGroup).\ |
1045 | + filter_by(project_id=project_id).\ |
1046 | + filter_by(deleted=False).\ |
1047 | + options(joinedload_all('rules')).\ |
1048 | + all() |
1049 | |
1050 | |
1051 | @require_context |
1052 | def security_group_get_by_instance(context, instance_id): |
1053 | session = get_session() |
1054 | - return session.query(models.SecurityGroup |
1055 | - ).filter_by(deleted=False |
1056 | - ).options(joinedload_all('rules') |
1057 | - ).join(models.SecurityGroup.instances |
1058 | - ).filter_by(id=instance_id |
1059 | - ).filter_by(deleted=False |
1060 | - ).all() |
1061 | + return session.query(models.SecurityGroup).\ |
1062 | + filter_by(deleted=False).\ |
1063 | + options(joinedload_all('rules')).\ |
1064 | + join(models.SecurityGroup.instances).\ |
1065 | + filter_by(id=instance_id).\ |
1066 | + filter_by(deleted=False).\ |
1067 | + all() |
1068 | |
1069 | |
1070 | @require_context |
1071 | @@ -1409,6 +1415,7 @@ |
1072 | 'where group_id=:id', |
1073 | {'id': security_group_id}) |
1074 | |
1075 | + |
1076 | @require_context |
1077 | def security_group_destroy_all(context, session=None): |
1078 | if not session: |
1079 | @@ -1427,16 +1434,16 @@ |
1080 | if not session: |
1081 | session = get_session() |
1082 | if is_admin_context(context): |
1083 | - result = session.query(models.SecurityGroupIngressRule |
1084 | - ).filter_by(deleted=can_read_deleted(context) |
1085 | - ).filter_by(id=security_group_rule_id |
1086 | - ).first() |
1087 | + result = session.query(models.SecurityGroupIngressRule).\ |
1088 | + filter_by(deleted=can_read_deleted(context)).\ |
1089 | + filter_by(id=security_group_rule_id).\ |
1090 | + first() |
1091 | else: |
1092 | # TODO(vish): Join to group and check for project_id |
1093 | - result = session.query(models.SecurityGroupIngressRule |
1094 | - ).filter_by(deleted=False |
1095 | - ).filter_by(id=security_group_rule_id |
1096 | - ).first() |
1097 | + result = session.query(models.SecurityGroupIngressRule).\ |
1098 | + filter_by(deleted=False).\ |
1099 | + filter_by(id=security_group_rule_id).\ |
1100 | + first() |
1101 | if not result: |
1102 | raise exception.NotFound("No secuity group rule with id %s" % |
1103 | security_group_rule_id) |
1104 | @@ -1451,6 +1458,7 @@ |
1105 | security_group_rule_ref.save() |
1106 | return security_group_rule_ref |
1107 | |
1108 | + |
1109 | @require_context |
1110 | def security_group_rule_destroy(context, security_group_rule_id): |
1111 | session = get_session() |
1112 | @@ -1468,10 +1476,10 @@ |
1113 | if not session: |
1114 | session = get_session() |
1115 | |
1116 | - result = session.query(models.User |
1117 | - ).filter_by(id=id |
1118 | - ).filter_by(deleted=can_read_deleted(context) |
1119 | - ).first() |
1120 | + result = session.query(models.User).\ |
1121 | + filter_by(id=id).\ |
1122 | + filter_by(deleted=can_read_deleted(context)).\ |
1123 | + first() |
1124 | |
1125 | if not result: |
1126 | raise exception.NotFound('No user for id %s' % id) |
1127 | @@ -1484,10 +1492,10 @@ |
1128 | if not session: |
1129 | session = get_session() |
1130 | |
1131 | - result = session.query(models.User |
1132 | - ).filter_by(access_key=access_key |
1133 | - ).filter_by(deleted=can_read_deleted(context) |
1134 | - ).first() |
1135 | + result = session.query(models.User).\ |
1136 | + filter_by(access_key=access_key).\ |
1137 | + filter_by(deleted=can_read_deleted(context)).\ |
1138 | + first() |
1139 | |
1140 | if not result: |
1141 | raise exception.NotFound('No user for access key %s' % access_key) |
1142 | @@ -1508,21 +1516,21 @@ |
1143 | def user_delete(context, id): |
1144 | session = get_session() |
1145 | with session.begin(): |
1146 | - session.execute('delete from user_project_association where user_id=:id', |
1147 | - {'id': id}) |
1148 | - session.execute('delete from user_role_association where user_id=:id', |
1149 | - {'id': id}) |
1150 | - session.execute('delete from user_project_role_association where user_id=:id', |
1151 | - {'id': id}) |
1152 | + session.execute('delete from user_project_association ' |
1153 | + 'where user_id=:id', {'id': id}) |
1154 | + session.execute('delete from user_role_association ' |
1155 | + 'where user_id=:id', {'id': id}) |
1156 | + session.execute('delete from user_project_role_association ' |
1157 | + 'where user_id=:id', {'id': id}) |
1158 | user_ref = user_get(context, id, session=session) |
1159 | session.delete(user_ref) |
1160 | |
1161 | |
1162 | def user_get_all(context): |
1163 | session = get_session() |
1164 | - return session.query(models.User |
1165 | - ).filter_by(deleted=can_read_deleted(context) |
1166 | - ).all() |
1167 | + return session.query(models.User).\ |
1168 | + filter_by(deleted=can_read_deleted(context)).\ |
1169 | + all() |
1170 | |
1171 | |
1172 | def project_create(_context, values): |
1173 | @@ -1547,11 +1555,11 @@ |
1174 | if not session: |
1175 | session = get_session() |
1176 | |
1177 | - result = session.query(models.Project |
1178 | - ).filter_by(deleted=False |
1179 | - ).filter_by(id=id |
1180 | - ).options(joinedload_all('members') |
1181 | - ).first() |
1182 | + result = session.query(models.Project).\ |
1183 | + filter_by(deleted=False).\ |
1184 | + filter_by(id=id).\ |
1185 | + options(joinedload_all('members')).\ |
1186 | + first() |
1187 | |
1188 | if not result: |
1189 | raise exception.NotFound("No project with id %s" % id) |
1190 | @@ -1561,18 +1569,18 @@ |
1191 | |
1192 | def project_get_all(context): |
1193 | session = get_session() |
1194 | - return session.query(models.Project |
1195 | - ).filter_by(deleted=can_read_deleted(context) |
1196 | - ).options(joinedload_all('members') |
1197 | - ).all() |
1198 | + return session.query(models.Project).\ |
1199 | + filter_by(deleted=can_read_deleted(context)).\ |
1200 | + options(joinedload_all('members')).\ |
1201 | + all() |
1202 | |
1203 | |
1204 | def project_get_by_user(context, user_id): |
1205 | session = get_session() |
1206 | - user = session.query(models.User |
1207 | - ).filter_by(deleted=can_read_deleted(context) |
1208 | - ).options(joinedload_all('projects') |
1209 | - ).first() |
1210 | + user = session.query(models.User).\ |
1211 | + filter_by(deleted=can_read_deleted(context)).\ |
1212 | + options(joinedload_all('projects')).\ |
1213 | + first() |
1214 | return user.projects |
1215 | |
1216 | |
1217 | @@ -1607,10 +1615,10 @@ |
1218 | def project_delete(context, id): |
1219 | session = get_session() |
1220 | with session.begin(): |
1221 | - session.execute('delete from user_project_association where project_id=:id', |
1222 | - {'id': id}) |
1223 | - session.execute('delete from user_project_role_association where project_id=:id', |
1224 | - {'id': id}) |
1225 | + session.execute('delete from user_project_association ' |
1226 | + 'where project_id=:id', {'id': id}) |
1227 | + session.execute('delete from user_project_role_association ' |
1228 | + 'where project_id=:id', {'id': id}) |
1229 | project_ref = project_get(context, id, session=session) |
1230 | session.delete(project_ref) |
1231 | |
1232 | @@ -1625,29 +1633,30 @@ |
1233 | def user_get_roles_for_project(context, user_id, project_id): |
1234 | session = get_session() |
1235 | with session.begin(): |
1236 | - res = session.query(models.UserProjectRoleAssociation |
1237 | - ).filter_by(user_id=user_id |
1238 | - ).filter_by(project_id=project_id |
1239 | - ).all() |
1240 | + res = session.query(models.UserProjectRoleAssociation).\ |
1241 | + filter_by(user_id=user_id).\ |
1242 | + filter_by(project_id=project_id).\ |
1243 | + all() |
1244 | return [association.role for association in res] |
1245 | |
1246 | + |
1247 | def user_remove_project_role(context, user_id, project_id, role): |
1248 | session = get_session() |
1249 | with session.begin(): |
1250 | - session.execute('delete from user_project_role_association where ' + \ |
1251 | - 'user_id=:user_id and project_id=:project_id and ' + \ |
1252 | - 'role=:role', { 'user_id' : user_id, |
1253 | - 'project_id' : project_id, |
1254 | - 'role' : role }) |
1255 | + session.execute('delete from user_project_role_association where ' |
1256 | + 'user_id=:user_id and project_id=:project_id and ' |
1257 | + 'role=:role', {'user_id': user_id, |
1258 | + 'project_id': project_id, |
1259 | + 'role': role}) |
1260 | |
1261 | |
1262 | def user_remove_role(context, user_id, role): |
1263 | session = get_session() |
1264 | with session.begin(): |
1265 | - res = session.query(models.UserRoleAssociation |
1266 | - ).filter_by(user_id=user_id |
1267 | - ).filter_by(role=role |
1268 | - ).all() |
1269 | + res = session.query(models.UserRoleAssociation).\ |
1270 | + filter_by(user_id=user_id).\ |
1271 | + filter_by(role=role).\ |
1272 | + all() |
1273 | for role in res: |
1274 | session.delete(role) |
1275 | |
1276 | @@ -1656,7 +1665,8 @@ |
1277 | session = get_session() |
1278 | with session.begin(): |
1279 | user_ref = user_get(context, user_id, session=session) |
1280 | - models.UserRoleAssociation(user=user_ref, role=role).save(session=session) |
1281 | + models.UserRoleAssociation(user=user_ref, role=role).\ |
1282 | + save(session=session) |
1283 | |
1284 | |
1285 | def user_add_project_role(context, user_id, project_id, role): |
1286 | @@ -1672,12 +1682,11 @@ |
1287 | ################### |
1288 | |
1289 | |
1290 | - |
1291 | @require_admin_context |
1292 | def host_get_networks(context, host): |
1293 | session = get_session() |
1294 | with session.begin(): |
1295 | - return session.query(models.Network |
1296 | - ).filter_by(deleted=False |
1297 | - ).filter_by(host=host |
1298 | - ).all() |
1299 | + return session.query(models.Network).\ |
1300 | + filter_by(deleted=False).\ |
1301 | + filter_by(host=host).\ |
1302 | + all() |
1303 | |
1304 | === modified file 'nova/db/sqlalchemy/models.py' |
1305 | --- nova/db/sqlalchemy/models.py 2010-10-14 06:17:40 +0000 |
1306 | +++ nova/db/sqlalchemy/models.py 2010-10-21 23:19:44 +0000 |
1307 | @@ -134,8 +134,8 @@ |
1308 | # """Represents a host where services are running""" |
1309 | # __tablename__ = 'hosts' |
1310 | # id = Column(String(255), primary_key=True) |
1311 | -# |
1312 | -# |
1313 | + |
1314 | + |
1315 | class Service(BASE, NovaBase): |
1316 | """Represents a running service on a host""" |
1317 | __tablename__ = 'services' |
1318 | @@ -277,7 +277,8 @@ |
1319 | class ExportDevice(BASE, NovaBase): |
1320 | """Represates a shelf and blade that a volume can be exported on""" |
1321 | __tablename__ = 'export_devices' |
1322 | - __table_args__ = (schema.UniqueConstraint("shelf_id", "blade_id"), {'mysql_engine': 'InnoDB'}) |
1323 | + __table_args__ = (schema.UniqueConstraint("shelf_id", "blade_id"), |
1324 | + {'mysql_engine': 'InnoDB'}) |
1325 | id = Column(Integer, primary_key=True) |
1326 | shelf_id = Column(Integer) |
1327 | blade_id = Column(Integer) |
1328 | @@ -308,10 +309,13 @@ |
1329 | |
1330 | instances = relationship(Instance, |
1331 | secondary="security_group_instance_association", |
1332 | - primaryjoin="and_(SecurityGroup.id == SecurityGroupInstanceAssociation.security_group_id," |
1333 | - "SecurityGroup.deleted == False)", |
1334 | - secondaryjoin="and_(SecurityGroupInstanceAssociation.instance_id == Instance.id," |
1335 | - "Instance.deleted == False)", |
1336 | + primaryjoin='and_(' |
1337 | + 'SecurityGroup.id == ' |
1338 | + 'SecurityGroupInstanceAssociation.security_group_id,' |
1339 | + 'SecurityGroup.deleted == False)', |
1340 | + secondaryjoin='and_(' |
1341 | + 'SecurityGroupInstanceAssociation.instance_id == Instance.id,' |
1342 | + 'Instance.deleted == False)', |
1343 | backref='security_groups') |
1344 | |
1345 | @property |
1346 | @@ -330,11 +334,12 @@ |
1347 | |
1348 | parent_group_id = Column(Integer, ForeignKey('security_groups.id')) |
1349 | parent_group = relationship("SecurityGroup", backref="rules", |
1350 | - foreign_keys=parent_group_id, |
1351 | - primaryjoin="and_(SecurityGroupIngressRule.parent_group_id == SecurityGroup.id," |
1352 | - "SecurityGroupIngressRule.deleted == False)") |
1353 | + foreign_keys=parent_group_id, |
1354 | + primaryjoin='and_(' |
1355 | + 'SecurityGroupIngressRule.parent_group_id == SecurityGroup.id,' |
1356 | + 'SecurityGroupIngressRule.deleted == False)') |
1357 | |
1358 | - protocol = Column(String(5)) # "tcp", "udp", or "icmp" |
1359 | + protocol = Column(String(5)) # "tcp", "udp", or "icmp" |
1360 | from_port = Column(Integer) |
1361 | to_port = Column(Integer) |
1362 | cidr = Column(String(255)) |
1363 | @@ -414,8 +419,9 @@ |
1364 | instance = relationship(Instance, |
1365 | backref=backref('fixed_ip', uselist=False), |
1366 | foreign_keys=instance_id, |
1367 | - primaryjoin='and_(FixedIp.instance_id==Instance.id,' |
1368 | - 'FixedIp.deleted==False)') |
1369 | + primaryjoin='and_(' |
1370 | + 'FixedIp.instance_id == Instance.id,' |
1371 | + 'FixedIp.deleted == False)') |
1372 | allocated = Column(Boolean, default=False) |
1373 | leased = Column(Boolean, default=False) |
1374 | reserved = Column(Boolean, default=False) |
1375 | @@ -455,13 +461,13 @@ |
1376 | __tablename__ = 'user_project_role_association' |
1377 | user_id = Column(String(255), primary_key=True) |
1378 | user = relationship(User, |
1379 | - primaryjoin=user_id==User.id, |
1380 | + primaryjoin=user_id == User.id, |
1381 | foreign_keys=[User.id], |
1382 | uselist=False) |
1383 | |
1384 | project_id = Column(String(255), primary_key=True) |
1385 | project = relationship(Project, |
1386 | - primaryjoin=project_id==Project.id, |
1387 | + primaryjoin=project_id == Project.id, |
1388 | foreign_keys=[Project.id], |
1389 | uselist=False) |
1390 | |
1391 | @@ -485,7 +491,6 @@ |
1392 | project_id = Column(String(255), ForeignKey(Project.id), primary_key=True) |
1393 | |
1394 | |
1395 | - |
1396 | class FloatingIp(BASE, NovaBase): |
1397 | """Represents a floating ip that dynamically forwards to a fixed ip""" |
1398 | __tablename__ = 'floating_ips' |
1399 | @@ -495,8 +500,9 @@ |
1400 | fixed_ip = relationship(FixedIp, |
1401 | backref=backref('floating_ips'), |
1402 | foreign_keys=fixed_ip_id, |
1403 | - primaryjoin='and_(FloatingIp.fixed_ip_id==FixedIp.id,' |
1404 | - 'FloatingIp.deleted==False)') |
1405 | + primaryjoin='and_(' |
1406 | + 'FloatingIp.fixed_ip_id == FixedIp.id,' |
1407 | + 'FloatingIp.deleted == False)') |
1408 | project_id = Column(String(255)) |
1409 | host = Column(String(255)) # , ForeignKey('hosts.id')) |
1410 | |
1411 | @@ -507,7 +513,7 @@ |
1412 | models = (Service, Instance, Volume, ExportDevice, FixedIp, |
1413 | FloatingIp, Network, SecurityGroup, |
1414 | SecurityGroupIngressRule, SecurityGroupInstanceAssociation, |
1415 | - AuthToken, User, Project) # , Image, Host |
1416 | + AuthToken, User, Project) # , Image, Host |
1417 | engine = create_engine(FLAGS.sql_connection, echo=False) |
1418 | for model in models: |
1419 | model.metadata.create_all(engine) |
1420 | |
1421 | === modified file 'nova/db/sqlalchemy/session.py' |
1422 | --- nova/db/sqlalchemy/session.py 2010-09-15 12:04:07 +0000 |
1423 | +++ nova/db/sqlalchemy/session.py 2010-10-21 23:19:44 +0000 |
1424 | @@ -29,6 +29,7 @@ |
1425 | _ENGINE = None |
1426 | _MAKER = None |
1427 | |
1428 | + |
1429 | def get_session(autocommit=True, expire_on_commit=False): |
1430 | """Helper method to grab session""" |
1431 | global _ENGINE |
1432 | @@ -39,5 +40,5 @@ |
1433 | _MAKER = (sessionmaker(bind=_ENGINE, |
1434 | autocommit=autocommit, |
1435 | expire_on_commit=expire_on_commit)) |
1436 | - session = _MAKER() |
1437 | + session = _MAKER() |
1438 | return session |
lgtm. This was originally how we had the file, termie suggested that the other format was more 'pythonic', I don't really care either way but i'd like other people to weigh in before we merge this.