Private
Public Access
1
0

fix: simplify success message after saving tags, removing Proxmox sync status

This commit is contained in:
2025-12-18 20:59:44 -05:00
parent 29321e9786
commit 0cfdaac766
2 changed files with 44 additions and 81 deletions

View File

@@ -5,13 +5,11 @@ namespace Paymenter\Extensions\Servers\Proxmox\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Paymenter\Extensions\Servers\Proxmox\Models\Server;
use Paymenter\Extensions\Servers\Proxmox\Proxmox;
class ServerTagController
{
/**
* Proxmox-style tag colors
* Based on Proxmox VE color generation for tags
*/
private static $proxmoxTagColors = [
'#e74c3c', // Red
@@ -34,15 +32,13 @@ class ServerTagController
/**
* Generate a consistent color for a tag based on its text
* Uses the same algorithm as Proxmox VE
*/
public static function generateTagColor(string $tagText): string
{
// Simple hash function to get a consistent color for each tag
$hash = 0;
for ($i = 0; $i < strlen($tagText); $i++) {
$hash = ord($tagText[$i]) + (($hash << 5) - $hash);
$hash = $hash & $hash; // Convert to 32-bit integer
$hash = $hash & $hash;
}
$index = abs($hash) % count(self::$proxmoxTagColors);
@@ -68,86 +64,55 @@ class ServerTagController
* Update tags for a server
*/
public function update(Request $request, $serverId)
{
$server = Server::where('id', $serverId)
->where('user_id', Auth::id())
->firstOrFail();
$tags = $request->input('tags', []);
// Ensure tags is an array
if (!is_array($tags)) {
$tags = [];
}
// Limit to 3 tags
$tags = array_slice($tags, 0, 3);
// Process tags and auto-generate colors if not provided
$processedTags = [];
foreach ($tags as $tag) {
if (!is_array($tag) || !isset($tag['text'])) continue;
$text = trim($tag['text'] ?? '');
if (empty($text)) continue;
// Convert to lowercase and sanitize (Proxmox requires this)
$text = strtolower(preg_replace('/[^a-zA-Z0-9_-]/', '', $text));
if (empty($text)) continue;
// Limit tag length to 32 chars
$text = substr($text, 0, 32);
$processedTags[] = [
'text' => $text,
'color' => $tag['color'] ?? self::generateTagColor($text),
];
}
// Update individual tag columns
$server->tag1 = $processedTags[0]['text'] ?? null;
$server->tag1color = $processedTags[0]['color'] ?? null;
$server->tag2 = $processedTags[1]['text'] ?? null;
$server->tag2color = $processedTags[1]['color'] ?? null;
$server->tag3 = $processedTags[2]['text'] ?? null;
$server->tag3color = $processedTags[2]['color'] ?? null;
$server->save();
// Sync to Proxmox
$proxmoxSuccess = $this->syncTagsToProxmox($server, $processedTags);
return response()->json([
'success' => true,
'tags' => $processedTags,
'proxmox_synced' => $proxmoxSuccess,
]);
}
/**
* Sync tags to Proxmox VM
*/
private function syncTagsToProxmox(Server $server, array $tags): bool
{
try {
$proxmox = new Proxmox;
$server = Server::where('id', $serverId)
->where('user_id', Auth::id())
->firstOrFail();
$tags = $request->input('tags', []);
// Convert tags array to Proxmox format (semicolon-separated)
// Proxmox stores tags as a simple semicolon-delimited string
$tagString = implode(';', array_column($tags, 'text'));
if (!is_array($tags)) {
$tags = [];
}
// Update VM config with tags
$proxmox->request('/nodes/' . $server->node->name . '/qemu/' . $server->vm_id . '/config', 'put', [
'tags' => $tagString,
$tags = array_slice($tags, 0, 3);
$processedTags = [];
foreach ($tags as $tag) {
if (!is_array($tag) || !isset($tag['text'])) continue;
$text = trim($tag['text'] ?? '');
if (empty($text)) continue;
$text = strtolower(preg_replace('/[^a-zA-Z0-9_-]/', '', $text));
if (empty($text)) continue;
$text = substr($text, 0, 32);
$processedTags[] = [
'text' => $text,
'color' => $tag['color'] ?? self::generateTagColor($text),
];
}
$server->tag1 = $processedTags[0]['text'] ?? null;
$server->tag1color = $processedTags[0]['color'] ?? null;
$server->tag2 = $processedTags[1]['text'] ?? null;
$server->tag2color = $processedTags[1]['color'] ?? null;
$server->tag3 = $processedTags[2]['text'] ?? null;
$server->tag3color = $processedTags[2]['color'] ?? null;
$server->save();
return response()->json([
'success' => true,
'tags' => $processedTags,
]);
return true;
} catch (\Exception $e) {
// Log the error but don't fail the request
\Log::error('Failed to sync tags to Proxmox for server ' . $server->id, [
return response()->json([
'success' => false,
'error' => $e->getMessage(),
'server_id' => $server->id,
]);
return false;
], 500);
}
}

View File

@@ -1308,9 +1308,7 @@
this.saving = false;
if (data.success) {
this.tags = data.tags;
this.message = data.proxmox_synced
? 'Tags saved and synced to Proxmox!'
: 'Tags saved (Proxmox sync failed)';
this.message = 'Tags saved!';
this.messageType = 'success';
setTimeout(() => { this.closeModal(); location.reload(); }, 1000);
} else {