Skip to content

Commit a7fe243

Browse files
committed
feat: modify convert virtual workspace id to workspace id when get, delete list
Signed-off-by: ImMin5 <mino@megazone.com>
1 parent 1edcab2 commit a7fe243

File tree

2 files changed

+53
-52
lines changed

2 files changed

+53
-52
lines changed

src/spaceone/cost_analysis/manager/cost_manager.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ def delete_cost_with_datasource(self, domain_id: str, data_source_id: str) -> No
9898
history_vos.delete()
9999

100100
def get_cost(
101-
self,
102-
cost_id: str,
103-
domain_id: str,
104-
workspace_id=None,
105-
user_projects: list = None,
101+
self,
102+
cost_id: str,
103+
domain_id: str,
104+
workspace_id=None,
105+
user_projects: list = None,
106106
):
107107
conditions = {"cost_id": cost_id, "domain_id": domain_id}
108108

@@ -171,7 +171,7 @@ def analyze_yearly_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
171171
expire=3600 * 24,
172172
)
173173
def stat_monthly_costs_with_cache(
174-
self, query, query_hash, domain_id, data_source_id
174+
self, query, query_hash, domain_id, data_source_id
175175
):
176176
return self.stat_monthly_costs(query, domain_id)
177177

@@ -180,7 +180,7 @@ def stat_monthly_costs_with_cache(
180180
expire=3600 * 24,
181181
)
182182
def analyze_costs_with_cache(
183-
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
183+
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
184184
):
185185
return self.analyze_costs(query, domain_id, target)
186186

@@ -189,7 +189,7 @@ def analyze_costs_with_cache(
189189
expire=3600 * 24,
190190
)
191191
def analyze_monthly_costs_with_cache(
192-
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
192+
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
193193
):
194194
return self.analyze_monthly_costs(query, domain_id, target)
195195

@@ -198,12 +198,12 @@ def analyze_monthly_costs_with_cache(
198198
expire=3600 * 24,
199199
)
200200
def analyze_yearly_costs_with_cache(
201-
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
201+
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
202202
):
203203
return self.analyze_yearly_costs(query, domain_id, target)
204204

205205
def analyze_costs_by_granularity(
206-
self, query: dict, domain_id: str, data_source_id: str
206+
self, query: dict, domain_id: str, data_source_id: str
207207
):
208208
self._check_group_by(query)
209209
self._check_date_range(query)
@@ -240,7 +240,7 @@ def analyze_costs_by_granularity(
240240
expire=600,
241241
)
242242
def create_cost_query_history(
243-
self, query: dict, query_hash: str, domain_id: str, data_source_id: str
243+
self, query: dict, query_hash: str, domain_id: str, data_source_id: str
244244
):
245245
def _rollback(history_vo):
246246
_LOGGER.info(
@@ -444,7 +444,7 @@ def _change_filter_project_group_id(self, query: dict, domain_id: str) -> dict:
444444
return query
445445

446446
def change_filter_v_workspace_id(
447-
self, query: dict, domain_id: str, data_source_id: str
447+
self, query: dict, domain_id: str, data_source_id: str
448448
) -> dict:
449449
change_filter = []
450450
workspace_ids = []
@@ -500,7 +500,7 @@ def change_filter_v_workspace_id(
500500
return query
501501

502502
def _change_response_workspace_group_by(
503-
self, response: dict, query: dict, domain_id: str, data_source_id: str
503+
self, response: dict, query: dict, domain_id: str, data_source_id: str
504504
) -> dict:
505505
if query_group_by := query.get("group_by"):
506506
if "workspace_id" in query_group_by:
@@ -533,7 +533,7 @@ def _change_response_workspace_group_by(
533533
return response
534534

535535
def _get_workspace_id_from_v_workspace_id(
536-
self, domain_id: str, v_workspace_id: str
536+
self, domain_id: str, v_workspace_id: str
537537
) -> str:
538538
workspace_id = v_workspace_id
539539
ds_account_vos = self.data_source_account_mgr.filter_data_source_accounts(
@@ -581,5 +581,5 @@ def _check_group_by(query: dict) -> None:
581581
elif key in ["cost", "usage_quantity"]:
582582
raise ERROR_INVALID_PARAMETER(
583583
key=key,
584-
reason=f"{key} are not allowed to group by.",
584+
reason=f"{key} is not allowed to group by.",
585585
)

src/spaceone/cost_analysis/service/cost_service.py

+38-37
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,29 @@ def create(self, params):
7777

7878
@transaction(permission="cost-analysis:Cost.write", role_types=["WORKSPACE_OWNER"])
7979
@check_required(["cost_id", "domain_id"])
80-
def delete(self, params):
80+
def delete(self, params: dict):
8181
"""Deregister cost
8282
8383
Args:
8484
params (dict): {
8585
'cost_id': 'str', # injected from path
86-
'workspace_id' : str', # injected from auth(optional)
86+
'workspace_id' : str', # injected from auth
8787
'domain_id': 'str' # injected from auth
8888
}
8989
9090
Returns:
9191
None
9292
"""
9393

94+
cost_id = params["cost_id"]
9495
domain_id = params["domain_id"]
96+
workspace_id = params.get("workspace_id")
9597

96-
cost_vo: Cost = self.cost_mgr.get_cost(
97-
params["cost_id"],
98-
params["domain_id"],
99-
params.get("workspace_id"),
100-
)
98+
if workspace_id:
99+
cost_vo: Cost = self.cost_mgr.get_cost(cost_id, domain_id)
100+
self._check_workspace_id_with_cost_vo(cost_vo, domain_id, workspace_id)
101+
else:
102+
cost_vo: Cost = self.cost_mgr.get_cost(cost_id, domain_id)
101103

102104
self.cost_mgr.remove_stat_cache(
103105
domain_id=domain_id,
@@ -134,15 +136,7 @@ def get(self, params: dict) -> Union[CostResponse, dict]:
134136

135137
if workspace_id:
136138
cost_vo: Cost = self.cost_mgr.get_cost(cost_id, domain_id, user_projects)
137-
138-
v_workspace_ids = self._get_v_workspace_ids_related_with_workspace_id(
139-
domain_id, workspace_id
140-
)
141-
if (
142-
cost_vo.workspace_id not in v_workspace_ids
143-
and cost_vo.workspace_id != workspace_id
144-
):
145-
raise ERROR_PERMISSION_DENIED()
139+
self._check_workspace_id_with_cost_vo(cost_vo, domain_id, workspace_id)
146140
else:
147141
cost_vo: Cost = self.cost_mgr.get_cost(
148142
cost_id, domain_id, workspace_id, user_projects
@@ -278,10 +272,11 @@ def analyze(self, params):
278272
domain_id = params["domain_id"]
279273
data_source_id = params["data_source_id"]
280274
query = params.get("query", {})
275+
workspace_id = query.get("workspace_id")
281276

282277
if self.transaction.get_meta("authorization.role_type") != "DOMAIN_ADMIN":
283278
data_source_vo = self.data_source_mgr.get_data_source(
284-
data_source_id, domain_id
279+
data_source_id, domain_id, workspace_id
285280
)
286281
self._check_fields_with_data_source_permissions(query, data_source_vo)
287282

@@ -406,29 +401,15 @@ def _page_results(response, page):
406401
if start < 1:
407402
start = 1
408403

409-
response["results"] = results[start - 1 : start + page["limit"] - 1]
404+
response["results"] = results[start - 1: start + page["limit"] - 1]
410405
else:
411406
response["results"] = results
412407

413408
return response
414409

415-
@staticmethod
416-
def _get_v_workspace_ids_related_with_workspace_id(
417-
domain_id: str, workspace_id: str
418-
) -> list:
419-
v_workspace_ids = []
420-
data_source_account_mgr = DataSourceAccountManager()
421-
data_source_account_vos = data_source_account_mgr.filter_data_source_accounts(
422-
domain_id=domain_id,
423-
workspace_id=workspace_id,
424-
)
425-
426-
v_workspace_ids.extend(data_source_account_vos.values_list("v_workspace_id"))
427-
return v_workspace_ids
428-
429410
@staticmethod
430411
def _remove_deny_fields_with_data_source_vo(
431-
cost_info: dict, data_source_vo: DataSource
412+
cost_info: dict, data_source_vo: DataSource
432413
):
433414
permissions = data_source_vo.permissions or {}
434415
if permissions:
@@ -440,15 +421,35 @@ def _remove_deny_fields_with_data_source_vo(
440421

441422
@staticmethod
442423
def _check_fields_with_data_source_permissions(
443-
query: dict, data_source_vo: DataSource
424+
query: dict, data_source_vo: DataSource
444425
):
445426
permissions = data_source_vo.permissions or {}
446427
deny = permissions.get("deny", [])
447428

448429
fields = query.get("fields", {})
449430

450-
for field_key in fields.keys():
451-
field_info = fields[field_key]
452-
if _field_info_key := field_info.get("key"):
431+
for field_key, field_info in fields.items():
432+
if _field_info_key := field_info.get("key", field_info.get("k")):
453433
if _field_info_key in deny:
454434
raise ERROR_PERMISSION_DENIED()
435+
436+
@staticmethod
437+
def _check_workspace_id_with_cost_vo(
438+
cost_vo: Cost, domain_id: str, workspace_id: str
439+
) -> None:
440+
if cost_vo.workspace_id.startswith("v-"):
441+
data_source_account_mgr = DataSourceAccountManager()
442+
data_source_account_vos = (
443+
data_source_account_mgr.filter_data_source_accounts(
444+
domain_id=domain_id,
445+
v_workspace_id=cost_vo.workspace_id,
446+
)
447+
)
448+
if not data_source_account_vos:
449+
raise ERROR_PERMISSION_DENIED()
450+
451+
if workspace_id != data_source_account_vos[0].workspace_id:
452+
raise ERROR_PERMISSION_DENIED()
453+
454+
elif cost_vo.workspace_id != workspace_id:
455+
raise ERROR_PERMISSION_DENIED()

0 commit comments

Comments
 (0)