feat: maintain netbox-inventory assets for clients and show the agent's software list
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d6622cf48f
commit
98c8647501
7 files changed
+239
-4
No files matched your search
@@ -810,7 +810,9 @@ class Actions:
|
||||
self.state.client_engine = engine
|
||||
self.state.clients_changed.emit()
|
||||
text = (f"{report['devices_created']} Geräte angelegt, {report['devices_updated']} aktualisiert, "
|
||||
f"{report['interfaces_created']} Schnittstellen, {report['ips_created']} IP-Adressen")
|
||||
f"{report['interfaces_created']} Schnittstellen, {report['ips_created']} IP-Adressen"
|
||||
+ (f", {report['assets_created']} Assets angelegt, {report['assets_updated']} aktualisiert"
|
||||
if report.get("assets_created") or report.get("assets_updated") else ""))
|
||||
area_log("client")(f"Client-Übertragung: {text}", "success")
|
||||
if report["errors"]:
|
||||
widgets.message(self.window, "Mit Problemen abgeschlossen", text + ".", "warning",
|
||||
|
||||
@@ -1,15 +1,83 @@
|
||||
"""Dialogs for transferring scanned Windows clients to NetBox."""
|
||||
|
||||
from PySide6.QtWidgets import QFormLayout, QHBoxLayout, QTreeWidget, QTreeWidgetItem
|
||||
import csv
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtGui import QGuiApplication
|
||||
from PySide6.QtWidgets import QFileDialog, QFormLayout, QHBoxLayout, QTreeWidget, QTreeWidgetItem
|
||||
|
||||
from netbox_sync.ui.dialogs import _Dialog, _mark_invalid
|
||||
from netbox_sync.ui.icons import icon
|
||||
from netbox_sync.ui.theme import theme
|
||||
from netbox_sync.ui.widgets import Badge, Banner, SearchableComboBox, checkbox, form_dialog_buttons, label
|
||||
from netbox_sync.ui.models import Column, FilterProxy, RowTableModel
|
||||
from netbox_sync.ui.widgets import (
|
||||
Badge,
|
||||
Banner,
|
||||
SearchableComboBox,
|
||||
SearchField,
|
||||
Table,
|
||||
button,
|
||||
checkbox,
|
||||
error,
|
||||
form_dialog_buttons,
|
||||
label,
|
||||
)
|
||||
|
||||
ROLE_HINTS = ("client", "arbeitsplatz", "workstation", "pc", "notebook")
|
||||
|
||||
|
||||
class ClientSoftwareDialog(_Dialog):
|
||||
"""Installed software of one client (from the NetBox Windows Agent), searchable and exportable."""
|
||||
|
||||
def __init__(self, parent, row):
|
||||
software = row.get("software") or []
|
||||
super().__init__(parent, f"Software – {row['name']}",
|
||||
f"{len(software)} Programme laut NetBox-Agent (maschinenweit installierte Software)"
|
||||
+ (f" · erfasst {row['scanned_text']}" if row.get("scanned_text") else ""), width=760)
|
||||
self.resize(900, 640)
|
||||
self.row = row
|
||||
self.search = SearchField("Programm, Version, Hersteller …")
|
||||
self.root.addWidget(self.search)
|
||||
columns = [Column("name", "Programm", 420), Column("version", "Version", 150),
|
||||
Column("publisher", "Hersteller", 260)]
|
||||
self.model = RowTableModel(columns, row_id=lambda item: (item["name"], item["version"]))
|
||||
self.model.set_rows(software)
|
||||
self.proxy = FilterProxy(self.model)
|
||||
self.search.textChanged.connect(self.proxy.set_text)
|
||||
table = Table()
|
||||
table.setModel(self.proxy)
|
||||
table.sortByColumn(0, Qt.AscendingOrder)
|
||||
for index, column in enumerate(columns):
|
||||
table.setColumnWidth(index, column.width)
|
||||
self.root.addWidget(table, 1)
|
||||
buttons = QHBoxLayout()
|
||||
buttons.addWidget(button("Als CSV speichern …", "download", on_click=self.save_csv))
|
||||
buttons.addWidget(button("Kopieren", "copy", on_click=self.copy))
|
||||
buttons.addStretch(1)
|
||||
buttons.addWidget(button("Schließen", variant="primary", on_click=self.accept))
|
||||
self.root.addLayout(buttons)
|
||||
|
||||
def _lines(self):
|
||||
return [(item["name"], item["version"], item["publisher"]) for item in self.row.get("software") or []]
|
||||
|
||||
def copy(self):
|
||||
QGuiApplication.clipboard().setText("\n".join("\t".join(line) for line in self._lines()))
|
||||
|
||||
def save_csv(self):
|
||||
path, _filter = QFileDialog.getSaveFileName(self, "Software speichern", f"{self.row['name']}-software.csv",
|
||||
"CSV (*.csv)")
|
||||
if not path:
|
||||
return
|
||||
try:
|
||||
# utf-8-sig and semicolons so that Excel opens it directly
|
||||
with open(path, "w", encoding="utf-8-sig", newline="") as stream:
|
||||
writer = csv.writer(stream, delimiter=";")
|
||||
writer.writerow(("Programm", "Version", "Hersteller"))
|
||||
writer.writerows(self._lines())
|
||||
except OSError as problem:
|
||||
error(self, "Speichern fehlgeschlagen", str(problem))
|
||||
|
||||
|
||||
class ClientTargetDialog(_Dialog):
|
||||
"""Choose site, role and tenant for the new devices."""
|
||||
|
||||
@@ -95,6 +163,9 @@ class ClientPreviewDialog(_Dialog):
|
||||
(f"{report['interfaces_created']} Schnittstellen", "info"),
|
||||
(f"{report['ips_created']} IP-Adressen", "info"),
|
||||
]
|
||||
assets = report.get("assets_created", 0) + report.get("assets_updated", 0)
|
||||
if assets:
|
||||
items.append((f"{assets} Assets", "info"))
|
||||
if report["errors"]:
|
||||
items.append((f"{len(report['errors'])} Probleme", "error"))
|
||||
for text, kind in items:
|
||||
|
||||
@@ -12,6 +12,7 @@ from PySide6.QtWidgets import (
|
||||
QGridLayout,
|
||||
QHBoxLayout,
|
||||
QLineEdit,
|
||||
QMenu,
|
||||
QPlainTextEdit,
|
||||
QRadioButton,
|
||||
QSpinBox,
|
||||
@@ -23,7 +24,10 @@ from netbox_sync.core import agent_client
|
||||
from netbox_sync.core.client_sync import clean_manufacturer
|
||||
from netbox_sync.core.clients import WINRM_HTTP_PORT, WINRM_HTTPS_PORT, os_label, os_version_label
|
||||
from netbox_sync.ui import widgets
|
||||
from netbox_sync.ui.client_dialogs import ClientSoftwareDialog
|
||||
from netbox_sync.ui.client_help import ClientHelpDialog
|
||||
from netbox_sync.ui.icons import icon
|
||||
from netbox_sync.ui.theme import theme
|
||||
from netbox_sync.ui.credentials import ask_credentials
|
||||
from netbox_sync.ui.models import Column, FilterProxy, RowTableModel
|
||||
from netbox_sync.ui.pages import Page
|
||||
@@ -235,6 +239,10 @@ class ClientsPage(Page):
|
||||
Column("chassis", "Bauform", 95),
|
||||
Column("user", "Angemeldet", 140),
|
||||
Column("ips", "IP-Adressen", 160, value=_ips),
|
||||
Column("software", "Software", 90,
|
||||
value=lambda row: f"{len(row['software'])} Programme" if row.get("software") else "",
|
||||
sort=lambda row: len(row.get("software") or []), color=lambda row: "accent",
|
||||
tooltip=lambda row: "Doppelklick: vollständige Liste" if row.get("software") else ""),
|
||||
Column("last_logon", "AD-Anmeldung", 105, value=lambda row: _date((row.get("ad") or {}).get("last_logon")),
|
||||
sort=lambda row: (row.get("ad") or {}).get("last_logon") or 0),
|
||||
]
|
||||
@@ -246,6 +254,8 @@ class ClientsPage(Page):
|
||||
for index, column in enumerate(columns):
|
||||
self.table.setColumnWidth(index, column.width)
|
||||
self.table.doubleClicked.connect(self._toggle)
|
||||
self.table.setContextMenuPolicy(Qt.CustomContextMenu)
|
||||
self.table.customContextMenuRequested.connect(self._context_menu)
|
||||
card.add(self.table, 1)
|
||||
|
||||
footer = QHBoxLayout()
|
||||
@@ -506,8 +516,28 @@ class ClientsPage(Page):
|
||||
|
||||
def _toggle(self, index):
|
||||
row = self.proxy.row_at(index)
|
||||
if self.model.columns[index.column()].key == "software" and row.get("software"):
|
||||
self.show_software(row)
|
||||
return
|
||||
self.model.set_checked({row["id"]}, row["id"] not in self.model.checked)
|
||||
|
||||
def show_software(self, row):
|
||||
ClientSoftwareDialog(self.window, dict(row, scanned_text=time.strftime(
|
||||
"%d.%m.%Y %H:%M", time.localtime(row["scanned_at"])) if row.get("scanned_at") else "")).exec()
|
||||
|
||||
def _context_menu(self, position):
|
||||
index = self.table.indexAt(position)
|
||||
if not index.isValid():
|
||||
return
|
||||
row = self.proxy.row_at(index)
|
||||
menu = QMenu(self)
|
||||
action = menu.addAction(icon("list", theme.color("text_muted")), "Software anzeigen …",
|
||||
lambda: self.show_software(row))
|
||||
action.setEnabled(bool(row.get("software")))
|
||||
if not row.get("software"):
|
||||
action.setText("Software anzeigen … (nur mit NetBox-Agent)")
|
||||
menu.exec(self.table.viewport().mapToGlobal(position))
|
||||
|
||||
def select_visible(self):
|
||||
self.model.set_checked({row["id"] for row in self.proxy.visible_rows()})
|
||||
|
||||
|
||||
Reference in new issue
Block a user