3
3
4
4
from mongoengine import QuerySet
5
5
from spaceone .core .manager import BaseManager
6
- from spaceone .core import utils
7
6
8
7
from spaceone .cost_analysis .model import DataSource
9
8
from spaceone .cost_analysis .model .data_source_account .database import DataSourceAccount
@@ -83,45 +82,28 @@ def filter_data_source_accounts(self, **conditions) -> QuerySet:
83
82
def stat_data_source_accounts (self , query : dict ) -> dict :
84
83
return self .data_source_account_model .stat (** query )
85
84
86
- def connect_cost_data (
87
- self , cost_data : dict
88
- ) -> Tuple [dict , Union [ DataSourceAccount , None ] ]:
89
- data_source_id = cost_data [ "data_source_id" ]
90
- domain_id = cost_data [ "domain_id" ]
85
+ def get_workspace_id_from_account_id (
86
+ self , domain_id : str , data_source_id : str
87
+ ) -> Tuple [str , str ]:
88
+ workspace_id = None
89
+ v_workspace_id = None
91
90
92
91
data_source_vo = self ._get_data_source (data_source_id , domain_id )
93
92
plugin_info_metadata = data_source_vo .plugin_info .metadata
94
93
95
94
use_account_routing = plugin_info_metadata .get ("use_account_routing" , False )
96
95
97
- ds_account_vo = None
98
96
if use_account_routing :
99
- account_connect_polices : list = plugin_info_metadata .get (
100
- "account_connect_polices"
101
- )
102
- for account_connect_policy in account_connect_polices :
103
- if account_connect_policy .get ("name" ) == "connect_cost_to_account" :
104
- name = account_connect_policy .get ("name" )
105
- policy = account_connect_policy ["polices" ].get (name )
106
- source = policy ["source" ]
107
- target_key = policy .get ("target" , "account_id" )
108
- target_value = utils .get_dict_value (cost_data , source )
109
- operator = policy .get ("operator" )
110
-
111
- if target_value :
112
- ds_account_vo = self ._get_data_source_account_vo (
113
- target_key ,
114
- target_value ,
115
- data_source_id ,
116
- domain_id ,
117
- operator ,
118
- )
119
-
120
- if ds_account_vo :
121
- cost_data ["account_id" ] = ds_account_vo .account_id
122
- cost_data ["workspace_id" ] = ds_account_vo .v_workspace_id
123
-
124
- return cost_data , ds_account_vo
97
+ account_match_key_value = plugin_info_metadata .get ("account_match_key" )
98
+ if account_match_key_value :
99
+ ds_account_vo = self .get_data_source_account (
100
+ data_source_id , account_match_key_value , domain_id
101
+ )
102
+
103
+ workspace_id = ds_account_vo .workspace_id
104
+ v_workspace_id = ds_account_vo .v_workspace_id
105
+
106
+ return workspace_id , v_workspace_id
125
107
126
108
def connect_account_by_data_source_vo (
127
109
self ,
@@ -130,64 +112,52 @@ def connect_account_by_data_source_vo(
130
112
) -> DataSourceAccount :
131
113
domain_id = data_source_vo .domain_id
132
114
133
- plugin_info_metadata = data_source_vo .plugin_info .metadata
134
- account_connect_polices : list = plugin_info_metadata .get (
135
- "account_connect_polices"
136
- )
137
-
138
- for account_connect_policy in account_connect_polices :
139
- if account_connect_policy .get ("name" ) == "connect_account_to_workspace" :
140
- name = account_connect_policy .get ("name" )
141
- policy = account_connect_policy ["polices" ].get (name )
142
- source = policy ["source" ]
143
- target_key = policy .get ("target" , "references" )
115
+ reference_id = data_source_account_vo .account_id
116
+ workspace_info = self ._get_workspace_by_references (reference_id , domain_id )
117
+ if workspace_info :
118
+ data_source_account_vo = self .update_data_source_account_by_vo (
119
+ {"workspace_id" : workspace_info .get ("workspace_id" )},
120
+ data_source_account_vo ,
121
+ )
144
122
145
- target_value = utils .get_dict_value (
146
- data_source_account_vo .to_dict (),
147
- source ,
148
- )
149
- operator = policy .get ("operator" )
150
-
151
- if target_value :
152
- workspace_info = self ._get_workspace (
153
- target_key , target_value , domain_id , operator
154
- )
155
-
156
- if workspace_info :
157
- data_source_account_vo = self .update_data_source_account_by_vo (
158
- {"workspace_id" : workspace_info .get ("workspace_id" )},
159
- data_source_account_vo ,
160
- )
161
123
return data_source_account_vo
162
124
163
- def _get_workspace (
164
- self , target_key : str , target_value : str , domain_id : str , operator : str = "eq"
125
+ def _get_workspace_by_references (
126
+ self , reference_id : str , domain_id : str
165
127
) -> Union [dict , None ]:
166
- if f"workspace:{ domain_id } :{ target_key } : { target_value } " in self ._workspace_info :
128
+ if f"workspace:{ domain_id } :references: { reference_id } " in self ._workspace_info :
167
129
return self ._workspace_info [
168
- f"workspace:{ domain_id } :{ target_key } : { target_value } "
130
+ f"workspace:{ domain_id } :references: { reference_id } "
169
131
]
170
132
171
133
query = {
172
134
"filter" : [
173
135
{"k" : "domain_id" , "v" : domain_id , "o" : "eq" },
136
+ {"k" : "references" , "v" : [reference_id ], "o" : "in" },
174
137
]
175
138
}
176
- if operator == "in" and not isinstance (target_value , list ):
177
- target_value = [target_value ]
178
- query ["filter" ].append ({"k" : target_key , "v" : target_value , "o" : operator })
179
139
180
140
identity_mgr = self .locator .get_manager ("IdentityManager" )
181
141
response = identity_mgr .list_workspaces ({"query" : query }, domain_id )
182
142
results = response .get ("results" , [])
183
143
total_count = response .get ("total_count" , 0 )
184
144
185
145
workspace_info = None
186
- if total_count > 0 :
146
+
147
+ if total_count == 0 :
148
+ response = identity_mgr .list_workspaces (
149
+ {"query" : {"filter" : [{"k" : "domain_id" , "v" : domain_id , "o" : "eq" }]}},
150
+ domain_id ,
151
+ )
152
+ total_count = response .get ("total_count" , 0 )
153
+ if total_count == 1 :
154
+ workspace_info = response .get ("results" )[0 ]
155
+
156
+ else :
187
157
workspace_info = results [0 ]
188
158
189
159
self ._workspace_info [
190
- f"workspace:{ domain_id } :{ target_key } : { target_value } "
160
+ f"workspace:{ domain_id } :references: { reference_id } "
191
161
] = workspace_info
192
162
193
163
return workspace_info
0 commit comments