fix(vm): khôi phục lọc network khi giữ IP cho VM

reserve() không còn lấy IP ngoài network được chọn hoặc IP chưa gán network.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-30 11:56:35 +07:00
co-authored by Claude Sonnet 5.5
parent 62596d9063
commit 1d40dfbb44
@@ -31,6 +31,7 @@ class VmIpReservationService
return DB::transaction(function () use ($networkId, $hostId, $dns) { return DB::transaction(function () use ($networkId, $hostId, $dns) {
// ip_pools.status is the single source of truth for availability. // ip_pools.status is the single source of truth for availability.
$ipPool = $this->availableIps() $ipPool = $this->availableIps()
->where('module_network_id', $networkId)
->lockForUpdate() ->lockForUpdate()
->inRandomOrder() ->inRandomOrder()
->first(); ->first();
@@ -48,6 +49,7 @@ class VmIpReservationService
// module_ip_address mirrors ip_pools — update the existing synced row // module_ip_address mirrors ip_pools — update the existing synced row
// (created when the IP was added to ip_pools) instead of inserting a new one. // (created when the IP was added to ip_pools) instead of inserting a new one.
$reservation = ModuleIpAddress::where('ip', $ipPool->ip_address) $reservation = ModuleIpAddress::where('ip', $ipPool->ip_address)
->where('module_network_id', $networkId)
->lockForUpdate() ->lockForUpdate()
->first(); ->first();
@@ -107,6 +109,7 @@ class VmIpReservationService
private function availableIps() private function availableIps()
{ {
return IpPool::whereIn('status', ['free', 'reserved']) return IpPool::whereIn('status', ['free', 'reserved'])
->whereNotNull('module_network_id')
->whereNotNull('gateway') ->whereNotNull('gateway')
->where('gateway', '!=', ''); ->where('gateway', '!=', '');
} }