Add usernames and sudo_usernames fields to VM request form

- DB: add usernames/sudo_usernames columns to vm_requests table
- create.php: add form inputs for usernames and sudo_usernames
- send.php: capture, validate, and store both fields in DB
- netbox_integration/netbox_api.php: pass custom_fields to NetBox VM API
- requests.php: show usernames/sudo_usernames in request list
This commit is contained in:
alex
2026-05-09 23:57:36 +02:00
parent d629689473
commit 8c069ee82a
5 changed files with 59 additions and 3 deletions

View File

@@ -114,6 +114,34 @@ $error = $_GET['error'] ?? '';
></textarea>
</div>
<div class="form-group">
<label for="usernames">Usernames <span style="font-weight:400;color:var(--text-tertiary)">(comma-separated, optional)</span></label>
<span class="field-label-sub">Linux user accounts to create on this VM (e.g. <em>jdoe, admin</em>).</span>
<input
type="text"
id="usernames"
name="usernames"
maxlength="500"
placeholder="jdoe, admin"
autocomplete="off"
spellcheck="false"
>
</div>
<div class="form-group">
<label for="sudo_usernames">Sudo Usernames <span style="font-weight:400;color:var(--text-tertiary)">(comma-separated, optional)</span></label>
<span class="field-label-sub">Which users should have sudo access (e.g. <em>admin</em>).</span>
<input
type="text"
id="sudo_usernames"
name="sudo_usernames"
maxlength="500"
placeholder="admin"
autocomplete="off"
spellcheck="false"
>
</div>
<button type="submit" id="submitBtn">
<span class="btn-label">Submit Request</span>
</button>

View File

@@ -119,6 +119,18 @@ class NetBoxClient {
'description' => $params['description'] ?? '',
];
// Custom fields: usernames and sudo_usernames
$cf = [];
if (!empty($params['usernames'])) {
$cf['usernames'] = $params['usernames'];
}
if (!empty($params['sudo_usernames'])) {
$cf['sudo_usernames'] = $params['sudo_usernames'];
}
if (!empty($cf)) {
$payload['custom_fields'] = $cf;
}
// Tag lookup for domain — tags must be numeric IDs in the NetBox API
if (!empty($params['domain'])) {
$tag = $this->findTag($params['domain']);

View File

@@ -13,7 +13,7 @@ $totalPages = max(1, (int) ceil($total / $perPage));
if ($page > $totalPages) $page = $totalPages;
$stmt = db()->prepare(
'SELECT id, vm_name, domain_name, size, vcpus, ram_gb, disk_gb, status, description, created_at, netbox_device_id
'SELECT id, vm_name, domain_name, size, vcpus, ram_gb, disk_gb, status, description, usernames, sudo_usernames, created_at, netbox_device_id
FROM vm_requests
ORDER BY created_at DESC
LIMIT :limit OFFSET :offset'
@@ -88,6 +88,14 @@ $recentRequests = $stmt->fetchAll();
<?= h($request['description']) ?>
</div>
<?php endif; ?>
<?php if (!empty($request['usernames'])): ?>
<div style="font-size:0.75rem;margin-top:3px;color:var(--text-secondary);">
Users: <strong><?= h($request['usernames']) ?></strong>
<?php if (!empty($request['sudo_usernames'])): ?>
<span style="color:var(--warning-text);font-weight:600;">| sudo: <?= h($request['sudo_usernames']) ?></span>
<?php endif; ?>
</div>
<?php endif; ?>
</td>
<td><?= h($request['domain_name']) ?></td>
<td>

View File

@@ -24,6 +24,8 @@ $vmName = trim((string) ($_POST['VM_Name'] ?? ''));
$size = trim((string) ($_POST['size'] ?? ''));
$domain = trim((string) ($_POST['domain'] ?? ''));
$description = trim((string) ($_POST['description'] ?? ''));
$usernames = trim((string) ($_POST['usernames'] ?? ''));
$sudoUsernames = trim((string) ($_POST['sudo_usernames'] ?? ''));
$errors = [];
if (!preg_match('/^[A-Za-z0-9][A-Za-z0-9._-]{1,63}$/', $vmName)) {
@@ -45,9 +47,9 @@ $resources = $sizeMapping[$size];
try {
$stmt = db()->prepare(
'INSERT INTO vm_requests
(vm_name, domain_name, size, vcpus, ram_gb, disk_gb, requested_by, status, description)
(vm_name, domain_name, size, vcpus, ram_gb, disk_gb, requested_by, status, description, usernames, sudo_usernames)
VALUES
(:vm_name, :domain_name, :size, :vcpus, :ram_gb, :disk_gb, :requested_by, :status, :description)'
(:vm_name, :domain_name, :size, :vcpus, :ram_gb, :disk_gb, :requested_by, :status, :description, :usernames, :sudo_usernames)'
);
$stmt->execute([
'vm_name' => $vmName,
@@ -59,6 +61,8 @@ try {
'requested_by' => $_SESSION['user'] ?? 'unknown',
'status' => 'queued',
'description' => $description,
'usernames' => $usernames,
'sudo_usernames' => $sudoUsernames,
]);
// Get the inserted request ID
@@ -82,6 +86,8 @@ try {
'disk_gb' => $resources['disk_gb'],
'domain' => $domain,
'description' => $description ?: ('Requested by ' . ($_SESSION['user'] ?? 'unknown')),
'usernames' => $usernames,
'sudo_usernames' => $sudoUsernames,
]);
$netboxVmId = $vm['id'] ?? null;

View File

@@ -20,6 +20,8 @@ CREATE TABLE IF NOT EXISTS vm_requests (
requested_by VARCHAR(50) NOT NULL,
status ENUM('queued', 'building', 'ready', 'failed', 'deployed_to_netbox', 'cancelled') NOT NULL DEFAULT 'queued',
description VARCHAR(500) DEFAULT NULL,
usernames VARCHAR(500) DEFAULT NULL,
sudo_usernames VARCHAR(500) DEFAULT NULL,
netbox_device_id INT UNSIGNED NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY vm_requests_vm_name_unique (vm_name),