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
@@ -117,6 +117,8 @@ class IpPoolController extends Controller
'ip_address' => 'required|string',
'module_network_id' => 'required|integer|exists:module_network,id',
'gateway' => 'nullable|string|max:45',
'cidr' => 'nullable|integer|min:0|max:32',
'netmask' => 'nullable|string|max:45',
]);
$networkId = (int) $request->module_network_id;
@@ -155,15 +157,35 @@ class IpPoolController extends Controller
], 422);
}
$hostId = ModuleNetwork::find($networkId)?->module_host_id;
$netmask = $request->netmask ?: '255.255.255.0';
DB::beginTransaction();
try {
$created = [];
foreach ($ips as $ip) {
$created[] = IpPool::create([
$pool = IpPool::create([
'ip_address' => $ip,
'module_network_id' => $networkId,
'gateway' => $request->gateway,
'cidr' => $request->cidr,
'netmask' => $netmask,
]);
// Mirror into module_ip_address so both tables stay in sync.
// Reservation logic (VmIpReservationService/AssignIpToGuestJob) treats
// ip_pools.status as the source of truth and just updates this row.
ModuleIpAddress::updateOrCreate(
['ip' => $ip, 'module_network_id' => $networkId],
[
'module_host_id' => $hostId,
'gateway' => $request->gateway,
'netmask' => $netmask,
'status' => 0,
]
);
$created[] = $pool;
}
DB::commit();
@@ -186,6 +208,8 @@ class IpPoolController extends Controller
'ip_address' => 'required|string|max:45',
'module_network_id' => 'required|integer|exists:module_network,id',
'gateway' => 'nullable|string|max:45',
'cidr' => 'nullable|integer|min:0|max:32',
'netmask' => 'nullable|string|max:45',
]);
if (!filter_var($request->ip_address, FILTER_VALIDATE_IP)) {
@@ -205,9 +229,30 @@ class IpPoolController extends Controller
], 422);
}
$oldIp = $pool->ip_address;
$oldNetworkId = $pool->module_network_id;
$netmask = $request->netmask ?: '255.255.255.0';
DB::beginTransaction();
try {
$pool->update($request->only(['ip_address', 'module_network_id', 'gateway']));
$pool->update([
...$request->only(['ip_address', 'module_network_id', 'gateway', 'cidr']),
'netmask' => $netmask,
]);
$hostId = ModuleNetwork::find($pool->module_network_id)?->module_host_id;
// Keep the module_ip_address mirror in sync with the new ip/network/host/gateway/netmask.
ModuleIpAddress::where('ip', $oldIp)
->where('module_network_id', $oldNetworkId)
->update([
'ip' => $pool->ip_address,
'module_network_id' => $pool->module_network_id,
'module_host_id' => $hostId,
'gateway' => $pool->gateway,
'netmask' => $pool->netmask,
]);
DB::commit();
return response()->json(['error' => false, 'data' => $pool->fresh('network')]);
@@ -238,6 +283,10 @@ class IpPoolController extends Controller
], 422);
}
ModuleIpAddress::where('ip', $pool->ip_address)
->where('module_network_id', $pool->module_network_id)
->delete();
$pool->delete();
return response()->json(['error' => false, 'message' => 'Đã xoá IP Pool thành công']);
@@ -270,6 +319,12 @@ class IpPoolController extends Controller
], 422);
}
foreach ($pools as $p) {
ModuleIpAddress::where('ip', $p->ip_address)
->where('module_network_id', $p->module_network_id)
->delete();
}
$deleted = IpPool::whereIn('id', $ids)->delete();
return response()->json([
@@ -279,6 +334,56 @@ class IpPoolController extends Controller
]);
}
/**
* Backfill/repair the module_ip_address mirror for every ip_pools row.
* Existing mirror rows only get gateway/netmask refreshed — status/vm_name
* (assigned VM state) is left untouched. Missing rows are created with
* status derived from the current ip_pools.status.
*/
public function sync(): JsonResponse
{
$created = 0;
$updated = 0;
$hostByNet = ModuleNetwork::pluck('module_host_id', 'id');
IpPool::chunkById(500, function ($pools) use (&$created, &$updated, $hostByNet) {
foreach ($pools as $pool) {
$hostId = $hostByNet[$pool->module_network_id] ?? null;
$netmask = $pool->netmask ?: '255.255.255.0';
$mia = ModuleIpAddress::where('ip', $pool->ip_address)
->where('module_network_id', $pool->module_network_id)
->first();
if ($mia) {
$mia->update([
'module_host_id' => $hostId,
'gateway' => $pool->gateway,
'netmask' => $netmask,
]);
$updated++;
} else {
ModuleIpAddress::create([
'ip' => $pool->ip_address,
'module_network_id' => $pool->module_network_id,
'module_host_id' => $hostId,
'gateway' => $pool->gateway,
'netmask' => $netmask,
'status' => $pool->status === 'assigned' ? 1 : 0,
]);
$created++;
}
}
});
return response()->json([
'error' => false,
'message' => "Đã đồng bộ: tạo mới {$created}, cập nhật {$updated}",
'created' => $created,
'updated' => $updated,
]);
}
public function networks(): JsonResponse
{
$networks = ModuleNetwork::select(['id', 'name', 'type', 'network', 'module_host_id'])
@@ -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,
];
}
}
@@ -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'),
};
@@ -406,7 +406,8 @@ class ClientController extends Controller
$customer->password = Hash::make($newPassword);
$customer->save();
\Illuminate\Support\Facades\Mail::to($customer->email)->send(
\Illuminate\Support\Facades\Mail::to($customer->email)->later(
now()->addSeconds(5),
new \App\Mail\Customer\PasswordResetByAdmin([
'fullname' => $customer->fullname,
'email' => $customer->email,