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
@@ -7,27 +7,37 @@ use App\Models\Os;
use App\Models\OsGroup;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class OsController extends Controller
{
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'version' => 'required|string|max:100',
'os_group_id' => 'required|integer|exists:os_group,id',
'template_id' => 'nullable|string|max:255',
'module_host_id' => 'nullable|integer|exists:module_host,id',
'datastore_id' => 'nullable|integer|exists:module_datastore,id',
'is_active' => 'boolean',
'version' => 'required|string|max:100',
'os_group_id' => 'required|integer|exists:os_group,id',
'template_id' => 'nullable|string|max:255',
'module_host_id' => 'nullable|integer|exists:module_host,id',
'datastore_id' => 'nullable|integer|exists:module_datastore,id',
'module_network_id' => 'nullable|integer|exists:module_network,id',
'resource_pool_id' => 'nullable|integer|exists:module_resource_pool,id',
'folder_id' => [
'nullable', 'integer',
Rule::exists('module_folder', 'id')->where('type', 'VIRTUAL_MACHINE'),
],
'is_active' => 'boolean',
]);
$os = Os::create([
'version' => $validated['version'],
'os_group_id' => $validated['os_group_id'],
'template_id' => $validated['template_id'] ?? null,
'module_host_id' => $validated['module_host_id'] ?? null,
'datastore_id' => $validated['datastore_id'] ?? null,
'status' => ($validated['is_active'] ?? true) ? 1 : 0,
'version' => $validated['version'],
'os_group_id' => $validated['os_group_id'],
'template_id' => $validated['template_id'] ?? null,
'module_host_id' => $validated['module_host_id'] ?? null,
'datastore_id' => $validated['datastore_id'] ?? null,
'module_network_id' => $validated['module_network_id'] ?? null,
'resource_pool_id' => $validated['resource_pool_id'] ?? null,
'folder_id' => $validated['folder_id'] ?? null,
'status' => ($validated['is_active'] ?? true) ? 1 : 0,
]);
return response()->json([
@@ -46,21 +56,30 @@ class OsController extends Controller
}
$validated = $request->validate([
'version' => 'sometimes|required|string|max:100',
'os_group_id' => 'sometimes|integer|exists:os_group,id',
'template_id' => 'nullable|string|max:255',
'module_host_id' => 'nullable|integer|exists:module_host,id',
'datastore_id' => 'nullable|integer|exists:module_datastore,id',
'is_active' => 'boolean',
'version' => 'sometimes|required|string|max:100',
'os_group_id' => 'sometimes|integer|exists:os_group,id',
'template_id' => 'nullable|string|max:255',
'module_host_id' => 'nullable|integer|exists:module_host,id',
'datastore_id' => 'nullable|integer|exists:module_datastore,id',
'module_network_id' => 'nullable|integer|exists:module_network,id',
'resource_pool_id' => 'nullable|integer|exists:module_resource_pool,id',
'folder_id' => [
'nullable', 'integer',
Rule::exists('module_folder', 'id')->where('type', 'VIRTUAL_MACHINE'),
],
'is_active' => 'boolean',
]);
$os->update([
'version' => $validated['version'] ?? $os->version,
'os_group_id' => $validated['os_group_id'] ?? $os->os_group_id,
'template_id' => array_key_exists('template_id', $validated) ? $validated['template_id'] : $os->template_id,
'module_host_id' => array_key_exists('module_host_id', $validated) ? $validated['module_host_id'] : $os->module_host_id,
'datastore_id' => array_key_exists('datastore_id', $validated) ? $validated['datastore_id'] : $os->datastore_id,
'status' => isset($validated['is_active']) ? ($validated['is_active'] ? 1 : 0) : $os->status,
'version' => $validated['version'] ?? $os->version,
'os_group_id' => $validated['os_group_id'] ?? $os->os_group_id,
'template_id' => array_key_exists('template_id', $validated) ? $validated['template_id'] : $os->template_id,
'module_host_id' => array_key_exists('module_host_id', $validated) ? $validated['module_host_id'] : $os->module_host_id,
'datastore_id' => array_key_exists('datastore_id', $validated) ? $validated['datastore_id'] : $os->datastore_id,
'module_network_id' => array_key_exists('module_network_id', $validated) ? $validated['module_network_id'] : $os->module_network_id,
'resource_pool_id' => array_key_exists('resource_pool_id', $validated) ? $validated['resource_pool_id'] : $os->resource_pool_id,
'folder_id' => array_key_exists('folder_id', $validated) ? $validated['folder_id'] : $os->folder_id,
'status' => isset($validated['is_active']) ? ($validated['is_active'] ? 1 : 0) : $os->status,
]);
return response()->json([
@@ -87,22 +106,31 @@ class OsController extends Controller
private function formatOs(Os $os): array
{
$os->loadMissing('host.datacenter.vCenter', 'datastore');
$os->loadMissing('host.datacenter.vCenter', 'datastore', 'network', 'resourcePool', 'folder');
$host = $os->host;
$datastore = $os->datastore;
$network = $os->network;
$resourcePool = $os->resourcePool;
$folder = $os->folder;
return [
'id' => $os->id,
'version' => $os->version,
'template_id' => $os->template_id,
'is_active' => (bool) $os->status,
'os_group_id' => $os->os_group_id,
'module_host_id' => $os->module_host_id,
'datastore_id' => $os->datastore_id,
'datastore_name' => $datastore?->name,
'host_name' => $host ? $host->name : null,
'datacenter_name' => $host?->datacenter?->name,
'vcenter_name' => $host?->datacenter?->vCenter?->host,
'id' => $os->id,
'version' => $os->version,
'template_id' => $os->template_id,
'is_active' => (bool) $os->status,
'os_group_id' => $os->os_group_id,
'module_host_id' => $os->module_host_id,
'datastore_id' => $os->datastore_id,
'module_network_id' => $os->module_network_id,
'resource_pool_id' => $os->resource_pool_id,
'folder_id' => $os->folder_id,
'datastore_name' => $datastore?->name,
'network_name' => $network?->name,
'resource_pool_name' => $resourcePool?->name,
'folder_name' => $folder?->name,
'host_name' => $host ? $host->name : null,
'datacenter_name' => $host?->datacenter?->name,
'vcenter_name' => $host?->datacenter?->vCenter?->host,
];
}
}
@@ -16,7 +16,7 @@ class OsGroupController extends Controller
public function index(Request $request): JsonResponse
{
$query = OsGroup::withCount('os')
->with(['os' => fn($q) => $q->with('host.datacenter.vCenter')->orderBy('version')])
->with(['os' => fn($q) => $q->with(['host.datacenter.vCenter', 'datastore', 'network'])->orderBy('version')])
->when(
$request->get('filter'),
fn($q, $f) => $q->where('name', 'like', "%{$f}%")
@@ -50,7 +50,7 @@ class OsGroupController extends Controller
public function show(int $id): JsonResponse
{
$group = OsGroup::withCount('os')
->with(['os' => fn($q) => $q->with('host.datacenter.vCenter')->orderBy('version')])
->with(['os' => fn($q) => $q->with(['host.datacenter.vCenter', 'datastore', 'network'])->orderBy('version')])
->find($id);
if (! $group) {
@@ -141,18 +141,24 @@ class OsGroupController extends Controller
private function formatOs(\App\Models\Os $os): array
{
$host = $os->relationLoaded('host') ? $os->host : null;
$host = $os->relationLoaded('host') ? $os->host : null;
$datastore = $os->relationLoaded('datastore') ? $os->datastore : null;
$network = $os->relationLoaded('network') ? $os->network : null;
return [
'id' => $os->id,
'version' => $os->version,
'template_id' => $os->template_id,
'is_active' => (bool) $os->status,
'os_group_id' => $os->os_group_id,
'module_host_id' => $os->module_host_id,
'host_name' => $host?->name,
'datacenter_name' => $host?->datacenter?->name,
'vcenter_name' => $host?->datacenter?->vCenter?->host,
'id' => $os->id,
'version' => $os->version,
'template_id' => $os->template_id,
'is_active' => (bool) $os->status,
'os_group_id' => $os->os_group_id,
'module_host_id' => $os->module_host_id,
'datastore_id' => $os->datastore_id,
'module_network_id' => $os->module_network_id,
'datastore_name' => $datastore?->name,
'network_name' => $network?->name,
'host_name' => $host?->name,
'datacenter_name' => $host?->datacenter?->name,
'vcenter_name' => $host?->datacenter?->vCenter?->host,
];
}
}