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
@@ -9,6 +9,7 @@ use App\Models\ModuleDatastore;
use App\Models\ModuleFolder;
use App\Models\ModuleHost;
use App\Models\ModuleNetwork;
use App\Models\ModuleResourcePool;
use App\Models\vCenter;
use App\Models\VirtualMachines;
use App\Http\Services\vCenter as ServiceVCenter;
@@ -148,6 +149,7 @@ class VCenterController extends Controller
'datastore' => ['name' => 'sometimes|string|max:255', 'datastore' => 'sometimes|string|max:255', 'type' => 'sometimes|nullable|string|max:255', 'space' => 'sometimes|nullable|integer', 'status' => 'sometimes|integer|in:0,1'],
'network' => ['name' => 'sometimes|string|max:255', 'network' => 'sometimes|string|max:255', 'type' => 'sometimes|nullable|string|max:255'],
'folder' => ['name' => 'sometimes|string|max:255', 'folder' => 'sometimes|string|max:255', 'type' => 'sometimes|nullable|string|max:255'],
'resource_pool' => ['name' => 'sometimes|string|max:255', 'resource_pool' => 'sometimes|string|max:255', 'status' => 'sometimes|integer|in:0,1'],
'virtual_machine' => ['name' => 'sometimes|string|max:255', 'vm_id' => 'sometimes|string|max:255'],
default => [],
};
@@ -172,7 +174,7 @@ class VCenterController extends Controller
$childCheck = match ($type) {
'vcenter' => $model->datacenters()->exists(),
'datacenter' => ModuleHost::where('module_datacenter_id', $id)->exists(),
'host' => $model->datastore()->exists() || $model->network()->exists(),
'host' => $model->datastore()->exists() || $model->network()->exists() || $model->resourcePool()->exists(),
default => false,
};
@@ -191,7 +193,7 @@ class VCenterController extends Controller
public function tree(Request $request): JsonResponse
{
$vcenters = vCenter::with(['datacenters'])->get();
$allHosts = ModuleHost::with(['datastore', 'network'])->get()->groupBy('module_datacenter_id');
$allHosts = ModuleHost::with(['datastore', 'network', 'resourcePool'])->get()->groupBy('module_datacenter_id');
$allFolders = ModuleFolder::all()->groupBy('module_datacenter_id');
// VMs with a host assignment — grouped by host DB id
$allVMs = VirtualMachines::with('vmware')->whereNotNull('host_id')->get()->groupBy('host_id');
@@ -219,6 +221,7 @@ class VCenterController extends Controller
'network' => $net->network,
'net_type' => $net->type,
'module_host_id' => $net->module_host_id,
'status' => $net->status,
],
]);
@@ -265,6 +268,20 @@ class VCenterController extends Controller
],
]);
$resourcePools = ($host->resourcePool ?? collect())
->unique('resource_pool')
->map(fn($rp) => [
'id' => 'resource_pool-' . $rp->id,
'name' => $rp->name,
'type' => 'resource_pool',
'meta' => [
'db_id' => $rp->id,
'resource_pool' => $rp->resource_pool,
'status' => $rp->status,
'module_host_id' => $rp->module_host_id,
],
]);
return [
'id' => 'host-' . $host->id,
'name' => $host->name,
@@ -277,9 +294,10 @@ class VCenterController extends Controller
'iso_path' => $host->iso_path,
'module_datacenter_id' => $host->module_datacenter_id,
'vm_count' => $hostVMs->count(),
'status' => $host->status,
],
// VMs first, then Datastores — correct vSphere order
'children' => array_values($hostVMs->concat($datastores)->toArray()),
// VMs first, then Datastores, then Resource Pools — correct vSphere order
'children' => array_values($hostVMs->concat($datastores)->concat($resourcePools)->toArray()),
];
});
@@ -334,6 +352,7 @@ class VCenterController extends Controller
'db_id' => $dc->id,
'datacenter_id' => $dc->datacenter_id,
'vcenter_id' => $dc->vcenter_id,
'status' => $dc->status,
],
// vSphere order: Hosts → Networks → Folders → Orphan VMs
'children' => array_values($dcHosts->concat($dcNetworks)->concat($dcFolders)->concat($dcOrphanVMs)->toArray()),
@@ -361,6 +380,7 @@ class VCenterController extends Controller
'datastore' => ModuleDatastore::findOrFail($id),
'network' => ModuleNetwork::findOrFail($id),
'folder' => ModuleFolder::findOrFail($id),
'resource_pool' => ModuleResourcePool::findOrFail($id),
'virtual_machine' => VirtualMachines::findOrFail($id),
default => abort(404, 'Unknown node type'),
};