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:
@@ -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'])
|
||||
|
||||
Reference in New Issue
Block a user