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:
@@ -90,12 +90,14 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
// DNS + network selector still come from product template; gateway + CIDR
|
||||
// are now read from the chosen ip_pools row at reservation time.
|
||||
// Network preferably comes from the OS's own binding (module_network_id);
|
||||
// falls back to the product's ipAddress template for legacy OS entries
|
||||
// that were never assigned a network. Gateway + CIDR are read from the
|
||||
// chosen ip_pools row at reservation time.
|
||||
$orderItem = $moduleVmware->order?->orderItems?->first();
|
||||
$osModel = $orderItem?->os_id ? Os::with('osGroup.host')->find($orderItem->os_id) : null;
|
||||
|
||||
$networkId = $orderItem?->product?->ipAddress?->module_network_id ?? 1;
|
||||
$networkId = $osModel?->module_network_id ?? $orderItem?->product?->ipAddress?->module_network_id ?? 1;
|
||||
$dns = $orderItem?->product?->ipAddress?->dns ?? '8.8.8.8';
|
||||
$hostId = $osModel?->osGroup?->host?->id ?? 1;
|
||||
$serviceId = Service::where('vm_id', $this->moduleVmwareId)->value('id');
|
||||
@@ -334,15 +336,9 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
string $dns,
|
||||
): ?array {
|
||||
return DB::transaction(function () use ($networkId, $hostId, $dns) {
|
||||
// Lock existing rows to prevent another job from picking the same IP
|
||||
$usedIps = ModuleIpAddress::where('module_network_id', $networkId)
|
||||
->lockForUpdate()
|
||||
->pluck('ip')
|
||||
->toArray();
|
||||
|
||||
// ip_pools.status is the single source of truth for availability.
|
||||
$ipPool = IpPool::where('module_network_id', $networkId)
|
||||
->whereIn('status', ['free', 'reserved'])
|
||||
->whereNotIn('ip_address', $usedIps)
|
||||
->lockForUpdate()
|
||||
->inRandomOrder()
|
||||
->first();
|
||||
@@ -356,10 +352,16 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
|
||||
$cidr = (int) ($ipPool->cidr ?: 24);
|
||||
$gateway = (string) ($ipPool->gateway ?? '');
|
||||
$netmask = $this->cidrToNetmask($cidr);
|
||||
$netmask = $ipPool->netmask ?: $this->cidrToNetmask($cidr);
|
||||
|
||||
// Create a pending reservation — acts as a distributed lock on this IP
|
||||
$reservation = ModuleIpAddress::create([
|
||||
// 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.
|
||||
$reservation = ModuleIpAddress::where('ip', $ipPool->ip_address)
|
||||
->where('module_network_id', $networkId)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
$attrs = [
|
||||
'vm_name' => '__pending__',
|
||||
'module_host_id' => $hostId,
|
||||
'module_network_id' => $networkId,
|
||||
@@ -368,7 +370,13 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
'netmask' => $netmask,
|
||||
'dns' => $dns,
|
||||
'status' => 0, // 0 = pending/reserved, not yet confirmed
|
||||
]);
|
||||
];
|
||||
|
||||
if ($reservation) {
|
||||
$reservation->update($attrs);
|
||||
} else {
|
||||
$reservation = ModuleIpAddress::create($attrs);
|
||||
}
|
||||
|
||||
return [
|
||||
'reservation' => $reservation,
|
||||
@@ -408,13 +416,13 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a failed reservation by deleting the pending ModuleIpAddress row.
|
||||
* Release a failed reservation by resetting the mirrored ModuleIpAddress row.
|
||||
* This returns the IP to the available pool.
|
||||
*/
|
||||
private function releaseIp(ModuleIpAddress $reservation, ?int $ipPoolId, VmProvisionLogger $logger): void
|
||||
{
|
||||
try {
|
||||
$reservation->delete();
|
||||
$reservation->update(['vm_name' => null, 'status' => 0]);
|
||||
if ($ipPoolId) {
|
||||
IpPool::where('id', $ipPoolId)->update(['status' => 'free']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user