Refactor vCenter snapshot handling and VM operations
- 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.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\VM;
|
||||
|
||||
use App\Http\Services\vCenter;
|
||||
use App\Models\ModuleVmware;
|
||||
|
||||
class PowerOnVMAction
|
||||
{
|
||||
public function __construct(private vCenter $vcenter) {}
|
||||
|
||||
/**
|
||||
* Power on a VM.
|
||||
*
|
||||
* Resolves the vCenter, logs in (sets apiUri / token), then delegates
|
||||
* to the existing StartVirtualMachine() method.
|
||||
*/
|
||||
public function execute(ModuleVmware $vm): 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->StartVirtualMachine($vm, $vm->vmware_id);
|
||||
} catch (\Throwable $e) {
|
||||
return ['error' => true, 'data' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user