@@ -43,7 +43,7 @@ def create(self, params: BudgetCreateRequest) -> Union[BudgetResponse, dict]:
43
43
'time_unit': 'str', # required
44
44
'start': 'str', # required
45
45
'end': 'str', # required
46
- 'notifications ': 'dict',
46
+ 'notification ': 'dict',
47
47
'tags': 'dict',
48
48
'resource_group': 'str', # required
49
49
'project_id': 'str',
@@ -65,14 +65,14 @@ def create(self, params: BudgetCreateRequest) -> Union[BudgetResponse, dict]:
65
65
start = params .start
66
66
end = params .end
67
67
68
- notifications = params .notifications or {}
68
+ notification = params .notification or {}
69
69
resource_group = params .resource_group
70
70
71
- self ._check_time_period (start , end )
72
-
73
71
if resource_group != "PROJECT" :
74
72
raise ERROR_NOT_IMPLEMENTED ()
75
73
74
+ self ._check_time_period (start , end )
75
+
76
76
identity_mgr : IdentityManager = self .locator .get_manager ("IdentityManager" )
77
77
identity_mgr .check_workspace (workspace_id , domain_id )
78
78
identity_mgr .get_project (project_id , domain_id )
@@ -101,8 +101,8 @@ def create(self, params: BudgetCreateRequest) -> Union[BudgetResponse, dict]:
101
101
for planned_limit in planned_limits :
102
102
params .limit += planned_limit .get ("limit" , 0 )
103
103
104
- # Check Notifications
105
- self ._check_notifications ( notifications , domain_id , workspace_id )
104
+ # Check Notification
105
+ self ._check_notification ( notification , domain_id , workspace_id )
106
106
107
107
# Check Duplicated Budget
108
108
budget_vos = self .budget_mgr .filter_budgets (
@@ -144,6 +144,8 @@ def update(self, params: BudgetUpdateRequest) -> Union[BudgetResponse, dict]:
144
144
'name': 'str',
145
145
'limit': 'float',
146
146
'planned_limits': 'list',
147
+ 'start': 'str',
148
+ 'end': 'str',
147
149
'tags': 'dict'
148
150
'workspace_id', 'str', # injected from auth (optional)
149
151
'domain_id': 'str' # injected from auth
@@ -164,12 +166,28 @@ def update(self, params: BudgetUpdateRequest) -> Union[BudgetResponse, dict]:
164
166
budget_id , domain_id , workspace_id
165
167
)
166
168
169
+ start = budget_vo .start
170
+ end = budget_vo .end
171
+
172
+ if params .start or params .end :
173
+ start = params .start or start
174
+ end = params .end or end
175
+ self ._check_time_period (start , end )
176
+ planned_limits = planned_limits or budget_vo .planned_limits
177
+
167
178
# Check limit and Planned Limits
179
+ if planned_limits :
180
+ self ._check_planned_limits (start , end , budget_vo .time_unit , planned_limits )
181
+
168
182
budget_vo = self .budget_mgr .update_budget_by_vo (
169
183
params .dict (exclude_unset = True ), budget_vo
170
184
)
171
185
172
- if "name" in params :
186
+ # Update Budget Usages
187
+ if planned_limits :
188
+ budget_usage_mgr .delete_budget_usage_by_budget_vo (budget_vo )
189
+ budget_usage_mgr .create_budget_usages (budget_vo )
190
+ elif "name" in params :
173
191
budget_usage_vos = budget_usage_mgr .filter_budget_usages (
174
192
budget_id = budget_id ,
175
193
domain_id = domain_id ,
@@ -197,7 +215,7 @@ def set_notification(
197
215
Args:
198
216
params (dict): {
199
217
'budget_id': 'str',
200
- 'notifications ': 'dict',
218
+ 'notification ': 'dict',
201
219
'workspace_id': 'str',
202
220
'domain_id': 'str'
203
221
'user_projects': 'list'
@@ -210,15 +228,15 @@ def set_notification(
210
228
project_id = params .project_id
211
229
workspace_id = params .workspace_id
212
230
domain_id = params .domain_id
213
- notifications = params .notifications or {}
231
+ notification = params .notification or {}
214
232
215
233
budget_vo : Budget = self .budget_mgr .get_budget (
216
234
budget_id , domain_id , workspace_id , project_id
217
235
)
218
236
219
- # Check Notifications
220
- self ._check_notifications ( notifications , domain_id , workspace_id )
221
- params .notifications = notifications
237
+ # Check notification
238
+ self ._check_notification ( notification , domain_id , workspace_id )
239
+ params .notification = notification
222
240
223
241
budget_vo = self .budget_mgr .update_budget_by_vo (
224
242
params .dict (exclude_unset = True ), budget_vo
@@ -356,7 +374,14 @@ def _check_time_period(start, end):
356
374
if start >= end :
357
375
raise ERROR_INVALID_TIME_RANGE (start = start , end = end )
358
376
359
- def _check_planned_limits (self , start , end , time_unit , planned_limits ):
377
+ def _check_planned_limits (
378
+ self , start : str , end : str , time_unit : str , planned_limits : list
379
+ ):
380
+ if time_unit == "TOTAL" :
381
+ raise ERROR_INVALID_PARAMETER (
382
+ key = "time_unit" , value = f"Only MONTHLY time_unit is allowed"
383
+ )
384
+
360
385
planned_limits_dict = self ._convert_planned_limits_data_type (planned_limits )
361
386
date_format = "%Y-%m"
362
387
@@ -381,7 +406,7 @@ def _check_planned_limits(self, start, end, time_unit, planned_limits):
381
406
raise ERROR_DATE_IS_WRONG (date = list (planned_limits_dict .keys ()))
382
407
383
408
@staticmethod
384
- def _convert_planned_limits_data_type (planned_limits ) :
409
+ def _convert_planned_limits_data_type (planned_limits : list ) -> dict :
385
410
planned_limits_dict = {}
386
411
387
412
for planned_limit in planned_limits :
@@ -398,12 +423,12 @@ def _convert_planned_limits_data_type(planned_limits):
398
423
return planned_limits_dict
399
424
400
425
@staticmethod
401
- def _check_notifications (
402
- notifications : dict ,
426
+ def _check_notification (
427
+ notification : dict ,
403
428
domain_id : str ,
404
429
workspace_id : str ,
405
430
) -> dict :
406
- plans = notifications .get ("plans" , [])
431
+ plans = notification .get ("plans" , [])
407
432
408
433
for plan in plans :
409
434
unit = plan ["unit" ]
@@ -420,7 +445,7 @@ def _check_notifications(
420
445
raise ERROR_THRESHOLD_IS_WRONG_IN_PERCENT_TYPE (value = plan )
421
446
422
447
# check recipients
423
- recipients = notifications .get ("recipients" , {})
448
+ recipients = notification .get ("recipients" , {})
424
449
users = list (set (recipients .get ("users" , [])))
425
450
role_types = list (set (recipients .get ("role_types" , [])))
426
451
@@ -449,8 +474,8 @@ def _check_notifications(
449
474
raise ERROR_NOT_FOUND (key = "user_id" , value = user_id )
450
475
recipients ["users" ] = users
451
476
452
- notifications ["recipients" ] = recipients
453
- return notifications
477
+ notification ["recipients" ] = recipients
478
+ return notification
454
479
455
480
@staticmethod
456
481
def _set_user_project_or_project_group_filter (params : dict ) -> dict :
0 commit comments