Files
portal-api/app/Services/VirtualMachine/VmInfrastructureResolver.php
T
2026-06-22 08:43:32 +07:00

67 lines
2.2 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 only:
*
* active vCenter → active Datacenter → active Host → active Datastore + Network
*
* Product / OS bindings are now informational only — the host/datacenter/vcenter
* configured per-product no longer drive VM creation. The whole platform clones
* onto whatever single chain is currently marked status=1.
*/
class VmInfrastructureResolver
{
public function __construct(
private readonly VmProvisionLogger $logger,
private readonly VmActiveProviderService $activeProvider,
) {
}
public function resolve(OrderItem $item, $product): VmInfrastructureContext
{
$os = Os::with('osGroup')->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();
$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' => $chain['host']->id,
'datastore_id' => $chain['datastore']->id,
'network_id' => $chain['network']->id,
]);
$osGroupName = $os->osGroup?->name ?? '';
$isWindows = str_contains(strtolower($osGroupName), 'windows');
return new VmInfrastructureContext(
os: $os,
host: $chain['host'],
datacenter: $chain['datacenter'],
vcenter: $chain['vcenter'],
template: $os->template_id,
isWindows: $isWindows,
vmUser: $isWindows ? 'administrator' : 'root',
vmPort: $isWindows ? 3389 : 22,
datastore: $chain['datastore'],
network: $chain['network'],
);
}
}