Browse Source

Merge "Refactor sql driver api"

tags/7.0.0
Zuul 6 months ago
parent
commit
40fe31c931
1 changed files with 221 additions and 393 deletions
  1. 221
    393
      freezer_api/db/sqlalchemy/api.py

+ 221
- 393
freezer_api/db/sqlalchemy/api.py View File

@@ -142,20 +142,157 @@ def model_query(session, model,
142 142
     return query
143 143
 
144 144
 
145
-def get_client(user_id, project_id=None, client_id=None, offset=0,
146
-               limit=10, search=None):
145
+def delete_tuple(tablename, user_id, tuple_id, project_id=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 {}
149
-    clients = []
168
+
169
+def get_tuple(tablename, user_id, tuple_id, project_id=None):
150 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
+
183
+def 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
+
196
+def 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 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()
221
+def 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
+
257
+def 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
+
275
+def 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 297
     for client in result:
161 298
         clientmap = {}
@@ -167,7 +304,6 @@ def get_client(user_id, project_id=None, client_id=None, offset=0,
167 304
                                 u'description': client.description}
168 305
         clients.append(clientmap)
169 306
 
170
-    session.close()
171 307
     return clients
172 308
 
173 309
 
@@ -197,80 +333,38 @@ def add_client(user_id, doc, project_id=None):
197 333
     values['description'] = client_json.get('description', None)
198 334
     client.update(values)
199 335
 
200
-    session = get_db_session()
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))
336
+    add_tuple(tuple=client)
208 337
 
209 338
     LOG.info('Client registered, client_id: {0}'.format(client_id))
210
-    session.close()
211 339
     return client_id
212 340
 
213 341
 
214 342
 def delete_client(user_id, client_id, project_id=None):
215
-    session = get_db_session()
216
-    query = model_query(session, models.Client, project_id=project_id)
217
-    query = query.filter_by(user_id=user_id).filter_by(client_id=client_id)
218
-    result = query.all()
219
-    if 1 == len(result):
220
-        try:
221
-            result[0].delete(session=session)
222
-        except Exception as e:
223
-            session.close()
224
-            raise freezer_api_exc.StorageEngineError(
225
-                message='mysql operation failed {0}'.format(e))
343
+
344
+    existing = get_client(project_id=project_id, user_id=user_id,
345
+                          client_id=client_id)
346
+
347
+    if existing:
348
+        clientt = existing[0].get('client')
349
+        clientid = clientt.get('uuid')
350
+        delete_tuple(tablename=models.Client, user_id=user_id,
351
+                     tuple_id=clientid, project_id=project_id)
226 352
         LOG.info('Client delete, client_id: {0} deleted'.
227 353
                  format(client_id))
228 354
     else:
229 355
         LOG.info('Client delete, client_id: {0} not found'.
230 356
                  format(client_id))
231
-
232
-    session.close()
233 357
     return client_id
234 358
 
235 359
 
236 360
 def delete_action(user_id, action_id, project_id=None):
237 361
 
238
-    session = get_db_session()
239
-    query = model_query(session, models.Action, project_id=project_id)
240
-    query = query.filter_by(user_id=user_id).filter_by(id=action_id)
362
+    tupleid = delete_tuple(tablename=models.Action, user_id=user_id,
363
+                           tuple_id=action_id, project_id=project_id)
241 364
 
242
-    result = query.all()
243
-    if 1 == len(result):
244
-        try:
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
365
+    tupleid = delete_tuple(tablename=models.ActionReport, user_id=user_id,
366
+                           tuple_id=action_id, project_id=project_id)
367
+    return tupleid
274 368
 
275 369
 
276 370
 def add_action(user_id, doc, project_id=None):
@@ -316,14 +410,7 @@ def add_action(user_id, doc, project_id=None):
316 410
 
317 411
     action.update(actionvalue)
318 412
 
319
-    session = get_db_session()
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))
413
+    add_tuple(tuple=action)
327 414
 
328 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 423
     actionReport.update(actionreportvalue)
337 424
 
338
-    with session.begin():
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
-
425
+    add_tuple(tuple=actionReport)
346 426
     LOG.info('Action Reports registered, action_id: {0}'.
347 427
              format(action_id))
348
-    session.close()
349 428
     return action_id
350 429
 
351 430
 
352 431
 def 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 436
     values = {}
365 437
     if 1 == len(result):
366 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
 
387 459
 def 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 462
     actions = []
392 463
 
393
-    session = get_db_session()
394
-    query = model_query(session, models.Action, project_id=project_id)
395
-    query = query.filter_by(user_id=user_id)
396
-
397
-    result = query.all()
398
-
464
+    result = search_tuple(tablename=models.Action, user_id=user_id,
465
+                          project_id=project_id, offset=offset,
466
+                          limit=limit, search=search)
399 467
     for action in result:
400 468
         actionmap = {}
401 469
         actionmap['project_id'] = project_id
@@ -421,7 +489,6 @@ def search_action(user_id, project_id=None, offset=0,
421 489
 
422 490
         actions.append(actionmap)
423 491
 
424
-    session.close()
425 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 521
     values['backup_metadata'] = json_utils.json_encode(freezer_action)
455 522
 
456
-    session = get_db_session()
457
-    with session.begin():
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()
523
+    update_tuple(tablename=models.Action, user_id=user_id, tuple_id=action_id,
524
+                 tuple_values=values, project_id=project_id)
468 525
 
469
-    if not result:
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
526
+    return action_id
476 527
 
477 528
 
478 529
 def 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 535
     values = {}
485 536
     keyt = ['action', 'mode', 'backup_name', 'container',
486 537
             'src_file', 'timeout', 'priority', 'mandatory', 'log_file']
487
-    bCreate = False
488 538
 
489 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 550
         if key in keyt:
501 551
             values[key] = freezer_action.get(key)
502 552
 
503
-    session = get_db_session()
504
-    with session.begin():
505
-        try:
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()
553
+    replace_tuple(tablename=models.Action, user_id=user_id,
554
+                  tuple_id=action_id, tuple_values=values,
555
+                  project_id=project_id)
531 556
 
532 557
     LOG.info('action replaced, action_id: {0}'.format(action_id))
533 558
     return action_id
534 559
 
535 560
 
536 561
 def delete_job(user_id, job_id, project_id=None):
537
-    session = get_db_session()
538
-    query = model_query(session, models.Job, project_id=project_id)
539
-    query = query.filter_by(user_id=user_id).filter_by(id=job_id)
540
-    result = query.all()
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
562
+
563
+    tupleid = delete_tuple(tablename=models.Job, user_id=user_id,
564
+                           tuple_id=job_id, project_id=project_id)
565
+    return tupleid
554 566
 
555 567
 
556 568
 def add_job(user_id, doc, project_id=None):
@@ -582,14 +594,7 @@ def add_job(user_id, doc, project_id=None):
582 594
         json_encode(job_doc.pop('job_actions', ''))
583 595
     job.update(jobvalue)
584 596
 
585
-    session = get_db_session()
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))
597
+    add_tuple(tuple=job)
593 598
 
594 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
 
599 604
 def 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 608
     values = {}
612 609
     if 1 == len(result):
613 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
 
627 624
 def search_job(user_id, project_id=None, offset=0,
628
-               limit=10, search=None):
629
-
630
-    search = search or {}
625
+               limit=100, search=None):
631 626
     jobs = []
632
-    session = get_db_session()
633
-    query = model_query(session, models.Job, project_id=project_id)
634
-    query = query.filter_by(user_id=user_id)
635
-
636
-    result = query.all()
637
-
627
+    result = search_tuple(tablename=models.Job, user_id=user_id,
628
+                          project_id=project_id, offset=offset,
629
+                          limit=limit, search=search)
638 630
     for job in result:
639 631
         jobmap = {}
640 632
         jobmap['job_id'] = job.get('id')
@@ -650,7 +642,6 @@ def search_job(user_id, project_id=None, offset=0,
650 642
 
651 643
         jobs.append(jobmap)
652 644
 
653
-    session.close()
654 645
     return jobs
655 646
 
656 647
 
@@ -670,26 +661,10 @@ def update_job(user_id, job_id, patch_doc, project_id=None):
670 661
         else:
671 662
             values[key] = valid_patch.get(key, None)
672 663
 
673
-    session = get_db_session()
674
-    with session.begin():
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))
664
+    update_tuple(tablename=models.Job, user_id=user_id, tuple_id=job_id,
665
+                 tuple_values=values, project_id=project_id)
683 666
 
684
-    session.close()
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
667
+    return 0
693 668
 
694 669
 
695 670
 def 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 676
     values = {}
702 677
     valuesnew = {}
703
-    bCreate = False
704 678
 
705 679
     values['id'] = job_id
706 680
     values['project_id'] = project_id
@@ -718,51 +692,17 @@ def replace_job(user_id, job_id, doc, project_id=None):
718 692
         if values[key] is not None:
719 693
             valuesnew[key] = values[key]
720 694
 
721
-    session = get_db_session()
722
-    with session.begin():
723
-        try:
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
-
695
+    replace_tuple(tablename=models.Job, user_id=user_id,
696
+                  tuple_id=job_id, tuple_values=valuesnew,
697
+                  project_id=project_id)
750 698
     LOG.info('job replaced, job_id: {0}'.format(job_id))
751 699
     return job_id
752 700
 
753 701
 
754 702
 def 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 706
     values = {}
767 707
     if 1 == len(result):
768 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 752
     backupvalue['backup_metadata'] = json_utils.json_encode(backup_metadata)
813 753
     backup.update(backupvalue)
814 754
 
815
-    session = get_db_session()
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
-
755
+    add_tuple(tuple=backup)
825 756
     LOG.info('Backup registered, backup_id: {0}'.format(backup_id))
826
-
827
-    session.close()
828 757
     return backup_id
829 758
 
830 759
 
831 760
 def 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()
851
-    return backup_id
762
+    tupleid = delete_tuple(tablename=models.Backup, user_id=user_id,
763
+                           tuple_id=backup_id, project_id=project_id)
764
+    return tupleid
852 765
 
853 766
 
854 767
 def 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 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 775
     for backup in result:
866 776
         backupmap = {}
867 777
         backupmap['project_id'] = project_id
@@ -874,24 +784,14 @@ def search_backup(user_id, project_id=None, offset=0,
874 784
             json_decode(backup.get('backup_metadata'))
875 785
         backups.append(backupmap)
876 786
 
877
-    session.close()
878 787
     return backups
879 788
 
880 789
 
881 790
 def get_session(user_id, session_id, project_id=None):
882 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 792
     values = {}
793
+    result = get_tuple(tablename=models.Session, user_id=user_id,
794
+                       tuple_id=session_id, project_id=project_id)
895 795
     if 1 == len(result):
896 796
         values['project_id'] = result[0].get('project_id')
897 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
 
918 818
 def 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()
924
-    if 1 == len(result):
925
-        try:
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
820
+    tupleid = delete_tuple(tablename=models.Session, user_id=user_id,
821
+                           tuple_id=session_id, project_id=project_id)
822
+    return tupleid
939 823
 
940 824
 
941 825
 def add_session(user_id, doc, project_id=None):
@@ -974,19 +858,9 @@ def add_session(user_id, doc, project_id=None):
974 858
     sessionvalue['schedule'] = json_utils.json_encode(schedulingjson)
975 859
     sessiont.update(sessionvalue)
976 860
 
977
-    session = get_db_session()
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
-
861
+    add_tuple(tuple=sessiont)
987 862
     LOG.info('Session registered, session_id: {0}'.format(session_id))
988 863
 
989
-    session.close()
990 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 891
         else:
1018 892
             values[key] = valid_patch.get(key, None)
1019 893
 
1020
-    session = get_db_session()
1021
-    with session.begin():
1022
-        try:
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))
894
+    update_tuple(tablename=models.Session, user_id=user_id,
895
+                 tuple_id=session_id, tuple_values=values,
896
+                 project_id=project_id)
1030 897
 
1031
-    session.close()
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
898
+    return 0
1040 899
 
1041 900
 
1042 901
 def 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 907
                                               project_id)
1049 908
     values = {}
1050 909
     valuesnew = {}
1051
-    bCreate = False
1052 910
 
1053 911
     values['id'] = session_id
1054 912
     values['project_id'] = project_id
@@ -1070,51 +928,22 @@ def replace_session(user_id, session_id, doc, project_id=None):
1070 928
         if values[key] is not None:
1071 929
             valuesnew[key] = values[key]
1072 930
 
1073
-    session = get_db_session()
1074
-    with session.begin():
1075
-        try:
1076
-            query = model_query(session, models.Session, 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
-
931
+    replace_tuple(tablename=models.Session, user_id=user_id,
932
+                  tuple_id=session_id,
933
+                  tuple_values=valuesnew,
934
+                  project_id=project_id)
1102 935
     LOG.info('session replaced, session_id: {0}'.format(session_id))
1103 936
     return session_id
1104 937
 
1105 938
 
1106 939
 def search_session(user_id, project_id=None, offset=0,
1107
-                   limit=10, search=None):
1108
-    search = search or {}
940
+                   limit=100, search=None):
1109 941
     sessions = []
1110 942
     jobt = {}
1111 943
 
1112
-    session = get_db_session()
1113
-    query = model_query(session, models.Session, project_id=project_id)
1114
-    query = query.filter_by(user_id=user_id)
1115
-
1116
-    result = query.all()
1117
-
944
+    result = search_tuple(tablename=models.Session, user_id=user_id,
945
+                          project_id=project_id, offset=offset,
946
+                          limit=limit, search=search)
1118 947
     for sessiont in result:
1119 948
         sessionmap = {}
1120 949
         sessionmap['project_id'] = project_id
@@ -1136,5 +965,4 @@ def search_session(user_id, project_id=None, offset=0,
1136 965
             sessionmap['jobs'] = json_utils.json_decode(sessiont.get('job'))
1137 966
         sessions.append(sessionmap)
1138 967
 
1139
-    session.close()
1140 968
     return sessions

Loading…
Cancel
Save