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:
@@ -45,21 +45,21 @@ class RemoveSnapshot implements ShouldQueue
|
||||
public function handle(ServiceVCenter $serviceVCenter)
|
||||
{
|
||||
try {
|
||||
// Load product and snapshot with necessary relationships
|
||||
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')->find($this->productId);
|
||||
// Load product and snapshot
|
||||
$product = ModuleVmware::find($this->productId);
|
||||
$snapshot = ModuleSnapshot::find($this->snapshotId);
|
||||
|
||||
if (!$product || !$snapshot) {
|
||||
throw new \Exception("Không tìm thấy thông tin VPS hoặc snapshot");
|
||||
}
|
||||
|
||||
// Kiểm tra cấu hình vCenter
|
||||
if (!$product->ipAddress?->host?->datacenter?->vCenter) {
|
||||
// Resolve vCenter using helper method
|
||||
$vcenter = $serviceVCenter->resolveVCenterFromProduct($product);
|
||||
|
||||
if (!$vcenter) {
|
||||
throw new \Exception("VPS không có thông tin vCenter được cấu hình");
|
||||
}
|
||||
|
||||
$vcenter = $product->ipAddress->host->datacenter->vCenter;
|
||||
|
||||
// Set vCenter connection info
|
||||
$serviceVCenter->token = $vcenter->token;
|
||||
$serviceVCenter->apiUri = $vcenter->host;
|
||||
@@ -72,7 +72,7 @@ class RemoveSnapshot implements ShouldQueue
|
||||
]);
|
||||
|
||||
// Attempt to remove snapshot
|
||||
$result = $serviceVCenter->RemoveSnapShot($product, $snapshot->name);
|
||||
$result = $serviceVCenter->RemoveSnapShot($product, $snapshot->snapshot_ref ?? $snapshot->name);
|
||||
|
||||
// Handle re-login if needed
|
||||
if ($result === 'Login_Again') {
|
||||
@@ -81,7 +81,7 @@ class RemoveSnapshot implements ShouldQueue
|
||||
if ($token) {
|
||||
$vcenter->update(['token' => $token]);
|
||||
$serviceVCenter->token = $token;
|
||||
$result = $serviceVCenter->RemoveSnapShot($product, $snapshot->name);
|
||||
$result = $serviceVCenter->RemoveSnapShot($product, $snapshot->snapshot_ref ?? $snapshot->name);
|
||||
} else {
|
||||
throw new \Exception("Không thể đăng nhập lại vào vCenter");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user