ucampostgresvro 0.1.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
ucampostgresvro/DBA.py ADDED
@@ -0,0 +1,792 @@
1
+ import logging
2
+ from typing import Optional
3
+
4
+ import psycopg2
5
+
6
+ from ucampostgresvro.exceptions import DbException
7
+ from ucampostgresvro.tools import DEFAULT_TABLES
8
+
9
+ LOG = logging.getLogger(__name__)
10
+
11
+
12
+ class DB:
13
+ def __init__(self, config: dict[str:str]) -> None:
14
+ db_params = config
15
+ self.connection = psycopg2.connect(**db_params)
16
+ self.cursor = self.connection.cursor()
17
+
18
+ def db_connection(self):
19
+ """Provied the connection details of DB
20
+
21
+ Returns:
22
+ object: connection informtion of the DB
23
+ """
24
+ return self.connection
25
+
26
+ def db_cursor(self):
27
+ """Provied the cursor details of DB
28
+
29
+ Returns:
30
+ object: cursor informtion of the DB
31
+ """
32
+ return self.cursor
33
+
34
+ def insert_vrauser(
35
+ self, crsid: str, name: str, table_name: str = DEFAULT_TABLES.get("user")
36
+ ) -> bool:
37
+ """Insertion of the vrauser detail.
38
+
39
+ Args:
40
+ crsid (str): crsid of the user.
41
+ name (str): name of the user.
42
+ table_name (str): table name of the user.
43
+
44
+ Raises:
45
+ DbException: Exception for the provided inputs.
46
+
47
+ Returns:
48
+ bool: True for the success and False for the failure.
49
+ """
50
+ with self.connection:
51
+ if crsid and name:
52
+ try:
53
+ self.cursor.execute(
54
+ f"INSERT INTO {table_name} (crsid, name) VALUES ('{crsid}', '{name}');"
55
+ )
56
+ LOG.info(
57
+ f"INFO: {table_name} insersion successful: CRSID {crsid} and Name {name}"
58
+ )
59
+ return True
60
+ except Exception as e:
61
+ LOG.error(f"Error: {table_name} insertion : {e}")
62
+ return False
63
+ else:
64
+ LOG.error(
65
+ f"Error: Please provide both crid and name for {table_name} insertion"
66
+ )
67
+ raise DbException(f"Error: {table_name} insertion fail")
68
+
69
+ def update_vrauser(
70
+ self,
71
+ old_crsid: str,
72
+ new_crsid: str,
73
+ name: str,
74
+ table_name: str = DEFAULT_TABLES.get("user"),
75
+ ) -> bool:
76
+ """Updation of the vrauser.
77
+
78
+ Args:
79
+ old_crsid (str): CRSID which need to be updated.
80
+ new_crsid (str): New CRSID which replaces the old CRSID.
81
+ name (str): Name of the user
82
+ table_name (str): table name of the user.
83
+
84
+ Returns:
85
+ bool: True for the success and False for the failure.
86
+ """
87
+ with self.connection:
88
+ try:
89
+ self.cursor.execute(
90
+ f"UPDATE {table_name} SET crsid ='{new_crsid}' , name='{name}' WHERE crsid='{old_crsid}';"
91
+ )
92
+ LOG.info(f"INFO: {table_name} update successful for CRSID {old_crsid}")
93
+ return True
94
+ except Exception as e:
95
+ LOG.error(f"Error: {table_name} Updating : {e}")
96
+ return False
97
+
98
+ def remove_vrauser(
99
+ self, crsid: str, table_name: str = DEFAULT_TABLES.get("user")
100
+ ) -> bool:
101
+ """Removal of the vrauser.
102
+
103
+ Args:
104
+ crsid (str): CRSID need to be removed of the vrauser.
105
+ table_name (str): table name of the user.
106
+
107
+ Returns:
108
+ bool: True for the success and False for the failure.
109
+ """
110
+ with self.connection:
111
+ try:
112
+ self.cursor.execute(f"DELETE from {table_name} WHERE crsid='{crsid}';")
113
+ LOG.info(f"INFO: {table_name} removed CRSID {crsid} successfully.")
114
+ return True
115
+ except Exception as e:
116
+ LOG.error(f"Error: {table_name} removing : {e}")
117
+ return False
118
+
119
+ def get_vrauser(
120
+ self,
121
+ crsid: Optional[str] = None,
122
+ name: Optional[str] = None,
123
+ table_name: str = DEFAULT_TABLES.get("user"),
124
+ ) -> any:
125
+ """Retreive the information from the vrauser table.
126
+
127
+ Args:
128
+ crsid (Optional[str], optional): CRSID need to be fetched. Defaults to None.
129
+ name (Optional[str], optional): Name of the user to fetcb. Defaults to None.
130
+ table_name (str): table name of the user.
131
+
132
+ Returns:
133
+ any: retreive the data from the vrauser database.
134
+ """
135
+ with self.connection:
136
+ if crsid:
137
+ self.cursor.execute(
138
+ f"SELECT * FROM {table_name} where crsid = '{crsid}';"
139
+ )
140
+ elif name:
141
+ self.cursor.execute(
142
+ f"SELECT * FROM {table_name} where name = '{name}';"
143
+ )
144
+ elif crsid and name:
145
+ self.cursor.execute(
146
+ f"SELECT * FROM {table_name} where crsid = '{crsid}' and name = '{name}';"
147
+ )
148
+ else:
149
+ self.cursor.execute(f"SELECT * FROM {table_name};")
150
+ LOG.info(f"INFO: {table_name} information is fetched successfully")
151
+ return self.cursor.fetchall()
152
+
153
+ def insert_deployment_id(
154
+ self, deployment_id: str, table_name: str = DEFAULT_TABLES.get("deploymentid")
155
+ ) -> bool:
156
+ """Insertion of the deployment detail.
157
+
158
+ Args:
159
+ deployment_id (str): deployment ID information.
160
+ table_name (str): table name of the deploymentid.
161
+
162
+ Raises:
163
+ DbException: Exception for the provided inputs.
164
+
165
+ Returns:
166
+ bool: True for the success and False for the failure.
167
+ """
168
+ with self.connection:
169
+ if deployment_id:
170
+ try:
171
+ self.cursor.execute(
172
+ f"INSERT INTO {table_name} (deploymentID) VALUES ('{deployment_id}');"
173
+ )
174
+ LOG.info(
175
+ f"INFO: deployment ID {deployment_id} inserted successfully"
176
+ )
177
+ return True
178
+ except Exception as e:
179
+ LOG.error(f"Error: deployment ID insertion in {table_name}: {e}")
180
+ return False
181
+ else:
182
+ LOG.error(
183
+ f"Error: Please provide deployment ID for {table_name} insertion"
184
+ )
185
+ raise DbException(
186
+ f"Error: Please provide deployment ID for {table_name} insertion"
187
+ )
188
+
189
+ def update_deployment_id(
190
+ self,
191
+ old_deployment_id: str,
192
+ new_deployment_id: str,
193
+ table_name: str = DEFAULT_TABLES.get("deploymentid"),
194
+ ) -> bool:
195
+ """Updation of the the deployment ID in deployment table.
196
+
197
+ Args:
198
+ old_deployment_id (str): Deployment ID which need to be updated.
199
+ new_deployment_id (str): New Deployment ID which replaces the old Deployment ID.
200
+ table_name (str): table name of the deploymentid.
201
+
202
+ Returns:
203
+ bool: True for the success and False for the failure.
204
+ """
205
+ with self.connection:
206
+ try:
207
+ self.cursor.execute(
208
+ f"UPDATE {table_name} SET deploymentID ='{new_deployment_id}' \
209
+ WHERE deploymentID='{old_deployment_id}';"
210
+ )
211
+ LOG.info(
212
+ f"INFO: deployment ID of {old_deployment_id} updated successfully with \
213
+ {new_deployment_id} in table {table_name} ."
214
+ )
215
+ return True
216
+ except Exception as e:
217
+ LOG.error(f"Error: deployment ID update for table {table_name}: {e}")
218
+ return False
219
+
220
+ def remove_deployment_id(
221
+ self, deployment_id: str, table_name: str = DEFAULT_TABLES.get("deploymentid")
222
+ ) -> bool:
223
+ """Removal of the deployment ID.
224
+
225
+ Args:
226
+ deployment_id (str): Deployment ID need to be removed from the Deployment table.
227
+ table_name (str): table name of the deploymentid.
228
+
229
+ Returns:
230
+ bool: True for the success and False for the failure.
231
+ """
232
+ with self.connection:
233
+ try:
234
+ self.cursor.execute(
235
+ f"DELETE from {table_name} WHERE deploymentID='{deployment_id}';"
236
+ )
237
+ LOG.info(
238
+ f"INFO: Removal of the deployment ID ' {deployment_id} ' has been performed successfully"
239
+ )
240
+ return True
241
+ except Exception as e:
242
+ LOG.error(
243
+ f"Error: deployment ID removing from table '{table_name}': {e}"
244
+ )
245
+ return False
246
+
247
+ def get_deployment_id(
248
+ self,
249
+ deployment_id: Optional[str] = None,
250
+ table_name: str = DEFAULT_TABLES.get("deploymentid"),
251
+ ) -> any:
252
+ """Retreive the information from the deployment table.
253
+
254
+ Args:
255
+ deployment_id (Optional[str], optional): Deployment ID need to be fetched. Defaults to None.
256
+ table_name (str): table name of the deploymentid.
257
+
258
+ Returns:
259
+ any: Retreive the data from the Deployment ID database.
260
+ """
261
+ with self.connection:
262
+ if deployment_id:
263
+ self.cursor.execute(
264
+ f"SELECT * FROM {table_name} where deploymentID = '{deployment_id}';"
265
+ )
266
+ else:
267
+ self.cursor.execute(f"SELECT * FROM {table_name};")
268
+ LOG.info("INFO: deployment ID information is fetched successfully")
269
+ return self.cursor.fetchall()
270
+
271
+ def insert_project(
272
+ self,
273
+ project_number: str,
274
+ paid_by: int,
275
+ amount: float,
276
+ table_name: str = DEFAULT_TABLES.get("proj"),
277
+ ) -> bool:
278
+ """Insertion of the project table.
279
+
280
+ Args:
281
+ project_number (str): payment order information.
282
+ paid_by (int): primary key of the vrauser.
283
+ amount (float): amount paid for the purchase.
284
+ table_name (str): table name of the purchaseOrder.
285
+
286
+ Raises:
287
+ DbException: Exception for the provided inputs.
288
+
289
+ Returns:
290
+ bool: True for the success and False for the failure.
291
+ """
292
+ with self.connection:
293
+ if project_number:
294
+ try:
295
+ self.cursor.execute(
296
+ f"INSERT INTO {table_name} (project_number, paid_by, amount) VALUES \
297
+ ('{project_number}', '{paid_by}', '{amount}');"
298
+ )
299
+ LOG.info(
300
+ f"INFO: Insertion of {project_number} and {amount} by {paid_by} is performed successfully"
301
+ )
302
+ return True
303
+ except Exception as e:
304
+ LOG.error(
305
+ f"Error: project insertion in a table '{table_name}':\n {e}"
306
+ )
307
+ return False
308
+ else:
309
+ LOG.error(
310
+ "Error: Please provide project_number, paid_by, amount for Payment Oder"
311
+ )
312
+ raise DbException(
313
+ f"Error: project insertion fail in table {table_name}"
314
+ )
315
+
316
+ def update_project(
317
+ self,
318
+ old_project_number: str,
319
+ new_project_number: str,
320
+ new_paid_by: int,
321
+ new_amount: float,
322
+ table_name: str = DEFAULT_TABLES.get("proj"),
323
+ ) -> bool:
324
+ """Updation of the the project detail in project table
325
+
326
+ Args:
327
+ old_project_number (str): old payment order information.
328
+ new_project_number (str): new payment order information to replace old payment order.
329
+ new_paid_by (int): new primary key of the vrauser.
330
+ new_amount (float): new amount paid for the purchase.
331
+ table_name (str): table name of the purchaseOrder.
332
+
333
+ Returns:
334
+ bool: True for the success and False for the failure.
335
+ """
336
+ with self.connection:
337
+ try:
338
+ self.cursor.execute(
339
+ f"UPDATE {table_name} SET \
340
+ project_number ='{new_project_number}', paid_by='{new_paid_by}', amount='{new_amount}' \
341
+ WHERE project_number='{old_project_number}';"
342
+ )
343
+ LOG.info(
344
+ f"INFO: Updation of the project {old_project_number} has been peformed successfully"
345
+ )
346
+ return True
347
+ except Exception as e:
348
+ LOG.error(f"Error: purchaseOrder Updating in table {table_name} : {e}")
349
+ return False
350
+
351
+ def remove_project(
352
+ self, project_number: str, table_name: str = DEFAULT_TABLES.get("proj")
353
+ ) -> bool:
354
+ """Removal of the project.
355
+
356
+ Args:
357
+ project_number (str): project which need to be removed.
358
+ table_name (str): table name of the purchaseOrder.
359
+
360
+ Returns:
361
+ bool: True for the success and False for the failure.
362
+ """
363
+ with self.connection:
364
+ try:
365
+ self.cursor.execute(
366
+ f"DELETE from {table_name} WHERE project_number='{project_number}';"
367
+ )
368
+ LOG.info(
369
+ f"INFO: Removing of the project '{project_number}' has been performed successfully."
370
+ )
371
+ return True
372
+ except Exception as e:
373
+ LOG.error(
374
+ f"Error: purchaseOrder removing from table '{table_name}': {e}"
375
+ )
376
+ raise DbException(
377
+ f"Error: purchaseOrder removing from table '{table_name}': {e}"
378
+ )
379
+
380
+ def get_project(
381
+ self,
382
+ project_number: Optional[str] = None,
383
+ table_name: str = DEFAULT_TABLES.get("proj"),
384
+ ) -> any:
385
+ """Retreive the information from the project table.
386
+
387
+ Args:
388
+ project_number (Optional[str], optional): project which need to be fetched. Defaults to None.
389
+ table_name (str): table name of the purchaseOrder.
390
+
391
+ Returns:
392
+ any: Retreive the data from the project database.
393
+ """
394
+ with self.connection:
395
+ if project_number:
396
+ self.cursor.execute(
397
+ f"SELECT * FROM {table_name} where project_number = '{project_number}';"
398
+ )
399
+ else:
400
+ self.cursor.execute(f"SELECT * FROM {table_name};")
401
+ LOG.info("INFO: project information has been fetched successfully.")
402
+ return self.cursor.fetchall()
403
+
404
+ def insert_grant(
405
+ self,
406
+ grant_number: str,
407
+ paid_by: int,
408
+ amount: float,
409
+ table_name: str = DEFAULT_TABLES.get("grant"),
410
+ ) -> bool:
411
+ """Insertion of the grant detail.
412
+
413
+ Args:
414
+ grant_number (str): grant information.
415
+ paid_by (int): primary key of the vrauser.
416
+ amount (float): amount paid for the purchase.
417
+ table_name (str): table name of the grant.
418
+
419
+
420
+ Raises:
421
+ DbException: Exception for the provided inputs.
422
+
423
+ Returns:
424
+ bool: True for the success and False for the failure.
425
+ """
426
+ with self.connection:
427
+ if grant_number:
428
+ try:
429
+ self.cursor.execute(
430
+ f"INSERT INTO {table_name} (grant_number, paid_by, amount) \
431
+ VALUES ('{grant_number}', '{paid_by}', '{amount}');"
432
+ )
433
+ LOG.info(
434
+ f"INFO: Insertion of the grant {grant_number} and {amount} by \
435
+ {paid_by} has been performed successfully."
436
+ )
437
+ return True
438
+ except Exception as e:
439
+ LOG.error(f"Error: grant Insert in table '{table_name}': {e}")
440
+ return False
441
+ else:
442
+ LOG.error(
443
+ "Error: Please provide grant_number, paid_by, amount for grants"
444
+ )
445
+ raise DbException(f"Error: grants insertion fail in table {table_name}")
446
+
447
+ def update_grant(
448
+ self,
449
+ old_grant: str,
450
+ new_grant: str,
451
+ new_paid_by: int,
452
+ new_amount: float,
453
+ table_name: str = DEFAULT_TABLES.get("grant"),
454
+ ) -> bool:
455
+ """ "Updation of the the grant detail in grant table
456
+
457
+ Args:
458
+ old_grant (str): old grant information.
459
+ new_grant (str): new grant information to replace old grant.
460
+ new_paid_by (int): new primary key of the vrauser.
461
+ new_amount (float): new amount paid for the purchase.
462
+ table_name (str): table name of the grant.
463
+
464
+ Returns:
465
+ bool: True for the success and False for the failure.
466
+ """
467
+ with self.connection:
468
+ try:
469
+ self.cursor.execute(
470
+ f"UPDATE {table_name} SET grant_number ='{new_grant}', paid_by='{new_paid_by}', \
471
+ amount='{new_amount}' WHERE grant_number='{old_grant}';"
472
+ )
473
+ LOG.info(
474
+ f"INFO: Updation of the grant {old_grant} has been performed successfully."
475
+ )
476
+ return True
477
+ except Exception as e:
478
+ LOG.error(
479
+ f"Error: grant Updating of '{old_grant}' in table '{table_name}': {e}"
480
+ )
481
+ return False
482
+
483
+ def remove_grant(
484
+ self, grant_number: str, table_name: str = DEFAULT_TABLES.get("grant")
485
+ ) -> bool:
486
+ """Removal of the grant.
487
+
488
+ Args:
489
+ grant_number (str): grant number which need to be replaced.
490
+ table_name (str): table name of the grant.
491
+
492
+ Returns:
493
+ bool: True for the success and False for the failure.
494
+ """
495
+ with self.connection:
496
+ try:
497
+ self.cursor.execute(
498
+ f"DELETE from {table_name} WHERE grant_number='{grant_number}';"
499
+ )
500
+ LOG.info(
501
+ f"INFO: Removal of the grant {grant_number} has been performed successfully."
502
+ )
503
+ return True
504
+ except Exception as e:
505
+ LOG.error(
506
+ f"Error: Removal of grant {grant_number} from table {table_name}: {e}"
507
+ )
508
+ raise DbException(
509
+ f"Error: Removal of grant {grant_number} from table {table_name}: {e}"
510
+ )
511
+
512
+ def get_grant(
513
+ self,
514
+ grant_number: str | None = None,
515
+ table_name: str = DEFAULT_TABLES.get("grant"),
516
+ ) -> any:
517
+ """Retreive the information from the grant table.
518
+
519
+ Args:
520
+ grant_number (str | None, optional): grant which need to be fetched. Defaults to None.
521
+ table_name (str): table name of the grant.
522
+
523
+ Returns:
524
+ any: Retreive the data from the grant database.
525
+ """
526
+ with self.connection:
527
+ if grant_number:
528
+ self.cursor.execute(
529
+ f"SELECT * FROM {table_name} where grant_number = '{grant_number}';"
530
+ )
531
+ else:
532
+ self.cursor.execute(f"SELECT * FROM {table_name};")
533
+ LOG.info("INFO: grant information has been fetched successfully.")
534
+ return self.cursor.fetchall()
535
+
536
+ def insert_costing(
537
+ self,
538
+ deployment_id: int,
539
+ typee: str,
540
+ project_id: Optional[int] = None,
541
+ grant_id: Optional[int] = None,
542
+ table_name: str = DEFAULT_TABLES.get("costing"),
543
+ ) -> bool:
544
+ """Insertion of the costing detail.
545
+
546
+ Args:
547
+ deployment_id (int): primary key of the deployment id.
548
+ typee (str): type of the license.
549
+ project_id (Optional[int], optional): primary key of the puchase order. Defaults to None.
550
+ grant_id (Optional[int], optional): primary key of the grant. Defaults to None.
551
+ table_name (str): table name of the costing.
552
+
553
+ Raises:
554
+ DbException: Exception for the provided inputs both PO and grant.
555
+ DbException: Exception for the provided inputs.
556
+
557
+ Returns:
558
+ bool: True for the success and False for the failure.
559
+ """
560
+ with self.connection:
561
+ if project_id and grant_id:
562
+ LOG.error(
563
+ "Error: Please do not provide both project_id & grant_id for costing"
564
+ )
565
+ raise DbException(
566
+ "Error: Please do not provide both project_id & grant_id for costing"
567
+ )
568
+ elif deployment_id and project_id and typee:
569
+ try:
570
+ self.cursor.execute(
571
+ f"INSERT INTO {table_name} (deployment_id, type, project_id) VALUES \
572
+ ('{deployment_id}', '{typee}', '{project_id}');"
573
+ )
574
+ LOG.info("INFO: Costing insertion has been performed successfully")
575
+ return True
576
+ except Exception as e:
577
+ LOG.error(
578
+ f"Error: cost removal failed in table '{table_name}': {e}"
579
+ )
580
+ return False
581
+ elif deployment_id and grant_id and typee:
582
+ try:
583
+ self.cursor.execute(
584
+ f"INSERT INTO {table_name} (deployment_id, type, grant_id) VALUES \
585
+ ('{deployment_id}', '{typee}', '{grant_id}');"
586
+ )
587
+ LOG.info("INFO: Insertion of costing has been successfully")
588
+ return True
589
+ except Exception as e:
590
+ LOG.error(
591
+ f"Error: cost removal failed in table '{table_name}': {e}"
592
+ )
593
+ return False
594
+ else:
595
+ LOG.error(
596
+ "Error: Please provide correct deployment_id, type, and, (project_id/grant_id) for costing"
597
+ )
598
+ raise DbException(
599
+ "Error: Please provide correct deployment_id, type, and, (project_id/grant_id) for costing"
600
+ )
601
+
602
+ def update_costing(
603
+ self,
604
+ deployment_id: int,
605
+ typee: str,
606
+ old_grant_id: Optional[int] = None,
607
+ old_project_id: Optional[int] = None,
608
+ grant_id: Optional[int] = None,
609
+ project_id: Optional[int] = None,
610
+ table_name: str = DEFAULT_TABLES.get("costing"),
611
+ ) -> bool:
612
+ """Updation of the costing database entry.
613
+
614
+ Args:
615
+ deployment_id (int): primary key of the deployment id.
616
+ typee (str): type of the license.
617
+ old_grant_id (Optional[int], optional): primary key of the old grant id. Defaults to None.
618
+ old_project_id (Optional[int], optional): primary key of the old payment order. Defaults to None.
619
+ grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
620
+ project_id (Optional[int], optional): primary key of the grant id. Defaults to None.
621
+ table_name (str): table name of the costing.
622
+
623
+ Raises:
624
+ DbException: Exception for the provided inputs.
625
+
626
+ Returns:
627
+ bool: True for the success and False for the failure.
628
+ """
629
+ if old_project_id and old_grant_id:
630
+ LOG.info(
631
+ "Error: Please do not provide deployment_id. type, and, (project_id/ grant_id) for costing"
632
+ )
633
+ raise DbException(
634
+ "Error: Please do not provide deployment_id. type, and, (project_id/ grant_id) for costing"
635
+ )
636
+ elif old_project_id:
637
+ with self.connection:
638
+ try:
639
+ grant_id = "NULL" if not grant_id else f"'{grant_id}'"
640
+ project_id = "NULL" if not project_id else f"'{project_id}'"
641
+ self.cursor.execute(
642
+ f"UPDATE {table_name} SET \
643
+ deployment_id ='{deployment_id}', type='{typee}', project_id={project_id}, \
644
+ grant_id={grant_id} WHERE project_id='{old_project_id}';"
645
+ )
646
+ LOG.info(
647
+ "INFO: updation of the costing has been performed successfully."
648
+ )
649
+ return True
650
+ except Exception as e:
651
+ LOG.error(
652
+ f"Error: Updation of costing has failed in table '{table_name}': \n {e}"
653
+ )
654
+ return False
655
+ elif old_grant_id:
656
+ with self.connection:
657
+ try:
658
+ grant_id = "NULL" if not grant_id else f"'{grant_id}'"
659
+ project_id = "NULL" if not project_id else f"'{project_id}'"
660
+ self.cursor.execute(
661
+ f"UPDATE {table_name} SET deployment_id ='{deployment_id}', type='{typee}', \
662
+ grant_id={grant_id}, project_id={project_id} WHERE grant_id='{old_grant_id}';"
663
+ )
664
+ LOG.info(
665
+ "INFO: updation of the costing has been performed successfully."
666
+ )
667
+ return True
668
+ except Exception as e:
669
+ LOG.error(f"Error: Updation of costing has failed: \n {e}")
670
+ return False
671
+ else:
672
+ LOG.error("Error: updation of the costing has been failed")
673
+ return False
674
+
675
+ def remove_costing(
676
+ self,
677
+ deployment_id: int,
678
+ typee: str,
679
+ project_id: Optional[int] = None,
680
+ grant_id: Optional[int] = None,
681
+ table_name: str = DEFAULT_TABLES.get("costing"),
682
+ ) -> bool:
683
+ """Removal of the costing detail from costing database.
684
+
685
+ Args:
686
+ deployment_id (int): primary key of the deployment id.
687
+ typee (str): type of the license.
688
+ project_id (Optional[int], optional): primary key of the payment order. Defaults to None.
689
+ grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
690
+ table_name (str): table name of the costing.
691
+
692
+ Raises:
693
+ DbException: Exception for the provided inputs.
694
+
695
+ Returns:
696
+ bool: True for the success and False for the failure.
697
+ """
698
+ with self.connection:
699
+ if grant_id and project_id:
700
+ LOG.error(
701
+ "Error: Please do not provide both project_id and grant_id for costing"
702
+ )
703
+ raise DbException(
704
+ "Error: Please do not provide both project_id and grant_id for costing"
705
+ )
706
+ elif deployment_id and grant_id:
707
+ try:
708
+ self.cursor.execute(
709
+ f"DELETE from {table_name} WHERE deployment_id = '{deployment_id}' and type = '{typee}' \
710
+ and grant_id = '{grant_id}';"
711
+ )
712
+ LOG.info(
713
+ "INFO: Removing of the costing has been performed successfully."
714
+ )
715
+ return True
716
+ except Exception as e:
717
+ LOG.error(
718
+ f"Error: Removing of costing has failed in table {table_name}: \n {e}"
719
+ )
720
+ return False
721
+ elif deployment_id and project_id:
722
+ try:
723
+ self.cursor.execute(
724
+ f"DELETE from {table_name} WHERE deployment_id = '{deployment_id}' and type = '{typee}' \
725
+ and project_id = '{project_id}';"
726
+ )
727
+ LOG.info(
728
+ "INFO: Removing of the costing has been performed successfully."
729
+ )
730
+ return True
731
+ except Exception as e:
732
+ LOG.error(
733
+ f"Error: Removing of costing has failed in table '{table_name}': \n {e}"
734
+ )
735
+ return False
736
+ else:
737
+ LOG.error(
738
+ "Error: Please provide correct deployment_id, type, and, (project_id/grant_id) for costing"
739
+ )
740
+ raise DbException(
741
+ "Error: Please provide correct deployment_id, type, and, (project_id/grant_id) for costing"
742
+ )
743
+
744
+ def get_costing(
745
+ self,
746
+ deployment_id: Optional[int] = None,
747
+ typee: Optional[str] = None,
748
+ project_id: Optional[int] = None,
749
+ grant_id: Optional[int] = None,
750
+ table_name: str = DEFAULT_TABLES.get("costing"),
751
+ ) -> any:
752
+ """Retreive the information from the costing table.
753
+
754
+ Args:
755
+ deployment_id (Optional[int], optional): primary key of the deployment id.. Defaults to None.
756
+ typee (Optional[str], optional): type of the license.. Defaults to None.
757
+ project_id (Optional[int], optional): primary key of the payment order. Defaults to None.
758
+ grant_id (Optional[int], optional): primary key of the grant id. Defaults to None.
759
+ table_name (str): table name of the costing.
760
+
761
+ Returns:
762
+ any: Retreive the data from the costing database.
763
+ """
764
+ with self.connection:
765
+ if deployment_id and project_id and typee:
766
+ self.cursor.execute(
767
+ f"SELECT * FROM {table_name} where deployment_id = '{deployment_id}' and type = '{typee}' \
768
+ and project_id = '{project_id}';"
769
+ )
770
+ elif deployment_id and grant_id and typee:
771
+ self.cursor.execute(
772
+ f"SELECT * FROM {table_name} where deployment_id = '{deployment_id}' and type = '{typee}' \
773
+ and grant_id = '{grant_id}';"
774
+ )
775
+ elif deployment_id and not grant_id and not typee and not project_id:
776
+ self.cursor.execute(
777
+ f"SELECT * FROM {table_name} where deployment_id = '{deployment_id}';"
778
+ )
779
+ elif not deployment_id and not grant_id and typee and not project_id:
780
+ self.cursor.execute(
781
+ f"SELECT * FROM {table_name} where type = '{typee}';"
782
+ )
783
+ else:
784
+ self.cursor.execute(f"SELECT * FROM {table_name};")
785
+ LOG.info("INFO: costing information has been performed successfully")
786
+ return self.cursor.fetchall()
787
+
788
+ def closedb(self) -> None:
789
+ """
790
+ To close the databse connection.
791
+ """
792
+ self.connection.close()