Skip to content

Commit 2ad9eb6

Browse files
committed
feat: modify change cost data logic (#199)
1 parent a99c24d commit 2ad9eb6

File tree

4 files changed

+119
-80
lines changed

4 files changed

+119
-80
lines changed

src/spaceone/cost_analysis/manager/cost_manager.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ def _rollback(vo: Cost):
4646
params["billed_year"] = billed_at.strftime("%Y")
4747
params["billed_month"] = billed_at.strftime("%Y-%m")
4848

49-
params, use_account_routing = self.data_source_account_mgr.connect_cost_data(
50-
params
51-
)
52-
if not use_account_routing:
53-
params = self.data_source_rule_mgr.change_cost_data(params)
49+
params, ds_account_vo = self.data_source_account_mgr.connect_cost_data(params)
50+
51+
params = self.data_source_rule_mgr.change_cost_data(params, ds_account_vo)
5452

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

src/spaceone/cost_analysis/manager/data_source_account_manager.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def filter_data_source_accounts(self, **conditions) -> QuerySet:
8383
def stat_data_source_accounts(self, query: dict) -> dict:
8484
return self.data_source_account_model.stat(**query)
8585

86-
def connect_cost_data(self, cost_data: dict) -> Tuple[dict, bool]:
86+
def connect_cost_data(
87+
self, cost_data: dict
88+
) -> Tuple[dict, Union[DataSourceAccount, None]]:
8789
data_source_id = cost_data["data_source_id"]
8890
domain_id = cost_data["domain_id"]
8991

@@ -92,6 +94,7 @@ def connect_cost_data(self, cost_data: dict) -> Tuple[dict, bool]:
9294

9395
use_account_routing = plugin_info_metadata.get("user_account_routing", False)
9496

97+
ds_account_vo = None
9598
if use_account_routing:
9699
account_connect_polices: list = plugin_info_metadata.get(
97100
"account_connect_polices"
@@ -107,25 +110,23 @@ def connect_cost_data(self, cost_data: dict) -> Tuple[dict, bool]:
107110
operator = polices["connect_account_to_workspace"].get("operator")
108111

109112
if target_value:
110-
ds_account_info = self._get_data_source_account(
113+
ds_account_vo = self._get_data_source_account_vo(
111114
target_key,
112115
target_value,
113116
data_source_id,
114117
domain_id,
115118
operator,
116119
)
117-
if ds_account_info:
118-
cost_data.update(
119-
{"account_id": ds_account_info.get("account_id")}
120-
)
120+
if ds_account_vo:
121+
cost_data.update({"account_id": ds_account_vo.account_id})
121122

122-
return cost_data, use_account_routing
123+
return cost_data, ds_account_vo
123124

124125
def connect_account_by_data_source_vo(
125126
self,
126127
data_source_account_vo: DataSourceAccount,
127128
data_source_vo: DataSource,
128-
) -> None:
129+
) -> DataSourceAccount:
129130
domain_id = data_source_vo.domain_id
130131

131132
plugin_info_metadata = data_source_vo.plugin_info.metadata
@@ -153,10 +154,11 @@ def connect_account_by_data_source_vo(
153154
)
154155

155156
if workspace_info:
156-
self.update_data_source_account_by_vo(
157+
data_source_account_vo = self.update_data_source_account_by_vo(
157158
{"workspace_id": workspace_info.get("workspace_id")},
158159
data_source_account_vo,
159160
)
161+
return data_source_account_vo
160162

161163
def _get_workspace(
162164
self, target_key: str, target_value: str, domain_id: str, operator: str = "eq"
@@ -203,14 +205,14 @@ def _get_data_source(self, data_source_id: str, domain_id: str) -> DataSource:
203205

204206
return data_source_vo
205207

206-
def _get_data_source_account(
208+
def _get_data_source_account_vo(
207209
self,
208210
target_key: str,
209211
target_value: str,
210212
data_source_id: str,
211213
domain_id: str,
212214
operator: str = "eq",
213-
) -> Union[dict, None]:
215+
) -> Union[DataSourceAccount, None]:
214216
query = {
215217
"filter": [
216218
{"k": "domain_id", "v": domain_id, "o": "eq"},
@@ -220,8 +222,8 @@ def _get_data_source_account(
220222
}
221223

222224
data_source_account_vos, total_count = self.list_data_source_accounts(query)
223-
data_source_account_info = None
225+
data_source_account_vo = None
224226
if total_count > 0:
225-
data_source_account_info = data_source_account_vos[0].to_dict()
227+
data_source_account_vo = data_source_account_vos[0]
226228

227-
return data_source_account_info
229+
return data_source_account_vo

src/spaceone/cost_analysis/manager/data_source_rule_manager.py

+41-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import logging
22
import functools
33

4+
from mongoengine import QuerySet
45
from spaceone.core import utils
56
from spaceone.core.manager import BaseManager
67
from spaceone.cost_analysis.manager.identity_manager import IdentityManager
8+
from spaceone.cost_analysis.model import DataSourceAccount
79
from spaceone.cost_analysis.model.data_source_rule_model import (
810
DataSourceRule,
911
DataSourceRuleCondition,
@@ -86,7 +88,9 @@ def list_data_source_rules(self, query: dict):
8688
def stat_data_source_rules(self, query):
8789
return self.data_source_rule_model.stat(**query)
8890

89-
def change_cost_data(self, cost_data):
91+
def change_cost_data(
92+
self, cost_data: dict, data_source_account_vo: DataSourceAccount = None
93+
) -> dict:
9094
data_source_id = cost_data["data_source_id"]
9195
domain_id = cost_data["domain_id"]
9296
(
@@ -95,31 +99,49 @@ def change_cost_data(self, cost_data):
9599
) = self._get_data_source_rules(data_source_id, domain_id)
96100

97101
cost_data = self._apply_data_source_rule_to_cost_data(
98-
cost_data, managed_data_source_rule_vos, domain_id
102+
cost_data, managed_data_source_rule_vos, domain_id, data_source_account_vo
99103
)
100104

101105
cost_data = self._apply_data_source_rule_to_cost_data(
102-
cost_data, custom_data_source_rule_vos, domain_id
106+
cost_data, custom_data_source_rule_vos, domain_id, data_source_account_vo
103107
)
104108

105109
return cost_data
106110

107111
def _apply_data_source_rule_to_cost_data(
108-
self, cost_data, data_source_rule_vos, domain_id
112+
self,
113+
cost_data: dict,
114+
data_source_rule_vos: QuerySet,
115+
domain_id: str,
116+
data_source_account_vo: DataSourceAccount = None,
109117
):
110118
for data_source_rule_vo in data_source_rule_vos:
111119
is_match = self._change_cost_data_by_rule(cost_data, data_source_rule_vo)
112120
if is_match:
113121
cost_data = self._change_cost_data_with_actions(
114-
cost_data, data_source_rule_vo.actions, domain_id
122+
cost_data,
123+
data_source_rule_vo.actions,
124+
domain_id,
125+
data_source_account_vo,
115126
)
116127

117128
if is_match and data_source_rule_vo.options.stop_processing:
118129
break
119130

120131
return cost_data
121132

122-
def _change_cost_data_with_actions(self, cost_data, actions, domain_id):
133+
def _change_cost_data_with_actions(
134+
self,
135+
cost_data: dict,
136+
actions: dict,
137+
domain_id: str,
138+
data_source_account_vo: DataSourceAccount = None,
139+
):
140+
if data_source_account_vo:
141+
workspace_id = data_source_account_vo.workspace_id
142+
else:
143+
workspace_id = None
144+
123145
for action, value in actions.items():
124146
if action == "change_project" and value:
125147
cost_data["project_id"] = value
@@ -130,7 +152,7 @@ def _change_cost_data_with_actions(self, cost_data, actions, domain_id):
130152
target_value = utils.get_dict_value(cost_data, source)
131153
if target_value:
132154
project_info = self._get_project(
133-
target_key, target_value, domain_id
155+
target_key, target_value, domain_id, workspace_id
134156
)
135157
if project_info:
136158
cost_data["workspace_id"] = project_info.get("workspace_id")
@@ -142,7 +164,7 @@ def _change_cost_data_with_actions(self, cost_data, actions, domain_id):
142164
target_value = utils.get_dict_value(cost_data, source)
143165
if target_value:
144166
service_account_info = self._get_service_account(
145-
target_key, target_value, domain_id
167+
target_key, target_value, domain_id, workspace_id
146168
)
147169
if service_account_info:
148170
cost_data["service_account_id"] = service_account_info[
@@ -159,7 +181,9 @@ def _change_cost_data_with_actions(self, cost_data, actions, domain_id):
159181

160182
return cost_data
161183

162-
def _get_service_account(self, target_key, target_value, domain_id):
184+
def _get_service_account(
185+
self, target_key, target_value, domain_id: str, workspace_id: str = None
186+
):
163187
if (
164188
f"service-account:{domain_id}:{target_key}:{target_value}"
165189
in self._service_account_info
@@ -176,6 +200,9 @@ def _get_service_account(self, target_key, target_value, domain_id):
176200
"only": ["service_account_id", "project_id", "workspace_id"],
177201
}
178202

203+
if workspace_id:
204+
query["filter"].append({"k": "workspace_id", "v": workspace_id, "o": "eq"})
205+
179206
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
180207
response = identity_mgr.list_service_accounts(query, domain_id)
181208
results = response.get("results", [])
@@ -190,7 +217,9 @@ def _get_service_account(self, target_key, target_value, domain_id):
190217
] = service_account_info
191218
return service_account_info
192219

193-
def _get_project(self, target_key, target_value, domain_id):
220+
def _get_project(
221+
self, target_key, target_value, domain_id: str, workspace_id: str = None
222+
):
194223
if f"project:{domain_id}:{target_key}:{target_value}" in self._project_info:
195224
return self._project_info[
196225
f"project:{domain_id}:{target_key}:{target_value}"
@@ -200,6 +229,8 @@ def _get_project(self, target_key, target_value, domain_id):
200229
"filter": [{"k": target_key, "v": target_value, "o": "eq"}],
201230
"only": ["project_id"],
202231
}
232+
if workspace_id:
233+
query["filter"].append({"k": "workspace_id", "v": workspace_id, "o": "eq"})
203234

204235
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
205236
response = identity_mgr.list_projects({"query": query}, domain_id)

0 commit comments

Comments
 (0)