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 PowerOffVMAction
|
||||
{
|
||||
public function __construct(private vCenter $vcenter) {}
|
||||
|
||||
/**
|
||||
* Hard power-off a VM (equivalent of pulling the plug).
|
||||
*
|
||||
* PowerOffVirtualMachine() calls checkApiExits() which requires a valid
|
||||
* session token, so loginServer() MUST be called first to set the token.
|
||||
*/
|
||||
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->PowerOffVirtualMachine($vm->vmware_id);
|
||||
} catch (\Throwable $e) {
|
||||
return ['error' => true, 'data' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user