Files
portal-api/app/Services/VirtualMachine/VmInfrastructureResolver.php
T
baodev0andClaude Sonnet 5.5 62596d9063 feat(vm): hoàn tiền khi cấp VM lỗi, theo dõi job queue, uuid dịch vụ cho ticket, chuẩn hoá RAM addon
- VmProvisionException/VmProvisionRefund xử lý lỗi cấp phát VM
- QueueJobInspector + QueueMonitorController xem chi tiết job
- Thêm service uuid cho ticket, thời lượng cho activity_logs
- Migration chuẩn hoá RAM addon sang MB, trạng thái deleting cho snapshot

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 11:54:05 +07:00

86 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Models\OrderItem;
use App\Models\Os;
use App\Services\VirtualMachine\Dto\VmInfrastructureContext;
use RuntimeException;
/**
* Resolves the provisioning target by walking the ACTIVE chain:
*
* active vCenter → active Datacenter → active Host → active Datastore + Network
*
* The OS host (and its datacenter) overrides the active host when set, because the
* OS template, datastore, network, resource pool and folder belong to that host. Datastore, network, resource pool and folder are exceptions: if
* the OS has datastore_id / module_network_id / resource_pool_id / folder_id
* set, they override the active chain / global default (an OS can target a
* different datastore/IP pool/resource pool/folder than whatever happens to
* be "active" or configured as the system-wide default).
*/
class VmInfrastructureResolver
{
public function __construct(
private readonly VmProvisionLogger $logger,
private readonly VmActiveProviderService $activeProvider,
private readonly VmProviderSettingsService $providerSettings,
) {
}
public function resolve(OrderItem $item, $product): VmInfrastructureContext
{
$os = Os::with(['osGroup', 'host.datacenter', 'network', 'datastore', 'resourcePool', 'folder'])->find($item->os_id);
if (!$os) {
throw new RuntimeException(
"Không tìm thấy OS id={$item->os_id} trong OrderItem #{$item->id}. os_id không hợp lệ."
);
}
$chain = $this->activeProvider->resolveChain();
$host = $os->host ?? $chain['host'];
$datacenter = $os->host?->datacenter ?? $chain['datacenter'];
$network = $os->network ?? $chain['network'];
$datastore = $os->datastore ?? $chain['datastore'];
$folderId = $os->folder?->folder ?? $this->providerSettings->defaultFolder();
$resourcePoolId = $os->resourcePool?->resource_pool ?? $this->providerSettings->defaultResourcePool();
$this->logger->step('Infrastructure resolved from active chain', [
'order_item_id' => $item->id,
'vcenter_id' => $chain['vcenter']->id,
'datacenter_id' => $chain['datacenter']->id,
'host_id' => $host->id,
'host_from_os' => $os->host !== null,
'datastore_id' => $datastore->id,
'datastore_from_os' => $os->datastore !== null,
'network_id' => $network->id,
'network_from_os' => $os->network !== null,
'folder_id' => $folderId,
'folder_from_os' => $os->folder !== null,
'resource_pool_id' => $resourcePoolId,
'resource_pool_from_os' => $os->resourcePool !== null,
]);
$osGroupName = $os->osGroup?->name ?? '';
$isWindows = str_contains(strtolower($osGroupName), 'windows');
return new VmInfrastructureContext(
os: $os,
host: $host,
datacenter: $datacenter,
vcenter: $chain['vcenter'],
template: $os->template_id,
isWindows: $isWindows,
vmUser: $isWindows ? 'administrator' : 'root',
vmPort: $isWindows ? 3389 : 22,
datastore: $datastore,
network: $network,
folderId: $folderId,
resourcePoolId: $resourcePoolId,
);
}
}