improve host import

pull/330/head
vapao 2021-04-28 10:54:22 +08:00
parent b84a156454
commit e0e791b01c
2 changed files with 7 additions and 24 deletions

View File

@ -101,7 +101,7 @@ def post_import(request):
password = request.POST.get('password')
file = request.FILES['file']
ws = load_workbook(file, read_only=True)['Sheet1']
summary = {'invalid': [], 'skip': [], 'fail': [], 'network': [], 'repeat': [], 'success': [], 'error': []}
summary = {'invalid': [], 'skip': [], 'repeat': [], 'success': []}
for i, row in enumerate(ws.rows):
if i == 0: # 第1行是表头 略过
continue
@ -120,23 +120,15 @@ def post_import(request):
if Host.objects.filter(hostname=data.hostname, port=data.port, username=data.username).exists():
summary['skip'].append(i)
continue
try:
if valid_ssh(data.hostname, data.port, data.username, data.pop('password') or password, None,
False) is False:
summary['fail'].append(i)
continue
except AuthenticationException:
summary['fail'].append(i)
continue
except socket.error:
summary['network'].append(i)
continue
except Exception:
summary['error'].append(i)
continue
if Host.objects.filter(name=data.name).exists():
summary['repeat'].append(i)
continue
try:
password = data.pop('password') or password
if valid_ssh(data.hostname, data.port, data.username, password, None, False) is False:
data.is_verified = True
except Exception:
pass
host = Host.objects.create(created_by=request.user, **data)
if request.user.role:
request.user.role.add_host_perm(host.id)

View File

@ -26,12 +26,6 @@ export default observer(function () {
title: '导入结果',
content: <Form labelCol={{span: 7}} wrapperCol={{span: 14}}>
<Form.Item style={{margin: 0}} label="导入成功">{res.success.length}</Form.Item>
{res['fail'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="验证失败">
<Tooltip title={`相关行:${res['fail'].join(', ')}`}>{res['fail'].length}</Tooltip>
</Form.Item>}
{res['network'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="网络错误">
<Tooltip title={`相关行:${res['network'].join(', ')}`}>{res['network'].length}</Tooltip>
</Form.Item>}
{res['skip'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="重复数据">
<Tooltip title={`相关行:${res['skip'].join(', ')}`}>{res['skip'].length}</Tooltip>
</Form.Item>}
@ -41,9 +35,6 @@ export default observer(function () {
{res['repeat'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="重复主机名">
<Tooltip title={`相关行:${res['repeat'].join(', ')}`}>{res['repeat'].length}</Tooltip>
</Form.Item>}
{res['error'].length > 0 && <Form.Item style={{margin: 0, color: '#1890ff'}} label="其他错误">
<Tooltip title={`请通过新建主机查看具体错误信息,相关行:${res['error'].join(', ')}`}>{res['error'].length}</Tooltip>
</Form.Item>}
</Form>
})
})