feat: Implement asynchronous VM power operations and refactor vCenter snapshot retrieval to use API calls.
This commit is contained in:
@@ -422,122 +422,6 @@ class ServiceController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Lấy danh sách snapshot với bảo mật và tối ưu hóa
|
||||
*/
|
||||
// public function getSnapshots(Request $request, vCenter $serviceVCenter)
|
||||
// {
|
||||
// try {
|
||||
// // Kiểm tra authentication
|
||||
// $user = customer();
|
||||
// if (!$user) {
|
||||
// return response()->json([
|
||||
// 'error' => true,
|
||||
// 'message' => __('Chưa đăng nhập. Vui lòng đăng nhập để sử dụng tính năng này.')
|
||||
// ], 401);
|
||||
// }
|
||||
|
||||
// // Validation với sanitization
|
||||
// $validator = Validator::make($request->all(), [
|
||||
// 'product_id' => 'required|string|max:255',
|
||||
// ]);
|
||||
|
||||
// if ($validator->fails()) {
|
||||
// return response()->json([
|
||||
// 'error' => true,
|
||||
// 'message' => __('Dữ liệu không hợp lệ'),
|
||||
// 'errors' => $validator->errors()
|
||||
// ], 422);
|
||||
// }
|
||||
|
||||
// $productId = $this->sanitizeInput($request->product_id);
|
||||
// $userId = $user->id;
|
||||
|
||||
// // Cache key cho snapshot data
|
||||
// $cacheKey = "snapshots_{$userId}_{$productId}";
|
||||
// $cacheDuration = 300; // 5 phút
|
||||
|
||||
// // Thử lấy từ cache trước
|
||||
// if (Cache::has($cacheKey)) {
|
||||
// $cachedData = Cache::get($cacheKey);
|
||||
// if ($cachedData && !$this->isCacheExpired($cachedData)) {
|
||||
// return response()->json([
|
||||
// 'error' => false,
|
||||
// 'message' => __('Lấy danh sách snapshot thành công'),
|
||||
// 'data' => $cachedData,
|
||||
// 'cached' => true
|
||||
// ]);
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Tìm VPS với eager loading tối ưu
|
||||
// $product = $this->findVmwareProduct($productId, $userId);
|
||||
// if (!$product) {
|
||||
// return response()->json([
|
||||
// 'error' => true,
|
||||
// 'message' => __('Không tìm thấy VPS hoặc bạn không có quyền truy cập')
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// // Kiểm tra cấu hình vCenter
|
||||
// $vcenter = $this->validateVcenterConfiguration($product);
|
||||
// if (!$vcenter) {
|
||||
// return response()->json([
|
||||
// 'error' => true,
|
||||
// 'message' => __('Cấu hình vCenter không hợp lệ')
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
// // Lấy snapshot từ vCenter
|
||||
// $snapshots = $this->fetchSnapshotsFromVCenter($serviceVCenter, $product, $vcenter);
|
||||
// if ($snapshots === false) {
|
||||
// return response()->json([
|
||||
// 'error' => true,
|
||||
// 'message' => __('Không thể kết nối đến vCenter')
|
||||
// ], 500);
|
||||
// }
|
||||
|
||||
// // Lấy thông tin bổ sung từ database
|
||||
// $dbSnapshots = $this->getDatabaseSnapshots($userId, $product->id);
|
||||
|
||||
// // Merge và sanitize data
|
||||
// $processedSnapshots = $this->processSnapshotsData($snapshots, $dbSnapshots);
|
||||
|
||||
// // Chuẩn bị response data
|
||||
// $responseData = [
|
||||
// 'snapshots' => $processedSnapshots,
|
||||
// 'total_count' => count($processedSnapshots),
|
||||
// 'vm_name' => $this->sanitizeOutput($product->name),
|
||||
// 'vm_id' => $product->vmware_id,
|
||||
// 'last_updated' => now()->format('Y-m-d H:i:s')
|
||||
// ];
|
||||
|
||||
// // Cache kết quả
|
||||
// Cache::put($cacheKey, $responseData, $cacheDuration);
|
||||
|
||||
// return response()->json([
|
||||
// 'error' => false,
|
||||
// 'message' => __('Lấy danh sách snapshot thành công'),
|
||||
// 'data' => $responseData,
|
||||
// 'cached' => false
|
||||
// ]);
|
||||
// } catch (\Exception $e) {
|
||||
// Log::error('Lỗi khi lấy danh sách snapshot', [
|
||||
// 'user_id' => $user->id ?? 'guest',
|
||||
// 'product_id' => $request->product_id ?? 'not_provided',
|
||||
// 'error' => $e->getMessage(),
|
||||
// 'file' => $e->getFile(),
|
||||
// 'line' => $e->getLine()
|
||||
// ]);
|
||||
|
||||
// return response()->json([
|
||||
// 'error' => true,
|
||||
// 'message' => __('Có lỗi xảy ra khi lấy danh sách snapshot')
|
||||
// ], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function getSnapshots(Request $request)
|
||||
{
|
||||
$user = customer();
|
||||
@@ -1002,7 +886,10 @@ class ServiceController extends Controller
|
||||
/**
|
||||
* Secure start VM using session authentication - NO BEARER TOKEN
|
||||
*/
|
||||
public function startVM(Request $request, vCenter $vCenterService)
|
||||
/**
|
||||
* Secure start VM using session authentication - NO BEARER TOKEN
|
||||
*/
|
||||
public function startVM(Request $request)
|
||||
{
|
||||
try {
|
||||
$user = customer();
|
||||
@@ -1037,28 +924,19 @@ class ServiceController extends Controller
|
||||
], 404);
|
||||
}
|
||||
|
||||
$result = $vCenterService->StartVirtualMachine($product, $product->vmware_id);
|
||||
if (is_array($result) && isset($result['error']) && $result['error'] === false) {
|
||||
// Clear VM info cache after starting VM
|
||||
Cache::forget('vm_info_' . $product->vmware_id);
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu khởi động máy ảo đã được gửi thành công.')
|
||||
]);
|
||||
} else {
|
||||
$msg = is_array($result) && isset($result['data']) ? $result['data'] : __('Không thể khởi động máy ảo');
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $msg
|
||||
], 500);
|
||||
}
|
||||
// Dispatch Job
|
||||
\App\Jobs\VirtualMachine\Power\StartVmJob::dispatch($product->id, $userId);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu khởi động máy ảo đã được gửi. Vui lòng chờ trong giây lát.')
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Lỗi khi khởi động máy ảo', [
|
||||
'user_id' => $user->id ?? 'guest',
|
||||
'product_id' => $request->product_id ?? 'not_provided',
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
@@ -1070,7 +948,7 @@ class ServiceController extends Controller
|
||||
/**
|
||||
* Secure stop VM using session authentication - NO BEARER TOKEN
|
||||
*/
|
||||
public function stopVM(Request $request, vCenter $vCenterService)
|
||||
public function stopVM(Request $request)
|
||||
{
|
||||
try {
|
||||
$user = customer();
|
||||
@@ -1105,27 +983,19 @@ class ServiceController extends Controller
|
||||
], 404);
|
||||
}
|
||||
|
||||
$result = $vCenterService->ShutdownGuestVirtualMachine($product, $product->vmware_id);
|
||||
if (is_array($result) && isset($result['error']) && $result['error'] === false) {
|
||||
Cache::forget('the_gioi_so_database_the_gioi_so_cache:vm_info_' . $product->vmware_id);
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu tắt máy ảo đã được gửi thành công.')
|
||||
]);
|
||||
} else {
|
||||
$msg = is_array($result) && isset($result['data']) ? $result['data'] : __('Không thể tắt máy ảo');
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $msg
|
||||
], 500);
|
||||
}
|
||||
// Dispatch Job
|
||||
\App\Jobs\VirtualMachine\Power\StopVmJob::dispatch($product->id, $userId);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu tắt máy ảo đã được gửi. Vui lòng chờ trong giây lát.')
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Lỗi khi tắt máy ảo', [
|
||||
'user_id' => $user->id ?? 'guest',
|
||||
'product_id' => $request->product_id ?? 'not_provided',
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
@@ -1137,7 +1007,7 @@ class ServiceController extends Controller
|
||||
/**
|
||||
* Secure reboot VM using session authentication - NO BEARER TOKEN
|
||||
*/
|
||||
public function rebootVM(Request $request, vCenter $vCenterService)
|
||||
public function rebootVM(Request $request)
|
||||
{
|
||||
try {
|
||||
$user = customer();
|
||||
@@ -1172,26 +1042,19 @@ class ServiceController extends Controller
|
||||
], 404);
|
||||
}
|
||||
|
||||
$result = $vCenterService->RebootVirtualMachine($product, $product->vmware_id);
|
||||
if (is_array($result) && isset($result['error']) && $result['error'] === false) {
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu khởi động lại máy ảo đã được gửi thành công.')
|
||||
]);
|
||||
} else {
|
||||
$msg = is_array($result) && isset($result['data']) ? $result['data'] : __('Không thể khởi động lại máy ảo');
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $msg
|
||||
], 500);
|
||||
}
|
||||
// Dispatch Job
|
||||
\App\Jobs\VirtualMachine\Power\RebootVmJob::dispatch($product->id, $userId);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu khởi động lại máy ảo đã được gửi. Vui lòng chờ trong giây lát.')
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Lỗi khi reboot VM', [
|
||||
'user_id' => $user->id ?? 'guest',
|
||||
'product_id' => $request->product_id ?? 'not_provided',
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
|
||||
Reference in New Issue
Block a user