feat: Integrate L5-Swagger for API documentation and enhance VM synchronization to include host-level details.
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Jobs\VirtualMachine\Sync;
|
||||
use App\Http\Services\Telegram;
|
||||
use App\Http\Services\vCenter as ServiceVCenter;
|
||||
use App\Models\ModuleDatacenter;
|
||||
use App\Models\ModuleHost;
|
||||
use App\Models\VirtualMachines;
|
||||
use App\Models\vCenter;
|
||||
use Illuminate\Bus\Queueable;
|
||||
@@ -67,15 +68,29 @@ class SyncVirtualMachine implements ShouldQueue
|
||||
$datacenters = ModuleDatacenter::where('vcenter_id', $item->id)->get();
|
||||
if ($datacenters->count() > 0) {
|
||||
foreach ($datacenters as $dc) {
|
||||
$vms = $this->vCenter->getAllVirtualMachine($dc->datacenter_id);
|
||||
$vmCount = $this->processVMs($vms, $item, $dc->id); // Extract method for reuse
|
||||
$vmTotalCount += $vmCount;
|
||||
// Get all hosts for this datacenter
|
||||
$hosts = ModuleHost::where('module_datacenter_id', $dc->id)->get();
|
||||
|
||||
if ($hosts->count() > 0) {
|
||||
foreach ($hosts as $host) {
|
||||
Log::info("Fetching VMs for host: {$host->host_id} in datacenter: {$dc->name}");
|
||||
$vms = $this->vCenter->getAllVirtualMachine($dc->datacenter_id, $host->host_id);
|
||||
$vmCount = $this->processVMs($vms, $item, $dc->id, $host->id);
|
||||
$vmTotalCount += $vmCount;
|
||||
}
|
||||
} else {
|
||||
// Fallback: Sync VMs by datacenter only
|
||||
Log::info("No hosts found for datacenter {$dc->name}, fetching VMs by datacenter only");
|
||||
$vms = $this->vCenter->getAllVirtualMachine($dc->datacenter_id);
|
||||
$vmCount = $this->processVMs($vms, $item, $dc->id, null);
|
||||
$vmTotalCount += $vmCount;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback: Sync all VMs without datacenter context
|
||||
Log::info("No datacenters found, fetching all VMs for {$item->host}");
|
||||
$vms = $this->vCenter->getAllVirtualMachine();
|
||||
$vmCount = $this->processVMs($vms, $item, null);
|
||||
$vmCount = $this->processVMs($vms, $item, null, null);
|
||||
$vmTotalCount += $vmCount;
|
||||
}
|
||||
|
||||
@@ -102,7 +117,7 @@ class SyncVirtualMachine implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
private function processVMs($vms, $vCenterItem, $datacenterId)
|
||||
private function processVMs($vms, $vCenterItem, $datacenterId, $hostId)
|
||||
{
|
||||
// If token expired, retry login (handled inside loop in previous logic, but simpler here if we just handle the array check)
|
||||
if ($vms === 'Login_Again') {
|
||||
@@ -113,7 +128,12 @@ class SyncVirtualMachine implements ShouldQueue
|
||||
$vCenterItem->save();
|
||||
$this->vCenter->token = $token;
|
||||
// Retry fetch
|
||||
$vms = $this->vCenter->getAllVirtualMachine($datacenterId ? ModuleDatacenter::find($datacenterId)->datacenter_id : '');
|
||||
$datacenter = $datacenterId ? ModuleDatacenter::find($datacenterId) : null;
|
||||
$host = $hostId ? ModuleHost::find($hostId) : null;
|
||||
$vms = $this->vCenter->getAllVirtualMachine(
|
||||
$datacenter ? $datacenter->datacenter_id : '',
|
||||
$host ? $host->host_id : ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +155,9 @@ class SyncVirtualMachine implements ShouldQueue
|
||||
$vmObj->power_state = $vmData['power_state'];
|
||||
$vmObj->cpu = $vmData['cpu_count'] ?? 0;
|
||||
$vmObj->ram_mb = $vmData['memory_size_MiB'] ?? 0;
|
||||
$vmObj->datacenter_id = $datacenterId; // Assign datacenter if known
|
||||
$vmObj->datacenter_id = $datacenterId;
|
||||
$vmObj->host_id = $hostId;
|
||||
$vmObj->folder = $vmData['folder'] ?? null;
|
||||
$vmObj->last_synced_at = now();
|
||||
|
||||
$vmObj->save();
|
||||
|
||||
Reference in New Issue
Block a user