- 781处 tsc -b 错误清零(unknown 属性访问/隐式any返回类型/类型转换不匹配)
- 移除 App.tsx 中已删除的 AnsibleManager/AnsibleWorkflow 导入和路由
- 修复 DeploymentView 中 _prefix 解构导致的属性不存在错误
- 修复 Header/Sidebar/PipelineEditor 中无效的类型断言
- asList() 返回类型 unknown[]→any[], 恢复 AgentMonitor 正常工作
- 放宽 noImplicitAny(临时), 待 types/ 定义完善后逐步恢复
- ESLint: unused=0, console=0, any=1041(Q1 后续降低)
- npm run build: ✅ 0 errors
472 lines
19 KiB
TypeScript
472 lines
19 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Card, CardContent } from '@/components/ui/card';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import {
|
|
Plus,
|
|
Search,
|
|
Trash2,
|
|
Pencil,
|
|
Key,
|
|
Cloud,
|
|
CheckCircle2,
|
|
XCircle,
|
|
RefreshCw,
|
|
Loader2,
|
|
} from 'lucide-react';
|
|
import { cloudApi } from '@/services/api';
|
|
import { CloudBrandIcon, getProviderColor } from '@/components/icons/CloudBrandIcon';
|
|
|
|
const providerLabels: Record<string, string> = {
|
|
aws: 'AWS',
|
|
aliyun: '阿里云',
|
|
tencent: '腾讯云',
|
|
huawei: '华为云',
|
|
azure: 'Azure',
|
|
gcp: 'GCP',
|
|
};
|
|
|
|
export function CloudAccountManager() {
|
|
const [accounts, setAccounts] = useState<any[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
const [selectedProvider, setSelectedProvider] = useState<string>('all');
|
|
|
|
// 创建/编辑账号表单状态
|
|
const [formOpen, setFormOpen] = useState(false);
|
|
const [submitting, setSubmitting] = useState(false);
|
|
const [editingAccount, setEditingAccount] = useState<any>(null);
|
|
const [formData, setFormData] = useState({
|
|
name: '',
|
|
provider: 'aliyun',
|
|
access_key: '',
|
|
secret_key: '',
|
|
monthly_cost: '',
|
|
status: 'active',
|
|
});
|
|
|
|
const resetForm = () => {
|
|
setEditingAccount(null);
|
|
setFormData({
|
|
name: '',
|
|
provider: 'aliyun',
|
|
access_key: '',
|
|
secret_key: '',
|
|
monthly_cost: '',
|
|
status: 'active',
|
|
});
|
|
};
|
|
|
|
const handleOpenCreate = () => {
|
|
resetForm();
|
|
setFormOpen(true);
|
|
};
|
|
|
|
const handleOpenEdit = ( account: any) => {
|
|
setEditingAccount(account);
|
|
setFormData({
|
|
name: account.name || '',
|
|
provider: account.provider || 'aliyun',
|
|
access_key: '',
|
|
secret_key: '',
|
|
monthly_cost: String(account.monthly_cost ?? 0),
|
|
status: account.status || 'active',
|
|
});
|
|
setFormOpen(true);
|
|
};
|
|
|
|
const handleSubmit = async () => {
|
|
if (!formData.name.trim() || !formData.provider.trim()) return;
|
|
try {
|
|
setSubmitting(true);
|
|
const payload: Record<string, any> = {
|
|
name: formData.name.trim(),
|
|
provider: formData.provider,
|
|
access_key: formData.access_key.trim(),
|
|
secret_key: formData.secret_key.trim(),
|
|
monthly_cost: Number(formData.monthly_cost) || 0,
|
|
status: formData.status,
|
|
};
|
|
if (editingAccount) {
|
|
await cloudApi.updateAccount(editingAccount.id, payload);
|
|
} else {
|
|
await cloudApi.createAccount(payload);
|
|
}
|
|
setFormOpen(false);
|
|
loadData();
|
|
} catch (e) {
|
|
console.error(editingAccount ? '更新云账号失败:' : '创建云账号失败:', e);
|
|
} finally {
|
|
setSubmitting(false);
|
|
}
|
|
};
|
|
|
|
const loadData = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const res: Record<string, any> = await cloudApi.getAccounts();
|
|
setAccounts(res?.list || []);
|
|
} catch (e) {
|
|
console.error('加载云账号列表失败:', e);
|
|
setAccounts([]);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
loadData();
|
|
}, []);
|
|
|
|
const handleDelete = async (id: string) => {
|
|
try {
|
|
await cloudApi.deleteAccount(id);
|
|
loadData();
|
|
} catch (e) {
|
|
console.error('删除云账号失败:', e);
|
|
}
|
|
};
|
|
|
|
const filteredAccounts = accounts.filter(account => {
|
|
const accessKey = account.access_key || account.accessKey || '';
|
|
const matchesSearch = (account.name || '').toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
accessKey.toLowerCase().includes(searchQuery.toLowerCase());
|
|
const matchesProvider = selectedProvider === 'all' || account.provider === selectedProvider;
|
|
return matchesSearch && matchesProvider;
|
|
});
|
|
|
|
const stats = {
|
|
total: accounts.length,
|
|
active: accounts.filter(a => a.status === 'active').length,
|
|
inactive: accounts.filter(a => a.status === 'inactive').length,
|
|
error: accounts.filter(a => a.status === 'error').length,
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
{/* Header */}
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h1 className="text-2xl font-bold text-slate-100">云账号管理</h1>
|
|
<p className="text-slate-400 mt-1">管理多云平台的访问凭证和账号信息</p>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<Button variant="outline" onClick={loadData} className="border-slate-700 text-slate-300">
|
|
<RefreshCw className={`h-4 w-4 mr-2 ${loading ? 'animate-spin' : ''}`} />
|
|
刷新
|
|
</Button>
|
|
<Button className="bg-blue-600 hover:bg-blue-700" onClick={handleOpenCreate}>
|
|
<Plus className="h-4 w-4 mr-2" />
|
|
添加账号
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Stats */}
|
|
<div className="grid grid-cols-4 gap-4">
|
|
<Card className="bg-slate-800 border-slate-700">
|
|
<CardContent className="p-4">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<p className="text-slate-400 text-sm">账号总数</p>
|
|
<p className="text-2xl font-bold text-slate-100 mt-1">{stats.total}</p>
|
|
</div>
|
|
<div className="w-10 h-10 rounded-lg bg-blue-500/20 flex items-center justify-center">
|
|
<Cloud className="h-5 w-5 text-blue-400" />
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
<Card className="bg-slate-800 border-slate-700">
|
|
<CardContent className="p-4">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<p className="text-slate-400 text-sm">正常</p>
|
|
<p className="text-2xl font-bold text-green-400 mt-1">{stats.active}</p>
|
|
</div>
|
|
<div className="w-10 h-10 rounded-lg bg-green-500/20 flex items-center justify-center">
|
|
<CheckCircle2 className="h-5 w-5 text-green-400" />
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
<Card className="bg-slate-800 border-slate-700">
|
|
<CardContent className="p-4">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<p className="text-slate-400 text-sm">已停用</p>
|
|
<p className="text-2xl font-bold text-slate-400 mt-1">{stats.inactive}</p>
|
|
</div>
|
|
<div className="w-10 h-10 rounded-lg bg-slate-500/20 flex items-center justify-center">
|
|
<XCircle className="h-5 w-5 text-slate-400" />
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
<Card className="bg-slate-800 border-slate-700">
|
|
<CardContent className="p-4">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<p className="text-slate-400 text-sm">异常</p>
|
|
<p className="text-2xl font-bold text-red-400 mt-1">{stats.error}</p>
|
|
</div>
|
|
<div className="w-10 h-10 rounded-lg bg-red-500/20 flex items-center justify-center">
|
|
<Key className="h-5 w-5 text-red-400" />
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
|
|
{/* Filters */}
|
|
<div className="flex items-center gap-4">
|
|
<div className="relative flex-1 max-w-md">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-slate-400" />
|
|
<Input
|
|
placeholder="搜索账号名称或AccessKey..."
|
|
value={searchQuery}
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
className="pl-10 bg-slate-800 border-slate-700 text-slate-100"
|
|
/>
|
|
</div>
|
|
<select
|
|
value={selectedProvider}
|
|
onChange={(e) => setSelectedProvider(e.target.value)}
|
|
className="px-3 py-2 bg-slate-800 border border-slate-700 rounded-md text-slate-100 text-sm"
|
|
>
|
|
<option value="all">全部平台</option>
|
|
<option value="aws">AWS</option>
|
|
<option value="aliyun">阿里云</option>
|
|
<option value="tencent">腾讯云</option>
|
|
<option value="huawei">华为云</option>
|
|
<option value="azure">Azure</option>
|
|
</select>
|
|
</div>
|
|
|
|
{/* Account List */}
|
|
{loading ? (
|
|
<Card className="bg-slate-800 border-slate-700">
|
|
<CardContent className="p-12 text-center text-slate-500">
|
|
<RefreshCw className="h-6 w-6 animate-spin mx-auto mb-3" />
|
|
加载中...
|
|
</CardContent>
|
|
</Card>
|
|
) : filteredAccounts.length === 0 ? (
|
|
<Card className="bg-slate-800 border-slate-700">
|
|
<CardContent className="p-12 text-center text-slate-500">暂无云账号数据</CardContent>
|
|
</Card>
|
|
) : (
|
|
<div className="grid grid-cols-2 gap-4">
|
|
{filteredAccounts.map((account) => {
|
|
const accessKey = account.access_key || account.accessKey || '';
|
|
const serverCount = account.server_count ?? account.serverCount ?? 0;
|
|
const lastSync = account.last_sync || account.lastSync || account.created_at || '-';
|
|
const monthlyCost = account.monthly_cost ?? account.monthlyCost ?? 0;
|
|
return (
|
|
<Card key={account.id} className="bg-slate-800 border-slate-700">
|
|
<CardContent className="p-4">
|
|
<div className="flex items-start justify-between">
|
|
<div className="flex items-start gap-3">
|
|
<div className="w-10 h-10 rounded-lg bg-slate-800 flex items-center justify-center" style={{ borderBottom: `2px solid ${getProviderColor(account.provider)}` }}>
|
|
<CloudBrandIcon provider={account.provider} className="h-6 w-6" />
|
|
</div>
|
|
<div>
|
|
<h3 className="font-medium text-slate-100">{account.name}</h3>
|
|
<p className="text-sm text-slate-400 mt-0.5">{providerLabels[account.provider] || account.provider}</p>
|
|
<div className="flex items-center gap-3 mt-2">
|
|
{accessKey && (
|
|
<Badge variant="secondary" className="bg-slate-700 text-slate-300">
|
|
<Key className="h-3 w-3 mr-1" />
|
|
{accessKey.slice(0, 8)}...
|
|
</Badge>
|
|
)}
|
|
{account.status === 'active' && (
|
|
<Badge className="bg-green-500/20 text-green-400 border-0">
|
|
<CheckCircle2 className="h-3 w-3 mr-1" />
|
|
正常
|
|
</Badge>
|
|
)}
|
|
{account.status === 'inactive' && (
|
|
<Badge className="bg-slate-500/20 text-slate-400 border-0">
|
|
<XCircle className="h-3 w-3 mr-1" />
|
|
已停用
|
|
</Badge>
|
|
)}
|
|
{account.status === 'error' && (
|
|
<Badge className="bg-red-500/20 text-red-400 border-0">
|
|
<Key className="h-3 w-3 mr-1" />
|
|
异常
|
|
</Badge>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center gap-1">
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-8 w-8 text-slate-400 hover:text-blue-400"
|
|
onClick={() => handleOpenEdit(account)}
|
|
>
|
|
<Pencil className="h-4 w-4" />
|
|
</Button>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-8 w-8 text-slate-400 hover:text-red-400"
|
|
onClick={() => handleDelete(account.id)}
|
|
>
|
|
<Trash2 className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className="grid grid-cols-3 gap-4 mt-4 pt-4 border-t border-slate-700">
|
|
<div>
|
|
<p className="text-xs text-slate-400">月费用</p>
|
|
<p className="text-lg font-medium text-emerald-400">¥{Number(monthlyCost).toLocaleString()}</p>
|
|
</div>
|
|
<div>
|
|
<p className="text-xs text-slate-400">服务器数</p>
|
|
<p className="text-lg font-medium text-slate-100">{serverCount}</p>
|
|
</div>
|
|
<div>
|
|
<p className="text-xs text-slate-400">最后同步</p>
|
|
<p className="text-sm text-slate-300">
|
|
{lastSync !== '-' ? new Date(lastSync).toLocaleString() : '-'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
|
|
{/* 创建账号 Dialog */}
|
|
<Dialog open={formOpen} onOpenChange={setFormOpen}>
|
|
<DialogContent className="bg-slate-800 border-slate-700 text-slate-100 max-w-lg">
|
|
<DialogHeader>
|
|
<DialogTitle className="text-slate-100 flex items-center gap-2">
|
|
<Cloud className="h-5 w-5 text-blue-400" />
|
|
{editingAccount ? '编辑云账号' : '添加云账号'}
|
|
</DialogTitle>
|
|
<DialogDescription className="text-slate-400">
|
|
{editingAccount ? '修改账号信息和访问凭证,留空则保持原凭证不变' : '录入多云平台的访问凭证,用于资源同步与成本统计'}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<div className="space-y-4 py-2">
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="space-y-1.5">
|
|
<Label className="text-slate-300">
|
|
账号名称 <span className="text-red-400">*</span>
|
|
</Label>
|
|
<Input
|
|
placeholder="阿里云生产账号"
|
|
value={formData.name}
|
|
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
className="bg-slate-900 border-slate-700 text-slate-100 placeholder:text-slate-500"
|
|
/>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label className="text-slate-300">
|
|
云平台 <span className="text-red-400">*</span>
|
|
</Label>
|
|
<select
|
|
value={formData.provider}
|
|
onChange={(e) => setFormData({ ...formData, provider: e.target.value })}
|
|
className="w-full px-3 py-2 bg-slate-900 border border-slate-700 rounded-md text-slate-100 text-sm"
|
|
>
|
|
<option value="aliyun">阿里云</option>
|
|
<option value="tencent">腾讯云</option>
|
|
<option value="aws">AWS</option>
|
|
<option value="huawei">华为云</option>
|
|
<option value="azure">Azure</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label className="text-slate-300">
|
|
AccessKey
|
|
{editingAccount && <span className="text-xs text-slate-500 ml-2">留空保持不变</span>}
|
|
</Label>
|
|
<Input
|
|
placeholder="LTAI..."
|
|
value={formData.access_key}
|
|
onChange={(e) => setFormData({ ...formData, access_key: e.target.value })}
|
|
className="bg-slate-900 border-slate-700 text-slate-100 placeholder:text-slate-500 font-mono text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label className="text-slate-300">
|
|
SecretKey
|
|
{editingAccount && <span className="text-xs text-slate-500 ml-2">留空保持不变</span>}
|
|
</Label>
|
|
<Input
|
|
type="password"
|
|
placeholder="••••••••"
|
|
value={formData.secret_key}
|
|
onChange={(e) => setFormData({ ...formData, secret_key: e.target.value })}
|
|
className="bg-slate-900 border-slate-700 text-slate-100 placeholder:text-slate-500 font-mono text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="space-y-1.5">
|
|
<Label className="text-slate-300">月费用 (元)</Label>
|
|
<Input
|
|
type="number"
|
|
min="0"
|
|
placeholder="0"
|
|
value={formData.monthly_cost}
|
|
onChange={(e) => setFormData({ ...formData, monthly_cost: e.target.value })}
|
|
className="bg-slate-900 border-slate-700 text-slate-100 placeholder:text-slate-500"
|
|
/>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label className="text-slate-300">状态</Label>
|
|
<select
|
|
value={formData.status}
|
|
onChange={(e) => setFormData({ ...formData, status: e.target.value })}
|
|
className="w-full px-3 py-2 bg-slate-900 border border-slate-700 rounded-md text-slate-100 text-sm"
|
|
>
|
|
<option value="active">正常</option>
|
|
<option value="inactive">已停用</option>
|
|
<option value="error">异常</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => setFormOpen(false)} className="border-slate-700 text-slate-300 hover:bg-slate-700">
|
|
取消
|
|
</Button>
|
|
<Button
|
|
className="bg-blue-600 hover:bg-blue-700"
|
|
disabled={!formData.name.trim() || submitting}
|
|
onClick={handleSubmit}
|
|
>
|
|
{submitting ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : editingAccount ? <Pencil className="h-4 w-4 mr-2" /> : <Plus className="h-4 w-4 mr-2" />}
|
|
{editingAccount ? '保存修改' : '创建账号'}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
}
|