fix: 修复账号批量添加模版账号时name没同步过来,资产创建时使用模版账号没有切换自,资产克隆时生成的账号没有切换自 (#11877)

Co-authored-by: feng <1304903146@qq.com>
pull/11878/head
fit2bot 2023-10-17 19:15:46 +08:00 committed by GitHub
parent 578c2af57c
commit b313598227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -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 = {}

View File

@ -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):