summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-11-06 06:24:44 +0000
committerGerrit Code Review <review@openstack.org>2018-11-06 06:24:44 +0000
commit40fe31c931f025abbe386b621f3a7d887ed3fc1d (patch)
tree1df9435bb7f63cc3f5f8902ae9fd824a091bb1f3
parent13af577a346806f7b8f4cf0b9526197c9309d7fb (diff)
parent50f65aec9dc0e562efd6d0dc8f663d838d706c12 (diff)
Merge "Refactor sql driver api"
-rw-r--r--freezer_api/db/sqlalchemy/api.py614
1 files changed, 221 insertions, 393 deletions
diff --git a/freezer_api/db/sqlalchemy/api.py b/freezer_api/db/sqlalchemy/api.py
index c65e09d..898033f 100644
--- a/freezer_api/db/sqlalchemy/api.py
+++ b/freezer_api/db/sqlalchemy/api.py
@@ -142,20 +142,157 @@ def model_query(session, model,
142 return query 142 return query
143 143
144 144
145def get_client(user_id, project_id=None, client_id=None, offset=0, 145def delete_tuple(tablename, user_id, tuple_id, project_id=None):
146 limit=10, search=None): 146 session = get_db_session()
147 with session.begin():
148 try:
149 query = model_query(session, tablename, project_id=project_id)
150 query = query.filter_by(user_id=user_id).filter_by(id=tuple_id)
151 result = query.all()
152 if 1 == len(result):
153 result[0].delete(session=session)
154 LOG.info('Tuple delete, Tuple_id: '
155 '{0} deleted in Table {1}'.
156 format(tuple_id, tablename))
157 else:
158 LOG.info('Tuple delete, Tuple_id: '
159 '{0} not found in Table {1}'.
160 format(tuple_id, tablename))
161 except Exception as e:
162 session.close()
163 raise freezer_api_exc.StorageEngineError(
164 message='Mysql operation failed {0}'.format(e))
165 session.close()
166 return tuple_id
147 167
148 search = search or {} 168
149 clients = [] 169def get_tuple(tablename, user_id, tuple_id, project_id=None):
150 session = get_db_session() 170 session = get_db_session()
151 query = model_query(session, models.Client, project_id=project_id) 171 with session.begin():
172 try:
173 query = model_query(session, tablename, project_id=project_id)
174 query = query.filter_by(user_id=user_id).filter_by(id=tuple_id)
175 result = query.all()
176 except Exception as e:
177 raise freezer_api_exc.StorageEngineError(
178 message='Mysql operation failed {0}'.format(e))
179 session.close()
180 return result
181
182
183def add_tuple(tuple):
184 session = get_db_session()
185 with session.begin():
186 try:
187 tuple.save(session=session)
188 except Exception as e:
189 session.close()
190 raise freezer_api_exc.StorageEngineError(
191 message='Mysql operation failed {0}'.format(e))
192
193 session.close()
194
195
196def update_tuple(tablename, user_id, tuple_id, tuple_values, project_id=None):
197
198 session = get_db_session()
199 with session.begin():
200 try:
201 query = model_query(session, tablename, project_id=project_id)
202 query = query.filter_by(user_id=user_id).filter_by(id=tuple_id)
203 result = query.update(tuple_values)
204 except Exception as e:
205 session.close()
206 raise freezer_api_exc.StorageEngineError(
207 message='Mysql operation failed {0}'.format(e))
208
209 session.close()
210
211 if not result:
212 raise freezer_api_exc.DocumentNotFound(
213 message='Tuple not registered with ID'
214 ' {0} in Table{1} '.format(tuple_id, tablename))
152 215
153 if client_id:
154 query = query.filter_by(user_id=user_id).filter_by(client_id=client_id)
155 else: 216 else:
156 query = query.filter_by(user_id=user_id) 217 LOG.info('Tuple updated, tuple_id: {0}'.format(tuple_id))
218 return tuple_id
219
157 220
158 result = query.all() 221def replace_tuple(tablename, user_id, tuple_id, tuple_values, project_id=None):
222
223 bCreate = False
224
225 session = get_db_session()
226 with session.begin():
227 try:
228 query = model_query(session, tablename, project_id=project_id)
229 query = query.filter_by(user_id=user_id).filter_by(id=tuple_id)
230 result = query.update(tuple_values)
231 if not result:
232 bCreate = True
233 except Exception as e:
234 session.close()
235 raise freezer_api_exc.StorageEngineError(
236 message='Mysql operation failed {0}'.format(e))
237
238 session.close()
239
240 if bCreate:
241 tuplet = models.Session()
242 tuplet.update(tuple_values)
243 session = get_db_session()
244 with session.begin():
245 try:
246 tuplet.save(session=session)
247 except Exception as e:
248 session.close()
249 raise freezer_api_exc.\
250 StorageEngineError(message='Mysql operation failed {0}'.
251 format(e))
252 session.close()
253
254 return tuple_id
255
256
257def search_tuple(tablename, user_id, project_id=None, offset=0,
258 limit=100, search=None):
259 session = get_db_session()
260 with session.begin():
261 try:
262 # TODO(gecong) search will be implemented in the future
263 query = model_query(session, tablename, project_id=project_id)
264 query = query.filter_by(user_id=user_id)
265 query = query.offset(offset)
266 query = query.limit(limit)
267 result = query.all()
268 except Exception as e:
269 raise freezer_api_exc.StorageEngineError(
270 message='Mysql operation failed {0}'.format(e))
271 session.close()
272 return result
273
274
275def get_client(user_id, project_id=None, client_id=None, offset=0,
276 limit=100, search=None):
277
278 search = search or {}
279 clients = []
280 session = get_db_session()
281 with session.begin():
282 try:
283 query = model_query(session, models.Client, project_id=project_id)
284 if client_id:
285 query = query.filter_by(user_id=user_id).filter_by(
286 client_id=client_id)
287 else:
288 query = query.filter_by(user_id=user_id)
289 query = query.offset(offset)
290 query = query.limit(limit)
291 result = query.all()
292 except Exception as e:
293 raise freezer_api_exc.StorageEngineError(
294 message='Mysql operation failed {0}'.format(e))
295 session.close()
159 296
160 for client in result: 297 for client in result:
161 clientmap = {} 298 clientmap = {}
@@ -167,7 +304,6 @@ def get_client(user_id, project_id=None, client_id=None, offset=0,
167 u'description': client.description} 304 u'description': client.description}
168 clients.append(clientmap) 305 clients.append(clientmap)
169 306
170 session.close()
171 return clients 307 return clients
172 308
173 309
@@ -197,80 +333,38 @@ def add_client(user_id, doc, project_id=None):
197 values['description'] = client_json.get('description', None) 333 values['description'] = client_json.get('description', None)
198 client.update(values) 334 client.update(values)
199 335
200 session = get_db_session() 336 add_tuple(tuple=client)
201 with session.begin():
202 try:
203 client.save(session=session)
204 except Exception as e:
205 session.close()
206 raise freezer_api_exc.StorageEngineError(
207 message='mysql operation failed {0}'.format(e))
208 337
209 LOG.info('Client registered, client_id: {0}'.format(client_id)) 338 LOG.info('Client registered, client_id: {0}'.format(client_id))
210 session.close()
211 return client_id 339 return client_id
212 340
213 341
214def delete_client(user_id, client_id, project_id=None): 342def delete_client(user_id, client_id, project_id=None):
215 session = get_db_session() 343
216 query = model_query(session, models.Client, project_id=project_id) 344 existing = get_client(project_id=project_id, user_id=user_id,
217 query = query.filter_by(user_id=user_id).filter_by(client_id=client_id) 345 client_id=client_id)
218 result = query.all() 346
219 if 1 == len(result): 347 if existing:
220 try: 348 clientt = existing[0].get('client')
221 result[0].delete(session=session) 349 clientid = clientt.get('uuid')
222 except Exception as e: 350 delete_tuple(tablename=models.Client, user_id=user_id,
223 session.close() 351 tuple_id=clientid, project_id=project_id)
224 raise freezer_api_exc.StorageEngineError(
225 message='mysql operation failed {0}'.format(e))
226 LOG.info('Client delete, client_id: {0} deleted'. 352 LOG.info('Client delete, client_id: {0} deleted'.
227 format(client_id)) 353 format(client_id))
228 else: 354 else:
229 LOG.info('Client delete, client_id: {0} not found'. 355 LOG.info('Client delete, client_id: {0} not found'.
230 format(client_id)) 356 format(client_id))
231
232 session.close()
233 return client_id 357 return client_id
234 358
235 359
236def delete_action(user_id, action_id, project_id=None): 360def delete_action(user_id, action_id, project_id=None):
237 361
238 session = get_db_session() 362 tupleid = delete_tuple(tablename=models.Action, user_id=user_id,
239 query = model_query(session, models.Action, project_id=project_id) 363 tuple_id=action_id, project_id=project_id)
240 query = query.filter_by(user_id=user_id).filter_by(id=action_id)
241 364
242 result = query.all() 365 tupleid = delete_tuple(tablename=models.ActionReport, user_id=user_id,
243 if 1 == len(result): 366 tuple_id=action_id, project_id=project_id)
244 try: 367 return tupleid
245 result[0].delete(session=session)
246 except Exception as e:
247 session.close()
248 raise freezer_api_exc.StorageEngineError(
249 message='mysql operation failed {0}'.format(e))
250 LOG.info('Action delete, action_id: {0} deleted'.
251 format(action_id))
252 else:
253 LOG.info('Action delete, action_id: {0} not found'.
254 format(action_id))
255
256 query = model_query(session, models.ActionReport, project_id=project_id)
257 query = query.filter_by(user_id=user_id).filter_by(id=action_id)
258
259 result = query.all()
260 if 1 == len(result):
261 try:
262 result[0].delete(session=session)
263 except Exception as e:
264 session.close()
265 raise freezer_api_exc.StorageEngineError(
266 message='mysql operation failed {0}'.format(e))
267 LOG.info('ActionReport delete, action_id: {0} deleted'.
268 format(action_id))
269 else:
270 LOG.info('ActionReport delete, action_id: {0} not found'.
271 format(action_id))
272 session.close()
273 return action_id
274 368
275 369
276def add_action(user_id, doc, project_id=None): 370def add_action(user_id, doc, project_id=None):
@@ -316,14 +410,7 @@ def add_action(user_id, doc, project_id=None):
316 410
317 action.update(actionvalue) 411 action.update(actionvalue)
318 412
319 session = get_db_session() 413 add_tuple(tuple=action)
320 with session.begin():
321 try:
322 action.save(session=session)
323 except Exception as e:
324 session.close()
325 raise freezer_api_exc.StorageEngineError(
326 message='mysql operation failed {0}'.format(e))
327 414
328 LOG.info('Action registered, action_id: {0}'.format(action_id)) 415 LOG.info('Action registered, action_id: {0}'.format(action_id))
329 416
@@ -335,32 +422,17 @@ def add_action(user_id, doc, project_id=None):
335 422
336 actionReport.update(actionreportvalue) 423 actionReport.update(actionreportvalue)
337 424
338 with session.begin(): 425 add_tuple(tuple=actionReport)
339 try:
340 actionReport.save(session=session)
341 except Exception as e:
342 session.close()
343 raise freezer_api_exc.StorageEngineError(
344 message='mysql operation failed {0}'.format(e))
345
346 LOG.info('Action Reports registered, action_id: {0}'. 426 LOG.info('Action Reports registered, action_id: {0}'.
347 format(action_id)) 427 format(action_id))
348 session.close()
349 return action_id 428 return action_id
350 429
351 430
352def get_action(user_id, action_id, project_id=None): 431def get_action(user_id, action_id, project_id=None):
353 session = get_db_session()
354 with session.begin():
355 try:
356 query = model_query(session, models.Action, project_id=project_id)
357 query = query.filter_by(user_id=user_id).filter_by(id=action_id)
358 result = query.all()
359 except Exception as e:
360 raise freezer_api_exc.StorageEngineError(
361 message='mysql operation failed {0}'.format(e))
362 432
363 session.close() 433 result = get_tuple(tablename=models.Action, user_id=user_id,
434 tuple_id=action_id, project_id=project_id)
435
364 values = {} 436 values = {}
365 if 1 == len(result): 437 if 1 == len(result):
366 values['project_id'] = result[0].get('project_id') 438 values['project_id'] = result[0].get('project_id')
@@ -385,17 +457,13 @@ def get_action(user_id, action_id, project_id=None):
385 457
386 458
387def search_action(user_id, project_id=None, offset=0, 459def search_action(user_id, project_id=None, offset=0,
388 limit=10, search=None): 460 limit=100, search=None):
389 461
390 search = search or {}
391 actions = [] 462 actions = []
392 463
393 session = get_db_session() 464 result = search_tuple(tablename=models.Action, user_id=user_id,
394 query = model_query(session, models.Action, project_id=project_id) 465 project_id=project_id, offset=offset,
395 query = query.filter_by(user_id=user_id) 466 limit=limit, search=search)
396
397 result = query.all()
398
399 for action in result: 467 for action in result:
400 actionmap = {} 468 actionmap = {}
401 actionmap['project_id'] = project_id 469 actionmap['project_id'] = project_id
@@ -421,7 +489,6 @@ def search_action(user_id, project_id=None, offset=0,
421 489
422 actions.append(actionmap) 490 actions.append(actionmap)
423 491
424 session.close()
425 return actions 492 return actions
426 493
427 494
@@ -453,26 +520,10 @@ def update_action(user_id, action_id, patch_doc, project_id=None):
453 520
454 values['backup_metadata'] = json_utils.json_encode(freezer_action) 521 values['backup_metadata'] = json_utils.json_encode(freezer_action)
455 522
456 session = get_db_session() 523 update_tuple(tablename=models.Action, user_id=user_id, tuple_id=action_id,
457 with session.begin(): 524 tuple_values=values, project_id=project_id)
458 try:
459 query = model_query(session, models.Action, project_id=project_id)
460 query = query.filter_by(user_id=user_id).filter_by(id=action_id)
461 result = query.update(values)
462 except Exception as e:
463 session.close()
464 raise freezer_api_exc.StorageEngineError(
465 message='mysql operation failed {0}'.format(e))
466
467 session.close()
468 525
469 if not result: 526 return action_id
470 raise freezer_api_exc.DocumentNotFound(
471 message='Action not registered with ID'
472 ' {0}'.format(action_id))
473 else:
474 LOG.info('action updated, action_id: {0}'.format(action_id))
475 return action_id
476 527
477 528
478def replace_action(user_id, action_id, doc, project_id=None): 529def replace_action(user_id, action_id, doc, project_id=None):
@@ -484,7 +535,6 @@ def replace_action(user_id, action_id, doc, project_id=None):
484 values = {} 535 values = {}
485 keyt = ['action', 'mode', 'backup_name', 'container', 536 keyt = ['action', 'mode', 'backup_name', 'container',
486 'src_file', 'timeout', 'priority', 'mandatory', 'log_file'] 537 'src_file', 'timeout', 'priority', 'mandatory', 'log_file']
487 bCreate = False
488 538
489 freezer_action = valid_doc.get('freezer_action', {}) 539 freezer_action = valid_doc.get('freezer_action', {})
490 540
@@ -500,57 +550,19 @@ def replace_action(user_id, action_id, doc, project_id=None):
500 if key in keyt: 550 if key in keyt:
501 values[key] = freezer_action.get(key) 551 values[key] = freezer_action.get(key)
502 552
503 session = get_db_session() 553 replace_tuple(tablename=models.Action, user_id=user_id,
504 with session.begin(): 554 tuple_id=action_id, tuple_values=values,
505 try: 555 project_id=project_id)
506 query = model_query(session, models.Action, project_id=project_id)
507 query = query.filter_by(user_id=user_id).filter_by(id=action_id)
508 result = query.update(values)
509 if not result:
510 bCreate = True
511 except Exception as e:
512 session.close()
513 raise freezer_api_exc.StorageEngineError(
514 message='mysql operation failed {0}'.format(e))
515
516 session.close()
517
518 if bCreate:
519 action = models.Action()
520 action.update(values)
521 session = get_db_session()
522 with session.begin():
523 try:
524 action.save(session=session)
525 except Exception as e:
526 session.close()
527 raise freezer_api_exc.\
528 StorageEngineError(message='mysql operation failed {0}'.
529 format(e))
530 session.close()
531 556
532 LOG.info('action replaced, action_id: {0}'.format(action_id)) 557 LOG.info('action replaced, action_id: {0}'.format(action_id))
533 return action_id 558 return action_id
534 559
535 560
536def delete_job(user_id, job_id, project_id=None): 561def delete_job(user_id, job_id, project_id=None):
537 session = get_db_session() 562
538 query = model_query(session, models.Job, project_id=project_id) 563 tupleid = delete_tuple(tablename=models.Job, user_id=user_id,
539 query = query.filter_by(user_id=user_id).filter_by(id=job_id) 564 tuple_id=job_id, project_id=project_id)
540 result = query.all() 565 return tupleid
541 if 1 == len(result):
542 try:
543 result[0].delete(session=session)
544 except Exception as e:
545 session.close()
546 raise freezer_api_exc.StorageEngineError(
547 message='mysql operation failed {0}'.format(e))
548 LOG.info('Job delete, job_id: {0} deleted'.format(job_id))
549 else:
550 LOG.info('Job delete, job_id: {0} not found'.
551 format(job_id))
552 session.close()
553 return job_id
554 566
555 567
556def add_job(user_id, doc, project_id=None): 568def add_job(user_id, doc, project_id=None):
@@ -582,14 +594,7 @@ def add_job(user_id, doc, project_id=None):
582 json_encode(job_doc.pop('job_actions', '')) 594 json_encode(job_doc.pop('job_actions', ''))
583 job.update(jobvalue) 595 job.update(jobvalue)
584 596
585 session = get_db_session() 597 add_tuple(tuple=job)
586 with session.begin():
587 try:
588 job.save(session=session)
589 except Exception as e:
590 session.close()
591 raise freezer_api_exc.StorageEngineError(
592 message='mysql operation failed {0}'.format(e))
593 598
594 LOG.info('Job registered, job_id: {0}'.format(job_id)) 599 LOG.info('Job registered, job_id: {0}'.format(job_id))
595 600
@@ -597,17 +602,9 @@ def add_job(user_id, doc, project_id=None):
597 602
598 603
599def get_job(user_id, job_id, project_id=None): 604def get_job(user_id, job_id, project_id=None):
600 session = get_db_session()
601 with session.begin():
602 try:
603 query = model_query(session, models.Job, project_id=project_id)
604 query = query.filter_by(user_id=user_id).filter_by(id=job_id)
605 result = query.all()
606 except Exception as e:
607 raise freezer_api_exc.StorageEngineError(
608 message='mysql operation failed {0}'.format(e))
609 605
610 session.close() 606 result = get_tuple(tablename=models.Job, user_id=user_id,
607 tuple_id=job_id, project_id=project_id)
611 values = {} 608 values = {}
612 if 1 == len(result): 609 if 1 == len(result):
613 values['job_id'] = result[0].get('id') 610 values['job_id'] = result[0].get('id')
@@ -625,16 +622,11 @@ def get_job(user_id, job_id, project_id=None):
625 622
626 623
627def search_job(user_id, project_id=None, offset=0, 624def search_job(user_id, project_id=None, offset=0,
628 limit=10, search=None): 625 limit=100, search=None):
629
630 search = search or {}
631 jobs = [] 626 jobs = []
632 session = get_db_session() 627 result = search_tuple(tablename=models.Job, user_id=user_id,
633 query = model_query(session, models.Job, project_id=project_id) 628 project_id=project_id, offset=offset,
634 query = query.filter_by(user_id=user_id) 629 limit=limit, search=search)
635
636 result = query.all()
637
638 for job in result: 630 for job in result:
639 jobmap = {} 631 jobmap = {}
640 jobmap['job_id'] = job.get('id') 632 jobmap['job_id'] = job.get('id')
@@ -650,7 +642,6 @@ def search_job(user_id, project_id=None, offset=0,
650 642
651 jobs.append(jobmap) 643 jobs.append(jobmap)
652 644
653 session.close()
654 return jobs 645 return jobs
655 646
656 647
@@ -670,26 +661,10 @@ def update_job(user_id, job_id, patch_doc, project_id=None):
670 else: 661 else:
671 values[key] = valid_patch.get(key, None) 662 values[key] = valid_patch.get(key, None)
672 663
673 session = get_db_session() 664 update_tuple(tablename=models.Job, user_id=user_id, tuple_id=job_id,
674 with session.begin(): 665 tuple_values=values, project_id=project_id)
675 try:
676 query = model_query(session, models.Job, project_id=project_id)
677 query = query.filter_by(user_id=user_id).filter_by(id=job_id)
678 result = query.update(values)
679 except Exception as e:
680 session.close()
681 raise freezer_api_exc.StorageEngineError(
682 message='mysql operation failed {0}'.format(e))
683 666
684 session.close() 667 return 0
685
686 if not result:
687 raise freezer_api_exc.DocumentNotFound(
688 message='Job not registered with ID'
689 ' {0}'.format(job_id))
690 else:
691 LOG.info('job updated, job_id: {0}'.format(job_id))
692 return 0
693 668
694 669
695def replace_job(user_id, job_id, doc, project_id=None): 670def replace_job(user_id, job_id, doc, project_id=None):
@@ -700,7 +675,6 @@ def replace_job(user_id, job_id, doc, project_id=None):
700 675
701 values = {} 676 values = {}
702 valuesnew = {} 677 valuesnew = {}
703 bCreate = False
704 678
705 values['id'] = job_id 679 values['id'] = job_id
706 values['project_id'] = project_id 680 values['project_id'] = project_id
@@ -718,51 +692,17 @@ def replace_job(user_id, job_id, doc, project_id=None):
718 if values[key] is not None: 692 if values[key] is not None:
719 valuesnew[key] = values[key] 693 valuesnew[key] = values[key]
720 694
721 session = get_db_session() 695 replace_tuple(tablename=models.Job, user_id=user_id,
722 with session.begin(): 696 tuple_id=job_id, tuple_values=valuesnew,
723 try: 697 project_id=project_id)
724 query = model_query(session, models.Job, project_id=project_id)
725 query = query.filter_by(user_id=user_id).filter_by(id=job_id)
726 result = query.update(valuesnew)
727 if not result:
728 bCreate = True
729 except Exception as e:
730 session.close()
731 raise freezer_api_exc.StorageEngineError(
732 message='mysql operation failed {0}'.format(e))
733
734 session.close()
735
736 if bCreate:
737 job = models.Job()
738 job.update(valuesnew)
739 session = get_db_session()
740 with session.begin():
741 try:
742 job.save(session=session)
743 except Exception as e:
744 session.close()
745 raise freezer_api_exc.\
746 StorageEngineError(message='mysql operation failed {0}'.
747 format(e))
748 session.close()
749
750 LOG.info('job replaced, job_id: {0}'.format(job_id)) 698 LOG.info('job replaced, job_id: {0}'.format(job_id))
751 return job_id 699 return job_id
752 700
753 701
754def get_backup(user_id, backup_id, project_id=None): 702def get_backup(user_id, backup_id, project_id=None):
755 session = get_db_session()
756 with session.begin():
757 try:
758 query = model_query(session, models.Backup, project_id=project_id)
759 query = query.filter_by(user_id=user_id).filter_by(id=backup_id)
760 result = query.all()
761 except Exception as e:
762 raise freezer_api_exc.StorageEngineError(
763 message='mysql operation failed {0}'.format(e))
764 703
765 session.close() 704 result = get_tuple(tablename=models.Backup, user_id=user_id,
705 tuple_id=backup_id, project_id=project_id)
766 values = {} 706 values = {}
767 if 1 == len(result): 707 if 1 == len(result):
768 values['project_id'] = result[0].get('project_id') 708 values['project_id'] = result[0].get('project_id')
@@ -812,56 +752,26 @@ def add_backup(user_id, user_name, doc, project_id=None):
812 backupvalue['backup_metadata'] = json_utils.json_encode(backup_metadata) 752 backupvalue['backup_metadata'] = json_utils.json_encode(backup_metadata)
813 backup.update(backupvalue) 753 backup.update(backupvalue)
814 754
815 session = get_db_session() 755 add_tuple(tuple=backup)
816 with session.begin():
817 try:
818 backup.save(session=session)
819 except Exception as e:
820 session.close()
821 raise freezer_api_exc.\
822 StorageEngineError(message='mysql operation failed {0}'.
823 format(e))
824
825 LOG.info('Backup registered, backup_id: {0}'.format(backup_id)) 756 LOG.info('Backup registered, backup_id: {0}'.format(backup_id))
826
827 session.close()
828 return backup_id 757 return backup_id
829 758
830 759
831def delete_backup(user_id, backup_id, project_id=None): 760def delete_backup(user_id, backup_id, project_id=None):
832 session = get_db_session()
833 query = model_query(session, models.Backup, project_id=project_id)
834 query = query.filter_by(user_id=user_id).filter_by(id=backup_id)
835
836 result = query.all()
837 if 1 == len(result):
838 try:
839 result[0].delete(session=session)
840 except Exception as e:
841 session.close()
842 raise freezer_api_exc.StorageEngineError(
843 message='mysql operation failed {0}'.format(e))
844 LOG.info('Backup delete, backup_id: {0} deleted'.
845 format(backup_id))
846 else:
847 LOG.info('Backup delete, backup_id: {0} not found'.
848 format(backup_id))
849 761
850 session.close() 762 tupleid = delete_tuple(tablename=models.Backup, user_id=user_id,
851 return backup_id 763 tuple_id=backup_id, project_id=project_id)
764 return tupleid
852 765
853 766
854def search_backup(user_id, project_id=None, offset=0, 767def search_backup(user_id, project_id=None, offset=0,
855 limit=10, search=None): 768 limit=100, search=None):
856 769
857 search = search or {}
858 backups = [] 770 backups = []
859 session = get_db_session()
860 query = model_query(session, models.Backup, project_id=project_id)
861 query = query.filter_by(user_id=user_id)
862
863 result = query.all()
864 771
772 result = search_tuple(tablename=models.Backup, user_id=user_id,
773 project_id=project_id, offset=offset,
774 limit=limit, search=search)
865 for backup in result: 775 for backup in result:
866 backupmap = {} 776 backupmap = {}
867 backupmap['project_id'] = project_id 777 backupmap['project_id'] = project_id
@@ -874,24 +784,14 @@ def search_backup(user_id, project_id=None, offset=0,
874 json_decode(backup.get('backup_metadata')) 784 json_decode(backup.get('backup_metadata'))
875 backups.append(backupmap) 785 backups.append(backupmap)
876 786
877 session.close()
878 return backups 787 return backups
879 788
880 789
881def get_session(user_id, session_id, project_id=None): 790def get_session(user_id, session_id, project_id=None):
882 jobt = {} 791 jobt = {}
883 session = get_db_session()
884 with session.begin():
885 try:
886 query = model_query(session, models.Session, project_id=project_id)
887 query = query.filter_by(user_id=user_id).filter_by(id=session_id)
888 result = query.all()
889 except Exception as e:
890 raise freezer_api_exc.StorageEngineError(
891 message='mysql operation failed {0}'.format(e))
892
893 session.close()
894 values = {} 792 values = {}
793 result = get_tuple(tablename=models.Session, user_id=user_id,
794 tuple_id=session_id, project_id=project_id)
895 if 1 == len(result): 795 if 1 == len(result):
896 values['project_id'] = result[0].get('project_id') 796 values['project_id'] = result[0].get('project_id')
897 values['session_id'] = result[0].get('id') 797 values['session_id'] = result[0].get('id')
@@ -916,26 +816,10 @@ def get_session(user_id, session_id, project_id=None):
916 816
917 817
918def delete_session(user_id, session_id, project_id=None): 818def delete_session(user_id, session_id, project_id=None):
919 session = get_db_session()
920 query = model_query(session, models.Session, project_id=project_id)
921 query = query.filter_by(user_id=user_id).filter_by(id=session_id)
922 819
923 result = query.all() 820 tupleid = delete_tuple(tablename=models.Session, user_id=user_id,
924 if 1 == len(result): 821 tuple_id=session_id, project_id=project_id)
925 try: 822 return tupleid
926 result[0].delete(session=session)
927 except Exception as e:
928 session.close()
929 raise freezer_api_exc.StorageEngineError(
930 message='mysql operation failed {0}'.format(e))
931 LOG.info('Session delete, session_id: {0} deleted'.
932 format(session_id))
933 else:
934 LOG.info('Session delete, session_id: {0} not found'.
935 format(session_id))
936
937 session.close()
938 return session_id
939 823
940 824
941def add_session(user_id, doc, project_id=None): 825def add_session(user_id, doc, project_id=None):
@@ -974,19 +858,9 @@ def add_session(user_id, doc, project_id=None):
974 sessionvalue['schedule'] = json_utils.json_encode(schedulingjson) 858 sessionvalue['schedule'] = json_utils.json_encode(schedulingjson)
975 sessiont.update(sessionvalue) 859 sessiont.update(sessionvalue)
976 860
977 session = get_db_session() 861 add_tuple(tuple=sessiont)
978 with session.begin():
979 try:
980 sessiont.save(session=session)
981 except Exception as e:
982 session.close()
983 raise freezer_api_exc.\
984 StorageEngineError(message='mysql operation failed {0}'.
985 format(e))
986
987 LOG.info('Session registered, session_id: {0}'.format(session_id)) 862 LOG.info('Session registered, session_id: {0}'.format(session_id))
988 863
989 session.close()
990 return session_id 864 return session_id
991 865
992 866
@@ -1017,26 +891,11 @@ def update_session(user_id, session_id, patch_doc, project_id=None):
1017 else: 891 else:
1018 values[key] = valid_patch.get(key, None) 892 values[key] = valid_patch.get(key, None)
1019 893
1020 session = get_db_session() 894 update_tuple(tablename=models.Session, user_id=user_id,
1021 with session.begin(): 895 tuple_id=session_id, tuple_values=values,
1022 try: 896 project_id=project_id)
1023 query = model_query(session, models.Session, project_id=project_id)
1024 query = query.filter_by(user_id=user_id).filter_by(id=session_id)
1025 result = query.update(values)
1026 except Exception as e:
1027 session.close()
1028 raise freezer_api_exc.StorageEngineError(
1029 message='mysql operation failed {0}'.format(e))
1030 897
1031 session.close() 898 return 0
1032
1033 if not result:
1034 raise freezer_api_exc.DocumentNotFound(
1035 message='session not registered with ID'
1036 ' {0}'.format(session_id))
1037 else:
1038 LOG.info('session updated, session_id: {0}'.format(session_id))
1039 return 0
1040 899
1041 900
1042def replace_session(user_id, session_id, doc, project_id=None): 901def replace_session(user_id, session_id, doc, project_id=None):
@@ -1048,7 +907,6 @@ def replace_session(user_id, session_id, doc, project_id=None):
1048 project_id) 907 project_id)
1049 values = {} 908 values = {}
1050 valuesnew = {} 909 valuesnew = {}
1051 bCreate = False
1052 910
1053 values['id'] = session_id 911 values['id'] = session_id
1054 values['project_id'] = project_id 912 values['project_id'] = project_id
@@ -1070,51 +928,22 @@ def replace_session(user_id, session_id, doc, project_id=None):
1070 if values[key] is not None: 928 if values[key] is not None:
1071 valuesnew[key] = values[key] 929 valuesnew[key] = values[key]
1072 930
1073 session = get_db_session() 931 replace_tuple(tablename=models.Session, user_id=user_id,
1074 with session.begin(): 932 tuple_id=session_id,
1075 try: 933 tuple_values=valuesnew,
1076 query = model_query(session, models.Session, project_id=project_id) 934 project_id=project_id)
1077 query = query.filter_by(user_id=user_id).filter_by(id=session_id)
1078 result = query.update(valuesnew)
1079 if not result:
1080 bCreate = True
1081 except Exception as e:
1082 session.close()
1083 raise freezer_api_exc.StorageEngineError(
1084 message='mysql operation failed {0}'.format(e))
1085
1086 session.close()
1087
1088 if bCreate:
1089 sessiont = models.Session()
1090 sessiont.update(valuesnew)
1091 session = get_db_session()
1092 with session.begin():
1093 try:
1094 sessiont.save(session=session)
1095 except Exception as e:
1096 session.close()
1097 raise freezer_api_exc.\
1098 StorageEngineError(message='mysql operation failed {0}'.
1099 format(e))
1100 session.close()
1101
1102 LOG.info('session replaced, session_id: {0}'.format(session_id)) 935 LOG.info('session replaced, session_id: {0}'.format(session_id))
1103 return session_id 936 return session_id
1104 937
1105 938
1106def search_session(user_id, project_id=None, offset=0, 939def search_session(user_id, project_id=None, offset=0,
1107 limit=10, search=None): 940 limit=100, search=None):
1108 search = search or {}
1109 sessions = [] 941 sessions = []
1110 jobt = {} 942 jobt = {}
1111 943
1112 session = get_db_session() 944 result = search_tuple(tablename=models.Session, user_id=user_id,
1113 query = model_query(session, models.Session, project_id=project_id) 945 project_id=project_id, offset=offset,
1114 query = query.filter_by(user_id=user_id) 946 limit=limit, search=search)
1115
1116 result = query.all()
1117
1118 for sessiont in result: 947 for sessiont in result:
1119 sessionmap = {} 948 sessionmap = {}
1120 sessionmap['project_id'] = project_id 949 sessionmap['project_id'] = project_id
@@ -1136,5 +965,4 @@ def search_session(user_id, project_id=None, offset=0,
1136 sessionmap['jobs'] = json_utils.json_decode(sessiont.get('job')) 965 sessionmap['jobs'] = json_utils.json_decode(sessiont.get('job'))
1137 sessions.append(sessionmap) 966 sessions.append(sessionmap)
1138 967
1139 session.close()
1140 return sessions 968 return sessions