Skip to content

Commit a99c24d

Browse files
committed
fix: fix connect account policy logic (#199)
1 parent 3555353 commit a99c24d

File tree

3 files changed

+75
-68
lines changed

3 files changed

+75
-68
lines changed

src/spaceone/cost_analysis/manager/data_source_account_manager.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ def _rollback(vo: DataSourceAccount):
2626
)
2727
vo.delete()
2828

29-
params.update(
30-
{
31-
"v_workspace_id": utils.generate_id("workspace"),
32-
}
33-
)
34-
3529
data_source_account_vo = self.data_source_account_model.create(params)
3630
self.transaction.add_rollback(_rollback, data_source_account_vo)
3731

@@ -104,10 +98,13 @@ def connect_cost_data(self, cost_data: dict) -> Tuple[dict, bool]:
10498
)
10599
for account_connect_policy in account_connect_polices:
106100
if account_connect_policy.get("name") == "connect_cost_to_account":
107-
source = account_connect_policy.get("source")
108-
target_key = account_connect_policy.get("target", "account_id")
101+
polices = account_connect_policy.get("polices")
102+
source = polices["connect_account_to_workspace"].get("source")
103+
target_key = polices["connect_account_to_workspace"].get(
104+
"target", "account_id"
105+
)
109106
target_value = utils.get_dict_value(cost_data, source)
110-
operator = account_connect_policy.get("operator")
107+
operator = polices["connect_account_to_workspace"].get("operator")
111108

112109
if target_value:
113110
ds_account_info = self._get_data_source_account(
@@ -138,17 +135,23 @@ def connect_account_by_data_source_vo(
138135

139136
for account_connect_policy in account_connect_polices:
140137
if account_connect_policy.get("name") == "connect_account_to_workspace":
141-
source = account_connect_policy.get("source")
142-
target_key = account_connect_policy.get("target", "references")
138+
polices = account_connect_policy.get("polices")
139+
source = polices["connect_account_to_workspace"].get("source")
140+
target_key = polices["connect_account_to_workspace"].get(
141+
"target", "references"
142+
)
143+
143144
target_value = utils.get_dict_value(
144-
data_source_account_vo.to_dict(), source
145+
data_source_account_vo.to_dict(),
146+
source,
145147
)
146-
operator = account_connect_policy.get("operator")
148+
operator = polices["connect_account_to_workspace"].get("operator")
147149

148150
if target_value:
149151
workspace_info = self._get_workspace(
150152
target_key, target_value, domain_id, operator
151153
)
154+
152155
if workspace_info:
153156
self.update_data_source_account_by_vo(
154157
{"workspace_id": workspace_info.get("workspace_id")},
@@ -158,8 +161,10 @@ def connect_account_by_data_source_vo(
158161
def _get_workspace(
159162
self, target_key: str, target_value: str, domain_id: str, operator: str = "eq"
160163
) -> Union[dict, None]:
161-
if f"workspace:{domain_id}:{target_key}" in self._workspace_info:
162-
return self._workspace_info[f"workspace:{domain_id}:{target_key}"]
164+
if f"workspace:{domain_id}:{target_key}:{target_value}" in self._workspace_info:
165+
return self._workspace_info[
166+
f"workspace:{domain_id}:{target_key}:{target_value}"
167+
]
163168

164169
query = {
165170
"filter": [
@@ -171,15 +176,17 @@ def _get_workspace(
171176
query["filter"].append({"k": target_key, "v": target_value, "o": operator})
172177

173178
identity_mgr = self.locator.get_manager("IdentityManager")
174-
response = identity_mgr.list_workspaces(query, domain_id)
179+
response = identity_mgr.list_workspaces({"query": query}, domain_id)
175180
results = response.get("results", [])
176181
total_count = response.get("total_count", 0)
177182

178183
workspace_info = None
179184
if total_count > 0:
180185
workspace_info = results[0]
181186

182-
self._workspace_info[f"workspace:{domain_id}:{target_key}"] = workspace_info
187+
self._workspace_info[
188+
f"workspace:{domain_id}:{target_key}:{target_value}"
189+
] = workspace_info
183190

184191
return workspace_info
185192

src/spaceone/cost_analysis/model/data_source_account/database.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class DataSourceAccount(MongoModel):
1111
name = StringField(max_length=255)
1212
is_sync = BooleanField(default=False)
1313
v_service_account_id = StringField(max_length=40, generate_id="v_sa", unique=True)
14-
v_project_id = StringField(max_length=40, generate_id="v_project", required=True)
14+
v_project_id = StringField(max_length=40, generate_id="v_project", unique=True)
1515
v_workspace_id = StringField(
16-
max_length=40, generate_id="v_workspace_id", required=True
16+
max_length=40, generate_id="v_workspace_id", unique=True
1717
)
1818
service_account_id = StringField(max_length=255, default=None, null=True)
1919
project_id = StringField(max_length=255, default=None, null=True)

src/spaceone/cost_analysis/service/job_service.py

+49-49
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def get_cost_data(self, params):
260260
is_canceled = False
261261

262262
for costs_data in self.ds_plugin_mgr.get_cost_data(
263-
options, secret_data, schema, task_options, domain_id
263+
options, secret_data, schema, task_options, domain_id
264264
):
265265
results = costs_data.get("results", [])
266266
for cost_data in results:
@@ -434,11 +434,11 @@ def create_cost_job(self, data_source_vo: DataSource, job_options):
434434
return job_vo
435435

436436
def _list_secret_ids_from_secret_type(
437-
self,
438-
data_source_vo: DataSource,
439-
secret_type: str,
440-
workspace_id: str,
441-
domain_id: str,
437+
self,
438+
data_source_vo: DataSource,
439+
secret_type: str,
440+
workspace_id: str,
441+
domain_id: str,
442442
):
443443
secret_ids = []
444444

@@ -459,7 +459,7 @@ def _list_secret_ids_from_secret_type(
459459
return secret_ids
460460

461461
def _list_secret_ids_from_secret_filter(
462-
self, secret_filter, provider: str, workspace_id: str, domain_id: str
462+
self, secret_filter, provider: str, workspace_id: str, domain_id: str
463463
):
464464
secret_manager: SecretManager = self.locator.get_manager(SecretManager)
465465

@@ -474,7 +474,7 @@ def _list_secret_ids_from_secret_filter(
474474

475475
@staticmethod
476476
def _set_secret_filter(
477-
secret_filter, provider: str, workspace_id: str, domain_id: str
477+
secret_filter, provider: str, workspace_id: str, domain_id: str
478478
):
479479
_filter = [{"k": "domain_id", "v": domain_id, "o": "eq"}]
480480

@@ -489,8 +489,8 @@ def _set_secret_filter(
489489
{"k": "secret_id", "v": secret_filter["secrets"], "o": "in"}
490490
)
491491
if (
492-
"service_accounts" in secret_filter
493-
and secret_filter["service_accounts"]
492+
"service_accounts" in secret_filter
493+
and secret_filter["service_accounts"]
494494
):
495495
_filter.append(
496496
{
@@ -586,10 +586,10 @@ def _create_cost_data(self, cost_data, job_task_vo, cost_options):
586586
self.cost_mgr.create_cost(cost_data, execute_rollback=False)
587587

588588
def _is_job_failed(
589-
self,
590-
job_id: str,
591-
domain_id: str,
592-
workspace_id: str,
589+
self,
590+
job_id: str,
591+
domain_id: str,
592+
workspace_id: str,
593593
):
594594
job_vo: Job = self.job_mgr.get_job(job_id, domain_id, workspace_id)
595595

@@ -599,12 +599,12 @@ def _is_job_failed(
599599
return False
600600

601601
def _close_job(
602-
self,
603-
job_id: str,
604-
data_source_id: str,
605-
domain_id: str,
606-
data_keys: list,
607-
workspace_id: str = None,
602+
self,
603+
job_id: str,
604+
data_source_id: str,
605+
domain_id: str,
606+
data_keys: list,
607+
workspace_id: str = None,
608608
) -> None:
609609
job_vo: Job = self.job_mgr.get_job(job_id, domain_id, workspace_id)
610610
no_preload_cache = job_vo.options.get("no_preload_cache", False)
@@ -754,7 +754,7 @@ def _delete_old_cost_data(self, data_source_id, domain_id):
754754
monthly_cost_vos.delete()
755755

756756
def _delete_changed_cost_data(
757-
self, job_vo: Job, start, end, change_filter, domain_id
757+
self, job_vo: Job, start, end, change_filter, domain_id
758758
):
759759
query = {
760760
"filter": [
@@ -800,7 +800,7 @@ def _aggregate_cost_data(self, job_vo: Job, data_keys: list):
800800

801801
for job_task_id in job_task_ids:
802802
for billed_month in self._distinct_billed_month(
803-
data_source_id, domain_id, job_id, job_task_id
803+
data_source_id, domain_id, job_id, job_task_id
804804
):
805805
self._aggregate_monthly_cost_data(
806806
data_source_id,
@@ -832,14 +832,14 @@ def _distinct_billed_month(self, data_source_id, domain_id, job_id, job_task_id)
832832
return values
833833

834834
def _aggregate_monthly_cost_data(
835-
self,
836-
data_source_id: str,
837-
domain_id: str,
838-
job_id: str,
839-
job_task_id: str,
840-
billed_month: str,
841-
data_keys: list,
842-
workspace_id: str = None,
835+
self,
836+
data_source_id: str,
837+
domain_id: str,
838+
job_id: str,
839+
job_task_id: str,
840+
billed_month: str,
841+
data_keys: list,
842+
workspace_id: str = None,
843843
):
844844
query = {
845845
"group_by": [
@@ -906,7 +906,7 @@ def _get_all_data_sources(self):
906906
)
907907

908908
def _check_duplicate_job(
909-
self, data_source_id: str, domain_id: str, this_job_vo: Job
909+
self, data_source_id: str, domain_id: str, this_job_vo: Job
910910
):
911911
query = {
912912
"filter": [
@@ -932,14 +932,6 @@ def _check_duplicate_job(
932932

933933
return False
934934

935-
@staticmethod
936-
def _get_start_last_synchronized_at(params):
937-
start = params.get("start")
938-
last_synchronized_at = utils.datetime_to_iso8601(
939-
params.get("last_synchronized_at")
940-
)
941-
return start, last_synchronized_at
942-
943935
def _get_job_task_ids(self, job_id, domain_id):
944936
job_task_ids = []
945937
job_task_vos = self.job_task_mgr.filter_job_tasks(
@@ -952,11 +944,11 @@ def _get_job_task_ids(self, job_id, domain_id):
952944
return job_task_ids
953945

954946
def _get_data_source_account_map(
955-
self,
956-
data_source_id: str,
957-
domain_id: str,
958-
workspace_id: str,
959-
resource_group: str,
947+
self,
948+
data_source_id: str,
949+
domain_id: str,
950+
workspace_id: str,
951+
resource_group: str,
960952
) -> Dict[str, DataSourceAccount]:
961953
data_source_account_map = {}
962954
conditions = {
@@ -978,11 +970,11 @@ def _get_data_source_account_map(
978970
return data_source_account_map
979971

980972
def _get_linked_accounts_from_data_source_vo(
981-
self,
982-
data_source_vo: DataSource,
983-
options: dict,
984-
secret_data: dict,
985-
schema: dict = None,
973+
self,
974+
data_source_vo: DataSource,
975+
options: dict,
976+
secret_data: dict,
977+
schema: dict = None,
986978
) -> list:
987979
linked_accounts = []
988980

@@ -1044,6 +1036,14 @@ def _update_data_source_is_synced(self, job_vo: Job) -> None:
10441036
f"[_update_data_source_account_sync_status] synced_account_ids: {synced_accounts} / {data_source_id} {domain_id}"
10451037
)
10461038

1039+
@staticmethod
1040+
def _get_start_last_synchronized_at(params):
1041+
start = params.get("start")
1042+
last_synchronized_at = utils.datetime_to_iso8601(
1043+
params.get("last_synchronized_at")
1044+
)
1045+
return start, last_synchronized_at
1046+
10471047
@staticmethod
10481048
def _check_use_account_routing(data_source_vo: DataSource) -> bool:
10491049
plugin_info = data_source_vo.plugin_info.to_dict() or {}

0 commit comments

Comments
 (0)