63
63
64
64
65
65
def split_quota (quotas : list [Any ]) -> Tuple [Dict [str , Any ], Dict [str , Any ]]:
66
- """Split quotas in total and per-users."""
66
+ """Split quotas in usage, per-user, per-project."""
67
+ db_items_usage = {
68
+ db_item .project .single ().uuid : db_item
69
+ for db_item in filter (lambda x : x .usage , quotas )
70
+ }
67
71
db_items_per_user = {
68
72
db_item .project .single ().uuid : db_item
69
- for db_item in filter (lambda x : x .per_user , quotas )
73
+ for db_item in filter (lambda x : x .per_user and not x . usage , quotas )
70
74
}
71
- db_items_total = {
75
+ db_items_per_project = {
72
76
db_item .project .single ().uuid : db_item
73
- for db_item in filter (lambda x : not x .per_user , quotas )
77
+ for db_item in filter (lambda x : not x .per_user and not x . usage , quotas )
74
78
}
75
- return (db_items_per_user , db_items_total )
79
+ return (db_items_usage , db_items_per_user , db_items_per_project )
76
80
77
81
78
82
class CRUDBlockStorageService (
@@ -150,7 +154,7 @@ def update(
150
154
update_data = super ().update (db_obj = db_obj , obj_in = obj_in , force = force )
151
155
return db_obj if edit else update_data
152
156
153
- def __update_quotas (
157
+ def __update_quotas ( # noqa: C901
154
158
self ,
155
159
* ,
156
160
db_obj : BlockStorageService ,
@@ -162,17 +166,37 @@ def __update_quotas(
162
166
Connect new quotas not already connect, leave untouched already linked ones and
163
167
delete old ones no more connected to the service.
164
168
165
- Split quotas in per_user and total . For each one of them, check the linked
166
- project. If the project already has a quota of that type, update that quota with
167
- the new received values.
169
+ Split quotas in usage, per_user and per_project . For each one of them, check the
170
+ linked project. If the project already has a quota of that type, update that
171
+ quota with the new received values.
168
172
"""
169
173
edit = False
170
174
171
- db_items_per_user , db_items_total = split_quota (db_obj .quotas )
175
+ db_items_usage , db_items_per_user , db_items_per_project = split_quota (
176
+ db_obj .quotas
177
+ )
172
178
db_projects = {db_item .uuid : db_item for db_item in provider_projects }
173
179
174
180
for item in obj_in .quotas :
175
- if item .per_user :
181
+ if item .usage :
182
+ db_item = db_items_usage .pop (item .project , None )
183
+ if not db_item :
184
+ block_storage_quota_mng .create (
185
+ obj_in = item ,
186
+ service = db_obj ,
187
+ project = db_projects .get (item .project ),
188
+ )
189
+ edit = True
190
+ else :
191
+ updated_data = block_storage_quota_mng .update (
192
+ db_obj = db_item ,
193
+ obj_in = item ,
194
+ projects = provider_projects ,
195
+ force = True ,
196
+ )
197
+ if not edit and updated_data is not None :
198
+ edit = True
199
+ elif item .per_user :
176
200
db_item = db_items_per_user .pop (item .project , None )
177
201
if not db_item :
178
202
block_storage_quota_mng .create (
@@ -191,7 +215,7 @@ def __update_quotas(
191
215
if not edit and updated_data is not None :
192
216
edit = True
193
217
else :
194
- db_item = db_items_total .pop (item .project , None )
218
+ db_item = db_items_per_project .pop (item .project , None )
195
219
if not db_item :
196
220
block_storage_quota_mng .create (
197
221
obj_in = item ,
@@ -209,10 +233,13 @@ def __update_quotas(
209
233
if not edit and updated_data is not None :
210
234
edit = True
211
235
236
+ for db_item in db_items_usage .values ():
237
+ block_storage_quota_mng .remove (db_obj = db_item )
238
+ edit = True
212
239
for db_item in db_items_per_user .values ():
213
240
block_storage_quota_mng .remove (db_obj = db_item )
214
241
edit = True
215
- for db_item in db_items_total .values ():
242
+ for db_item in db_items_per_project .values ():
216
243
block_storage_quota_mng .remove (db_obj = db_item )
217
244
edit = True
218
245
@@ -388,7 +415,7 @@ def __update_images(
388
415
edit = True
389
416
return edit
390
417
391
- def __update_quotas (
418
+ def __update_quotas ( # noqa: C901
392
419
self ,
393
420
* ,
394
421
db_obj : ComputeService ,
@@ -406,11 +433,31 @@ def __update_quotas(
406
433
"""
407
434
edit = False
408
435
409
- db_items_per_user , db_items_total = split_quota (db_obj .quotas )
436
+ db_items_usage , db_items_per_user , db_items_per_project = split_quota (
437
+ db_obj .quotas
438
+ )
410
439
db_projects = {db_item .uuid : db_item for db_item in provider_projects }
411
440
412
441
for item in obj_in .quotas :
413
- if item .per_user :
442
+ if item .usage :
443
+ db_item = db_items_usage .pop (item .project , None )
444
+ if not db_item :
445
+ block_storage_quota_mng .create (
446
+ obj_in = item ,
447
+ service = db_obj ,
448
+ project = db_projects .get (item .project ),
449
+ )
450
+ edit = True
451
+ else :
452
+ updated_data = block_storage_quota_mng .update (
453
+ db_obj = db_item ,
454
+ obj_in = item ,
455
+ projects = provider_projects ,
456
+ force = True ,
457
+ )
458
+ if not edit and updated_data is not None :
459
+ edit = True
460
+ elif item .per_user :
414
461
db_item = db_items_per_user .pop (item .project , None )
415
462
if not db_item :
416
463
compute_quota_mng .create (
@@ -429,7 +476,7 @@ def __update_quotas(
429
476
if not edit and updated_data is not None :
430
477
edit = True
431
478
else :
432
- db_item = db_items_total .pop (item .project , None )
479
+ db_item = db_items_per_project .pop (item .project , None )
433
480
if not db_item :
434
481
compute_quota_mng .create (
435
482
obj_in = item ,
@@ -447,10 +494,13 @@ def __update_quotas(
447
494
if not edit and updated_data is not None :
448
495
edit = True
449
496
497
+ for db_item in db_items_usage .values ():
498
+ block_storage_quota_mng .remove (db_obj = db_item )
499
+ edit = True
450
500
for db_item in db_items_per_user .values ():
451
501
compute_quota_mng .remove (db_obj = db_item )
452
502
edit = True
453
- for db_item in db_items_total .values ():
503
+ for db_item in db_items_per_project .values ():
454
504
compute_quota_mng .remove (db_obj = db_item )
455
505
edit = True
456
506
@@ -611,7 +661,7 @@ def __update_networks(
611
661
edit = True
612
662
return edit
613
663
614
- def __update_quotas (
664
+ def __update_quotas ( # noqa: C901
615
665
self ,
616
666
* ,
617
667
db_obj : NetworkService ,
@@ -629,11 +679,31 @@ def __update_quotas(
629
679
"""
630
680
edit = False
631
681
632
- db_items_per_user , db_items_total = split_quota (db_obj .quotas )
682
+ db_items_usage , db_items_per_user , db_items_per_project = split_quota (
683
+ db_obj .quotas
684
+ )
633
685
db_projects = {db_item .uuid : db_item for db_item in provider_projects }
634
686
635
687
for item in obj_in .quotas :
636
- if item .per_user :
688
+ if item .usage :
689
+ db_item = db_items_usage .pop (item .project , None )
690
+ if not db_item :
691
+ block_storage_quota_mng .create (
692
+ obj_in = item ,
693
+ service = db_obj ,
694
+ project = db_projects .get (item .project ),
695
+ )
696
+ edit = True
697
+ else :
698
+ updated_data = block_storage_quota_mng .update (
699
+ db_obj = db_item ,
700
+ obj_in = item ,
701
+ projects = provider_projects ,
702
+ force = True ,
703
+ )
704
+ if not edit and updated_data is not None :
705
+ edit = True
706
+ elif item .per_user :
637
707
db_item = db_items_per_user .pop (item .project , None )
638
708
if not db_item :
639
709
network_quota_mng .create (
@@ -652,7 +722,7 @@ def __update_quotas(
652
722
if not edit and updated_data is not None :
653
723
edit = True
654
724
else :
655
- db_item = db_items_total .pop (item .project , None )
725
+ db_item = db_items_per_project .pop (item .project , None )
656
726
if not db_item :
657
727
network_quota_mng .create (
658
728
obj_in = item ,
@@ -670,10 +740,13 @@ def __update_quotas(
670
740
if not edit and updated_data is not None :
671
741
edit = True
672
742
743
+ for db_item in db_items_usage .values ():
744
+ block_storage_quota_mng .remove (db_obj = db_item )
745
+ edit = True
673
746
for db_item in db_items_per_user .values ():
674
747
network_quota_mng .remove (db_obj = db_item )
675
748
edit = True
676
- for db_item in db_items_total .values ():
749
+ for db_item in db_items_per_project .values ():
677
750
network_quota_mng .remove (db_obj = db_item )
678
751
edit = True
679
752
@@ -755,7 +828,7 @@ def update(
755
828
update_data = super ().update (db_obj = db_obj , obj_in = obj_in , force = force )
756
829
return db_obj if edit else update_data
757
830
758
- def __update_quotas (
831
+ def __update_quotas ( # noqa: C901
759
832
self ,
760
833
* ,
761
834
db_obj : ObjectStoreService ,
@@ -773,11 +846,31 @@ def __update_quotas(
773
846
"""
774
847
edit = False
775
848
776
- db_items_per_user , db_items_total = split_quota (db_obj .quotas )
849
+ db_items_usage , db_items_per_user , db_items_per_project = split_quota (
850
+ db_obj .quotas
851
+ )
777
852
db_projects = {db_item .uuid : db_item for db_item in provider_projects }
778
853
779
854
for item in obj_in .quotas :
780
- if item .per_user :
855
+ if item .usage :
856
+ db_item = db_items_usage .pop (item .project , None )
857
+ if not db_item :
858
+ block_storage_quota_mng .create (
859
+ obj_in = item ,
860
+ service = db_obj ,
861
+ project = db_projects .get (item .project ),
862
+ )
863
+ edit = True
864
+ else :
865
+ updated_data = block_storage_quota_mng .update (
866
+ db_obj = db_item ,
867
+ obj_in = item ,
868
+ projects = provider_projects ,
869
+ force = True ,
870
+ )
871
+ if not edit and updated_data is not None :
872
+ edit = True
873
+ elif item .per_user :
781
874
db_item = db_items_per_user .pop (item .project , None )
782
875
if not db_item :
783
876
object_store_quota_mng .create (
@@ -796,7 +889,7 @@ def __update_quotas(
796
889
if not edit and updated_data is not None :
797
890
edit = True
798
891
else :
799
- db_item = db_items_total .pop (item .project , None )
892
+ db_item = db_items_per_project .pop (item .project , None )
800
893
if not db_item :
801
894
object_store_quota_mng .create (
802
895
obj_in = item ,
@@ -814,10 +907,13 @@ def __update_quotas(
814
907
if not edit and updated_data is not None :
815
908
edit = True
816
909
910
+ for db_item in db_items_usage .values ():
911
+ block_storage_quota_mng .remove (db_obj = db_item )
912
+ edit = True
817
913
for db_item in db_items_per_user .values ():
818
914
object_store_quota_mng .remove (db_obj = db_item )
819
915
edit = True
820
- for db_item in db_items_total .values ():
916
+ for db_item in db_items_per_project .values ():
821
917
object_store_quota_mng .remove (db_obj = db_item )
822
918
edit = True
823
919
0 commit comments