- Introduced a new method `resolveVCenterFromProduct` in `SnapshotService` to streamline vCenter resolution from product data. - Updated `CreateSnapshot`, `RemoveSnapshot`, and `RevertSnapshot` jobs to utilize the new vCenter resolution method. - Enhanced the `VmController` to support UUID-based service identification for VM operations. - Added new actions for changing VM IP and RDP port, encapsulating the logic for these operations. - Improved error handling and logging for VM operations, including detailed responses for API consumers. - Removed unnecessary relationships in model queries to optimize performance.
49 lines
1.4 KiB
PHP
49 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\VM;
|
|
|
|
use App\Http\Services\vCenter;
|
|
use App\Models\ModuleVmware;
|
|
|
|
class ChangePortVMAction
|
|
{
|
|
public function __construct(private vCenter $vcenter) {}
|
|
|
|
/**
|
|
* Change the RDP port for a Windows VM.
|
|
*
|
|
* Delegates EXACTLY to the existing changeRDPPortWindows() — no logic is
|
|
* duplicated here. loginServer() must be called first because
|
|
* CommandChangePort() uses $this->apiUri / username / password to build
|
|
* the VMware.PowerCLI connection string.
|
|
*
|
|
* @param ModuleVmware $vm
|
|
* @param string $newPort
|
|
* @return array|string
|
|
*/
|
|
public function execute(ModuleVmware $vm, string $newPort): array|string
|
|
{
|
|
try {
|
|
$vcenter = $vm->vCenter
|
|
?? $vm->ipAddress?->host?->datacenter?->vCenter;
|
|
|
|
if (!$vcenter) {
|
|
return ['error' => true, 'data' => 'Không tìm thấy thông tin vCenter cho VM này.'];
|
|
}
|
|
|
|
if (!$this->vcenter->loginServer($vcenter)) {
|
|
return 'Login_Again';
|
|
}
|
|
|
|
return $this->vcenter->changeRDPPortWindows(
|
|
(string) $vm->name,
|
|
(string) $vm->user,
|
|
(string) $vm->getRawOriginal('password'),
|
|
$newPort
|
|
);
|
|
} catch (\Throwable $e) {
|
|
return ['error' => true, 'data' => $e->getMessage()];
|
|
}
|
|
}
|
|
}
|