From 4c4ff7c773580269a48205132cf2ce72d7556e01 Mon Sep 17 00:00:00 2001 From: zhu327 Date: Wed, 3 Nov 2021 15:33:58 +0800 Subject: [PATCH 1/7] feat: remove policy environment --- saas/backend/apps/organization/models.py | 9 ++++++-- saas/backend/apps/organization/tasks.py | 7 +++--- .../migrations/0008_auto_20211103_1458.py | 22 +++++++++++++++++++ saas/backend/apps/policy/models.py | 9 -------- saas/backend/apps/policy/serializers.py | 6 ----- saas/backend/service/models/policy.py | 1 - 6 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 saas/backend/apps/policy/migrations/0008_auto_20211103_1458.py diff --git a/saas/backend/apps/organization/models.py b/saas/backend/apps/organization/models.py index db0df11a1..1ad2f132b 100644 --- a/saas/backend/apps/organization/models.py +++ b/saas/backend/apps/organization/models.py @@ -17,8 +17,13 @@ from mptt.managers import TreeManager from mptt.models import MPTTModel, TreeForeignKey -from backend.apps.organization.constants import SYNC_TASK_DEFAULT_EXECUTOR, StaffStatus, SyncTaskStatus, SyncType, \ - TriggerType +from backend.apps.organization.constants import ( + SYNC_TASK_DEFAULT_EXECUTOR, + StaffStatus, + SyncTaskStatus, + SyncType, + TriggerType, +) from backend.apps.organization.managers import SyncErrorLogManager from backend.biz.organization import get_category_name from backend.common.models import TimestampedModel diff --git a/saas/backend/apps/organization/tasks.py b/saas/backend/apps/organization/tasks.py index e95cae6ab..faf355fb3 100644 --- a/saas/backend/apps/organization/tasks.py +++ b/saas/backend/apps/organization/tasks.py @@ -53,9 +53,7 @@ def sync_organization(executor: str = SYNC_TASK_DEFAULT_EXECUTOR): record = SyncRecord.objects.create( executor=executor, type=SyncType.Full.value, status=SyncTaskStatus.Failed.value ) - SyncErrorLog.objects.create( - sync_record_id=record.id, exception_msg=exception_msg, traceback_msg=exception_msg - ) + SyncErrorLog.objects.create(sync_record_id=record.id, exception_msg=exception_msg, traceback_msg=exception_msg) return try: # 1. SaaS 从用户管理同步组织架构 @@ -108,7 +106,8 @@ def sync_organization(executor: str = SYNC_TASK_DEFAULT_EXECUTOR): SyncRecord.objects.filter(id=record_id).update(status=sync_status, updated_time=timezone.now()) if sync_status == SyncTaskStatus.Failed.value: SyncErrorLog.objects.create( - sync_record_id=record_id, exception_msg=exception_msg, traceback_msg=traceback_msg) + sync_record_id=record_id, exception_msg=exception_msg, traceback_msg=traceback_msg + ) @task(ignore_result=True) diff --git a/saas/backend/apps/policy/migrations/0008_auto_20211103_1458.py b/saas/backend/apps/policy/migrations/0008_auto_20211103_1458.py new file mode 100644 index 000000000..f96bf5c42 --- /dev/null +++ b/saas/backend/apps/policy/migrations/0008_auto_20211103_1458.py @@ -0,0 +1,22 @@ +# Generated by Django 2.2.24 on 2021-11-03 06:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('policy', '0007_delete_attachpolicy'), + ] + + operations = [ + migrations.RemoveField( + model_name='policy', + name='_environment', + ), + migrations.AlterField( + model_name='policy', + name='action_type', + field=models.CharField(default='', max_length=32, verbose_name='操作类型'), + ), + ] diff --git a/saas/backend/apps/policy/models.py b/saas/backend/apps/policy/models.py index fb4e8bafa..771818c21 100644 --- a/saas/backend/apps/policy/models.py +++ b/saas/backend/apps/policy/models.py @@ -34,7 +34,6 @@ class Policy(BaseModel): # policy _resources = models.TextField("资源策略", db_column="resources") # json - _environment = models.TextField("可用条件", db_column="environment") # json policy_id = models.BigIntegerField("后端policy_id", default=0) class Meta: @@ -50,11 +49,3 @@ def resources(self): @resources.setter def resources(self, resources): self._resources = json_dumps(resources) - - @property - def environment(self): - return json.loads(self._environment) - - @environment.setter - def environment(self, environment): - self._environment = json_dumps(environment) diff --git a/saas/backend/apps/policy/serializers.py b/saas/backend/apps/policy/serializers.py index 3952efa18..aa35bfa01 100644 --- a/saas/backend/apps/policy/serializers.py +++ b/saas/backend/apps/policy/serializers.py @@ -95,10 +95,6 @@ def validate(self, data): return data -class EnvironmentSLZ(serializers.Serializer): - pass - - class PolicySLZ(serializers.Serializer): type = serializers.CharField(label="操作类型") id = serializers.CharField(label="操作ID") @@ -107,7 +103,6 @@ class PolicySLZ(serializers.Serializer): name = serializers.CharField(label="操作名称", allow_blank=True) description = serializers.CharField(label="操作描述") related_resource_types = serializers.ListField(label="资源类型条件", child=ResourceTypeSLZ(label="资源类型"), required=True) - environment = EnvironmentSLZ(label="可用条件") expired_at = serializers.IntegerField(label="过期时间", max_value=PERMANENT_SECONDS) expired_display = serializers.CharField() @@ -183,7 +178,6 @@ class BasePolicyActionSLZ(serializers.Serializer): id = serializers.CharField(label="操作ID", required=True) type = serializers.CharField(label="操作类型", required=True, allow_blank=True) related_resource_types = serializers.ListField(label="资源类型条件", child=ResourceTypeSLZ(label="资源类型"), required=True) - environment = EnvironmentSLZ(label="可用条件", required=True) class PolicyActionSLZ(BasePolicyActionSLZ): diff --git a/saas/backend/service/models/policy.py b/saas/backend/service/models/policy.py index 50fe110c6..d9221a816 100644 --- a/saas/backend/service/models/policy.py +++ b/saas/backend/service/models/policy.py @@ -107,7 +107,6 @@ def to_db_model(self, system_id: str, subject: Subject) -> PolicyModel: action_id=self.action_id, ) p.resources = [rt.dict() for rt in self.related_resource_types] - p.environment = {} return p def to_backend_dict(self): From 212b7d687f2efa8a13e4f39065d3851be36cbc39 Mon Sep 17 00:00:00 2001 From: zhu327 Date: Thu, 4 Nov 2021 15:54:30 +0800 Subject: [PATCH 2/7] test: add policy_tag related_policy tests --- saas/tests/biz/policy_tag_tests.py | 246 +++++ saas/tests/biz/related_policy_tests.py | 1311 ++++++++++++++++++++++++ 2 files changed, 1557 insertions(+) create mode 100644 saas/tests/biz/policy_tag_tests.py create mode 100644 saas/tests/biz/related_policy_tests.py diff --git a/saas/tests/biz/policy_tag_tests.py b/saas/tests/biz/policy_tag_tests.py new file mode 100644 index 000000000..a9f776bbe --- /dev/null +++ b/saas/tests/biz/policy_tag_tests.py @@ -0,0 +1,246 @@ +from django.test import TestCase + +from backend.biz.policy_tag import ConditionTagBean, ConditionTagBiz + + +class DiffConditionsTests(TestCase): + def test_right(self): + new_conditions = [ + { + "id": "1", + "instances": [], + "attributes": [ + { + "id": "id2", + "name": "name1", + "values": [{"id": "id1", "name": "name1"}, {"id": "id2", "name": "name2"}], + } + ], + }, + { + "id": "2", + "instances": [], + "attributes": [ + { + "id": "id3", + "name": "name1", + "values": [{"id": "id1", "name": "name1"}, {"id": "id2", "name": "name2"}], + } + ], + }, + { + "id": "3", + "instances": [ + { + "type": "test1", + "name": "test1", + "path": [[{"type": "test1", "type_name": "test1", "id": "id1", "name": "id1"}]], + } + ], + "attributes": [ + { + "id": "id2", + "name": "name1", + "values": [{"id": "id1", "name": "name1"}, {"id": "id2", "name": "name2"}], + } + ], + }, + { + "id": "4", + "instances": [ + { + "type": "test1", + "name": "test1", + "path": [[{"type": "test1", "type_name": "test1", "id": "id1", "name": "id1"}]], + } + ], + "attributes": [ + { + "id": "id3", + "name": "name1", + "values": [{"id": "id1", "name": "name1"}, {"id": "id2", "name": "name2"}], + } + ], + }, + ] + + old_conditions = [ + { + "id": "2", + "instances": [], + "attributes": [ + { + "id": "id3", + "name": "name1", + "values": [{"id": "id1", "name": "name1"}, {"id": "id2", "name": "name2"}], + } + ], + }, + {"id": "3", "instances": [], "attributes": []}, + { + "id": "4", + "instances": [ + { + "type": "test1", + "name": "test1", + "path": [[{"type": "test1", "type_name": "test1", "id": "id1", "name": "id1"}]], + } + ], + "attributes": [ + { + "id": "id3", + "name": "name1", + "values": [{"id": "id1", "name": "name1"}, {"id": "id2", "name": "name2"}], + } + ], + }, + { + "id": "5", + "instances": [ + { + "type": "test1", + "name": "test1", + "path": [[{"type": "test1", "type_name": "test1", "id": "id1", "name": "id1"}]], + } + ], + "attributes": [ + { + "id": "id4", + "name": "name1", + "values": [{"id": "id1", "name": "name1"}, {"id": "id2", "name": "name2"}], + } + ], + }, + ] + + new_conditions = [ConditionTagBean(**c) for c in new_conditions] + old_conditions = [ConditionTagBean(**c) for c in old_conditions] + + svc = ConditionTagBiz() + + result = [ + ConditionTagBean(**c) + for c in [ + { + "instances": [], + "attributes": [ + { + "id": "id2", + "name": "name1", + "values": [ + {"id": "id1", "name": "name1", "tag": "add"}, + {"id": "id2", "name": "name2", "tag": "add"}, + ], + "tag": "add", + } + ], + "id": "1", + "tag": "add", + }, + { + "instances": [], + "attributes": [ + { + "id": "id3", + "name": "name1", + "values": [ + {"id": "id1", "name": "name1", "tag": "unchanged"}, + {"id": "id2", "name": "name2", "tag": "unchanged"}, + ], + "tag": "unchanged", + } + ], + "id": "2", + "tag": "unchanged", + }, + { + "instances": [ + { + "type": "test1", + "name": "test1", + "name_en": "", + "path": [ + [{"tag": "add", "type": "test1", "type_name": "test1", "id": "id1", "name": "id1"}], + ], + "tag": "add", + } + ], + "attributes": [ + { + "id": "id2", + "name": "name1", + "values": [ + {"id": "id1", "name": "name1", "tag": "add"}, + {"id": "id2", "name": "name2", "tag": "add"}, + ], + "tag": "add", + } + ], + "id": "3", + "tag": "add", + }, + {"instances": [], "attributes": [], "id": "3", "tag": "delete"}, + { + "instances": [ + { + "type": "test1", + "name": "test1", + "name_en": "", + "path": [ + [ + { + "tag": "unchanged", + "type": "test1", + "type_name": "test1", + "id": "id1", + "name": "id1", + } + ], + ], + "tag": "unchanged", + } + ], + "attributes": [ + { + "id": "id3", + "name": "name1", + "values": [ + {"id": "id1", "name": "name1", "tag": "unchanged"}, + {"id": "id2", "name": "name2", "tag": "unchanged"}, + ], + "tag": "unchanged", + } + ], + "id": "4", + "tag": "unchanged", + }, + { + "instances": [ + { + "type": "test1", + "name": "test1", + "name_en": "", + "path": [ + [{"tag": "delete", "type": "test1", "type_name": "test1", "id": "id1", "name": "id1"}], + ], + "tag": "delete", + } + ], + "attributes": [ + { + "id": "id4", + "name": "name1", + "values": [ + {"id": "id1", "name": "name1", "tag": "delete"}, + {"id": "id2", "name": "name2", "tag": "delete"}, + ], + "tag": "delete", + } + ], + "id": "5", + "tag": "delete", + }, + ] + ] + + self.assertEqual(svc.compare_and_tag(new_conditions, old_conditions, True), result) diff --git a/saas/tests/biz/related_policy_tests.py b/saas/tests/biz/related_policy_tests.py new file mode 100644 index 000000000..88266cd0e --- /dev/null +++ b/saas/tests/biz/related_policy_tests.py @@ -0,0 +1,1311 @@ +import mock +from django.test import TestCase + +from backend.biz.policy import ConditionBean, InstanceBean, PathNodeBean, PolicyBean, RelatedResourceBean +from backend.biz.related_policy import RelatedPolicyBiz +from backend.common.time import PERMANENT_SECONDS +from backend.service.constants import SelectionMode +from backend.service.models import Action, Attribute, InstanceSelection, RelatedResourceType +from backend.service.models.instance_selection import ChainNode + + +class DependingActionCheckPathTests(TestCase): + def test_check_path_same_type_1(self): + svc = RelatedPolicyBiz() + path = [ + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ] + selections = [ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=True, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ChainNode(system_id="bk_cmdb", id="module"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ) + ] + self.assertEqual( + svc._check_path_by_instance_selection(path, selections), + [PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"})], + ) + + def test_check_path_same_type_2(self): + svc = RelatedPolicyBiz() + path = [ + PathNodeBean(**{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "set", "id": "set1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "module", "id": "module1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ] + selections = [ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=True, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ) + ] + self.assertEqual(svc._check_path_by_instance_selection(path, selections), None) + + def test_check_path_same_type_4(self): + svc = RelatedPolicyBiz() + path = [ + PathNodeBean(**{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "set", "id": "set1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "module", "id": "module1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ] + selections = [ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ChainNode(system_id="bk_cmdb", id="module"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ) + ] + self.assertEqual( + svc._check_path_by_instance_selection(path, selections), + [ + PathNodeBean(**{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "set", "id": "set1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "module", "id": "module1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ], + ) + + def test_check_path_same_type_5(self): + svc = RelatedPolicyBiz() + path = [ + PathNodeBean(**{"system_id": "bk_job", "type": "biz", "id": "biz1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "set", "id": "set1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "module", "id": "module1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ] + selections = [ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ChainNode(system_id="bk_cmdb", id="module"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ) + ] + self.assertEqual(svc._check_path_by_instance_selection(path, selections), None) + + def test_check_path_same_type_6(self): + svc = RelatedPolicyBiz() + path = [ + PathNodeBean(**{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ] + selections = [ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ChainNode(system_id="bk_cmdb", id="module"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ), + InstanceSelection( + id="test2", + system_id="bk_cmdb", + name="test2", + name_en="test2", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ), + ] + self.assertEqual( + svc._check_path_by_instance_selection(path, selections), + [ + PathNodeBean(**{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ], + ) + + def test_check_path_different_type_1(self): + svc = RelatedPolicyBiz() + path = [ + PathNodeBean(**{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}), + PathNodeBean(**{"system_id": "bk_cmdb", "type": "host", "id": "host1"}), + ] + selections = [ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ], + ), + InstanceSelection( + id="test2", + system_id="bk_cmdb", + name="test2", + name_en="test2", + ignore_iam_path=False, + resource_type_chain=[ChainNode(system_id="bk_cmdb", id="biz")], + ), + ] + self.assertEqual( + svc._check_path_by_instance_selection(path, selections), + [PathNodeBean(**{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"})], + ) + + +class FilterResourceTypeSameTypeTests(TestCase): + def setUp(self) -> None: + import uuid + + uuid.uuid4 = mock.Mock(return_value=uuid.UUID("fa17b2cbf38141d7a5a0591573fc0f82")) + + def test_attribute(self): + svc = RelatedPolicyBiz() + policy_rrt = RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [{"system_id": "bk_cmdb", "type": "set", "id": "set1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ConditionBean( + instances=[ + InstanceBean(type="host", path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host3"}]]) + ], + attributes=[Attribute(id="", name="", values=[{"id": "test1", "name": "test1"}])], + ), + ConditionBean( + instances=[], + attributes=[Attribute(id="", name="", values=[{"id": "test2", "name": "test2"}])], + ), + ], + ) + action_rrt = RelatedResourceType( + id="host", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.ATTRIBUTE.value, + ) + self.assertEqual( + svc._filter_condition_of_same_type(policy_rrt, action_rrt), + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[], attributes=[Attribute(id="", name="", values=[{"id": "test2", "name": "test2"}])] + ) + ], + ), + ) + + def test_instance(self): + svc = RelatedPolicyBiz() + policy_rrt = RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [{"system_id": "bk_cmdb", "type": "set", "id": "set1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ConditionBean( + instances=[ + InstanceBean(type="host", path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host3"}]]) + ], + attributes=[Attribute(id="", name="", values=[{"id": "test1", "name": "test1"}])], + ), + ConditionBean( + instances=[], + attributes=[Attribute(id="", name="", values=[{"id": "test2", "name": "test2"}])], + ), + ], + ) + action_rrt = RelatedResourceType( + id="host", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.INSTANCE.value, + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ChainNode(system_id="bk_cmdb", id="module"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ) + ], + ) + self.assertEqual( + svc._filter_condition_of_same_type(policy_rrt, action_rrt), + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ], + ), + ) + + def test_all(self): + svc = RelatedPolicyBiz() + policy_rrt = RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[], + attributes=[Attribute(id="", name="", values=[{"id": "test2", "name": "test2"}])], + ), + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [{"system_id": "bk_cmdb", "type": "set", "id": "set1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ConditionBean( + instances=[ + InstanceBean(type="host", path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host3"}]]) + ], + attributes=[Attribute(id="", name="", values=[{"id": "test1", "name": "test1"}])], + ), + ], + ) + action_rrt = RelatedResourceType( + id="host", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.ALL.value, + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ChainNode(system_id="bk_cmdb", id="module"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ) + ], + ) + self.assertEqual( + svc._filter_condition_of_same_type(policy_rrt, action_rrt), + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ConditionBean( + instances=[ + InstanceBean(type="host", path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host3"}]]) + ], + attributes=[Attribute(id="", name="", values=[{"id": "test1", "name": "test1"}])], + ), + ConditionBean( + instances=[], + attributes=[Attribute(id="", name="", values=[{"id": "test2", "name": "test2"}])], + ), + ], + ), + ) + + def test_none(self): + svc = RelatedPolicyBiz() + policy_rrt = RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [{"system_id": "bk_cmdb", "type": "set", "id": "set1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ], + ) + action_rrt = RelatedResourceType( + id="host", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.ATTRIBUTE.value, + ) + self.assertEqual(svc._filter_condition_of_same_type(policy_rrt, action_rrt), None) + + +class FilterConditionDifferentTypeTests(TestCase): + def test_right_1(self): + svc = RelatedPolicyBiz() + conditions = [ConditionBean(instances=[], attributes=[])] + selections = [] + self.assertEqual(svc._filter_condition_of_different_type_by_instance_selection(conditions, selections), []) + + def test_right_2(self): + import uuid + + uuid.uuid4 = mock.Mock(return_value=uuid.UUID("fa17b2cbf38141d7a5a0591573fc0f82")) + + svc = RelatedPolicyBiz() + conditions = [ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [{"system_id": "bk_cmdb", "type": "set", "id": "set1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host2"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ConditionBean( + instances=[ + InstanceBean(type="host", path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host3"}]]) + ], + attributes=[], + ), + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host2"}, + ], + ], + ), + ], + attributes=[], + ), + ] + selections = [ + InstanceSelection( + id="test2", + system_id="bk_cmdb", + name="test2", + name_en="test2", + ignore_iam_path=False, + resource_type_chain=[ChainNode(system_id="bk_cmdb", id="biz")], + ), + ] + self.assertEqual( + svc._filter_condition_of_different_type_by_instance_selection(conditions, selections), + [ + ConditionBean( + id="fa17b2cbf38141d7a5a0591573fc0f82", + instances=[ + InstanceBean( + type="biz", + path=[ + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}], + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}], + ], + ), + ], + attributes=[], + ), + ], + ) + + +class FilterResourceTypeDifferentTypeTests(TestCase): + def test_attribute(self): + svc = RelatedPolicyBiz() + policy_rrt = RelatedResourceBean(system_id="bk_cmdb", type="host", condition=[]) + action_rrt = RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.ATTRIBUTE.value, + ) + self.assertEqual(svc._filter_condition_of_different_type([policy_rrt], action_rrt), None) + + def test_one_rrt(self): + import uuid + + uuid.uuid4 = mock.Mock(return_value=uuid.UUID("fa17b2cbf38141d7a5a0591573fc0f82")) + + svc = RelatedPolicyBiz() + policy_rrt = RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[], + attributes=[Attribute(id="", name="", values=[{"id": "test2", "name": "test2"}])], + ), + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [{"system_id": "bk_cmdb", "type": "set", "id": "set1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ConditionBean( + instances=[ + InstanceBean(type="host", path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host3"}]]) + ], + attributes=[Attribute(id="", name="", values=[{"id": "test1", "name": "test1"}])], + ), + ], + ) + action_rrt = RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.ALL.value, + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ChainNode(system_id="bk_cmdb", id="biz")], + ) + ], + ) + self.assertEqual( + svc._filter_condition_of_different_type([policy_rrt], action_rrt), + RelatedResourceBean( + system_id="bk_cmdb", + type="biz", + condition=[ + ConditionBean( + id="fa17b2cbf38141d7a5a0591573fc0f82", + instances=[ + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}]]), + ], + attributes=[], + ), + ], + ), + ) + + def test_multi_rrt(self): + import uuid + + uuid.uuid4 = mock.Mock(return_value=uuid.UUID("fa17b2cbf38141d7a5a0591573fc0f82")) + + svc = RelatedPolicyBiz() + policy_rrts = [ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}]]), + ], + attributes=[], + ), + ], + ), + RelatedResourceBean( + system_id="bk_job", + type="job", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="job", + path=[ + [{"system_id": "bk_job", "type": "job", "id": "job1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_job", "type": "set", "id": "job2"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}]]), + ], + attributes=[], + ), + ], + ), + ] + action_rrt = action_rrt = RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.ALL.value, + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ChainNode(system_id="bk_cmdb", id="biz")], + ) + ], + ) + self.assertEqual( + svc._filter_condition_of_different_type(policy_rrts, action_rrt), + RelatedResourceBean( + system_id="bk_cmdb", + type="biz", + condition=[ + ConditionBean( + id="fa17b2cbf38141d7a5a0591573fc0f82", + instances=[ + InstanceBean( + type="biz", + path=[ + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}], + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}], + ], + ), + ], + attributes=[], + ), + ], + ), + ) + + def test_multi_rrt_none(self): + svc = RelatedPolicyBiz() + policy_rrts = [ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host1"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}]]), + ], + attributes=[], + ), + ], + ), + RelatedResourceBean( + system_id="bk_job", + type="job", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="job", + path=[ + [{"system_id": "bk_job", "type": "job", "id": "job1"}], + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz3"}, + {"system_id": "bk_job", "type": "set", "id": "job2"}, + ], + ], + ), + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz4"}]]), + ], + attributes=[], + ), + ], + ), + ] + action_rrt = action_rrt = RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + selection_mode=SelectionMode.ALL.value, + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ChainNode(system_id="bk_cmdb", id="biz")], + ) + ], + ) + self.assertEqual(svc._filter_condition_of_different_type(policy_rrts, action_rrt), None) + + +class MergeMultiConditionsTests(TestCase): + def test_empty(self): + svc = RelatedPolicyBiz() + rrt_conditions = [ + [ + ConditionBean( + instances=[ + InstanceBean( + type="biz", + path=[ + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}], + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}], + ], + ), + ], + attributes=[], + ), + ], + [ + ConditionBean( + instances=[ + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz3"}]]) + ], + attributes=[], + ), + ], + [ + ConditionBean( + instances=[ + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz4"}]]) + ], + attributes=[], + ), + ], + ] + self.assertEqual(svc._merge_multi_conditions(rrt_conditions), []) + + def test_right(self): + svc = RelatedPolicyBiz() + rrt_conditions = [ + [ + ConditionBean( + instances=[ + InstanceBean( + type="biz", + path=[ + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz1"}], + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}], + ], + ), + InstanceBean( + type="host", + path=[ + [{"system_id": "bk_cmdb", "type": "host", "id": "host1"}], + [{"system_id": "bk_cmdb", "type": "host", "id": "host2"}], + ], + ), + ], + attributes=[], + ), + ], + [ + ConditionBean( + instances=[ + InstanceBean( + type="biz", + path=[ + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz3"}], + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}], + ], + ), + InstanceBean( + type="host", + path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host3"}]], + ), + ], + attributes=[], + ), + ], + [ + ConditionBean( + instances=[ + InstanceBean( + type="biz", + path=[ + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz4"}], + [{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}], + ], + ), + InstanceBean( + type="host", + path=[[{"system_id": "bk_cmdb", "type": "host", "id": "host4"}]], + ), + ], + attributes=[], + ), + ], + ] + self.assertEqual( + svc._merge_multi_conditions(rrt_conditions), + [ + ConditionBean( + instances=[ + InstanceBean(type="biz", path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}]]) + ], + attributes=[], + ) + ], + ) + + +class CreateDependingPolicyTests(TestCase): + def test_branch_1(self): + policy = PolicyBean( + id="create_host", + related_resource_types=[], + environment={}, + expired_at=PERMANENT_SECONDS, + ) + action = Action( + id="multi_rrt", + name="test", + name_en="test", + description="test", + description_en="test", + type="create", + related_resource_types=[ + RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + ), + RelatedResourceType( + id="host", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + ), + ], + related_actions=[], + ) + + svc = RelatedPolicyBiz() + self.assertEqual(svc._create_related_policy(policy, action), None) + + def test_branch_2(self): + policy = PolicyBean( + id="create_host", + related_resource_types=[], + environment={}, + expired_at=PERMANENT_SECONDS, + ) + action = Action( + id="empty", + name="test", + name_en="test", + description="test", + description_en="test", + type="create", + related_resource_types=[], + related_actions=[], + ) + + svc = RelatedPolicyBiz() + self.assertEqual( + svc._create_related_policy(policy, action), + PolicyBean( + id="empty", + related_resource_types=[], + environment={}, + expired_at=policy.expired_at, + ), + ) + + def test_branch_3(self): + policy = PolicyBean( + id="create_host", + related_resource_types=[], + environment={}, + expired_at=PERMANENT_SECONDS, + ) + action = Action( + id="empty", + name="test", + name_en="test", + description="test", + description_en="test", + type="create", + related_resource_types=[ + RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + ) + ], + related_actions=[], + ) + + svc = RelatedPolicyBiz() + self.assertEqual(svc._create_related_policy(policy, action), None) + + def test_branch_4(self): + policy = PolicyBean( + id="edit_host", + related_resource_types=[ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[], + ) + ], + environment={}, + expired_at=PERMANENT_SECONDS, + ) + action = Action( + id="view_host", + name="test", + name_en="test", + description="test", + description_en="test", + type="", + related_resource_types=[ + RelatedResourceType( + id="host", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + ) + ], + related_actions=[], + ) + + svc = RelatedPolicyBiz() + self.assertEqual( + svc._create_related_policy(policy, action), + PolicyBean( + id="view_host", + related_resource_types=[ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[], + ) + ], + environment={}, + expired_at=PERMANENT_SECONDS, + ), + ) + + def test_branch_5(self): + policy = PolicyBean( + id="edit_host", + related_resource_types=[ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host2"}, + ], + ], + ), + ], + attributes=[], + ) + ], + ) + ], + environment={}, + expired_at=PERMANENT_SECONDS, + ) + action = Action( + id="view_host", + name="test", + name_en="test", + description="test", + description_en="test", + type="", + related_resource_types=[ + RelatedResourceType( + id="host", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ + ChainNode(system_id="bk_cmdb", id="biz"), + ChainNode(system_id="bk_cmdb", id="set"), + ChainNode(system_id="bk_cmdb", id="module"), + ChainNode(system_id="bk_cmdb", id="host"), + ], + ) + ], + ) + ], + related_actions=[], + ) + + svc = RelatedPolicyBiz() + self.assertEqual( + svc._create_related_policy(policy, action), + PolicyBean( + id="view_host", + related_resource_types=[ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host2"}, + ], + ], + ), + ], + attributes=[], + ) + ], + ) + ], + environment={}, + expired_at=PERMANENT_SECONDS, + ), + ) + + def test_branch_6(self): + import uuid + + uuid.uuid4 = mock.Mock(return_value=uuid.UUID("fa17b2cbf38141d7a5a0591573fc0f82")) + + policy = PolicyBean( + id="edit_host", + related_resource_types=[ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host2"}, + ], + ], + ), + ], + attributes=[], + ) + ], + ) + ], + environment={}, + expired_at=PERMANENT_SECONDS, + ) + action = Action( + id="view_biz", + name="test", + name_en="test", + description="test", + description_en="test", + type="", + related_resource_types=[ + RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ChainNode(system_id="bk_cmdb", id="biz")], + ) + ], + ) + ], + related_actions=[], + ) + + svc = RelatedPolicyBiz() + self.assertEqual( + svc._create_related_policy(policy, action), + PolicyBean( + id="view_biz", + related_resource_types=[ + RelatedResourceBean( + system_id="bk_cmdb", + type="biz", + condition=[ + ConditionBean( + id="fa17b2cbf38141d7a5a0591573fc0f82", + instances=[ + InstanceBean( + type="biz", + path=[[{"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}]], + ), + ], + attributes=[], + ) + ], + ) + ], + environment={}, + expired_at=PERMANENT_SECONDS, + ), + ) + + def test_branch_7(self): + policy = PolicyBean( + id="edit_host", + related_resource_types=[ + RelatedResourceBean( + system_id="bk_cmdb", + type="host", + condition=[ + ConditionBean( + instances=[ + InstanceBean( + type="host", + path=[ + [ + {"system_id": "bk_cmdb", "type": "biz", "id": "biz2"}, + {"system_id": "bk_cmdb", "type": "set", "id": "set1"}, + {"system_id": "bk_cmdb", "type": "module", "id": "module1"}, + {"system_id": "bk_cmdb", "type": "host", "id": "host2"}, + ], + ], + ), + ], + attributes=[], + ) + ], + ) + ], + environment={}, + expired_at=PERMANENT_SECONDS, + ) + action = Action( + id="view_biz", + name="test", + name_en="test", + description="test", + description_en="test", + type="", + related_resource_types=[ + RelatedResourceType( + id="biz", + system_id="bk_cmdb", + name_alias="test", + name_alias_en="test", + instance_selections=[ + InstanceSelection( + id="test1", + system_id="bk_cmdb", + name="test1", + name_en="test1", + ignore_iam_path=False, + resource_type_chain=[ChainNode(system_id="bk_cmdb", id="set")], + ) + ], + ) + ], + related_actions=[], + ) + + svc = RelatedPolicyBiz() + self.assertEqual(svc._create_related_policy(policy, action), None) From 9d1705f69315d278fad81c73f8a41c0ced5b5bb0 Mon Sep 17 00:00:00 2001 From: PoloHuang <43561423+huangpeng0428@users.noreply.github.com> Date: Thu, 4 Nov 2021 20:58:07 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD=20(#345)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 取消勾选对应权限的实例报错 #92356085 * fix: optimize the code * fix: 修复后台关联数据时tag返回值bug * feat: related_policy add update tag * docs: v1.5.4 * feat: modify bkmessage #0 * feat: add record-list page * feat: delete fetchMemberList function * feat: delete useless code * fix: 管理员身份下无权限跳转失败问题 * fix: 隐藏同步记录入口 * feat: complete record list page Co-authored-by: polo Co-authored-by: zhu327 Co-authored-by: poloohuang --- frontend/src/language/lang/en.js | 4 +- frontend/src/language/lang/zh.js | 4 +- frontend/src/store/modules/organization.js | 25 ++++ .../src/views/user/components/record-list.vue | 139 ++++++++---------- .../views/user/components/render-status.vue | 60 ++++++++ frontend/src/views/user/index.vue | 1 + 6 files changed, 151 insertions(+), 82 deletions(-) create mode 100644 frontend/src/views/user/components/render-status.vue diff --git a/frontend/src/language/lang/en.js b/frontend/src/language/lang/en.js index e5882b9f3..03e77cfc1 100644 --- a/frontend/src/language/lang/en.js +++ b/frontend/src/language/lang/en.js @@ -567,7 +567,9 @@ export const m = { '耗时': 'time consuming', '操作人': 'Operator', '触发类型': 'Trigger type', - '日志详情': 'Log details' + '日志详情': 'Log details', + '成功': 'Succeed', + '失败': 'Failed' }, grading: { '分级管理员': 'Grading Manager', diff --git a/frontend/src/language/lang/zh.js b/frontend/src/language/lang/zh.js index e9f2e757f..23b7a73ad 100644 --- a/frontend/src/language/lang/zh.js +++ b/frontend/src/language/lang/zh.js @@ -632,7 +632,9 @@ export const m = { '耗时': '耗时', '操作人': '操作人', '触发类型': '触发类型', - '日志详情': '日志详情' + '日志详情': '日志详情', + '成功': '成功', + '失败': '失败' }, grading: { '分级管理员': '分级管理员', diff --git a/frontend/src/store/modules/organization.js b/frontend/src/store/modules/organization.js index 75004eabe..f615106a6 100644 --- a/frontend/src/store/modules/organization.js +++ b/frontend/src/store/modules/organization.js @@ -157,6 +157,31 @@ export default { */ verifyManualUser ({ commit, state, dispatch }, params, config) { return http.post(`${AJAX_URL_PREFIX}/organizations/users/query/`, params, config) + }, + + /** + * 获取同步记录 + * + * @param {Function} commit store commit mutation handler + * @param {Object} state store state + * @param {Function} dispatch store dispatch action handler + * @param {Object} params 请求参数 + * @return {Promise} promise 对象 + */ + getRecordsList ({ commit, state, dispatch }, params, config = {}) { + return http.get(`${AJAX_URL_PREFIX}/organizations/sync_records/?${json2Query(params)}`, {}, config) + }, + /** + * 获取日志详情 + * + * @param {Function} commit store commit mutation handler + * @param {Object} state store state + * @param {Function} dispatch store dispatch action handler + * @param {Object} params 请求参数 + * @return {Promise} promise 对象 + */ + getRecordsLog ({ commit, state, dispatch }, id, config) { + return http.get(`${AJAX_URL_PREFIX}/organizations/sync_records/${id}/logs/`, config) } } } diff --git a/frontend/src/views/user/components/record-list.vue b/frontend/src/views/user/components/record-list.vue index 266b5c83a..5173c4615 100644 --- a/frontend/src/views/user/components/record-list.vue +++ b/frontend/src/views/user/components/record-list.vue @@ -1,7 +1,6 @@ + diff --git a/frontend/src/views/user/index.vue b/frontend/src/views/user/index.vue index eb3eb0b3f..167e9d71b 100644 --- a/frontend/src/views/user/index.vue +++ b/frontend/src/views/user/index.vue @@ -560,6 +560,7 @@ return } this.$store.commit('updateSync', true) + this.curSelectedData.type = 'depart' try { const res = await this.$store.dispatch('organization/organizationsSyncTask') if (res.result) { From 583214f8e68892d4f29c57408e9d49298a680864 Mon Sep 17 00:00:00 2001 From: PoloHuang <43561423+huangpeng0428@users.noreply.github.com> Date: Fri, 5 Nov 2021 12:53:51 +0800 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=93=8D=E4=BD=9Cbug=20(#348)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 取消勾选对应权限的实例报错 #92356085 * fix: optimize the code * fix: 修复后台关联数据时tag返回值bug * feat: related_policy add update tag * docs: v1.5.4 * feat: modify bkmessage #0 * feat: add record-list page * feat: delete fetchMemberList function * feat: delete useless code * fix: 管理员身份下无权限跳转失败问题 * fix: 隐藏同步记录入口 * feat: complete record list page * fix: 修复依赖操作bug Co-authored-by: polo Co-authored-by: zhu327 Co-authored-by: poloohuang --- .../components/resource-instance-table.vue | 6 +- .../src/views/user/components/record-list.vue | 62 +++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/frontend/src/views/perm-apply/components/resource-instance-table.vue b/frontend/src/views/perm-apply/components/resource-instance-table.vue index c128ccef0..4b412734e 100644 --- a/frontend/src/views/perm-apply/components/resource-instance-table.vue +++ b/frontend/src/views/perm-apply/components/resource-instance-table.vue @@ -676,7 +676,11 @@ const inOriginalList = !!this.originalList.filter( original => String(original.id) === String(item.id) ).length - item.expired_at = item.expired_at - this.user.timestamp + if ((item.tag === 'add' || item.tag === 'update' || item.isExpired) && item.expired_at !== PERMANENT_TIMESTAMP) { + if (!item.isShowRenewal) { + item.expired_at = item.expired_at - this.user.timestamp + } + } this.tableList.splice( curIndex, 1, diff --git a/frontend/src/views/user/components/record-list.vue b/frontend/src/views/user/components/record-list.vue index 5173c4615..f4236bf84 100644 --- a/frontend/src/views/user/components/record-list.vue +++ b/frontend/src/views/user/components/record-list.vue @@ -2,6 +2,15 @@
同步记录 + @@ -76,9 +87,36 @@