feat: Enhance vCenter integration with resource pool and folder support

- Added methods to ResourceService for fetching resource pools and updated existing methods to include datacenter filtering.
- Modified CreateVirtualMachine to accept folder and resource pool parameters from product data.
- Updated various jobs to synchronize resource pools and include them in the provisioning process.
- Introduced new ModuleResourcePool model and migration for resource pool management.
- Enhanced existing models (ModuleDatastore, ModuleNetwork, Os) to include relationships with vCenter and resource pools.
- Added netmask field to IpPool and updated related logic for IP reservation.
- Improved error handling and logging in VM creation and IP reservation processes.
- Updated API routes to include sync functionality for vCenter.
This commit is contained in:
baodev0
2026-07-21 21:37:28 +07:00
parent 2d43e95855
commit 5d3ed57b32
33 changed files with 708 additions and 152 deletions
@@ -0,0 +1,92 @@
<?php
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\ModuleResourcePool;
use App\Models\vCenter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SyncResourcePool implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $vCenter = null;
protected $data = null;
protected Telegram $telegram;
public function __construct()
{
$this->data = vCenter::select('id', 'host', 'user', 'password', 'token')->get();
$this->vCenter = new ServiceVCenter;
$this->telegram = new Telegram();
}
public function handle()
{
$resourcePoolCount = 0;
foreach ($this->data as $item) {
$token = $this->vCenter->loginServer($item);
if (!$token) {
continue;
}
$dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)
->select('id', 'datacenter_id')
->get();
foreach ($dataCenter as $_item) {
$allHost = ModuleHost::where('module_datacenter_id', $_item->id)
->select('id', 'name', 'host_id')
->get();
foreach ($allHost as $hostItem) {
$resourcePool = $this->vCenter->getAllResourcePool($hostItem->host_id, $_item->datacenter_id);
if ($resourcePool === 'Login_Again') {
$token = $this->vCenter->loginServer($item);
if ($token) {
$resourcePool = $this->vCenter->getAllResourcePool($hostItem->host_id, $_item->datacenter_id);
} else {
continue;
}
}
if (!is_array($resourcePool)) {
continue;
}
foreach ($resourcePool as $_subItem) {
ModuleResourcePool::updateOrCreate(
[
'resource_pool' => $_subItem['resource_pool'],
'module_host_id' => $hostItem->id,
],
[
'name' => $_subItem['name'],
'vcenter_id' => $item->id,
'status' => 1,
]
);
$resourcePoolCount++;
}
}
}
}
$this->telegram->SendJobRunInfo('SyncResourcePool', 'Success', [
'message' => 'Sync resource pool completed successfully',
'synced_vcenters' => $this->data->count(),
'synced_resource_pool_count' => $resourcePoolCount,
]);
}
}