Skip to content

Commit

Permalink
feat: add feature for DataSourceAccount (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMin5 committed Apr 24, 2024
1 parent 7187bcd commit 9525480
Show file tree
Hide file tree
Showing 23 changed files with 964 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def get_tasks(
domain_id: str,
start: str = None,
last_synchronized_at: str = None,
linked_accounts: list = None,
):
params = {
"options": self.options or options,
Expand All @@ -64,8 +65,23 @@ def get_tasks(
"last_synchronized_at": last_synchronized_at,
"domain_id": domain_id,
}
if linked_accounts:
params["linked_accounts"] = linked_accounts

return self.client.dispatch("Job.get_tasks", params)

def get_linked_accounts(
self, options: dict, secret_data: dict, domain_id: str, schema: dict = None
) -> dict:
params = {
"options": options,
"secret_data": secret_data,
"schema": schema,
"domain_id": domain_id,
}

return self.client.dispatch("Cost.get_linked_accounts", params)

def get_cost_data(self, options, secret_data, schema, task_options, domain_id):
params = {
"options": self.options or options,
Expand Down
19 changes: 18 additions & 1 deletion src/spaceone/cost_analysis/info/job_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from spaceone.api.cost_analysis.v1 import job_pb2
from spaceone.core.pygrpc.message_type import *
from spaceone.core import utils
from spaceone.cost_analysis.model.job_model import Job, Changed
from spaceone.cost_analysis.model.job_model import Job, Changed, SyncedAccount

__all__ = ["JobInfo", "JobsInfo"]

Expand All @@ -26,6 +26,22 @@ def ChangedInfo(changed_vos: List[Changed]):
return changed_info


def SyncedAccountInfo(synced_account_vos: List[SyncedAccount]):
if synced_account_vos is None:
synced_account_vos = []

synced_account_info = []

for vo in synced_account_vos:
info = {
"account_id": vo.account_id,
}

synced_account_info.append(job_pb2.SyncedAccountInfo(**info))

return synced_account_info


def JobInfo(job_vo: Job, minimal=False):
info = {
"job_id": job_vo.job_id,
Expand All @@ -45,6 +61,7 @@ def JobInfo(job_vo: Job, minimal=False):
"resource_group": job_vo.resource_group,
"domain_id": job_vo.domain_id,
"changed": ChangedInfo(job_vo.changed),
"synced_accounts": SyncedAccountInfo(job_vo.synced_accounts),
"created_at": utils.datetime_to_iso8601(job_vo.created_at),
"updated_at": utils.datetime_to_iso8601(job_vo.updated_at),
"finished_at": utils.datetime_to_iso8601(job_vo.finished_at),
Expand Down
2 changes: 2 additions & 0 deletions src/spaceone/cost_analysis/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .cost import Cost
from .cost_query_set import CostQuerySet
from .data_source import DataSource
from .data_source_account import DataSourceAccount
from .data_source_rule import DataSourceRule
from .job import Job
from .job_task import JobTask
Expand All @@ -22,6 +23,7 @@
app.add_service(CostReportData)
app.add_service(CostQuerySet)
app.add_service(DataSource)
app.add_service(DataSourceAccount)
app.add_service(DataSourceRule)
app.add_service(Job)
app.add_service(JobTask)
50 changes: 50 additions & 0 deletions src/spaceone/cost_analysis/interface/grpc/data_source_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import logging

from spaceone.api.cost_analysis.v1 import (
data_source_account_pb2,
data_source_account_pb2_grpc,
)
from spaceone.core.pygrpc import BaseAPI

from spaceone.cost_analysis.service.data_source_account_service import (
DataSourceAccountService,
)

_LOGGER = logging.getLogger(__name__)


class DataSourceAccount(
BaseAPI, data_source_account_pb2_grpc.DataSourceAccountServicer
):
pb2 = data_source_account_pb2
pb2_grpc = data_source_account_pb2_grpc

def update(self, request, context):
params, metadata = self.parse_request(request, context)
data_source_account_svc = DataSourceAccountService(metadata)
response: dict = data_source_account_svc.update(params)
return self.dict_to_message(response)

def reset(self, request, context):
params, metadata = self.parse_request(request, context)
data_source_account_svc = DataSourceAccountService(metadata)
response: dict = data_source_account_svc.reset(params)
return self.dict_to_message(response)

def get(self, request, context):
params, metadata = self.parse_request(request, context)
data_source_account_svc = DataSourceAccountService(metadata)
response: dict = data_source_account_svc.get(params)
return self.dict_to_message(response)

def list(self, request, context):
params, metadata = self.parse_request(request, context)
data_source_account_svc = DataSourceAccountService(metadata)
response: dict = data_source_account_svc.list(params)
return self.dict_to_message(response)

def stat(self, request, context):
params, metadata = self.parse_request(request, context)
data_source_account_svc = DataSourceAccountService(metadata)
response: dict = data_source_account_svc.stat(params)
return self.dict_to_message(response)
3 changes: 3 additions & 0 deletions src/spaceone/cost_analysis/manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from spaceone.cost_analysis.manager.data_source_manager import DataSourceManager
from spaceone.cost_analysis.manager.data_source_account_manager import (
DataSourceAccountManager,
)
from spaceone.cost_analysis.manager.data_source_rule_manager import (
DataSourceRuleManager,
)
Expand Down
20 changes: 17 additions & 3 deletions src/spaceone/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from spaceone.cost_analysis.manager.data_source_rule_manager import (
DataSourceRuleManager,
)
from spaceone.cost_analysis.manager.data_source_account_manager import (
DataSourceAccountManager,
)
from spaceone.cost_analysis.manager.identity_manager import IdentityManager

_LOGGER = logging.getLogger(__name__)
Expand All @@ -26,6 +29,9 @@ def __init__(self, *args, **kwargs):
self.data_source_rule_mgr: DataSourceRuleManager = self.locator.get_manager(
"DataSourceRuleManager"
)
self.data_source_account_mgr: DataSourceAccountManager = (
self.locator.get_manager("DataSourceAccountManager")
)

def create_cost(self, params: dict, execute_rollback=True):
def _rollback(vo: Cost):
Expand All @@ -40,7 +46,11 @@ def _rollback(vo: Cost):
params["billed_year"] = billed_at.strftime("%Y")
params["billed_month"] = billed_at.strftime("%Y-%m")

params = self.data_source_rule_mgr.change_cost_data(params)
params, use_account_routing = self.data_source_account_mgr.connect_cost_data(
params
)
if not use_account_routing:
params = self.data_source_rule_mgr.change_cost_data(params)

cost_vo: Cost = self.cost_model.create(params)

Expand All @@ -60,7 +70,7 @@ def delete_cost(self, cost_id, domain_id):
def delete_cost_by_vo(cost_vo: Cost):
cost_vo.delete()

def delete_cost_with_datasource(self, domain_id, data_source_id):
def delete_cost_with_datasource(self, domain_id: str, data_source_id: str) -> None:
_LOGGER.debug(f"[delete_cost_with_datasource] data_source_id: {data_source_id}")
cost_vos = self.cost_model.filter(
domain_id=domain_id, data_source_id=data_source_id
Expand All @@ -78,7 +88,11 @@ def delete_cost_with_datasource(self, domain_id, data_source_id):
history_vos.delete()

def get_cost(
self, cost_id, domain_id, workspace_id=None, user_projects: list = None
self,
cost_id: str,
domain_id: str,
workspace_id=None,
user_projects: list = None,
):
conditions = {"cost_id": cost_id, "domain_id": domain_id}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,22 @@ def delete_cost_report_config_by_vo(
) -> None:
cost_report_config_vo.delete()

# TODO: Business Logic
def run_cost_report_config(self, cost_report_config_vo: CostReportConfig) -> None:
pass

def get_cost_report_config(
self, domain_id: str, cost_report_config_id: str
) -> CostReportConfig:
return self.cost_report_config_model.get(
domain_id=domain_id, cost_report_config_id=cost_report_config_id
)

def list_cost_report_config(self, query: dict, domain_id) -> Tuple[QuerySet, int]:
def list_cost_report_configs(self, query: dict, domain_id) -> Tuple[QuerySet, int]:
self._create_default_cost_report_config(domain_id)

return self.cost_report_config_model.query(**query)

def filter_cost_report_configs(self, **conditions) -> QuerySet:
return self.cost_report_config_model.filter(**conditions)

def stat_cost_report_config(self, query: dict) -> dict:
def stat_cost_report_configs(self, query: dict) -> dict:
return self.cost_report_config_model.stat(**query)

def _create_default_cost_report_config(self, domain_id):
Expand Down
Loading

0 comments on commit 9525480

Please sign in to comment.