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 = { aws: 'AWS', aliyun: '阿里云', tencent: '腾讯云', huawei: '华为云', azure: 'Azure', gcp: 'GCP', }; export function CloudAccountManager() { const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(''); const [selectedProvider, setSelectedProvider] = useState('all'); // 创建/编辑账号表单状态 const [formOpen, setFormOpen] = useState(false); const [submitting, setSubmitting] = useState(false); const [editingAccount, setEditingAccount] = useState(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 = { 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 = 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 (
{/* Header */}

云账号管理

管理多云平台的访问凭证和账号信息

{/* Stats */}

账号总数

{stats.total}

正常

{stats.active}

已停用

{stats.inactive}

异常

{stats.error}

{/* Filters */}
setSearchQuery(e.target.value)} className="pl-10 bg-slate-800 border-slate-700 text-slate-100" />
{/* Account List */} {loading ? ( 加载中... ) : filteredAccounts.length === 0 ? ( 暂无云账号数据 ) : (
{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 (

{account.name}

{providerLabels[account.provider] || account.provider}

{accessKey && ( {accessKey.slice(0, 8)}... )} {account.status === 'active' && ( 正常 )} {account.status === 'inactive' && ( 已停用 )} {account.status === 'error' && ( 异常 )}

月费用

¥{Number(monthlyCost).toLocaleString()}

服务器数

{serverCount}

最后同步

{lastSync !== '-' ? new Date(lastSync).toLocaleString() : '-'}

); })}
)} {/* 创建账号 Dialog */} {editingAccount ? '编辑云账号' : '添加云账号'} {editingAccount ? '修改账号信息和访问凭证,留空则保持原凭证不变' : '录入多云平台的访问凭证,用于资源同步与成本统计'}
setFormData({ ...formData, name: e.target.value })} className="bg-slate-900 border-slate-700 text-slate-100 placeholder:text-slate-500" />
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" />
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" />
setFormData({ ...formData, monthly_cost: e.target.value })} className="bg-slate-900 border-slate-700 text-slate-100 placeholder:text-slate-500" />
); }