Skip to content

Commit

Permalink
feat: modify data source apis
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMin5 committed Dec 20, 2023
1 parent 7087b4e commit f8dfb8c
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 82 deletions.
10 changes: 6 additions & 4 deletions src/spaceone/cost_analysis/manager/budget_usage_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def create_budget_usages(self, budget_vo: Budget):
"currency": budget_vo.currency,
"provider_filter": budget_vo.provider_filter.to_dict(),
"budget": budget_vo,
"project_id": budget_vo.project_id,
"project_group_id": budget_vo.project_group_id,
"resource_group": budget_vo.resource_group,
"data_source_id": budget_vo.data_source_id,
"project_id": budget_vo.project_id,
"workspace_id": budget_vo.workspace_id,
"domain_id": budget_vo.domain_id,
}

Expand All @@ -65,9 +66,10 @@ def create_budget_usages(self, budget_vo: Budget):
"currency": budget_vo.currency,
"provider_filter": budget_vo.provider_filter.to_dict(),
"budget": budget_vo,
"project_id": budget_vo.project_id,
"project_group_id": budget_vo.project_group_id,
"resource_group": budget_vo.resource_group,
"data_source_id": budget_vo.data_source_id,
"project_id": budget_vo.project_id,
"workspace_id": budget_vo.workspace_id,
"domain_id": budget_vo.domain_id,
}

Expand Down
4 changes: 2 additions & 2 deletions src/spaceone/cost_analysis/manager/data_source_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def deregister_data_source(self, data_source_id, domain_id):
def deregister_data_source_by_vo(data_source_vo):
data_source_vo.delete()

def get_data_source(self, data_source_id, domain_id, workspace_id=None):
conditions = {"data_source_id": data_source_id, "domain_id": domain_id}
def get_data_source(self, data_source_id: str, workspace_id=None):
conditions = {"data_source_id": data_source_id}

if workspace_id:
conditions["workspace_id"] = workspace_id
Expand Down
125 changes: 85 additions & 40 deletions src/spaceone/cost_analysis/manager/data_source_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,147 @@

from spaceone.core.manager import BaseManager
from spaceone.cost_analysis.manager.plugin_manager import PluginManager
from spaceone.cost_analysis.manager.data_source_rule_manager import DataSourceRuleManager
from spaceone.cost_analysis.service.data_source_rule_service import DataSourceRuleService
from spaceone.cost_analysis.connector.datasource_plugin_connector import DataSourcePluginConnector
from spaceone.cost_analysis.manager.data_source_rule_manager import (
DataSourceRuleManager,
)
from spaceone.cost_analysis.service.data_source_rule_service import (
DataSourceRuleService,
)
from spaceone.cost_analysis.connector.datasource_plugin_connector import (
DataSourcePluginConnector,
)
from spaceone.cost_analysis.model.data_source_model import DataSource

_LOGGER = logging.getLogger(__name__)


class DataSourcePluginManager(BaseManager):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dsp_connector: DataSourcePluginConnector = self.locator.get_connector('DataSourcePluginConnector')
self.dsp_connector: DataSourcePluginConnector = self.locator.get_connector(
"DataSourcePluginConnector"
)

def initialize(self, endpoint):
_LOGGER.debug(f'[initialize] data source plugin endpoint: {endpoint}')
_LOGGER.debug(f"[initialize] data source plugin endpoint: {endpoint}")
self.dsp_connector.initialize(endpoint)

def init_plugin(self, options, domain_id):
plugin_info = self.dsp_connector.init(options, domain_id)

_LOGGER.debug(f'[plugin_info] {plugin_info}')
plugin_metadata = plugin_info.get('metadata', {})
_LOGGER.debug(f"[plugin_info] {plugin_info}")
plugin_metadata = plugin_info.get("metadata", {})

return plugin_metadata

def verify_plugin(self, options, secret_data, schema, domain_id):
self.dsp_connector.verify(options, secret_data, schema, domain_id)

def get_tasks(self, options, secret_id, secret_data, schema, start, last_synchronized_at, domain_id):
response = self.dsp_connector.get_tasks(options, secret_data, schema, domain_id, start, last_synchronized_at)
tasks = response.get('tasks', [])
def get_tasks(
self,
options,
secret_id,
secret_data,
schema,
start,
last_synchronized_at,
domain_id,
):
response = self.dsp_connector.get_tasks(
options, secret_data, schema, domain_id, start, last_synchronized_at
)
tasks = response.get("tasks", [])

for task in tasks:
task.update({
'secret_id': secret_id,
'schema': schema
})
task.update({"secret_id": secret_id, "schema": schema})

return tasks, response.get('changed', [])
return tasks, response.get("changed", [])

def get_cost_data(self, options, secret_data, schema, task_options, domain_id):
return self.dsp_connector.get_cost_data(options, secret_data, schema, task_options, domain_id)
return self.dsp_connector.get_cost_data(
options, secret_data, schema, task_options, domain_id
)

def get_data_source_plugin_endpoint_by_vo(self, data_source_vo: DataSource):
plugin_info = data_source_vo.plugin_info.to_dict()
endpoint, updated_version = self.get_data_source_plugin_endpoint(plugin_info, data_source_vo.domain_id)
endpoint, updated_version = self.get_data_source_plugin_endpoint(
plugin_info, data_source_vo.domain_id
)

if updated_version:
_LOGGER.debug(
f'[get_data_source_plugin_endpoint_by_vo] upgrade plugin version: {plugin_info["version"]} -> {updated_version}')
self.upgrade_data_source_plugin_version(data_source_vo, endpoint, updated_version)
f'[get_data_source_plugin_endpoint_by_vo] upgrade plugin version: {plugin_info["version"]} -> {updated_version}'
)
self.upgrade_data_source_plugin_version(
data_source_vo, endpoint, updated_version
)

return endpoint

def get_data_source_plugin_endpoint(self, plugin_info, domain_id):
plugin_mgr: PluginManager = self.locator.get_manager('PluginManager')
plugin_mgr: PluginManager = self.locator.get_manager("PluginManager")
return plugin_mgr.get_plugin_endpoint(plugin_info, domain_id)

def upgrade_data_source_plugin_version(self, data_source_vo: DataSource, endpoint, updated_version):
def upgrade_data_source_plugin_version(
self, data_source_vo: DataSource, endpoint, updated_version
):
plugin_info = data_source_vo.plugin_info.to_dict()

resource_group = data_source_vo.resource_group
data_source_id = data_source_vo.data_source_id
workspace_id = data_source_vo.workspace_id
domain_id = data_source_vo.domain_id

self.initialize(endpoint)

plugin_options = plugin_info.get('options', {})
plugin_options = plugin_info.get("options", {})

plugin_metadata = self.init_plugin(plugin_options, domain_id)
plugin_info['version'] = updated_version
plugin_info['metadata'] = plugin_metadata
data_source_vo.update({'plugin_info': plugin_info})
plugin_info["version"] = updated_version
plugin_info["metadata"] = plugin_metadata
data_source_vo.update({"plugin_info": plugin_info})

self.delete_data_source_rules(data_source_id, domain_id)
self.create_data_source_rules_by_metadata(plugin_metadata, data_source_id, domain_id)
self.create_data_source_rules_by_metadata(
plugin_metadata, resource_group, data_source_id, workspace_id, domain_id
)

def delete_data_source_rules(self, data_source_id, domain_id):
_LOGGER.debug(f'[_delete_data_source_rules] delete all data source rules: {data_source_id}')
data_source_rule_mgr: DataSourceRuleManager = self.locator.get_manager('DataSourceRuleManager')
old_data_source_rule_vos = data_source_rule_mgr.filter_data_source_rules(data_source_id=data_source_id,
rule_type='MANAGED',
domain_id=domain_id)
_LOGGER.debug(
f"[_delete_data_source_rules] delete all data source rules: {data_source_id}"
)
data_source_rule_mgr: DataSourceRuleManager = self.locator.get_manager(
"DataSourceRuleManager"
)
old_data_source_rule_vos = data_source_rule_mgr.filter_data_source_rules(
data_source_id=data_source_id, rule_type="MANAGED", domain_id=domain_id
)

old_data_source_rule_vos.delete()

def create_data_source_rules_by_metadata(self, metadata, data_source_id, domain_id):
data_source_rules = metadata.get('data_source_rules', [])
def create_data_source_rules_by_metadata(
self,
metadata: dict,
resource_group: str,
data_source_id: str,
workspace_id,
domain_id: str,
):
data_source_rules = metadata.get("data_source_rules", [])

if len(data_source_rules) > 0:
_LOGGER.debug(f'[_create_data_source_rules_by_metadata] create data source rules: {data_source_id} / '
f'rule count = {len(data_source_rules)}')
_LOGGER.debug(
f"[_create_data_source_rules_by_metadata] create data source rules: {data_source_id} / "
f"rule count = {len(data_source_rules)}"
)

data_source_rule_svc: DataSourceRuleService = self.locator.get_service('DataSourceRuleService', metadata)
data_source_rule_svc: DataSourceRuleService = self.locator.get_service(
"DataSourceRuleService", metadata
)
for data_source_rule_params in data_source_rules:
data_source_rule_params['data_source_id'] = data_source_id
data_source_rule_params['domain_id'] = domain_id
data_source_rule_params['rule_type'] = 'MANAGED'
data_source_rule_params["resource_group"] = resource_group
data_source_rule_params["data_source_id"] = data_source_id
data_source_rule_params["workspace_id"] = workspace_id
data_source_rule_params["domain_id"] = domain_id
data_source_rule_params["rule_type"] = "MANAGED"
data_source_rule_svc.create_data_source_rule(data_source_rule_params)
38 changes: 20 additions & 18 deletions src/spaceone/cost_analysis/service/budget_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__(self, *args, **kwargs):
"start",
"end",
"resource_group",
"workspace_id",
"domain_id",
]
)
Expand All @@ -45,17 +44,17 @@ def create(self, params):
Args:
params (dict): {
'data_source_id': 'str',
'data_source_id': 'str', # required
'name': 'str',
'limit': 'float',
'planned_limits': 'list',
'time_unit': 'str',
'start': 'str',
'end': 'str',
'time_unit': 'str', # required
'start': 'str', # required
'end': 'str', # required
'provider_filter': 'dict',
'notifications': 'list',
'tags': 'dict',
'resource_group': 'str',
'resource_group': 'str', # required
'project_id': 'str',
'workspace_id': 'str', # injected from auth
'domain_id': 'str' # injected from auth
Expand All @@ -81,7 +80,10 @@ def create(self, params):
# self._check_target(project_id, project_group_id, domain_id)
self._check_time_period(start, end)

if resource_group == "PROJECT":
if resource_group == "WORKSPACE":
params["project_id"] = "*"

else:
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
identity_mgr.get_project(project_id)

Expand All @@ -95,9 +97,7 @@ def create(self, params):
data_source_mgr: DataSourceManager = self.locator.get_manager(
"DataSourceManager"
)
data_source_vo: DataSource = data_source_mgr.get_data_source(
data_source_id, domain_id
)
data_source_vo: DataSource = data_source_mgr.get_data_source(data_source_id)
data_source_metadata = data_source_vo.plugin_info.metadata
params["currency"] = data_source_metadata.get("currency", "USD")

Expand Down Expand Up @@ -177,7 +177,9 @@ def update(self, params):
)

budget_vo: Budget = self.budget_mgr.get_budget(
budget_id, workspace_id, domain_id
budget_id,
domain_id,
workspace_id,
)

# Check limit and Planned Limits
Expand Down Expand Up @@ -223,7 +225,7 @@ def set_notification(self, params):
notifications = params.get("notifications", [])

budget_vo: Budget = self.budget_mgr.get_budget(
budget_id, workspace_id, domain_id
budget_id, domain_id, workspace_id
)

# Check Notifications
Expand All @@ -242,9 +244,9 @@ def delete(self, params):
Args:
params (dict): {
'budget_id': 'str',
'workspace_id': 'str',
'domain_id': 'str'
'budget_id': 'str', # required
'workspace_id': 'str', # injected from auth (optional)
'domain_id': 'str' # injected from auth
}
Returns:
Expand All @@ -259,7 +261,6 @@ def delete(self, params):
permission="cost-analysis:Budget.read",
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"],
)
@change_value_by_rule("APPEND", "workspace_id", "*")
@check_required(["budget_id", "domain_id"])
def get(self, params):
"""Get budget
Expand All @@ -286,6 +287,7 @@ def get(self, params):
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"],
)
@check_required(["domain_id"])
@change_value_by_rule("APPEND", "workspace_id", "*")
@append_query_filter(
[
"budget_id",
Expand All @@ -307,11 +309,10 @@ def list(self, params):
'budget_id': 'str',
'name': 'str',
'time_unit': 'str',
'project_group_id': 'str',
'data_source_id': 'str',
'project_id': 'str',
'workspace_id': 'str',
'domain_id': 'str',
'data_source_id': 'str',
}
Returns:
Expand All @@ -326,6 +327,7 @@ def list(self, params):
permission="cost-analysis:Budget.read",
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"],
)
@change_value_by_rule("APPEND", "workspace_id", "*")
@check_required(["query", "domain_id"])
@append_query_filter(["domain_id"])
@append_keyword_filter(["budget_id", "name"])
Expand Down
2 changes: 2 additions & 0 deletions src/spaceone/cost_analysis/service/budget_usage_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@mutation_handler
@event_handler
class BudgetUsageService(BaseService):
resource = "BudgetUsage"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.budget_usage_mgr: BudgetUsageManager = self.locator.get_manager(
Expand Down
2 changes: 2 additions & 0 deletions src/spaceone/cost_analysis/service/cost_query_set_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@mutation_handler
@event_handler
class CostQuerySetService(BaseService):
resource = "CostQuerySet"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cost_query_set_mgr: CostQuerySetManager = self.locator.get_manager(
Expand Down
2 changes: 2 additions & 0 deletions src/spaceone/cost_analysis/service/cost_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@mutation_handler
@event_handler
class CostService(BaseService):
resource = "Cost"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cost_mgr: CostManager = self.locator.get_manager("CostManager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
@mutation_handler
@event_handler
class DataSourceRuleService(BaseService):
resource = "DataSourceRule"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.data_source_rule_mgr: DataSourceRuleManager = self.locator.get_manager(
Expand Down
Loading

0 comments on commit f8dfb8c

Please sign in to comment.