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:
@@ -25,25 +25,36 @@ class CreateSnapshot implements ShouldQueue
|
||||
|
||||
public function handle(ServiceVCenter $serviceVCenter)
|
||||
{
|
||||
$snapshot = ModuleSnapshot::with('vm.ipAddress.host.datacenter.vCenter')
|
||||
$snapshot = ModuleSnapshot::with('vm')
|
||||
->find($this->snapshotId);
|
||||
|
||||
if (!$snapshot) {
|
||||
Log::error('Snapshot not found', ['snapshot_id' => $this->snapshotId]);
|
||||
return;
|
||||
}
|
||||
|
||||
$snapshot->update(['status' => 'processing']);
|
||||
|
||||
$product = $snapshot->vm;
|
||||
$vcenter = $product->ipAddress->host->datacenter->vCenter;
|
||||
|
||||
// Resolve vCenter using helper method
|
||||
$vcenter = $serviceVCenter->resolveVCenterFromProduct($product);
|
||||
|
||||
if (!$vcenter) {
|
||||
$snapshot->update([
|
||||
'status' => 'failed',
|
||||
'error_message' => 'Không tìm thấy thông tin vCenter',
|
||||
]);
|
||||
throw new \Exception('Không tìm thấy thông tin vCenter cho VM');
|
||||
}
|
||||
|
||||
try {
|
||||
// Config vCenter
|
||||
$serviceVCenter->token = $vcenter->token;
|
||||
$serviceVCenter->apiUri = $vcenter->host;
|
||||
|
||||
// Call vCenter
|
||||
$result = $serviceVCenter->CreateSnapShot($product);
|
||||
// Call vCenter with snapshot name from DB
|
||||
$result = $serviceVCenter->CreateSnapShot($product, $snapshot->name);
|
||||
|
||||
if ($result === 'Login_Again') {
|
||||
$token = $serviceVCenter->loginServer($vcenter);
|
||||
@@ -53,7 +64,7 @@ class CreateSnapshot implements ShouldQueue
|
||||
|
||||
$vcenter->update(['token' => $token]);
|
||||
$serviceVCenter->token = $token;
|
||||
$result = $serviceVCenter->CreateSnapShot($product);
|
||||
$result = $serviceVCenter->CreateSnapShot($product, $snapshot->name);
|
||||
}
|
||||
|
||||
if (is_array($result) && ($result['error'] ?? false)) {
|
||||
@@ -62,10 +73,8 @@ class CreateSnapshot implements ShouldQueue
|
||||
|
||||
// Update DB SUCCESS
|
||||
$snapshot->update([
|
||||
'name' => $result['name'] ?? $snapshot->name,
|
||||
'status' => 'success',
|
||||
'snapshot_ref' => $result['snapshot_id'] ?? null,
|
||||
'vcenter_task_id' => $result['task_id'] ?? null,
|
||||
'snapshot_ref' => $result['data'] ?? $snapshot->name,
|
||||
]);
|
||||
|
||||
// Increment quota CHỈ khi success
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -44,21 +44,21 @@ class RevertSnapshot 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;
|
||||
@@ -71,7 +71,7 @@ class RevertSnapshot implements ShouldQueue
|
||||
]);
|
||||
|
||||
// Attempt to revert snapshot
|
||||
$result = $serviceVCenter->RevertSnapShot($product, $snapshot->name);
|
||||
$result = $serviceVCenter->RevertSnapShot($product, $snapshot->snapshot_ref ?? $snapshot->name);
|
||||
|
||||
// Handle re-login if needed
|
||||
if ($result === 'Login_Again') {
|
||||
@@ -80,7 +80,7 @@ class RevertSnapshot implements ShouldQueue
|
||||
if ($token) {
|
||||
$vcenter->update(['token' => $token]);
|
||||
$serviceVCenter->token = $token;
|
||||
$result = $serviceVCenter->RevertSnapShot($product, $snapshot->name);
|
||||
$result = $serviceVCenter->RevertSnapShot($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