Skip to content

Commit

Permalink
Merge pull request #293 from ImMin5/master
Browse files Browse the repository at this point in the history
ix create unified cost error when workspace_id is None
  • Loading branch information
ImMin5 authored Oct 2, 2024
2 parents 2a48923 + ab8921b commit 5078749
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/spaceone/cost_analysis/conf/global_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
COST_REPORT_RUN_HOUR = 0 # Hour (UTC)
COST_REPORT_RETRY_DAYS = 7 # Day
UNIFIED_COST_RUN_HOUR = 0 # Hour (UTC)
UNIFIED_COST_AGGREGATION_DAY = 15 # Day

DEFAULT_EXCHANGE_RATE = {
# 'KRW': 1178.7,
Expand Down
5 changes: 4 additions & 1 deletion src/spaceone/cost_analysis/manager/identity_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Union

from spaceone.core import cache
from spaceone.core import config
Expand Down Expand Up @@ -76,7 +77,9 @@ def check_workspace(self, workspace_id: str, domain_id: str) -> None:
@cache.cacheable(
key="cost-analysis:workspace-name:{domain_id}:{workspace_id}:name", expire=300
)
def get_workspace(self, workspace_id: str, domain_id: str) -> str:
def get_workspace(self, workspace_id: Union[str, None], domain_id: str) -> str:
if not workspace_id:
return workspace_id
try:
workspace_info = self.identity_conn.dispatch(
"Workspace.get",
Expand Down
2 changes: 1 addition & 1 deletion src/spaceone/cost_analysis/manager/unified_cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, *args, **kwargs):
def create_unified_cost(self, params: dict) -> UnifiedCost:
def _rollback(vo: UnifiedCost):
_LOGGER.info(
f"[create_unified_cost._rollback] Delete unified_cost : {vo.unified_cost_id}, {vo.unified_cost_id} "
f"[create_unified_cost._rollback] Delete unified_cost : {vo.unified_cost_id}, {vo.to_dict()} "
)
vo.delete()

Expand Down
2 changes: 1 addition & 1 deletion src/spaceone/cost_analysis/model/unified_cost/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UnifiedCost(MongoModel):
service_account_name = StringField(max_length=255)
data_source_name = StringField(max_length=255)
project_name = StringField(max_length=255)
workspace_name = StringField(max_length=255)
workspace_name = StringField(max_length=255, default=None, null=True)
service_account_id = StringField(max_length=40)
data_source_id = StringField(max_length=40)
project_id = StringField(max_length=40, default=None, null=True)
Expand Down
15 changes: 10 additions & 5 deletions src/spaceone/cost_analysis/service/unified_cost_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,15 @@ def create_unified_cost_with_workspace(
exchange_date: datetime,
aggregation_date: datetime,
is_confirmed: bool = False,
):
) -> None:
if workspace_id:
return

identity_mgr = IdentityManager(token=config.get_global("TOKEN"))
workspace_name = identity_mgr.get_workspace(workspace_id, domain_id)
workspace_ids = [workspace_id]

workspace_name = identity_mgr.get_workspace(workspace_id, domain_id)

v_workspace_ids = self._get_virtual_workspace_ids_from_ds_account(
domain_id, workspace_id
)
Expand Down Expand Up @@ -421,11 +425,12 @@ def create_unified_cost_with_workspace(
aggregated_unified_cost_data["domain_id"] = domain_id

# set workspace name
aggregated_unified_cost_data["workspace_id"] = workspace_id
aggregated_unified_cost_data["workspace_name"] = workspace_name
if workspace_id:
aggregated_unified_cost_data["workspace_id"] = workspace_id
aggregated_unified_cost_data["workspace_name"] = workspace_name

# set project name
project_id = aggregated_unified_cost_data.get("project_id")
project_id = aggregated_unified_cost_data.get("project_id", None)
aggregated_unified_cost_data["project_name"] = project_name_map.get(
project_id, project_id
)
Expand Down

0 comments on commit 5078749

Please sign in to comment.