From b313598227859a0ed3128f8715c93431e7297d3d Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 17 Oct 2023 19:15:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=B7=BB=E5=8A=A0=E6=A8=A1=E7=89=88=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E6=97=B6name=E6=B2=A1=E5=90=8C=E6=AD=A5=E8=BF=87?= =?UTF-8?q?=E6=9D=A5=EF=BC=8C=E8=B5=84=E4=BA=A7=E5=88=9B=E5=BB=BA=E6=97=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=A8=A1=E7=89=88=E8=B4=A6=E5=8F=B7=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=88=87=E6=8D=A2=E8=87=AA=EF=BC=8C=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E5=85=8B=E9=9A=86=E6=97=B6=E7=94=9F=E6=88=90=E7=9A=84=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E6=B2=A1=E6=9C=89=E5=88=87=E6=8D=A2=E8=87=AA=20(#1187?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- apps/accounts/serializers/account/account.py | 3 +- apps/assets/serializers/asset/common.py | 38 ++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/apps/accounts/serializers/account/account.py b/apps/accounts/serializers/account/account.py index 0864499b6..8b8d9be36 100644 --- a/apps/accounts/serializers/account/account.py +++ b/apps/accounts/serializers/account/account.py @@ -78,7 +78,8 @@ class AccountCreateUpdateSerializerMixin(serializers.Serializer): def get_template_attr_for_account(template): # Set initial data from template field_names = [ - 'username', 'secret', 'secret_type', 'privileged', 'is_active' + 'name', 'username', 'secret', + 'secret_type', 'privileged', 'is_active' ] attrs = {} diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index 78f691237..aae31c7ff 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -281,14 +281,48 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali return protocols_data_map.values() @staticmethod - def accounts_create(accounts_data, asset): + def update_account_su_from(accounts, include_su_from_accounts): + if not include_su_from_accounts: + return + name_map = {account.name: account for account in accounts} + username_secret_type_map = { + (account.username, account.secret_type): account for account in accounts + } + + for name, username_secret_type in include_su_from_accounts.items(): + account = name_map.get(name) + if not account: + continue + su_from_account = username_secret_type_map.get(username_secret_type) + if su_from_account: + account.su_from = su_from_account + account.save() + + def accounts_create(self, accounts_data, asset): + from accounts.models import AccountTemplate if not accounts_data: return + su_from_name_username_secret_type_map = {} for data in accounts_data: data['asset'] = asset.id + name = data.get('name') + su_from = data.pop('su_from', None) + template_id = data.get('template', None) + if template_id: + template = AccountTemplate.objects.get(id=template_id) + if template and template.su_from: + su_from_name_username_secret_type_map[template.name] = ( + template.su_from.username, template.su_from.secret_type + ) + elif isinstance(su_from, dict): + su_from = Account.objects.get(id=su_from.get('id')) + su_from_name_username_secret_type_map[name] = ( + su_from.username, su_from.secret_type + ) s = AssetAccountSerializer(data=accounts_data, many=True) s.is_valid(raise_exception=True) - s.save() + accounts = s.save() + self.update_account_su_from(accounts, su_from_name_username_secret_type_map) @atomic def create(self, validated_data):