feat(ne): batch apply jump proxy on selected NEs

Add POST /v1/managed-ne/batch-hop, shared HopProxyFields form, and toolbar button next to connectivity test.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-05-28 17:28:14 +08:00
parent 4575fef523
commit d395aa7174
8 changed files with 329 additions and 82 deletions

View file

@ -8,8 +8,9 @@ from .db import get_db
from .device_types import SUPPORTED_DEVICE_TYPES, SUPPORTED_VENDORS
from .ne_connect import schedule_connect_tests
from .ne_crypto import credentials_configured
from .ne_schemas import ConnectTestRequest, ManagedNeCreate, ManagedNeUpdate
from .ne_schemas import BatchHopApplyRequest, ConnectTestRequest, ManagedNeCreate, ManagedNeUpdate
from .ne_service import (
batch_apply_hop_proxy,
build_managed_ne_import_template,
create_managed_ne,
delete_managed_ne,
@ -90,6 +91,11 @@ async def api_import_managed_ne(file: UploadFile = File(...), db: Session = Depe
return import_managed_ne(db, content, file.filename or "import.xlsx").model_dump()
@router.post("/batch-hop")
def api_batch_apply_hop(body: BatchHopApplyRequest, db: Session = Depends(get_db)):
return batch_apply_hop_proxy(db, body.ids, body.hop)
@router.post("/connect-test")
def api_connect_test(body: ConnectTestRequest, db: Session = Depends(get_db)):
ids = [str(x).strip() for x in body.ids if str(x).strip()]

View file

@ -108,6 +108,24 @@ class ConnectTestRequest(BaseModel):
ids: list[str] = Field(min_length=1)
class HopProxyConfig(BaseModel):
"""Shared jump-host (proxy) settings applied to one or many NEs."""
hop_vendor: str = "zte"
hop_host: str
hop_port: int = 22
hop_protocol: str = "ssh"
hop_username: str
hop_password: str
hop_command_template: str = ""
hop_vrf: str = ""
class BatchHopApplyRequest(BaseModel):
ids: list[str] = Field(min_length=1)
hop: HopProxyConfig
class ImportFailure(BaseModel):
row: int
reason: str

View file

@ -11,7 +11,15 @@ from sqlalchemy.orm import Session
from .device_types import SUPPORTED_DEVICE_TYPES, SUPPORTED_VENDORS
from .models import ManagedNE
from .ne_crypto import CredentialCryptoError, credentials_configured, decrypt_secret, encrypt_secret
from .ne_schemas import ImportFailure, ImportResult, ManagedNeCreate, ManagedNeOut, ManagedNeUpdate
from .ne_schemas import (
HopProxyConfig,
ImportFailure,
ImportResult,
ManagedNeCreate,
ManagedNeOut,
ManagedNeUpdate,
)
from .ne_session_factory import default_zte_hop_template
IMPORT_COLUMNS = (
"device_type",
@ -266,6 +274,50 @@ def update_managed_ne(db: Session, ne_id: str, body: ManagedNeUpdate) -> Managed
return row_to_out(row)
def batch_apply_hop_proxy(db: Session, ids: list[str], hop: HopProxyConfig) -> dict[str, Any]:
"""Apply the same jump-host (proxy) settings to multiple managed NEs."""
_require_crypto()
hop_host = str(hop.hop_host or "").strip()
hop_user = str(hop.hop_username or "").strip()
hop_pass = str(hop.hop_password or "").strip()
if not hop_host:
raise HTTPException(status_code=400, detail="hop_host_required")
if not hop_user:
raise HTTPException(status_code=400, detail="hop_username_required")
if not hop_pass:
raise HTTPException(status_code=400, detail="hop_password_required")
template = str(hop.hop_command_template or "").strip()
if not template:
template = default_zte_hop_template(hop.hop_protocol, hop.hop_vrf)
ne_ids = [str(x).strip() for x in ids if str(x).strip()]
if not ne_ids:
raise HTTPException(status_code=400, detail="ids_required")
rows = db.query(ManagedNE).filter(ManagedNE.id.in_(ne_ids)).all()
found_ids = {str(r.id) for r in rows}
missing = [x for x in ne_ids if x not in found_ids]
if missing:
raise HTTPException(status_code=404, detail=f"managed_ne_not_found: {','.join(missing[:5])}")
enc = encrypt_secret(hop_pass)
now = _now()
for row in rows:
row.hop_enabled = True
row.hop_vendor = _normalize_hop_vendor(hop.hop_vendor)
row.hop_host = hop_host
row.hop_port = int(hop.hop_port or 22)
row.hop_protocol = _normalize_protocol(hop.hop_protocol)
row.hop_username = hop_user
row.hop_password_enc = enc
row.hop_command_template = template
row.hop_vrf = str(hop.hop_vrf or "").strip()
row.updated_at = now
db.commit()
return {"ok": True, "updated": len(rows)}
def delete_managed_ne(db: Session, ne_id: str) -> dict[str, bool]:
row = db.get(ManagedNE, ne_id)
if not row:

View file

@ -0,0 +1,134 @@
import type { ReactNode } from "react";
import { useI18n } from "../i18n";
import { isAutoHopTemplate, zteHopTemplate } from "../utils/zteHop";
export type HopProxyFieldsState = {
hop_host: string;
hop_port: number;
hop_protocol: string;
hop_username: string;
hop_password: string;
hop_command_template: string;
hop_vrf: string;
};
export const emptyHopProxyFields = (): HopProxyFieldsState => ({
hop_host: "",
hop_port: 22,
hop_protocol: "ssh",
hop_username: "",
hop_password: "",
hop_command_template: zteHopTemplate("ssh", ""),
hop_vrf: "",
});
function FormLabel({ children, required }: { children: ReactNode; required?: boolean }) {
return (
<span className="form-label">
{children}
{required ? (
<span className="form-label__required" title="required" aria-hidden="true">
{" "}
*
</span>
) : null}
</span>
);
}
function applyHopTemplate(
prev: HopProxyFieldsState,
protocol: string,
vrf: string,
force = false,
): Partial<HopProxyFieldsState> {
if (!force && !isAutoHopTemplate(prev.hop_command_template, prev.hop_protocol, prev.hop_vrf)) {
return {};
}
return { hop_command_template: zteHopTemplate(protocol, vrf) };
}
type Props = {
value: HopProxyFieldsState;
onChange: (patch: Partial<HopProxyFieldsState>) => void;
hopPasswordRequired?: boolean;
hopPasswordOptional?: boolean;
};
export function HopProxyFields({
value,
onChange,
hopPasswordRequired = true,
hopPasswordOptional = false,
}: Props) {
const { t } = useI18n();
const set = (patch: Partial<HopProxyFieldsState>) => onChange(patch);
return (
<div className="form-grid">
<label>
<FormLabel required>{t("managedNe.hop.host")}</FormLabel>
<input required value={value.hop_host} onChange={(e) => set({ hop_host: e.target.value })} />
</label>
<label>
<FormLabel>{t("managedNe.hop.port")}</FormLabel>
<input
type="number"
value={value.hop_port}
onChange={(e) => set({ hop_port: Number(e.target.value) || 22 })}
/>
</label>
<label>
<FormLabel>{t("managedNe.hop.protocol")}</FormLabel>
<select
value={value.hop_protocol}
onChange={(e) => {
const hop_protocol = e.target.value;
set({ hop_protocol, ...applyHopTemplate(value, hop_protocol, value.hop_vrf) });
}}
>
<option value="ssh">ssh</option>
<option value="telnet">telnet</option>
</select>
</label>
<label>
<FormLabel required>{t("managedNe.hop.username")}</FormLabel>
<input required value={value.hop_username} onChange={(e) => set({ hop_username: e.target.value })} />
</label>
<label>
<FormLabel required={hopPasswordRequired}>
{t("managedNe.hop.password")}
{hopPasswordOptional ? (
<span className="form-label__optional"> ({t("managedNe.form.passwordOptional")})</span>
) : null}
</FormLabel>
<input
type="password"
required={hopPasswordRequired}
value={value.hop_password}
onChange={(e) => set({ hop_password: e.target.value })}
/>
</label>
<label>
<FormLabel>{t("managedNe.hop.vrf")}</FormLabel>
<input
value={value.hop_vrf}
onChange={(e) => {
const hop_vrf = e.target.value;
set({ hop_vrf, ...applyHopTemplate(value, value.hop_protocol, hop_vrf) });
}}
/>
</label>
<label className="form-grid__full">
<FormLabel>{t("managedNe.hop.commandTemplate")}</FormLabel>
<input
value={value.hop_command_template}
onChange={(e) => set({ hop_command_template: e.target.value })}
placeholder={zteHopTemplate(value.hop_protocol, value.hop_vrf)}
/>
<span className="form-field-hint">{t("managedNe.hop.templateHint")}</span>
</label>
</div>
);
}

View file

@ -192,6 +192,13 @@ const en = {
hostRequired: "Jump host is required",
userRequired: "Jump username is required",
passwordRequired: "Jump password is required",
batchAdd: "Add proxy",
batchTitle: "Batch add jump proxy",
batchHint: "Apply the jump-host settings below to {{n}} selected NE(s) (overwrites existing proxy config).",
batchDone: "Proxy applied to {{n}} NE(s)",
selectRequired: "Select network elements first",
applying: "Applying…",
apply: "Apply to selected",
},
},
ume: {

View file

@ -191,6 +191,13 @@ const zh = {
hostRequired: "请填写跳板地址",
userRequired: "请填写跳板用户名",
passwordRequired: "请填写跳板密码",
batchAdd: "添加代理",
batchTitle: "批量添加代理",
batchHint: "将以下跳板配置应用到已选中的 {{n}} 台网元(会覆盖原有跳板设置)。",
batchDone: "已为 {{n}} 台网元配置代理",
selectRequired: "请先勾选要配置的网元",
applying: "应用中…",
apply: "应用到选中网元",
},
},
ume: {

View file

@ -1,6 +1,7 @@
import { useMemo, useRef, useState, type ReactNode } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
batchApplyHopManagedNe,
connectTestManagedNe,
createManagedNe,
deleteManagedNe,
@ -10,6 +11,7 @@ import {
managedNeImportTemplateUrl,
updateManagedNe,
} from "../services/api";
import { HopProxyFields, emptyHopProxyFields, type HopProxyFieldsState } from "../components/HopProxyFields";
import { queryKeys } from "../constants/queryKeys";
import { useI18n } from "../i18n";
import { useToast } from "../hooks/useToast";
@ -101,8 +103,10 @@ export function NePage() {
const [pageSize, setPageSize] = useState(50);
const [selected, setSelected] = useState<string[]>([]);
const [modalOpen, setModalOpen] = useState(false);
const [batchHopOpen, setBatchHopOpen] = useState(false);
const [editing, setEditing] = useState<ManagedNeItem | null>(null);
const [form, setForm] = useState<FormState>(emptyForm);
const [batchHop, setBatchHop] = useState<HopProxyFieldsState>(emptyHopProxyFields);
const metaQuery = useQuery({
queryKey: queryKeys.managedNeMeta,
@ -196,6 +200,27 @@ export function NePage() {
onError: (err) => showError(String(err)),
});
const batchHopMutation = useMutation({
mutationFn: () =>
batchApplyHopManagedNe(selected, {
hop_vendor: "zte",
hop_host: batchHop.hop_host.trim(),
hop_port: batchHop.hop_port,
hop_protocol: batchHop.hop_protocol,
hop_username: batchHop.hop_username.trim(),
hop_password: batchHop.hop_password,
hop_command_template: batchHop.hop_command_template.trim(),
hop_vrf: batchHop.hop_vrf.trim(),
}),
onSuccess: async (res) => {
setBatchHopOpen(false);
setBatchHop(emptyHopProxyFields());
showOk(t("managedNe.hop.batchDone", { n: res.updated }));
await invalidateList();
},
onError: (err) => showError(String(err)),
});
const importMutation = useMutation({
mutationFn: importManagedNe,
onSuccess: async (res) => {
@ -318,6 +343,20 @@ export function NePage() {
>
{connectMutation.isPending ? t("managedNe.connect.running") : t("managedNe.connect.run")}
</button>
<button
type="button"
disabled={!credsOk || selected.length === 0 || batchHopMutation.isPending}
onClick={() => {
if (selected.length === 0) {
showError(t("managedNe.hop.selectRequired"));
return;
}
setBatchHop(emptyHopProxyFields());
setBatchHopOpen(true);
}}
>
{batchHopMutation.isPending ? t("managedNe.hop.applying") : t("managedNe.hop.batchAdd")}
</button>
<button type="button" onClick={() => invalidateList()}>
{t("common.refresh")}
</button>
@ -565,86 +604,20 @@ export function NePage() {
{t("managedNe.hop.enable")}
</label>
{form.hop_enabled ? (
<div className="form-grid">
<label>
<FormLabel required>{t("managedNe.hop.host")}</FormLabel>
<input
required
value={form.hop_host}
onChange={(e) => setForm({ ...form, hop_host: e.target.value })}
/>
</label>
<label>
<FormLabel>{t("managedNe.hop.port")}</FormLabel>
<input
type="number"
value={form.hop_port}
onChange={(e) => setForm({ ...form, hop_port: Number(e.target.value) || 22 })}
/>
</label>
<label>
<FormLabel>{t("managedNe.hop.protocol")}</FormLabel>
<select
value={form.hop_protocol}
onChange={(e) => {
const hop_protocol = e.target.value;
setForm((prev) => ({
...prev,
hop_protocol,
...applyHopTemplate(prev, hop_protocol, prev.hop_vrf),
}));
}}
>
<option value="ssh">ssh</option>
<option value="telnet">telnet</option>
</select>
</label>
<label>
<FormLabel required>{t("managedNe.hop.username")}</FormLabel>
<input
required
value={form.hop_username}
onChange={(e) => setForm({ ...form, hop_username: e.target.value })}
/>
</label>
<label>
<FormLabel required={!editing}>
{t("managedNe.hop.password")}
{editing ? (
<span className="form-label__optional"> ({t("managedNe.form.passwordOptional")})</span>
) : null}
</FormLabel>
<input
type="password"
required={!editing}
value={form.hop_password}
onChange={(e) => setForm({ ...form, hop_password: e.target.value })}
/>
</label>
<label>
<FormLabel>{t("managedNe.hop.vrf")}</FormLabel>
<input
value={form.hop_vrf}
onChange={(e) => {
const hop_vrf = e.target.value;
setForm((prev) => ({
...prev,
hop_vrf,
...applyHopTemplate(prev, prev.hop_protocol, hop_vrf),
}));
}}
/>
</label>
<label className="form-grid__full">
<FormLabel>{t("managedNe.hop.commandTemplate")}</FormLabel>
<input
value={form.hop_command_template}
onChange={(e) => setForm({ ...form, hop_command_template: e.target.value })}
placeholder={zteHopTemplate(form.hop_protocol, form.hop_vrf)}
/>
<span className="form-field-hint">{t("managedNe.hop.templateHint")}</span>
</label>
</div>
<HopProxyFields
value={{
hop_host: form.hop_host,
hop_port: form.hop_port,
hop_protocol: form.hop_protocol,
hop_username: form.hop_username,
hop_password: form.hop_password,
hop_command_template: form.hop_command_template,
hop_vrf: form.hop_vrf,
}}
onChange={(patch) => setForm((prev) => ({ ...prev, ...patch }))}
hopPasswordRequired={!editing}
hopPasswordOptional={Boolean(editing)}
/>
) : null}
</fieldset>
<div className="modal__actions">
@ -658,6 +631,42 @@ export function NePage() {
</div>
</div>
) : null}
{batchHopOpen ? (
<div className="modal-backdrop" role="presentation" onClick={() => setBatchHopOpen(false)}>
<div className="modal" role="dialog" onClick={(e) => e.stopPropagation()}>
<h3>{t("managedNe.hop.batchTitle")}</h3>
<p className="form-hint">{t("managedNe.hop.batchHint", { n: selected.length })}</p>
<HopProxyFields value={batchHop} onChange={(patch) => setBatchHop((prev) => ({ ...prev, ...patch }))} />
<div className="modal__actions">
<button type="button" onClick={() => setBatchHopOpen(false)}>
{t("managedNe.form.cancel")}
</button>
<button
type="button"
disabled={batchHopMutation.isPending}
onClick={() => {
if (!batchHop.hop_host.trim()) {
showError(t("managedNe.hop.hostRequired"));
return;
}
if (!batchHop.hop_username.trim()) {
showError(t("managedNe.hop.userRequired"));
return;
}
if (!batchHop.hop_password) {
showError(t("managedNe.hop.passwordRequired"));
return;
}
batchHopMutation.mutate();
}}
>
{batchHopMutation.isPending ? t("managedNe.hop.applying") : t("managedNe.hop.apply")}
</button>
</div>
</div>
</div>
) : null}
</div>
);
}

View file

@ -129,6 +129,20 @@ export const deleteManagedNe = (id: string) => apiDelete<{ ok: boolean }>(`/v1/m
export const connectTestManagedNe = (ids: string[]) =>
apiPost<{ ok: boolean; submitted: number }>("/v1/managed-ne/connect-test", { ids });
export const batchApplyHopManagedNe = (
ids: string[],
hop: {
hop_host: string;
hop_port: number;
hop_protocol: string;
hop_username: string;
hop_password: string;
hop_command_template: string;
hop_vrf: string;
hop_vendor?: string;
},
) => apiPost<{ ok: boolean; updated: number }>("/v1/managed-ne/batch-hop", { ids, hop });
export const managedNeImportTemplateUrl = (format: "xlsx" | "csv" = "xlsx") =>
`/v1/managed-ne/import/template?format=${format}`;