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 | 256 | """Get all instances.""" | 256 | """Get all instances.""" |
6 | 257 | return IMPL.instance_get_all(context) | 257 | return IMPL.instance_get_all(context) |
7 | 258 | 258 | ||
8 | 259 | |||
9 | 259 | def instance_get_all_by_user(context, user_id): | 260 | def instance_get_all_by_user(context, user_id): |
10 | 260 | """Get all instances.""" | 261 | """Get all instances.""" |
11 | 261 | return IMPL.instance_get_all_by_user(context, user_id) | 262 | return IMPL.instance_get_all_by_user(context, user_id) |
12 | 262 | 263 | ||
13 | 264 | |||
14 | 263 | def instance_get_all_by_project(context, project_id): | 265 | def instance_get_all_by_project(context, project_id): |
15 | 264 | """Get all instance belonging to a project.""" | 266 | """Get all instance belonging to a project.""" |
16 | 265 | return IMPL.instance_get_all_by_project(context, project_id) | 267 | return IMPL.instance_get_all_by_project(context, project_id) |
17 | @@ -306,7 +308,8 @@ | |||
18 | 306 | 308 | ||
19 | 307 | def instance_add_security_group(context, instance_id, security_group_id): | 309 | def instance_add_security_group(context, instance_id, security_group_id): |
20 | 308 | """Associate the given security group with the given instance""" | 310 | """Associate the given security group with the given instance""" |
22 | 309 | return IMPL.instance_add_security_group(context, instance_id, security_group_id) | 311 | return IMPL.instance_add_security_group(context, instance_id, |
23 | 312 | security_group_id) | ||
24 | 310 | 313 | ||
25 | 311 | 314 | ||
26 | 312 | ################### | 315 | ################### |
27 | @@ -482,10 +485,12 @@ | |||
28 | 482 | """Destroy an auth token""" | 485 | """Destroy an auth token""" |
29 | 483 | return IMPL.auth_destroy_token(context, token) | 486 | return IMPL.auth_destroy_token(context, token) |
30 | 484 | 487 | ||
31 | 488 | |||
32 | 485 | def auth_get_token(context, token_hash): | 489 | def auth_get_token(context, token_hash): |
33 | 486 | """Retrieves a token given the hash representing it""" | 490 | """Retrieves a token given the hash representing it""" |
34 | 487 | return IMPL.auth_get_token(context, token_hash) | 491 | return IMPL.auth_get_token(context, token_hash) |
35 | 488 | 492 | ||
36 | 493 | |||
37 | 489 | def auth_create_token(context, token): | 494 | def auth_create_token(context, token): |
38 | 490 | """Creates a new token""" | 495 | """Creates a new token""" |
39 | 491 | return IMPL.auth_create_token(context, token) | 496 | return IMPL.auth_create_token(context, token) |
40 | @@ -644,7 +649,9 @@ | |||
41 | 644 | 649 | ||
42 | 645 | def security_group_rule_get_by_security_group(context, security_group_id): | 650 | def security_group_rule_get_by_security_group(context, security_group_id): |
43 | 646 | """Get all rules for a a given security group""" | 651 | """Get all rules for a a given security group""" |
45 | 647 | return IMPL.security_group_rule_get_by_security_group(context, security_group_id) | 652 | return IMPL.security_group_rule_get_by_security_group(context, |
46 | 653 | security_group_id) | ||
47 | 654 | |||
48 | 648 | 655 | ||
49 | 649 | def security_group_rule_destroy(context, security_group_rule_id): | 656 | def security_group_rule_destroy(context, security_group_rule_id): |
50 | 650 | """Deletes a security group rule""" | 657 | """Deletes a security group rule""" |
51 | @@ -767,4 +774,3 @@ | |||
52 | 767 | network host | 774 | network host |
53 | 768 | """ | 775 | """ |
54 | 769 | return IMPL.host_get_networks(context, host) | 776 | return IMPL.host_get_networks(context, host) |
55 | 770 | |||
56 | 771 | 777 | ||
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 | 124 | if not session: | 124 | if not session: |
62 | 125 | session = get_session() | 125 | session = get_session() |
63 | 126 | 126 | ||
68 | 127 | result = session.query(models.Service | 127 | result = session.query(models.Service).\ |
69 | 128 | ).filter_by(id=service_id | 128 | filter_by(id=service_id).\ |
70 | 129 | ).filter_by(deleted=can_read_deleted(context) | 129 | filter_by(deleted=can_read_deleted(context)).\ |
71 | 130 | ).first() | 130 | first() |
72 | 131 | 131 | ||
73 | 132 | if not result: | 132 | if not result: |
74 | 133 | raise exception.NotFound('No service for id %s' % service_id) | 133 | raise exception.NotFound('No service for id %s' % service_id) |
75 | @@ -138,23 +138,23 @@ | |||
76 | 138 | @require_admin_context | 138 | @require_admin_context |
77 | 139 | def service_get_all_by_topic(context, topic): | 139 | def service_get_all_by_topic(context, topic): |
78 | 140 | session = get_session() | 140 | session = get_session() |
84 | 141 | return session.query(models.Service | 141 | return session.query(models.Service).\ |
85 | 142 | ).filter_by(deleted=False | 142 | filter_by(deleted=False).\ |
86 | 143 | ).filter_by(disabled=False | 143 | filter_by(disabled=False).\ |
87 | 144 | ).filter_by(topic=topic | 144 | filter_by(topic=topic).\ |
88 | 145 | ).all() | 145 | all() |
89 | 146 | 146 | ||
90 | 147 | 147 | ||
91 | 148 | @require_admin_context | 148 | @require_admin_context |
92 | 149 | def _service_get_all_topic_subquery(context, session, topic, subq, label): | 149 | def _service_get_all_topic_subquery(context, session, topic, subq, label): |
93 | 150 | sort_value = getattr(subq.c, label) | 150 | sort_value = getattr(subq.c, label) |
101 | 151 | return session.query(models.Service, func.coalesce(sort_value, 0) | 151 | return session.query(models.Service, func.coalesce(sort_value, 0)).\ |
102 | 152 | ).filter_by(topic=topic | 152 | filter_by(topic=topic).\ |
103 | 153 | ).filter_by(deleted=False | 153 | filter_by(deleted=False).\ |
104 | 154 | ).filter_by(disabled=False | 154 | filter_by(disabled=False).\ |
105 | 155 | ).outerjoin((subq, models.Service.host == subq.c.host) | 155 | outerjoin((subq, models.Service.host == subq.c.host)).\ |
106 | 156 | ).order_by(sort_value | 156 | order_by(sort_value).\ |
107 | 157 | ).all() | 157 | all() |
108 | 158 | 158 | ||
109 | 159 | 159 | ||
110 | 160 | @require_admin_context | 160 | @require_admin_context |
111 | @@ -171,10 +171,10 @@ | |||
112 | 171 | topic = 'compute' | 171 | topic = 'compute' |
113 | 172 | label = 'instance_cores' | 172 | label = 'instance_cores' |
114 | 173 | subq = session.query(models.Instance.host, | 173 | subq = session.query(models.Instance.host, |
119 | 174 | func.sum(models.Instance.vcpus).label(label) | 174 | func.sum(models.Instance.vcpus).label(label)).\ |
120 | 175 | ).filter_by(deleted=False | 175 | filter_by(deleted=False).\ |
121 | 176 | ).group_by(models.Instance.host | 176 | group_by(models.Instance.host).\ |
122 | 177 | ).subquery() | 177 | subquery() |
123 | 178 | return _service_get_all_topic_subquery(context, | 178 | return _service_get_all_topic_subquery(context, |
124 | 179 | session, | 179 | session, |
125 | 180 | topic, | 180 | topic, |
126 | @@ -189,10 +189,10 @@ | |||
127 | 189 | topic = 'network' | 189 | topic = 'network' |
128 | 190 | label = 'network_count' | 190 | label = 'network_count' |
129 | 191 | subq = session.query(models.Network.host, | 191 | subq = session.query(models.Network.host, |
134 | 192 | func.count(models.Network.id).label(label) | 192 | func.count(models.Network.id).label(label)).\ |
135 | 193 | ).filter_by(deleted=False | 193 | filter_by(deleted=False).\ |
136 | 194 | ).group_by(models.Network.host | 194 | group_by(models.Network.host).\ |
137 | 195 | ).subquery() | 195 | subquery() |
138 | 196 | return _service_get_all_topic_subquery(context, | 196 | return _service_get_all_topic_subquery(context, |
139 | 197 | session, | 197 | session, |
140 | 198 | topic, | 198 | topic, |
141 | @@ -207,10 +207,10 @@ | |||
142 | 207 | topic = 'volume' | 207 | topic = 'volume' |
143 | 208 | label = 'volume_gigabytes' | 208 | label = 'volume_gigabytes' |
144 | 209 | subq = session.query(models.Volume.host, | 209 | subq = session.query(models.Volume.host, |
149 | 210 | func.sum(models.Volume.size).label(label) | 210 | func.sum(models.Volume.size).label(label)).\ |
150 | 211 | ).filter_by(deleted=False | 211 | filter_by(deleted=False).\ |
151 | 212 | ).group_by(models.Volume.host | 212 | group_by(models.Volume.host).\ |
152 | 213 | ).subquery() | 213 | subquery() |
153 | 214 | return _service_get_all_topic_subquery(context, | 214 | return _service_get_all_topic_subquery(context, |
154 | 215 | session, | 215 | session, |
155 | 216 | topic, | 216 | topic, |
156 | @@ -221,11 +221,11 @@ | |||
157 | 221 | @require_admin_context | 221 | @require_admin_context |
158 | 222 | def service_get_by_args(context, host, binary): | 222 | def service_get_by_args(context, host, binary): |
159 | 223 | session = get_session() | 223 | session = get_session() |
165 | 224 | result = session.query(models.Service | 224 | result = session.query(models.Service).\ |
166 | 225 | ).filter_by(host=host | 225 | filter_by(host=host).\ |
167 | 226 | ).filter_by(binary=binary | 226 | filter_by(binary=binary).\ |
168 | 227 | ).filter_by(deleted=can_read_deleted(context) | 227 | filter_by(deleted=can_read_deleted(context)).\ |
169 | 228 | ).first() | 228 | first() |
170 | 229 | if not result: | 229 | if not result: |
171 | 230 | raise exception.NotFound('No service for %s, %s' % (host, binary)) | 230 | raise exception.NotFound('No service for %s, %s' % (host, binary)) |
172 | 231 | 231 | ||
173 | @@ -259,13 +259,13 @@ | |||
174 | 259 | authorize_project_context(context, project_id) | 259 | authorize_project_context(context, project_id) |
175 | 260 | session = get_session() | 260 | session = get_session() |
176 | 261 | with session.begin(): | 261 | with session.begin(): |
184 | 262 | floating_ip_ref = session.query(models.FloatingIp | 262 | floating_ip_ref = session.query(models.FloatingIp).\ |
185 | 263 | ).filter_by(host=host | 263 | filter_by(host=host).\ |
186 | 264 | ).filter_by(fixed_ip_id=None | 264 | filter_by(fixed_ip_id=None).\ |
187 | 265 | ).filter_by(project_id=None | 265 | filter_by(project_id=None).\ |
188 | 266 | ).filter_by(deleted=False | 266 | filter_by(deleted=False).\ |
189 | 267 | ).with_lockmode('update' | 267 | with_lockmode('update').\ |
190 | 268 | ).first() | 268 | first() |
191 | 269 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, | 269 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
192 | 270 | # then this has concurrency issues | 270 | # then this has concurrency issues |
193 | 271 | if not floating_ip_ref: | 271 | if not floating_ip_ref: |
194 | @@ -288,10 +288,10 @@ | |||
195 | 288 | def floating_ip_count_by_project(context, project_id): | 288 | def floating_ip_count_by_project(context, project_id): |
196 | 289 | authorize_project_context(context, project_id) | 289 | authorize_project_context(context, project_id) |
197 | 290 | session = get_session() | 290 | session = get_session() |
202 | 291 | return session.query(models.FloatingIp | 291 | return session.query(models.FloatingIp).\ |
203 | 292 | ).filter_by(project_id=project_id | 292 | filter_by(project_id=project_id).\ |
204 | 293 | ).filter_by(deleted=False | 293 | filter_by(deleted=False).\ |
205 | 294 | ).count() | 294 | count() |
206 | 295 | 295 | ||
207 | 296 | 296 | ||
208 | 297 | @require_context | 297 | @require_context |
209 | @@ -354,31 +354,31 @@ | |||
210 | 354 | @require_admin_context | 354 | @require_admin_context |
211 | 355 | def floating_ip_get_all(context): | 355 | def floating_ip_get_all(context): |
212 | 356 | session = get_session() | 356 | session = get_session() |
217 | 357 | return session.query(models.FloatingIp | 357 | return session.query(models.FloatingIp).\ |
218 | 358 | ).options(joinedload_all('fixed_ip.instance') | 358 | options(joinedload_all('fixed_ip.instance')).\ |
219 | 359 | ).filter_by(deleted=False | 359 | filter_by(deleted=False).\ |
220 | 360 | ).all() | 360 | all() |
221 | 361 | 361 | ||
222 | 362 | 362 | ||
223 | 363 | @require_admin_context | 363 | @require_admin_context |
224 | 364 | def floating_ip_get_all_by_host(context, host): | 364 | def floating_ip_get_all_by_host(context, host): |
225 | 365 | session = get_session() | 365 | session = get_session() |
231 | 366 | return session.query(models.FloatingIp | 366 | return session.query(models.FloatingIp).\ |
232 | 367 | ).options(joinedload_all('fixed_ip.instance') | 367 | options(joinedload_all('fixed_ip.instance')).\ |
233 | 368 | ).filter_by(host=host | 368 | filter_by(host=host).\ |
234 | 369 | ).filter_by(deleted=False | 369 | filter_by(deleted=False).\ |
235 | 370 | ).all() | 370 | all() |
236 | 371 | 371 | ||
237 | 372 | 372 | ||
238 | 373 | @require_context | 373 | @require_context |
239 | 374 | def floating_ip_get_all_by_project(context, project_id): | 374 | def floating_ip_get_all_by_project(context, project_id): |
240 | 375 | authorize_project_context(context, project_id) | 375 | authorize_project_context(context, project_id) |
241 | 376 | session = get_session() | 376 | session = get_session() |
247 | 377 | return session.query(models.FloatingIp | 377 | return session.query(models.FloatingIp).\ |
248 | 378 | ).options(joinedload_all('fixed_ip.instance') | 378 | options(joinedload_all('fixed_ip.instance')).\ |
249 | 379 | ).filter_by(project_id=project_id | 379 | filter_by(project_id=project_id).\ |
250 | 380 | ).filter_by(deleted=False | 380 | filter_by(deleted=False).\ |
251 | 381 | ).all() | 381 | all() |
252 | 382 | 382 | ||
253 | 383 | 383 | ||
254 | 384 | @require_context | 384 | @require_context |
255 | @@ -387,10 +387,10 @@ | |||
256 | 387 | if not session: | 387 | if not session: |
257 | 388 | session = get_session() | 388 | session = get_session() |
258 | 389 | 389 | ||
263 | 390 | result = session.query(models.FloatingIp | 390 | result = session.query(models.FloatingIp).\ |
264 | 391 | ).filter_by(address=address | 391 | filter_by(address=address).\ |
265 | 392 | ).filter_by(deleted=can_read_deleted(context) | 392 | filter_by(deleted=can_read_deleted(context)).\ |
266 | 393 | ).first() | 393 | first() |
267 | 394 | if not result: | 394 | if not result: |
268 | 395 | raise exception.NotFound('No fixed ip for address %s' % address) | 395 | raise exception.NotFound('No fixed ip for address %s' % address) |
269 | 396 | 396 | ||
270 | @@ -405,12 +405,12 @@ | |||
271 | 405 | session = get_session() | 405 | session = get_session() |
272 | 406 | with session.begin(): | 406 | with session.begin(): |
273 | 407 | instance = instance_get(context, instance_id, session=session) | 407 | instance = instance_get(context, instance_id, session=session) |
280 | 408 | fixed_ip_ref = session.query(models.FixedIp | 408 | fixed_ip_ref = session.query(models.FixedIp).\ |
281 | 409 | ).filter_by(address=address | 409 | filter_by(address=address).\ |
282 | 410 | ).filter_by(deleted=False | 410 | filter_by(deleted=False).\ |
283 | 411 | ).filter_by(instance=None | 411 | filter_by(instance=None).\ |
284 | 412 | ).with_lockmode('update' | 412 | with_lockmode('update').\ |
285 | 413 | ).first() | 413 | first() |
286 | 414 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, | 414 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
287 | 415 | # then this has concurrency issues | 415 | # then this has concurrency issues |
288 | 416 | if not fixed_ip_ref: | 416 | if not fixed_ip_ref: |
289 | @@ -425,13 +425,13 @@ | |||
290 | 425 | with session.begin(): | 425 | with session.begin(): |
291 | 426 | network_or_none = or_(models.FixedIp.network_id == network_id, | 426 | network_or_none = or_(models.FixedIp.network_id == network_id, |
292 | 427 | models.FixedIp.network_id == None) | 427 | models.FixedIp.network_id == None) |
300 | 428 | fixed_ip_ref = session.query(models.FixedIp | 428 | fixed_ip_ref = session.query(models.FixedIp).\ |
301 | 429 | ).filter(network_or_none | 429 | filter(network_or_none).\ |
302 | 430 | ).filter_by(reserved=False | 430 | filter_by(reserved=False).\ |
303 | 431 | ).filter_by(deleted=False | 431 | filter_by(deleted=False).\ |
304 | 432 | ).filter_by(instance=None | 432 | filter_by(instance=None).\ |
305 | 433 | ).with_lockmode('update' | 433 | with_lockmode('update').\ |
306 | 434 | ).first() | 434 | first() |
307 | 435 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, | 435 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
308 | 436 | # then this has concurrency issues | 436 | # then this has concurrency issues |
309 | 437 | if not fixed_ip_ref: | 437 | if not fixed_ip_ref: |
310 | @@ -455,6 +455,7 @@ | |||
311 | 455 | fixed_ip_ref.save() | 455 | fixed_ip_ref.save() |
312 | 456 | return fixed_ip_ref['address'] | 456 | return fixed_ip_ref['address'] |
313 | 457 | 457 | ||
314 | 458 | |||
315 | 458 | @require_context | 459 | @require_context |
316 | 459 | def fixed_ip_disassociate(context, address): | 460 | def fixed_ip_disassociate(context, address): |
317 | 460 | session = get_session() | 461 | session = get_session() |
318 | @@ -465,6 +466,7 @@ | |||
319 | 465 | fixed_ip_ref.instance = None | 466 | fixed_ip_ref.instance = None |
320 | 466 | fixed_ip_ref.save(session=session) | 467 | fixed_ip_ref.save(session=session) |
321 | 467 | 468 | ||
322 | 469 | |||
323 | 468 | @require_admin_context | 470 | @require_admin_context |
324 | 469 | def fixed_ip_disassociate_all_by_timeout(_context, host, time): | 471 | def fixed_ip_disassociate_all_by_timeout(_context, host, time): |
325 | 470 | session = get_session() | 472 | session = get_session() |
326 | @@ -486,12 +488,12 @@ | |||
327 | 486 | def fixed_ip_get_by_address(context, address, session=None): | 488 | def fixed_ip_get_by_address(context, address, session=None): |
328 | 487 | if not session: | 489 | if not session: |
329 | 488 | session = get_session() | 490 | session = get_session() |
336 | 489 | result = session.query(models.FixedIp | 491 | result = session.query(models.FixedIp).\ |
337 | 490 | ).filter_by(address=address | 492 | filter_by(address=address).\ |
338 | 491 | ).filter_by(deleted=can_read_deleted(context) | 493 | filter_by(deleted=can_read_deleted(context)).\ |
339 | 492 | ).options(joinedload('network') | 494 | options(joinedload('network')).\ |
340 | 493 | ).options(joinedload('instance') | 495 | options(joinedload('instance')).\ |
341 | 494 | ).first() | 496 | first() |
342 | 495 | if not result: | 497 | if not result: |
343 | 496 | raise exception.NotFound('No floating ip for address %s' % address) | 498 | raise exception.NotFound('No floating ip for address %s' % address) |
344 | 497 | 499 | ||
345 | @@ -552,10 +554,10 @@ | |||
346 | 552 | def instance_data_get_for_project(context, project_id): | 554 | def instance_data_get_for_project(context, project_id): |
347 | 553 | session = get_session() | 555 | session = get_session() |
348 | 554 | result = session.query(func.count(models.Instance.id), | 556 | result = session.query(func.count(models.Instance.id), |
353 | 555 | func.sum(models.Instance.vcpus) | 557 | func.sum(models.Instance.vcpus)).\ |
354 | 556 | ).filter_by(project_id=project_id | 558 | filter_by(project_id=project_id).\ |
355 | 557 | ).filter_by(deleted=False | 559 | filter_by(deleted=False).\ |
356 | 558 | ).first() | 560 | first() |
357 | 559 | # NOTE(vish): convert None to 0 | 561 | # NOTE(vish): convert None to 0 |
358 | 560 | return (result[0] or 0, result[1] or 0) | 562 | return (result[0] or 0, result[1] or 0) |
359 | 561 | 563 | ||
360 | @@ -575,18 +577,18 @@ | |||
361 | 575 | result = None | 577 | result = None |
362 | 576 | 578 | ||
363 | 577 | if is_admin_context(context): | 579 | if is_admin_context(context): |
369 | 578 | result = session.query(models.Instance | 580 | result = session.query(models.Instance).\ |
370 | 579 | ).options(joinedload('security_groups') | 581 | options(joinedload('security_groups')).\ |
371 | 580 | ).filter_by(id=instance_id | 582 | filter_by(id=instance_id).\ |
372 | 581 | ).filter_by(deleted=can_read_deleted(context) | 583 | filter_by(deleted=can_read_deleted(context)).\ |
373 | 582 | ).first() | 584 | first() |
374 | 583 | elif is_user_context(context): | 585 | elif is_user_context(context): |
381 | 584 | result = session.query(models.Instance | 586 | result = session.query(models.Instance).\ |
382 | 585 | ).options(joinedload('security_groups') | 587 | options(joinedload('security_groups')).\ |
383 | 586 | ).filter_by(project_id=context.project_id | 588 | filter_by(project_id=context.project_id).\ |
384 | 587 | ).filter_by(id=instance_id | 589 | filter_by(id=instance_id).\ |
385 | 588 | ).filter_by(deleted=False | 590 | filter_by(deleted=False).\ |
386 | 589 | ).first() | 591 | first() |
387 | 590 | if not result: | 592 | if not result: |
388 | 591 | raise exception.NotFound('No instance for id %s' % instance_id) | 593 | raise exception.NotFound('No instance for id %s' % instance_id) |
389 | 592 | 594 | ||
390 | @@ -596,22 +598,22 @@ | |||
391 | 596 | @require_admin_context | 598 | @require_admin_context |
392 | 597 | def instance_get_all(context): | 599 | def instance_get_all(context): |
393 | 598 | session = get_session() | 600 | session = get_session() |
399 | 599 | return session.query(models.Instance | 601 | return session.query(models.Instance).\ |
400 | 600 | ).options(joinedload_all('fixed_ip.floating_ips') | 602 | options(joinedload_all('fixed_ip.floating_ips')).\ |
401 | 601 | ).options(joinedload('security_groups') | 603 | options(joinedload('security_groups')).\ |
402 | 602 | ).filter_by(deleted=can_read_deleted(context) | 604 | filter_by(deleted=can_read_deleted(context)).\ |
403 | 603 | ).all() | 605 | all() |
404 | 604 | 606 | ||
405 | 605 | 607 | ||
406 | 606 | @require_admin_context | 608 | @require_admin_context |
407 | 607 | def instance_get_all_by_user(context, user_id): | 609 | def instance_get_all_by_user(context, user_id): |
408 | 608 | session = get_session() | 610 | session = get_session() |
415 | 609 | return session.query(models.Instance | 611 | return session.query(models.Instance).\ |
416 | 610 | ).options(joinedload_all('fixed_ip.floating_ips') | 612 | options(joinedload_all('fixed_ip.floating_ips')).\ |
417 | 611 | ).options(joinedload('security_groups') | 613 | options(joinedload('security_groups')).\ |
418 | 612 | ).filter_by(deleted=can_read_deleted(context) | 614 | filter_by(deleted=can_read_deleted(context)).\ |
419 | 613 | ).filter_by(user_id=user_id | 615 | filter_by(user_id=user_id).\ |
420 | 614 | ).all() | 616 | all() |
421 | 615 | 617 | ||
422 | 616 | 618 | ||
423 | 617 | @require_context | 619 | @require_context |
424 | @@ -619,12 +621,12 @@ | |||
425 | 619 | authorize_project_context(context, project_id) | 621 | authorize_project_context(context, project_id) |
426 | 620 | 622 | ||
427 | 621 | session = get_session() | 623 | session = get_session() |
434 | 622 | return session.query(models.Instance | 624 | return session.query(models.Instance).\ |
435 | 623 | ).options(joinedload_all('fixed_ip.floating_ips') | 625 | options(joinedload_all('fixed_ip.floating_ips')).\ |
436 | 624 | ).options(joinedload('security_groups') | 626 | options(joinedload('security_groups')).\ |
437 | 625 | ).filter_by(project_id=project_id | 627 | filter_by(project_id=project_id).\ |
438 | 626 | ).filter_by(deleted=can_read_deleted(context) | 628 | filter_by(deleted=can_read_deleted(context)).\ |
439 | 627 | ).all() | 629 | all() |
440 | 628 | 630 | ||
441 | 629 | 631 | ||
442 | 630 | @require_context | 632 | @require_context |
443 | @@ -632,20 +634,20 @@ | |||
444 | 632 | session = get_session() | 634 | session = get_session() |
445 | 633 | 635 | ||
446 | 634 | if is_admin_context(context): | 636 | if is_admin_context(context): |
453 | 635 | return session.query(models.Instance | 637 | return session.query(models.Instance).\ |
454 | 636 | ).options(joinedload_all('fixed_ip.floating_ips') | 638 | options(joinedload_all('fixed_ip.floating_ips')).\ |
455 | 637 | ).options(joinedload('security_groups') | 639 | options(joinedload('security_groups')).\ |
456 | 638 | ).filter_by(reservation_id=reservation_id | 640 | filter_by(reservation_id=reservation_id).\ |
457 | 639 | ).filter_by(deleted=can_read_deleted(context) | 641 | filter_by(deleted=can_read_deleted(context)).\ |
458 | 640 | ).all() | 642 | all() |
459 | 641 | elif is_user_context(context): | 643 | elif is_user_context(context): |
467 | 642 | return session.query(models.Instance | 644 | return session.query(models.Instance).\ |
468 | 643 | ).options(joinedload_all('fixed_ip.floating_ips') | 645 | options(joinedload_all('fixed_ip.floating_ips')).\ |
469 | 644 | ).options(joinedload('security_groups') | 646 | options(joinedload('security_groups')).\ |
470 | 645 | ).filter_by(project_id=context.project_id | 647 | filter_by(project_id=context.project_id).\ |
471 | 646 | ).filter_by(reservation_id=reservation_id | 648 | filter_by(reservation_id=reservation_id).\ |
472 | 647 | ).filter_by(deleted=False | 649 | filter_by(deleted=False).\ |
473 | 648 | ).all() | 650 | all() |
474 | 649 | 651 | ||
475 | 650 | 652 | ||
476 | 651 | @require_context | 653 | @require_context |
477 | @@ -653,18 +655,18 @@ | |||
478 | 653 | session = get_session() | 655 | session = get_session() |
479 | 654 | 656 | ||
480 | 655 | if is_admin_context(context): | 657 | if is_admin_context(context): |
486 | 656 | result = session.query(models.Instance | 658 | result = session.query(models.Instance).\ |
487 | 657 | ).options(joinedload('security_groups') | 659 | options(joinedload('security_groups')).\ |
488 | 658 | ).filter_by(internal_id=internal_id | 660 | filter_by(internal_id=internal_id).\ |
489 | 659 | ).filter_by(deleted=can_read_deleted(context) | 661 | filter_by(deleted=can_read_deleted(context)).\ |
490 | 660 | ).first() | 662 | first() |
491 | 661 | elif is_user_context(context): | 663 | elif is_user_context(context): |
498 | 662 | result = session.query(models.Instance | 664 | result = session.query(models.Instance).\ |
499 | 663 | ).options(joinedload('security_groups') | 665 | options(joinedload('security_groups')).\ |
500 | 664 | ).filter_by(project_id=context.project_id | 666 | filter_by(project_id=context.project_id).\ |
501 | 665 | ).filter_by(internal_id=internal_id | 667 | filter_by(internal_id=internal_id).\ |
502 | 666 | ).filter_by(deleted=False | 668 | filter_by(deleted=False).\ |
503 | 667 | ).first() | 669 | first() |
504 | 668 | if not result: | 670 | if not result: |
505 | 669 | raise exception.NotFound('Instance %s not found' % (internal_id)) | 671 | raise exception.NotFound('Instance %s not found' % (internal_id)) |
506 | 670 | 672 | ||
507 | @@ -675,9 +677,9 @@ | |||
508 | 675 | def instance_internal_id_exists(context, internal_id, session=None): | 677 | def instance_internal_id_exists(context, internal_id, session=None): |
509 | 676 | if not session: | 678 | if not session: |
510 | 677 | session = get_session() | 679 | session = get_session() |
514 | 678 | return session.query( | 680 | return session.query(exists().\ |
515 | 679 | exists().where(models.Instance.internal_id==internal_id) | 681 | where(models.Instance.internal_id == internal_id)).\ |
516 | 680 | ).one()[0] | 682 | one()[0] |
517 | 681 | 683 | ||
518 | 682 | 684 | ||
519 | 683 | @require_context | 685 | @require_context |
520 | @@ -782,11 +784,11 @@ | |||
521 | 782 | if not session: | 784 | if not session: |
522 | 783 | session = get_session() | 785 | session = get_session() |
523 | 784 | 786 | ||
529 | 785 | result = session.query(models.KeyPair | 787 | result = session.query(models.KeyPair).\ |
530 | 786 | ).filter_by(user_id=user_id | 788 | filter_by(user_id=user_id).\ |
531 | 787 | ).filter_by(name=name | 789 | filter_by(name=name).\ |
532 | 788 | ).filter_by(deleted=can_read_deleted(context) | 790 | filter_by(deleted=can_read_deleted(context)).\ |
533 | 789 | ).first() | 791 | first() |
534 | 790 | if not result: | 792 | if not result: |
535 | 791 | raise exception.NotFound('no keypair for user %s, name %s' % | 793 | raise exception.NotFound('no keypair for user %s, name %s' % |
536 | 792 | (user_id, name)) | 794 | (user_id, name)) |
537 | @@ -797,10 +799,10 @@ | |||
538 | 797 | def key_pair_get_all_by_user(context, user_id): | 799 | def key_pair_get_all_by_user(context, user_id): |
539 | 798 | authorize_user_context(context, user_id) | 800 | authorize_user_context(context, user_id) |
540 | 799 | session = get_session() | 801 | session = get_session() |
545 | 800 | return session.query(models.KeyPair | 802 | return session.query(models.KeyPair).\ |
546 | 801 | ).filter_by(user_id=user_id | 803 | filter_by(user_id=user_id).\ |
547 | 802 | ).filter_by(deleted=False | 804 | filter_by(deleted=False).\ |
548 | 803 | ).all() | 805 | all() |
549 | 804 | 806 | ||
550 | 805 | 807 | ||
551 | 806 | ################### | 808 | ################### |
552 | @@ -810,11 +812,11 @@ | |||
553 | 810 | def network_associate(context, project_id): | 812 | def network_associate(context, project_id): |
554 | 811 | session = get_session() | 813 | session = get_session() |
555 | 812 | with session.begin(): | 814 | with session.begin(): |
561 | 813 | network_ref = session.query(models.Network | 815 | network_ref = session.query(models.Network).\ |
562 | 814 | ).filter_by(deleted=False | 816 | filter_by(deleted=False).\ |
563 | 815 | ).filter_by(project_id=None | 817 | filter_by(project_id=None).\ |
564 | 816 | ).with_lockmode('update' | 818 | with_lockmode('update').\ |
565 | 817 | ).first() | 819 | first() |
566 | 818 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, | 820 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
567 | 819 | # then this has concurrency issues | 821 | # then this has concurrency issues |
568 | 820 | if not network_ref: | 822 | if not network_ref: |
569 | @@ -827,40 +829,40 @@ | |||
570 | 827 | @require_admin_context | 829 | @require_admin_context |
571 | 828 | def network_count(context): | 830 | def network_count(context): |
572 | 829 | session = get_session() | 831 | session = get_session() |
576 | 830 | return session.query(models.Network | 832 | return session.query(models.Network).\ |
577 | 831 | ).filter_by(deleted=can_read_deleted(context) | 833 | filter_by(deleted=can_read_deleted(context)).\ |
578 | 832 | ).count() | 834 | count() |
579 | 833 | 835 | ||
580 | 834 | 836 | ||
581 | 835 | @require_admin_context | 837 | @require_admin_context |
582 | 836 | def network_count_allocated_ips(context, network_id): | 838 | def network_count_allocated_ips(context, network_id): |
583 | 837 | session = get_session() | 839 | session = get_session() |
589 | 838 | return session.query(models.FixedIp | 840 | return session.query(models.FixedIp).\ |
590 | 839 | ).filter_by(network_id=network_id | 841 | filter_by(network_id=network_id).\ |
591 | 840 | ).filter_by(allocated=True | 842 | filter_by(allocated=True).\ |
592 | 841 | ).filter_by(deleted=False | 843 | filter_by(deleted=False).\ |
593 | 842 | ).count() | 844 | count() |
594 | 843 | 845 | ||
595 | 844 | 846 | ||
596 | 845 | @require_admin_context | 847 | @require_admin_context |
597 | 846 | def network_count_available_ips(context, network_id): | 848 | def network_count_available_ips(context, network_id): |
598 | 847 | session = get_session() | 849 | session = get_session() |
605 | 848 | return session.query(models.FixedIp | 850 | return session.query(models.FixedIp).\ |
606 | 849 | ).filter_by(network_id=network_id | 851 | filter_by(network_id=network_id).\ |
607 | 850 | ).filter_by(allocated=False | 852 | filter_by(allocated=False).\ |
608 | 851 | ).filter_by(reserved=False | 853 | filter_by(reserved=False).\ |
609 | 852 | ).filter_by(deleted=False | 854 | filter_by(deleted=False).\ |
610 | 853 | ).count() | 855 | count() |
611 | 854 | 856 | ||
612 | 855 | 857 | ||
613 | 856 | @require_admin_context | 858 | @require_admin_context |
614 | 857 | def network_count_reserved_ips(context, network_id): | 859 | def network_count_reserved_ips(context, network_id): |
615 | 858 | session = get_session() | 860 | session = get_session() |
621 | 859 | return session.query(models.FixedIp | 861 | return session.query(models.FixedIp).\ |
622 | 860 | ).filter_by(network_id=network_id | 862 | filter_by(network_id=network_id).\ |
623 | 861 | ).filter_by(reserved=True | 863 | filter_by(reserved=True).\ |
624 | 862 | ).filter_by(deleted=False | 864 | filter_by(deleted=False).\ |
625 | 863 | ).count() | 865 | count() |
626 | 864 | 866 | ||
627 | 865 | 867 | ||
628 | 866 | @require_admin_context | 868 | @require_admin_context |
629 | @@ -893,16 +895,16 @@ | |||
630 | 893 | result = None | 895 | result = None |
631 | 894 | 896 | ||
632 | 895 | if is_admin_context(context): | 897 | if is_admin_context(context): |
637 | 896 | result = session.query(models.Network | 898 | result = session.query(models.Network).\ |
638 | 897 | ).filter_by(id=network_id | 899 | filter_by(id=network_id).\ |
639 | 898 | ).filter_by(deleted=can_read_deleted(context) | 900 | filter_by(deleted=can_read_deleted(context)).\ |
640 | 899 | ).first() | 901 | first() |
641 | 900 | elif is_user_context(context): | 902 | elif is_user_context(context): |
647 | 901 | result = session.query(models.Network | 903 | result = session.query(models.Network).\ |
648 | 902 | ).filter_by(project_id=context.project_id | 904 | filter_by(project_id=context.project_id).\ |
649 | 903 | ).filter_by(id=network_id | 905 | filter_by(id=network_id).\ |
650 | 904 | ).filter_by(deleted=False | 906 | filter_by(deleted=False).\ |
651 | 905 | ).first() | 907 | first() |
652 | 906 | if not result: | 908 | if not result: |
653 | 907 | raise exception.NotFound('No network for id %s' % network_id) | 909 | raise exception.NotFound('No network for id %s' % network_id) |
654 | 908 | 910 | ||
655 | @@ -915,21 +917,21 @@ | |||
656 | 915 | @require_admin_context | 917 | @require_admin_context |
657 | 916 | def network_get_associated_fixed_ips(context, network_id): | 918 | def network_get_associated_fixed_ips(context, network_id): |
658 | 917 | session = get_session() | 919 | session = get_session() |
665 | 918 | return session.query(models.FixedIp | 920 | return session.query(models.FixedIp).\ |
666 | 919 | ).options(joinedload_all('instance') | 921 | options(joinedload_all('instance')).\ |
667 | 920 | ).filter_by(network_id=network_id | 922 | filter_by(network_id=network_id).\ |
668 | 921 | ).filter(models.FixedIp.instance_id != None | 923 | filter(models.FixedIp.instance_id != None).\ |
669 | 922 | ).filter_by(deleted=False | 924 | filter_by(deleted=False).\ |
670 | 923 | ).all() | 925 | all() |
671 | 924 | 926 | ||
672 | 925 | 927 | ||
673 | 926 | @require_admin_context | 928 | @require_admin_context |
674 | 927 | def network_get_by_bridge(context, bridge): | 929 | def network_get_by_bridge(context, bridge): |
675 | 928 | session = get_session() | 930 | session = get_session() |
680 | 929 | result = session.query(models.Network | 931 | result = session.query(models.Network).\ |
681 | 930 | ).filter_by(bridge=bridge | 932 | filter_by(bridge=bridge).\ |
682 | 931 | ).filter_by(deleted=False | 933 | filter_by(deleted=False).\ |
683 | 932 | ).first() | 934 | first() |
684 | 933 | 935 | ||
685 | 934 | if not result: | 936 | if not result: |
686 | 935 | raise exception.NotFound('No network for bridge %s' % bridge) | 937 | raise exception.NotFound('No network for bridge %s' % bridge) |
687 | @@ -939,12 +941,12 @@ | |||
688 | 939 | @require_admin_context | 941 | @require_admin_context |
689 | 940 | def network_get_by_instance(_context, instance_id): | 942 | def network_get_by_instance(_context, instance_id): |
690 | 941 | session = get_session() | 943 | session = get_session() |
697 | 942 | rv = session.query(models.Network | 944 | rv = session.query(models.Network).\ |
698 | 943 | ).filter_by(deleted=False | 945 | filter_by(deleted=False).\ |
699 | 944 | ).join(models.Network.fixed_ips | 946 | join(models.Network.fixed_ips).\ |
700 | 945 | ).filter_by(instance_id=instance_id | 947 | filter_by(instance_id=instance_id).\ |
701 | 946 | ).filter_by(deleted=False | 948 | filter_by(deleted=False).\ |
702 | 947 | ).first() | 949 | first() |
703 | 948 | if not rv: | 950 | if not rv: |
704 | 949 | raise exception.NotFound('No network for instance %s' % instance_id) | 951 | raise exception.NotFound('No network for instance %s' % instance_id) |
705 | 950 | return rv | 952 | return rv |
706 | @@ -954,11 +956,11 @@ | |||
707 | 954 | def network_set_host(context, network_id, host_id): | 956 | def network_set_host(context, network_id, host_id): |
708 | 955 | session = get_session() | 957 | session = get_session() |
709 | 956 | with session.begin(): | 958 | with session.begin(): |
715 | 957 | network_ref = session.query(models.Network | 959 | network_ref = session.query(models.Network).\ |
716 | 958 | ).filter_by(id=network_id | 960 | filter_by(id=network_id).\ |
717 | 959 | ).filter_by(deleted=False | 961 | filter_by(deleted=False).\ |
718 | 960 | ).with_lockmode('update' | 962 | with_lockmode('update').\ |
719 | 961 | ).first() | 963 | first() |
720 | 962 | if not network_ref: | 964 | if not network_ref: |
721 | 963 | raise exception.NotFound('No network for id %s' % network_id) | 965 | raise exception.NotFound('No network for id %s' % network_id) |
722 | 964 | 966 | ||
723 | @@ -987,10 +989,10 @@ | |||
724 | 987 | @require_context | 989 | @require_context |
725 | 988 | def project_get_network(context, project_id): | 990 | def project_get_network(context, project_id): |
726 | 989 | session = get_session() | 991 | session = get_session() |
731 | 990 | rv = session.query(models.Network | 992 | rv = session.query(models.Network).\ |
732 | 991 | ).filter_by(project_id=project_id | 993 | filter_by(project_id=project_id).\ |
733 | 992 | ).filter_by(deleted=False | 994 | filter_by(deleted=False).\ |
734 | 993 | ).first() | 995 | first() |
735 | 994 | if not rv: | 996 | if not rv: |
736 | 995 | try: | 997 | try: |
737 | 996 | return network_associate(context, project_id) | 998 | return network_associate(context, project_id) |
738 | @@ -998,10 +1000,10 @@ | |||
739 | 998 | # NOTE(vish): We hit this if there is a race and two | 1000 | # NOTE(vish): We hit this if there is a race and two |
740 | 999 | # processes are attempting to allocate the | 1001 | # processes are attempting to allocate the |
741 | 1000 | # network at the same time | 1002 | # network at the same time |
746 | 1001 | rv = session.query(models.Network | 1003 | rv = session.query(models.Network).\ |
747 | 1002 | ).filter_by(project_id=project_id | 1004 | filter_by(project_id=project_id).\ |
748 | 1003 | ).filter_by(deleted=False | 1005 | filter_by(deleted=False).\ |
749 | 1004 | ).first() | 1006 | first() |
750 | 1005 | return rv | 1007 | return rv |
751 | 1006 | 1008 | ||
752 | 1007 | 1009 | ||
753 | @@ -1019,9 +1021,9 @@ | |||
754 | 1019 | @require_admin_context | 1021 | @require_admin_context |
755 | 1020 | def export_device_count(context): | 1022 | def export_device_count(context): |
756 | 1021 | session = get_session() | 1023 | session = get_session() |
760 | 1022 | return session.query(models.ExportDevice | 1024 | return session.query(models.ExportDevice).\ |
761 | 1023 | ).filter_by(deleted=can_read_deleted(context) | 1025 | filter_by(deleted=can_read_deleted(context)).\ |
762 | 1024 | ).count() | 1026 | count() |
763 | 1025 | 1027 | ||
764 | 1026 | 1028 | ||
765 | 1027 | @require_admin_context | 1029 | @require_admin_context |
766 | @@ -1043,18 +1045,20 @@ | |||
767 | 1043 | session = get_session() | 1045 | session = get_session() |
768 | 1044 | session.delete(token) | 1046 | session.delete(token) |
769 | 1045 | 1047 | ||
770 | 1048 | |||
771 | 1046 | def auth_get_token(_context, token_hash): | 1049 | def auth_get_token(_context, token_hash): |
772 | 1047 | session = get_session() | 1050 | session = get_session() |
776 | 1048 | tk = session.query(models.AuthToken | 1051 | tk = session.query(models.AuthToken).\ |
777 | 1049 | ).filter_by(token_hash=token_hash | 1052 | filter_by(token_hash=token_hash).\ |
778 | 1050 | ).first() | 1053 | first() |
779 | 1051 | if not tk: | 1054 | if not tk: |
780 | 1052 | raise exception.NotFound('Token %s does not exist' % token_hash) | 1055 | raise exception.NotFound('Token %s does not exist' % token_hash) |
781 | 1053 | return tk | 1056 | return tk |
782 | 1054 | 1057 | ||
783 | 1058 | |||
784 | 1055 | def auth_create_token(_context, token): | 1059 | def auth_create_token(_context, token): |
785 | 1056 | tk = models.AuthToken() | 1060 | tk = models.AuthToken() |
787 | 1057 | for k,v in token.iteritems(): | 1061 | for k, v in token.iteritems(): |
788 | 1058 | tk[k] = v | 1062 | tk[k] = v |
789 | 1059 | tk.save() | 1063 | tk.save() |
790 | 1060 | return tk | 1064 | return tk |
791 | @@ -1068,10 +1072,10 @@ | |||
792 | 1068 | if not session: | 1072 | if not session: |
793 | 1069 | session = get_session() | 1073 | session = get_session() |
794 | 1070 | 1074 | ||
799 | 1071 | result = session.query(models.Quota | 1075 | result = session.query(models.Quota).\ |
800 | 1072 | ).filter_by(project_id=project_id | 1076 | filter_by(project_id=project_id).\ |
801 | 1073 | ).filter_by(deleted=can_read_deleted(context) | 1077 | filter_by(deleted=can_read_deleted(context)).\ |
802 | 1074 | ).first() | 1078 | first() |
803 | 1075 | if not result: | 1079 | if not result: |
804 | 1076 | raise exception.NotFound('No quota for project_id %s' % project_id) | 1080 | raise exception.NotFound('No quota for project_id %s' % project_id) |
805 | 1077 | 1081 | ||
806 | @@ -1112,11 +1116,11 @@ | |||
807 | 1112 | def volume_allocate_shelf_and_blade(context, volume_id): | 1116 | def volume_allocate_shelf_and_blade(context, volume_id): |
808 | 1113 | session = get_session() | 1117 | session = get_session() |
809 | 1114 | with session.begin(): | 1118 | with session.begin(): |
815 | 1115 | export_device = session.query(models.ExportDevice | 1119 | export_device = session.query(models.ExportDevice).\ |
816 | 1116 | ).filter_by(volume=None | 1120 | filter_by(volume=None).\ |
817 | 1117 | ).filter_by(deleted=False | 1121 | filter_by(deleted=False).\ |
818 | 1118 | ).with_lockmode('update' | 1122 | with_lockmode('update').\ |
819 | 1119 | ).first() | 1123 | first() |
820 | 1120 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, | 1124 | # NOTE(vish): if with_lockmode isn't supported, as in sqlite, |
821 | 1121 | # then this has concurrency issues | 1125 | # then this has concurrency issues |
822 | 1122 | if not export_device: | 1126 | if not export_device: |
823 | @@ -1134,7 +1138,8 @@ | |||
824 | 1134 | volume_ref['status'] = 'in-use' | 1138 | volume_ref['status'] = 'in-use' |
825 | 1135 | volume_ref['mountpoint'] = mountpoint | 1139 | volume_ref['mountpoint'] = mountpoint |
826 | 1136 | volume_ref['attach_status'] = 'attached' | 1140 | volume_ref['attach_status'] = 'attached' |
828 | 1137 | volume_ref.instance = instance_get(context, instance_id, session=session) | 1141 | volume_ref.instance = instance_get(context, instance_id, |
829 | 1142 | session=session) | ||
830 | 1138 | volume_ref.save(session=session) | 1143 | volume_ref.save(session=session) |
831 | 1139 | 1144 | ||
832 | 1140 | 1145 | ||
833 | @@ -1158,10 +1163,10 @@ | |||
834 | 1158 | def volume_data_get_for_project(context, project_id): | 1163 | def volume_data_get_for_project(context, project_id): |
835 | 1159 | session = get_session() | 1164 | session = get_session() |
836 | 1160 | result = session.query(func.count(models.Volume.id), | 1165 | result = session.query(func.count(models.Volume.id), |
841 | 1161 | func.sum(models.Volume.size) | 1166 | func.sum(models.Volume.size)).\ |
842 | 1162 | ).filter_by(project_id=project_id | 1167 | filter_by(project_id=project_id).\ |
843 | 1163 | ).filter_by(deleted=False | 1168 | filter_by(deleted=False).\ |
844 | 1164 | ).first() | 1169 | first() |
845 | 1165 | # NOTE(vish): convert None to 0 | 1170 | # NOTE(vish): convert None to 0 |
846 | 1166 | return (result[0] or 0, result[1] or 0) | 1171 | return (result[0] or 0, result[1] or 0) |
847 | 1167 | 1172 | ||
848 | @@ -1197,16 +1202,16 @@ | |||
849 | 1197 | result = None | 1202 | result = None |
850 | 1198 | 1203 | ||
851 | 1199 | if is_admin_context(context): | 1204 | if is_admin_context(context): |
856 | 1200 | result = session.query(models.Volume | 1205 | result = session.query(models.Volume).\ |
857 | 1201 | ).filter_by(id=volume_id | 1206 | filter_by(id=volume_id).\ |
858 | 1202 | ).filter_by(deleted=can_read_deleted(context) | 1207 | filter_by(deleted=can_read_deleted(context)).\ |
859 | 1203 | ).first() | 1208 | first() |
860 | 1204 | elif is_user_context(context): | 1209 | elif is_user_context(context): |
866 | 1205 | result = session.query(models.Volume | 1210 | result = session.query(models.Volume).\ |
867 | 1206 | ).filter_by(project_id=context.project_id | 1211 | filter_by(project_id=context.project_id).\ |
868 | 1207 | ).filter_by(id=volume_id | 1212 | filter_by(id=volume_id).\ |
869 | 1208 | ).filter_by(deleted=False | 1213 | filter_by(deleted=False).\ |
870 | 1209 | ).first() | 1214 | first() |
871 | 1210 | if not result: | 1215 | if not result: |
872 | 1211 | raise exception.NotFound('No volume for id %s' % volume_id) | 1216 | raise exception.NotFound('No volume for id %s' % volume_id) |
873 | 1212 | 1217 | ||
874 | @@ -1216,19 +1221,20 @@ | |||
875 | 1216 | @require_admin_context | 1221 | @require_admin_context |
876 | 1217 | def volume_get_all(context): | 1222 | def volume_get_all(context): |
877 | 1218 | session = get_session() | 1223 | session = get_session() |
881 | 1219 | return session.query(models.Volume | 1224 | return session.query(models.Volume).\ |
882 | 1220 | ).filter_by(deleted=can_read_deleted(context) | 1225 | filter_by(deleted=can_read_deleted(context)).\ |
883 | 1221 | ).all() | 1226 | all() |
884 | 1227 | |||
885 | 1222 | 1228 | ||
886 | 1223 | @require_context | 1229 | @require_context |
887 | 1224 | def volume_get_all_by_project(context, project_id): | 1230 | def volume_get_all_by_project(context, project_id): |
888 | 1225 | authorize_project_context(context, project_id) | 1231 | authorize_project_context(context, project_id) |
889 | 1226 | 1232 | ||
890 | 1227 | session = get_session() | 1233 | session = get_session() |
895 | 1228 | return session.query(models.Volume | 1234 | return session.query(models.Volume).\ |
896 | 1229 | ).filter_by(project_id=project_id | 1235 | filter_by(project_id=project_id).\ |
897 | 1230 | ).filter_by(deleted=can_read_deleted(context) | 1236 | filter_by(deleted=can_read_deleted(context)).\ |
898 | 1231 | ).all() | 1237 | all() |
899 | 1232 | 1238 | ||
900 | 1233 | 1239 | ||
901 | 1234 | @require_context | 1240 | @require_context |
902 | @@ -1237,16 +1243,16 @@ | |||
903 | 1237 | result = None | 1243 | result = None |
904 | 1238 | 1244 | ||
905 | 1239 | if is_admin_context(context): | 1245 | if is_admin_context(context): |
910 | 1240 | result = session.query(models.Volume | 1246 | result = session.query(models.Volume).\ |
911 | 1241 | ).filter_by(ec2_id=ec2_id | 1247 | filter_by(ec2_id=ec2_id).\ |
912 | 1242 | ).filter_by(deleted=can_read_deleted(context) | 1248 | filter_by(deleted=can_read_deleted(context)).\ |
913 | 1243 | ).first() | 1249 | first() |
914 | 1244 | elif is_user_context(context): | 1250 | elif is_user_context(context): |
920 | 1245 | result = session.query(models.Volume | 1251 | result = session.query(models.Volume).\ |
921 | 1246 | ).filter_by(project_id=context.project_id | 1252 | filter_by(project_id=context.project_id).\ |
922 | 1247 | ).filter_by(ec2_id=ec2_id | 1253 | filter_by(ec2_id=ec2_id).\ |
923 | 1248 | ).filter_by(deleted=False | 1254 | filter_by(deleted=False).\ |
924 | 1249 | ).first() | 1255 | first() |
925 | 1250 | else: | 1256 | else: |
926 | 1251 | raise exception.NotAuthorized() | 1257 | raise exception.NotAuthorized() |
927 | 1252 | 1258 | ||
928 | @@ -1261,19 +1267,19 @@ | |||
929 | 1261 | if not session: | 1267 | if not session: |
930 | 1262 | session = get_session() | 1268 | session = get_session() |
931 | 1263 | 1269 | ||
935 | 1264 | return session.query(exists( | 1270 | return session.query(exists().\ |
936 | 1265 | ).where(models.Volume.id==ec2_id) | 1271 | where(models.Volume.id == ec2_id)).\ |
937 | 1266 | ).one()[0] | 1272 | one()[0] |
938 | 1267 | 1273 | ||
939 | 1268 | 1274 | ||
940 | 1269 | @require_admin_context | 1275 | @require_admin_context |
941 | 1270 | def volume_get_instance(context, volume_id): | 1276 | def volume_get_instance(context, volume_id): |
942 | 1271 | session = get_session() | 1277 | session = get_session() |
948 | 1272 | result = session.query(models.Volume | 1278 | result = session.query(models.Volume).\ |
949 | 1273 | ).filter_by(id=volume_id | 1279 | filter_by(id=volume_id).\ |
950 | 1274 | ).filter_by(deleted=can_read_deleted(context) | 1280 | filter_by(deleted=can_read_deleted(context)).\ |
951 | 1275 | ).options(joinedload('instance') | 1281 | options(joinedload('instance')).\ |
952 | 1276 | ).first() | 1282 | first() |
953 | 1277 | if not result: | 1283 | if not result: |
954 | 1278 | raise exception.NotFound('Volume %s not found' % ec2_id) | 1284 | raise exception.NotFound('Volume %s not found' % ec2_id) |
955 | 1279 | 1285 | ||
956 | @@ -1283,9 +1289,9 @@ | |||
957 | 1283 | @require_admin_context | 1289 | @require_admin_context |
958 | 1284 | def volume_get_shelf_and_blade(context, volume_id): | 1290 | def volume_get_shelf_and_blade(context, volume_id): |
959 | 1285 | session = get_session() | 1291 | session = get_session() |
963 | 1286 | result = session.query(models.ExportDevice | 1292 | result = session.query(models.ExportDevice).\ |
964 | 1287 | ).filter_by(volume_id=volume_id | 1293 | filter_by(volume_id=volume_id).\ |
965 | 1288 | ).first() | 1294 | first() |
966 | 1289 | if not result: | 1295 | if not result: |
967 | 1290 | raise exception.NotFound('No export device found for volume %s' % | 1296 | raise exception.NotFound('No export device found for volume %s' % |
968 | 1291 | volume_id) | 1297 | volume_id) |
969 | @@ -1309,10 +1315,10 @@ | |||
970 | 1309 | @require_context | 1315 | @require_context |
971 | 1310 | def security_group_get_all(context): | 1316 | def security_group_get_all(context): |
972 | 1311 | session = get_session() | 1317 | session = get_session() |
977 | 1312 | return session.query(models.SecurityGroup | 1318 | return session.query(models.SecurityGroup).\ |
978 | 1313 | ).filter_by(deleted=can_read_deleted(context) | 1319 | filter_by(deleted=can_read_deleted(context)).\ |
979 | 1314 | ).options(joinedload_all('rules') | 1320 | options(joinedload_all('rules')).\ |
980 | 1315 | ).all() | 1321 | all() |
981 | 1316 | 1322 | ||
982 | 1317 | 1323 | ||
983 | 1318 | @require_context | 1324 | @require_context |
984 | @@ -1320,18 +1326,18 @@ | |||
985 | 1320 | if not session: | 1326 | if not session: |
986 | 1321 | session = get_session() | 1327 | session = get_session() |
987 | 1322 | if is_admin_context(context): | 1328 | if is_admin_context(context): |
993 | 1323 | result = session.query(models.SecurityGroup | 1329 | result = session.query(models.SecurityGroup).\ |
994 | 1324 | ).filter_by(deleted=can_read_deleted(context), | 1330 | filter_by(deleted=can_read_deleted(context),).\ |
995 | 1325 | ).filter_by(id=security_group_id | 1331 | filter_by(id=security_group_id).\ |
996 | 1326 | ).options(joinedload_all('rules') | 1332 | options(joinedload_all('rules')).\ |
997 | 1327 | ).first() | 1333 | first() |
998 | 1328 | else: | 1334 | else: |
1005 | 1329 | result = session.query(models.SecurityGroup | 1335 | result = session.query(models.SecurityGroup).\ |
1006 | 1330 | ).filter_by(deleted=False | 1336 | filter_by(deleted=False).\ |
1007 | 1331 | ).filter_by(id=security_group_id | 1337 | filter_by(id=security_group_id).\ |
1008 | 1332 | ).filter_by(project_id=context.project_id | 1338 | filter_by(project_id=context.project_id).\ |
1009 | 1333 | ).options(joinedload_all('rules') | 1339 | options(joinedload_all('rules')).\ |
1010 | 1334 | ).first() | 1340 | first() |
1011 | 1335 | if not result: | 1341 | if not result: |
1012 | 1336 | raise exception.NotFound("No secuity group with id %s" % | 1342 | raise exception.NotFound("No secuity group with id %s" % |
1013 | 1337 | security_group_id) | 1343 | security_group_id) |
1014 | @@ -1341,13 +1347,13 @@ | |||
1015 | 1341 | @require_context | 1347 | @require_context |
1016 | 1342 | def security_group_get_by_name(context, project_id, group_name): | 1348 | def security_group_get_by_name(context, project_id, group_name): |
1017 | 1343 | session = get_session() | 1349 | session = get_session() |
1025 | 1344 | result = session.query(models.SecurityGroup | 1350 | result = session.query(models.SecurityGroup).\ |
1026 | 1345 | ).filter_by(project_id=project_id | 1351 | filter_by(project_id=project_id).\ |
1027 | 1346 | ).filter_by(name=group_name | 1352 | filter_by(name=group_name).\ |
1028 | 1347 | ).filter_by(deleted=False | 1353 | filter_by(deleted=False).\ |
1029 | 1348 | ).options(joinedload_all('rules') | 1354 | options(joinedload_all('rules')).\ |
1030 | 1349 | ).options(joinedload_all('instances') | 1355 | options(joinedload_all('instances')).\ |
1031 | 1350 | ).first() | 1356 | first() |
1032 | 1351 | if not result: | 1357 | if not result: |
1033 | 1352 | raise exception.NotFound( | 1358 | raise exception.NotFound( |
1034 | 1353 | 'No security group named %s for project: %s' \ | 1359 | 'No security group named %s for project: %s' \ |
1035 | @@ -1358,23 +1364,23 @@ | |||
1036 | 1358 | @require_context | 1364 | @require_context |
1037 | 1359 | def security_group_get_by_project(context, project_id): | 1365 | def security_group_get_by_project(context, project_id): |
1038 | 1360 | session = get_session() | 1366 | session = get_session() |
1044 | 1361 | return session.query(models.SecurityGroup | 1367 | return session.query(models.SecurityGroup).\ |
1045 | 1362 | ).filter_by(project_id=project_id | 1368 | filter_by(project_id=project_id).\ |
1046 | 1363 | ).filter_by(deleted=False | 1369 | filter_by(deleted=False).\ |
1047 | 1364 | ).options(joinedload_all('rules') | 1370 | options(joinedload_all('rules')).\ |
1048 | 1365 | ).all() | 1371 | all() |
1049 | 1366 | 1372 | ||
1050 | 1367 | 1373 | ||
1051 | 1368 | @require_context | 1374 | @require_context |
1052 | 1369 | def security_group_get_by_instance(context, instance_id): | 1375 | def security_group_get_by_instance(context, instance_id): |
1053 | 1370 | session = get_session() | 1376 | session = get_session() |
1061 | 1371 | return session.query(models.SecurityGroup | 1377 | return session.query(models.SecurityGroup).\ |
1062 | 1372 | ).filter_by(deleted=False | 1378 | filter_by(deleted=False).\ |
1063 | 1373 | ).options(joinedload_all('rules') | 1379 | options(joinedload_all('rules')).\ |
1064 | 1374 | ).join(models.SecurityGroup.instances | 1380 | join(models.SecurityGroup.instances).\ |
1065 | 1375 | ).filter_by(id=instance_id | 1381 | filter_by(id=instance_id).\ |
1066 | 1376 | ).filter_by(deleted=False | 1382 | filter_by(deleted=False).\ |
1067 | 1377 | ).all() | 1383 | all() |
1068 | 1378 | 1384 | ||
1069 | 1379 | 1385 | ||
1070 | 1380 | @require_context | 1386 | @require_context |
1071 | @@ -1409,6 +1415,7 @@ | |||
1072 | 1409 | 'where group_id=:id', | 1415 | 'where group_id=:id', |
1073 | 1410 | {'id': security_group_id}) | 1416 | {'id': security_group_id}) |
1074 | 1411 | 1417 | ||
1075 | 1418 | |||
1076 | 1412 | @require_context | 1419 | @require_context |
1077 | 1413 | def security_group_destroy_all(context, session=None): | 1420 | def security_group_destroy_all(context, session=None): |
1078 | 1414 | if not session: | 1421 | if not session: |
1079 | @@ -1427,16 +1434,16 @@ | |||
1080 | 1427 | if not session: | 1434 | if not session: |
1081 | 1428 | session = get_session() | 1435 | session = get_session() |
1082 | 1429 | if is_admin_context(context): | 1436 | if is_admin_context(context): |
1087 | 1430 | result = session.query(models.SecurityGroupIngressRule | 1437 | result = session.query(models.SecurityGroupIngressRule).\ |
1088 | 1431 | ).filter_by(deleted=can_read_deleted(context) | 1438 | filter_by(deleted=can_read_deleted(context)).\ |
1089 | 1432 | ).filter_by(id=security_group_rule_id | 1439 | filter_by(id=security_group_rule_id).\ |
1090 | 1433 | ).first() | 1440 | first() |
1091 | 1434 | else: | 1441 | else: |
1092 | 1435 | # TODO(vish): Join to group and check for project_id | 1442 | # TODO(vish): Join to group and check for project_id |
1097 | 1436 | result = session.query(models.SecurityGroupIngressRule | 1443 | result = session.query(models.SecurityGroupIngressRule).\ |
1098 | 1437 | ).filter_by(deleted=False | 1444 | filter_by(deleted=False).\ |
1099 | 1438 | ).filter_by(id=security_group_rule_id | 1445 | filter_by(id=security_group_rule_id).\ |
1100 | 1439 | ).first() | 1446 | first() |
1101 | 1440 | if not result: | 1447 | if not result: |
1102 | 1441 | raise exception.NotFound("No secuity group rule with id %s" % | 1448 | raise exception.NotFound("No secuity group rule with id %s" % |
1103 | 1442 | security_group_rule_id) | 1449 | security_group_rule_id) |
1104 | @@ -1451,6 +1458,7 @@ | |||
1105 | 1451 | security_group_rule_ref.save() | 1458 | security_group_rule_ref.save() |
1106 | 1452 | return security_group_rule_ref | 1459 | return security_group_rule_ref |
1107 | 1453 | 1460 | ||
1108 | 1461 | |||
1109 | 1454 | @require_context | 1462 | @require_context |
1110 | 1455 | def security_group_rule_destroy(context, security_group_rule_id): | 1463 | def security_group_rule_destroy(context, security_group_rule_id): |
1111 | 1456 | session = get_session() | 1464 | session = get_session() |
1112 | @@ -1468,10 +1476,10 @@ | |||
1113 | 1468 | if not session: | 1476 | if not session: |
1114 | 1469 | session = get_session() | 1477 | session = get_session() |
1115 | 1470 | 1478 | ||
1120 | 1471 | result = session.query(models.User | 1479 | result = session.query(models.User).\ |
1121 | 1472 | ).filter_by(id=id | 1480 | filter_by(id=id).\ |
1122 | 1473 | ).filter_by(deleted=can_read_deleted(context) | 1481 | filter_by(deleted=can_read_deleted(context)).\ |
1123 | 1474 | ).first() | 1482 | first() |
1124 | 1475 | 1483 | ||
1125 | 1476 | if not result: | 1484 | if not result: |
1126 | 1477 | raise exception.NotFound('No user for id %s' % id) | 1485 | raise exception.NotFound('No user for id %s' % id) |
1127 | @@ -1484,10 +1492,10 @@ | |||
1128 | 1484 | if not session: | 1492 | if not session: |
1129 | 1485 | session = get_session() | 1493 | session = get_session() |
1130 | 1486 | 1494 | ||
1135 | 1487 | result = session.query(models.User | 1495 | result = session.query(models.User).\ |
1136 | 1488 | ).filter_by(access_key=access_key | 1496 | filter_by(access_key=access_key).\ |
1137 | 1489 | ).filter_by(deleted=can_read_deleted(context) | 1497 | filter_by(deleted=can_read_deleted(context)).\ |
1138 | 1490 | ).first() | 1498 | first() |
1139 | 1491 | 1499 | ||
1140 | 1492 | if not result: | 1500 | if not result: |
1141 | 1493 | raise exception.NotFound('No user for access key %s' % access_key) | 1501 | raise exception.NotFound('No user for access key %s' % access_key) |
1142 | @@ -1508,21 +1516,21 @@ | |||
1143 | 1508 | def user_delete(context, id): | 1516 | def user_delete(context, id): |
1144 | 1509 | session = get_session() | 1517 | session = get_session() |
1145 | 1510 | with session.begin(): | 1518 | with session.begin(): |
1152 | 1511 | session.execute('delete from user_project_association where user_id=:id', | 1519 | session.execute('delete from user_project_association ' |
1153 | 1512 | {'id': id}) | 1520 | 'where user_id=:id', {'id': id}) |
1154 | 1513 | session.execute('delete from user_role_association where user_id=:id', | 1521 | session.execute('delete from user_role_association ' |
1155 | 1514 | {'id': id}) | 1522 | 'where user_id=:id', {'id': id}) |
1156 | 1515 | session.execute('delete from user_project_role_association where user_id=:id', | 1523 | session.execute('delete from user_project_role_association ' |
1157 | 1516 | {'id': id}) | 1524 | 'where user_id=:id', {'id': id}) |
1158 | 1517 | user_ref = user_get(context, id, session=session) | 1525 | user_ref = user_get(context, id, session=session) |
1159 | 1518 | session.delete(user_ref) | 1526 | session.delete(user_ref) |
1160 | 1519 | 1527 | ||
1161 | 1520 | 1528 | ||
1162 | 1521 | def user_get_all(context): | 1529 | def user_get_all(context): |
1163 | 1522 | session = get_session() | 1530 | session = get_session() |
1167 | 1523 | return session.query(models.User | 1531 | return session.query(models.User).\ |
1168 | 1524 | ).filter_by(deleted=can_read_deleted(context) | 1532 | filter_by(deleted=can_read_deleted(context)).\ |
1169 | 1525 | ).all() | 1533 | all() |
1170 | 1526 | 1534 | ||
1171 | 1527 | 1535 | ||
1172 | 1528 | def project_create(_context, values): | 1536 | def project_create(_context, values): |
1173 | @@ -1547,11 +1555,11 @@ | |||
1174 | 1547 | if not session: | 1555 | if not session: |
1175 | 1548 | session = get_session() | 1556 | session = get_session() |
1176 | 1549 | 1557 | ||
1182 | 1550 | result = session.query(models.Project | 1558 | result = session.query(models.Project).\ |
1183 | 1551 | ).filter_by(deleted=False | 1559 | filter_by(deleted=False).\ |
1184 | 1552 | ).filter_by(id=id | 1560 | filter_by(id=id).\ |
1185 | 1553 | ).options(joinedload_all('members') | 1561 | options(joinedload_all('members')).\ |
1186 | 1554 | ).first() | 1562 | first() |
1187 | 1555 | 1563 | ||
1188 | 1556 | if not result: | 1564 | if not result: |
1189 | 1557 | raise exception.NotFound("No project with id %s" % id) | 1565 | raise exception.NotFound("No project with id %s" % id) |
1190 | @@ -1561,18 +1569,18 @@ | |||
1191 | 1561 | 1569 | ||
1192 | 1562 | def project_get_all(context): | 1570 | def project_get_all(context): |
1193 | 1563 | session = get_session() | 1571 | session = get_session() |
1198 | 1564 | return session.query(models.Project | 1572 | return session.query(models.Project).\ |
1199 | 1565 | ).filter_by(deleted=can_read_deleted(context) | 1573 | filter_by(deleted=can_read_deleted(context)).\ |
1200 | 1566 | ).options(joinedload_all('members') | 1574 | options(joinedload_all('members')).\ |
1201 | 1567 | ).all() | 1575 | all() |
1202 | 1568 | 1576 | ||
1203 | 1569 | 1577 | ||
1204 | 1570 | def project_get_by_user(context, user_id): | 1578 | def project_get_by_user(context, user_id): |
1205 | 1571 | session = get_session() | 1579 | session = get_session() |
1210 | 1572 | user = session.query(models.User | 1580 | user = session.query(models.User).\ |
1211 | 1573 | ).filter_by(deleted=can_read_deleted(context) | 1581 | filter_by(deleted=can_read_deleted(context)).\ |
1212 | 1574 | ).options(joinedload_all('projects') | 1582 | options(joinedload_all('projects')).\ |
1213 | 1575 | ).first() | 1583 | first() |
1214 | 1576 | return user.projects | 1584 | return user.projects |
1215 | 1577 | 1585 | ||
1216 | 1578 | 1586 | ||
1217 | @@ -1607,10 +1615,10 @@ | |||
1218 | 1607 | def project_delete(context, id): | 1615 | def project_delete(context, id): |
1219 | 1608 | session = get_session() | 1616 | session = get_session() |
1220 | 1609 | with session.begin(): | 1617 | with session.begin(): |
1225 | 1610 | session.execute('delete from user_project_association where project_id=:id', | 1618 | session.execute('delete from user_project_association ' |
1226 | 1611 | {'id': id}) | 1619 | 'where project_id=:id', {'id': id}) |
1227 | 1612 | session.execute('delete from user_project_role_association where project_id=:id', | 1620 | session.execute('delete from user_project_role_association ' |
1228 | 1613 | {'id': id}) | 1621 | 'where project_id=:id', {'id': id}) |
1229 | 1614 | project_ref = project_get(context, id, session=session) | 1622 | project_ref = project_get(context, id, session=session) |
1230 | 1615 | session.delete(project_ref) | 1623 | session.delete(project_ref) |
1231 | 1616 | 1624 | ||
1232 | @@ -1625,29 +1633,30 @@ | |||
1233 | 1625 | def user_get_roles_for_project(context, user_id, project_id): | 1633 | def user_get_roles_for_project(context, user_id, project_id): |
1234 | 1626 | session = get_session() | 1634 | session = get_session() |
1235 | 1627 | with session.begin(): | 1635 | with session.begin(): |
1240 | 1628 | res = session.query(models.UserProjectRoleAssociation | 1636 | res = session.query(models.UserProjectRoleAssociation).\ |
1241 | 1629 | ).filter_by(user_id=user_id | 1637 | filter_by(user_id=user_id).\ |
1242 | 1630 | ).filter_by(project_id=project_id | 1638 | filter_by(project_id=project_id).\ |
1243 | 1631 | ).all() | 1639 | all() |
1244 | 1632 | return [association.role for association in res] | 1640 | return [association.role for association in res] |
1245 | 1633 | 1641 | ||
1246 | 1642 | |||
1247 | 1634 | def user_remove_project_role(context, user_id, project_id, role): | 1643 | def user_remove_project_role(context, user_id, project_id, role): |
1248 | 1635 | session = get_session() | 1644 | session = get_session() |
1249 | 1636 | with session.begin(): | 1645 | with session.begin(): |
1255 | 1637 | session.execute('delete from user_project_role_association where ' + \ | 1646 | session.execute('delete from user_project_role_association where ' |
1256 | 1638 | 'user_id=:user_id and project_id=:project_id and ' + \ | 1647 | 'user_id=:user_id and project_id=:project_id and ' |
1257 | 1639 | 'role=:role', { 'user_id' : user_id, | 1648 | 'role=:role', {'user_id': user_id, |
1258 | 1640 | 'project_id' : project_id, | 1649 | 'project_id': project_id, |
1259 | 1641 | 'role' : role }) | 1650 | 'role': role}) |
1260 | 1642 | 1651 | ||
1261 | 1643 | 1652 | ||
1262 | 1644 | def user_remove_role(context, user_id, role): | 1653 | def user_remove_role(context, user_id, role): |
1263 | 1645 | session = get_session() | 1654 | session = get_session() |
1264 | 1646 | with session.begin(): | 1655 | with session.begin(): |
1269 | 1647 | res = session.query(models.UserRoleAssociation | 1656 | res = session.query(models.UserRoleAssociation).\ |
1270 | 1648 | ).filter_by(user_id=user_id | 1657 | filter_by(user_id=user_id).\ |
1271 | 1649 | ).filter_by(role=role | 1658 | filter_by(role=role).\ |
1272 | 1650 | ).all() | 1659 | all() |
1273 | 1651 | for role in res: | 1660 | for role in res: |
1274 | 1652 | session.delete(role) | 1661 | session.delete(role) |
1275 | 1653 | 1662 | ||
1276 | @@ -1656,7 +1665,8 @@ | |||
1277 | 1656 | session = get_session() | 1665 | session = get_session() |
1278 | 1657 | with session.begin(): | 1666 | with session.begin(): |
1279 | 1658 | user_ref = user_get(context, user_id, session=session) | 1667 | user_ref = user_get(context, user_id, session=session) |
1281 | 1659 | models.UserRoleAssociation(user=user_ref, role=role).save(session=session) | 1668 | models.UserRoleAssociation(user=user_ref, role=role).\ |
1282 | 1669 | save(session=session) | ||
1283 | 1660 | 1670 | ||
1284 | 1661 | 1671 | ||
1285 | 1662 | def user_add_project_role(context, user_id, project_id, role): | 1672 | def user_add_project_role(context, user_id, project_id, role): |
1286 | @@ -1672,12 +1682,11 @@ | |||
1287 | 1672 | ################### | 1682 | ################### |
1288 | 1673 | 1683 | ||
1289 | 1674 | 1684 | ||
1290 | 1675 | |||
1291 | 1676 | @require_admin_context | 1685 | @require_admin_context |
1292 | 1677 | def host_get_networks(context, host): | 1686 | def host_get_networks(context, host): |
1293 | 1678 | session = get_session() | 1687 | session = get_session() |
1294 | 1679 | with session.begin(): | 1688 | with session.begin(): |
1299 | 1680 | return session.query(models.Network | 1689 | return session.query(models.Network).\ |
1300 | 1681 | ).filter_by(deleted=False | 1690 | filter_by(deleted=False).\ |
1301 | 1682 | ).filter_by(host=host | 1691 | filter_by(host=host).\ |
1302 | 1683 | ).all() | 1692 | all() |
1303 | 1684 | 1693 | ||
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 | 134 | # """Represents a host where services are running""" | 134 | # """Represents a host where services are running""" |
1309 | 135 | # __tablename__ = 'hosts' | 135 | # __tablename__ = 'hosts' |
1310 | 136 | # id = Column(String(255), primary_key=True) | 136 | # id = Column(String(255), primary_key=True) |
1313 | 137 | # | 137 | |
1314 | 138 | # | 138 | |
1315 | 139 | class Service(BASE, NovaBase): | 139 | class Service(BASE, NovaBase): |
1316 | 140 | """Represents a running service on a host""" | 140 | """Represents a running service on a host""" |
1317 | 141 | __tablename__ = 'services' | 141 | __tablename__ = 'services' |
1318 | @@ -277,7 +277,8 @@ | |||
1319 | 277 | class ExportDevice(BASE, NovaBase): | 277 | class ExportDevice(BASE, NovaBase): |
1320 | 278 | """Represates a shelf and blade that a volume can be exported on""" | 278 | """Represates a shelf and blade that a volume can be exported on""" |
1321 | 279 | __tablename__ = 'export_devices' | 279 | __tablename__ = 'export_devices' |
1323 | 280 | __table_args__ = (schema.UniqueConstraint("shelf_id", "blade_id"), {'mysql_engine': 'InnoDB'}) | 280 | __table_args__ = (schema.UniqueConstraint("shelf_id", "blade_id"), |
1324 | 281 | {'mysql_engine': 'InnoDB'}) | ||
1325 | 281 | id = Column(Integer, primary_key=True) | 282 | id = Column(Integer, primary_key=True) |
1326 | 282 | shelf_id = Column(Integer) | 283 | shelf_id = Column(Integer) |
1327 | 283 | blade_id = Column(Integer) | 284 | blade_id = Column(Integer) |
1328 | @@ -308,10 +309,13 @@ | |||
1329 | 308 | 309 | ||
1330 | 309 | instances = relationship(Instance, | 310 | instances = relationship(Instance, |
1331 | 310 | secondary="security_group_instance_association", | 311 | secondary="security_group_instance_association", |
1336 | 311 | primaryjoin="and_(SecurityGroup.id == SecurityGroupInstanceAssociation.security_group_id," | 312 | primaryjoin='and_(' |
1337 | 312 | "SecurityGroup.deleted == False)", | 313 | 'SecurityGroup.id == ' |
1338 | 313 | secondaryjoin="and_(SecurityGroupInstanceAssociation.instance_id == Instance.id," | 314 | 'SecurityGroupInstanceAssociation.security_group_id,' |
1339 | 314 | "Instance.deleted == False)", | 315 | 'SecurityGroup.deleted == False)', |
1340 | 316 | secondaryjoin='and_(' | ||
1341 | 317 | 'SecurityGroupInstanceAssociation.instance_id == Instance.id,' | ||
1342 | 318 | 'Instance.deleted == False)', | ||
1343 | 315 | backref='security_groups') | 319 | backref='security_groups') |
1344 | 316 | 320 | ||
1345 | 317 | @property | 321 | @property |
1346 | @@ -330,11 +334,12 @@ | |||
1347 | 330 | 334 | ||
1348 | 331 | parent_group_id = Column(Integer, ForeignKey('security_groups.id')) | 335 | parent_group_id = Column(Integer, ForeignKey('security_groups.id')) |
1349 | 332 | parent_group = relationship("SecurityGroup", backref="rules", | 336 | parent_group = relationship("SecurityGroup", backref="rules", |
1353 | 333 | foreign_keys=parent_group_id, | 337 | foreign_keys=parent_group_id, |
1354 | 334 | primaryjoin="and_(SecurityGroupIngressRule.parent_group_id == SecurityGroup.id," | 338 | primaryjoin='and_(' |
1355 | 335 | "SecurityGroupIngressRule.deleted == False)") | 339 | 'SecurityGroupIngressRule.parent_group_id == SecurityGroup.id,' |
1356 | 340 | 'SecurityGroupIngressRule.deleted == False)') | ||
1357 | 336 | 341 | ||
1359 | 337 | protocol = Column(String(5)) # "tcp", "udp", or "icmp" | 342 | protocol = Column(String(5)) # "tcp", "udp", or "icmp" |
1360 | 338 | from_port = Column(Integer) | 343 | from_port = Column(Integer) |
1361 | 339 | to_port = Column(Integer) | 344 | to_port = Column(Integer) |
1362 | 340 | cidr = Column(String(255)) | 345 | cidr = Column(String(255)) |
1363 | @@ -414,8 +419,9 @@ | |||
1364 | 414 | instance = relationship(Instance, | 419 | instance = relationship(Instance, |
1365 | 415 | backref=backref('fixed_ip', uselist=False), | 420 | backref=backref('fixed_ip', uselist=False), |
1366 | 416 | foreign_keys=instance_id, | 421 | foreign_keys=instance_id, |
1369 | 417 | primaryjoin='and_(FixedIp.instance_id==Instance.id,' | 422 | primaryjoin='and_(' |
1370 | 418 | 'FixedIp.deleted==False)') | 423 | 'FixedIp.instance_id == Instance.id,' |
1371 | 424 | 'FixedIp.deleted == False)') | ||
1372 | 419 | allocated = Column(Boolean, default=False) | 425 | allocated = Column(Boolean, default=False) |
1373 | 420 | leased = Column(Boolean, default=False) | 426 | leased = Column(Boolean, default=False) |
1374 | 421 | reserved = Column(Boolean, default=False) | 427 | reserved = Column(Boolean, default=False) |
1375 | @@ -455,13 +461,13 @@ | |||
1376 | 455 | __tablename__ = 'user_project_role_association' | 461 | __tablename__ = 'user_project_role_association' |
1377 | 456 | user_id = Column(String(255), primary_key=True) | 462 | user_id = Column(String(255), primary_key=True) |
1378 | 457 | user = relationship(User, | 463 | user = relationship(User, |
1380 | 458 | primaryjoin=user_id==User.id, | 464 | primaryjoin=user_id == User.id, |
1381 | 459 | foreign_keys=[User.id], | 465 | foreign_keys=[User.id], |
1382 | 460 | uselist=False) | 466 | uselist=False) |
1383 | 461 | 467 | ||
1384 | 462 | project_id = Column(String(255), primary_key=True) | 468 | project_id = Column(String(255), primary_key=True) |
1385 | 463 | project = relationship(Project, | 469 | project = relationship(Project, |
1387 | 464 | primaryjoin=project_id==Project.id, | 470 | primaryjoin=project_id == Project.id, |
1388 | 465 | foreign_keys=[Project.id], | 471 | foreign_keys=[Project.id], |
1389 | 466 | uselist=False) | 472 | uselist=False) |
1390 | 467 | 473 | ||
1391 | @@ -485,7 +491,6 @@ | |||
1392 | 485 | project_id = Column(String(255), ForeignKey(Project.id), primary_key=True) | 491 | project_id = Column(String(255), ForeignKey(Project.id), primary_key=True) |
1393 | 486 | 492 | ||
1394 | 487 | 493 | ||
1395 | 488 | |||
1396 | 489 | class FloatingIp(BASE, NovaBase): | 494 | class FloatingIp(BASE, NovaBase): |
1397 | 490 | """Represents a floating ip that dynamically forwards to a fixed ip""" | 495 | """Represents a floating ip that dynamically forwards to a fixed ip""" |
1398 | 491 | __tablename__ = 'floating_ips' | 496 | __tablename__ = 'floating_ips' |
1399 | @@ -495,8 +500,9 @@ | |||
1400 | 495 | fixed_ip = relationship(FixedIp, | 500 | fixed_ip = relationship(FixedIp, |
1401 | 496 | backref=backref('floating_ips'), | 501 | backref=backref('floating_ips'), |
1402 | 497 | foreign_keys=fixed_ip_id, | 502 | foreign_keys=fixed_ip_id, |
1405 | 498 | primaryjoin='and_(FloatingIp.fixed_ip_id==FixedIp.id,' | 503 | primaryjoin='and_(' |
1406 | 499 | 'FloatingIp.deleted==False)') | 504 | 'FloatingIp.fixed_ip_id == FixedIp.id,' |
1407 | 505 | 'FloatingIp.deleted == False)') | ||
1408 | 500 | project_id = Column(String(255)) | 506 | project_id = Column(String(255)) |
1409 | 501 | host = Column(String(255)) # , ForeignKey('hosts.id')) | 507 | host = Column(String(255)) # , ForeignKey('hosts.id')) |
1410 | 502 | 508 | ||
1411 | @@ -507,7 +513,7 @@ | |||
1412 | 507 | models = (Service, Instance, Volume, ExportDevice, FixedIp, | 513 | models = (Service, Instance, Volume, ExportDevice, FixedIp, |
1413 | 508 | FloatingIp, Network, SecurityGroup, | 514 | FloatingIp, Network, SecurityGroup, |
1414 | 509 | SecurityGroupIngressRule, SecurityGroupInstanceAssociation, | 515 | SecurityGroupIngressRule, SecurityGroupInstanceAssociation, |
1416 | 510 | AuthToken, User, Project) # , Image, Host | 516 | AuthToken, User, Project) # , Image, Host |
1417 | 511 | engine = create_engine(FLAGS.sql_connection, echo=False) | 517 | engine = create_engine(FLAGS.sql_connection, echo=False) |
1418 | 512 | for model in models: | 518 | for model in models: |
1419 | 513 | model.metadata.create_all(engine) | 519 | model.metadata.create_all(engine) |
1420 | 514 | 520 | ||
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 | 29 | _ENGINE = None | 29 | _ENGINE = None |
1426 | 30 | _MAKER = None | 30 | _MAKER = None |
1427 | 31 | 31 | ||
1428 | 32 | |||
1429 | 32 | def get_session(autocommit=True, expire_on_commit=False): | 33 | def get_session(autocommit=True, expire_on_commit=False): |
1430 | 33 | """Helper method to grab session""" | 34 | """Helper method to grab session""" |
1431 | 34 | global _ENGINE | 35 | global _ENGINE |
1432 | @@ -39,5 +40,5 @@ | |||
1433 | 39 | _MAKER = (sessionmaker(bind=_ENGINE, | 40 | _MAKER = (sessionmaker(bind=_ENGINE, |
1434 | 40 | autocommit=autocommit, | 41 | autocommit=autocommit, |
1435 | 41 | expire_on_commit=expire_on_commit)) | 42 | expire_on_commit=expire_on_commit)) |
1437 | 42 | session = _MAKER() | 43 | session = _MAKER() |
1438 | 43 | return session | 44 | 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.