Skip to content

Commit

Permalink
Merge pull request #166 from ImMin5/feature-cost-report
Browse files Browse the repository at this point in the history
Modify url format for get_url api
  • Loading branch information
ImMin5 authored Jan 28, 2024
2 parents b6c2cb3 + f566412 commit f76c432
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
1 change: 1 addition & 0 deletions pkg/pip_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ numpy
jinja2
finance-datareader
plotly
bs4
1 change: 1 addition & 0 deletions src/spaceone/cost_analysis/model/cost_report/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CostReport(MongoModel):
max_length=20, choices=("IN_PROGRESS", "SUCCESS"), default="IN_PROGRESS"
)
report_number = StringField(max_length=255)
currency = StringField(choices=["KRW", "USD", "JPY"], default="KRW")
issue_date = StringField(max_length=10)
report_year = StringField(max_length=10)
report_month = StringField(max_length=10)
Expand Down
19 changes: 9 additions & 10 deletions src/spaceone/cost_analysis/service/cost_report_data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def create_cost_report_data_by_cost_report_config(self, params: dict) -> None:
"""Create cost report by cost report config"""

for cost_report_config_vo in self._get_all_cost_report_configs():
self.create_cost_report_data(cost_report_config_vo)
issue_day = self._get_issue_day(cost_report_config_vo)
if issue_day == datetime.utcnow().day:
self.create_cost_report_data(cost_report_config_vo)

@transaction(
permission="cost-analysis:CostReportData.read",
Expand Down Expand Up @@ -286,9 +288,8 @@ def _get_issue_day(cost_report_config_vo: CostReportConfig) -> int:
else:
return min(cost_report_config_vo.issue_day, last_day)

@staticmethod
def _get_workspace_name_map(domain_id: str) -> Tuple[dict, list]:
identity_mgr = IdentityManager()
def _get_workspace_name_map(self, domain_id: str) -> Tuple[dict, list]:
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
workspace_name_map = {}
workspaces = identity_mgr.list_workspaces(
{"query": {"filter": [{"k": "state", "v": "ENABLED", "o": "eq"}]}},
Expand Down Expand Up @@ -334,9 +335,8 @@ def _get_data_source_currency_map(

return data_source_currency_map, data_source_ids

@staticmethod
def _get_project_name_map(workspace_ids, domain_id: str) -> dict:
identity_mgr = IdentityManager()
def _get_project_name_map(self, workspace_ids, domain_id: str) -> dict:
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
project_name_map = {}
projects = identity_mgr.list_projects(
{
Expand All @@ -353,9 +353,8 @@ def _get_project_name_map(workspace_ids, domain_id: str) -> dict:
project_name_map[project["project_id"]] = project["name"]
return project_name_map

@staticmethod
def _get_service_account_name_map(workspace_ids, domain_id: str) -> dict:
identity_mgr = IdentityManager()
def _get_service_account_name_map(self, workspace_ids, domain_id: str) -> dict:
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
service_account_name_map = {}
service_accounts = identity_mgr.list_service_accounts(
{
Expand Down
69 changes: 34 additions & 35 deletions src/spaceone/cost_analysis/service/cost_report_serivce.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def send(self, params: CostReportSendRequest) -> None:
conditions = {
"cost_report_id": params.cost_report_id,
"domain_id": domain_id,
# {"k": "status", "v": "SUCCESS", "o": "eq"},
"status": "SUCCESS",
}

if workspace_id is not None:
conditions.update({"workspace_id": workspace_id})

Expand Down Expand Up @@ -325,7 +326,7 @@ def send_cost_report(self, cost_report_vo: CostReport) -> None:
emails = recipients.get("emails", [])

# list workspace owner role bindings
identity_mgr = IdentityManager()
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")

workspace_ids = []
if workspace_id is not None:
Expand Down Expand Up @@ -368,6 +369,19 @@ def send_cost_report(self, cost_report_vo: CostReport) -> None:
{users_info.get('total_count', 0)} users"
)

def _get_workspace_name_map(self, domain_id: str) -> Tuple[dict, list]:
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
workspace_name_map = {}
workspaces = identity_mgr.list_workspaces(
{"query": {"filter": [{"k": "state", "v": "ENABLED", "o": "eq"}]}},
domain_id,
)
workspace_ids = []
for workspace in workspaces.get("results", []):
workspace_name_map[workspace["workspace_id"]] = workspace["name"]
workspace_ids.append(workspace["workspace_id"])
return workspace_name_map, workspace_ids

def _get_console_cost_report_url(
self, domain_id: str, cost_report_id: str, token: str
) -> str:
Expand All @@ -376,7 +390,24 @@ def _get_console_cost_report_url(
console_domain = config.get_global("EMAIL_CONSOLE_DOMAIN")
console_domain = console_domain.format(domain_name=domain_name)

return f"{console_domain}cost-report?sso_access_token={token}&cost_report_id={cost_report_id}"
return f"{console_domain}/cost-report?sso_access_token={token}&cost_report_id={cost_report_id}"

def _get_domain_name(self, domain_id: str) -> str:
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
domain_name = identity_mgr.get_domain_name(domain_id)
return domain_name

def _get_temporary_sso_access_token(self, domain_id: str) -> str:
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
system_token = config.get_global("TOKEN")
params = {
"grant_type": "SYSTEM_TOKEN",
"scope": "SYSTEM",
"token": system_token,
}
# todo : make temporary token
token = identity_mgr.grant_token(params)
return token

@staticmethod
def _get_current_and_last_month() -> Tuple[str, str]:
Expand Down Expand Up @@ -404,20 +435,6 @@ def generate_report_number(report_month: str, issue_day: int) -> str:

return f"CostReport_{date_object.strftime('%y%m%d%H%M')}"

@staticmethod
def _get_workspace_name_map(domain_id: str) -> Tuple[dict, list]:
identity_mgr = IdentityManager()
workspace_name_map = {}
workspaces = identity_mgr.list_workspaces(
{"query": {"filter": [{"k": "state", "v": "ENABLED", "o": "eq"}]}},
domain_id,
)
workspace_ids = []
for workspace in workspaces.get("results", []):
workspace_name_map[workspace["workspace_id"]] = workspace["name"]
workspace_ids.append(workspace["workspace_id"])
return workspace_name_map, workspace_ids

@staticmethod
def _get_data_source_currency_map(
data_source_filter: dict, workspace_ids: list, domain_id: str
Expand Down Expand Up @@ -463,21 +480,3 @@ def _aggregate_result_by_currency(results: list) -> list:
workspace_result_map[workspace_id] = result.copy()

return [workspace_result for workspace_result in workspace_result_map.values()]

@staticmethod
def _get_domain_name(domain_id: str) -> str:
identity_mgr = IdentityManager()
domain_name = identity_mgr.get_domain_name(domain_id)
return domain_name

def _get_temporary_sso_access_token(self, domain_id: str) -> str:
identity_mgr = IdentityManager()
system_token = config.get_global("TOKEN")
params = {
"grant_type": "SYSTEM_TOKEN",
"scope": "SYSTEM",
"token": system_token,
}
# todo : make temporary token
token = identity_mgr.grant_token(params)
return token

0 comments on commit f76c432

Please sign in to comment.