1
1
import logging
2
2
import functools
3
3
4
+ from mongoengine import QuerySet
4
5
from spaceone .core import utils
5
6
from spaceone .core .manager import BaseManager
6
7
from spaceone .cost_analysis .manager .identity_manager import IdentityManager
8
+ from spaceone .cost_analysis .model import DataSourceAccount
7
9
from spaceone .cost_analysis .model .data_source_rule_model import (
8
10
DataSourceRule ,
9
11
DataSourceRuleCondition ,
@@ -86,7 +88,9 @@ def list_data_source_rules(self, query: dict):
86
88
def stat_data_source_rules (self , query ):
87
89
return self .data_source_rule_model .stat (** query )
88
90
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 :
90
94
data_source_id = cost_data ["data_source_id" ]
91
95
domain_id = cost_data ["domain_id" ]
92
96
(
@@ -95,31 +99,49 @@ def change_cost_data(self, cost_data):
95
99
) = self ._get_data_source_rules (data_source_id , domain_id )
96
100
97
101
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
99
103
)
100
104
101
105
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
103
107
)
104
108
105
109
return cost_data
106
110
107
111
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 ,
109
117
):
110
118
for data_source_rule_vo in data_source_rule_vos :
111
119
is_match = self ._change_cost_data_by_rule (cost_data , data_source_rule_vo )
112
120
if is_match :
113
121
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 ,
115
126
)
116
127
117
128
if is_match and data_source_rule_vo .options .stop_processing :
118
129
break
119
130
120
131
return cost_data
121
132
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
+
123
145
for action , value in actions .items ():
124
146
if action == "change_project" and value :
125
147
cost_data ["project_id" ] = value
@@ -130,7 +152,7 @@ def _change_cost_data_with_actions(self, cost_data, actions, domain_id):
130
152
target_value = utils .get_dict_value (cost_data , source )
131
153
if target_value :
132
154
project_info = self ._get_project (
133
- target_key , target_value , domain_id
155
+ target_key , target_value , domain_id , workspace_id
134
156
)
135
157
if project_info :
136
158
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):
142
164
target_value = utils .get_dict_value (cost_data , source )
143
165
if target_value :
144
166
service_account_info = self ._get_service_account (
145
- target_key , target_value , domain_id
167
+ target_key , target_value , domain_id , workspace_id
146
168
)
147
169
if service_account_info :
148
170
cost_data ["service_account_id" ] = service_account_info [
@@ -159,7 +181,9 @@ def _change_cost_data_with_actions(self, cost_data, actions, domain_id):
159
181
160
182
return cost_data
161
183
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
+ ):
163
187
if (
164
188
f"service-account:{ domain_id } :{ target_key } :{ target_value } "
165
189
in self ._service_account_info
@@ -176,6 +200,9 @@ def _get_service_account(self, target_key, target_value, domain_id):
176
200
"only" : ["service_account_id" , "project_id" , "workspace_id" ],
177
201
}
178
202
203
+ if workspace_id :
204
+ query ["filter" ].append ({"k" : "workspace_id" , "v" : workspace_id , "o" : "eq" })
205
+
179
206
identity_mgr : IdentityManager = self .locator .get_manager ("IdentityManager" )
180
207
response = identity_mgr .list_service_accounts (query , domain_id )
181
208
results = response .get ("results" , [])
@@ -190,7 +217,9 @@ def _get_service_account(self, target_key, target_value, domain_id):
190
217
] = service_account_info
191
218
return service_account_info
192
219
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
+ ):
194
223
if f"project:{ domain_id } :{ target_key } :{ target_value } " in self ._project_info :
195
224
return self ._project_info [
196
225
f"project:{ domain_id } :{ target_key } :{ target_value } "
@@ -200,6 +229,8 @@ def _get_project(self, target_key, target_value, domain_id):
200
229
"filter" : [{"k" : target_key , "v" : target_value , "o" : "eq" }],
201
230
"only" : ["project_id" ],
202
231
}
232
+ if workspace_id :
233
+ query ["filter" ].append ({"k" : "workspace_id" , "v" : workspace_id , "o" : "eq" })
203
234
204
235
identity_mgr : IdentityManager = self .locator .get_manager ("IdentityManager" )
205
236
response = identity_mgr .list_projects ({"query" : query }, domain_id )
0 commit comments