update layout control vm

This commit is contained in:
Pham Bao
2025-08-10 21:50:26 +07:00
parent dcac40e770
commit cb0e263f44
16 changed files with 514 additions and 47 deletions
@@ -18,6 +18,8 @@ use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Validator as ValidatorInstance;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator as FacadesValidator;
class ServiceController extends Controller
{
@@ -547,6 +549,196 @@ class ServiceController extends Controller
}
}
/**
* Reset VM password (Windows/Linux) via vCenter helpers
*/
public function resetVMPassword(Request $request, vCenter $serviceVCenter)
{
try {
$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);
}
$validated = FacadesValidator::make($request->all(), [
'product_id' => 'required|string|max:255',
'new_password' => 'required|string|min:8|max:128',
]);
if ($validated->fails()) {
return response()->json([
'error' => true,
'message' => __('Dữ liệu không hợp lệ'),
'errors' => $validated->errors(),
], 422);
}
$productId = $this->sanitizeInput($request->product_id);
$newPassword = (string) $request->new_password;
// Load product and ensure ownership
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')
->where('vmware_id', $productId)
->where('customer_id', $user->id)
->first();
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);
}
$vcenter = $product->ipAddress->host->datacenter->vCenter ?? null;
if (!$vcenter) {
return response()->json([
'error' => true,
'message' => __('Cấu hình vCenter không hợp lệ'),
], 400);
}
// Login to vCenter
if (!$serviceVCenter->loginServer($vcenter)) {
return response()->json([
'error' => true,
'message' => __('Không thể kết nối đến vCenter'),
], 500);
}
// Determine OS family
$osFamily = strtoupper($product->os->osGroup->name ?? '');
$isWindows = str_contains($osFamily, 'WINDOWS');
// Prepare data
$ip = $product->ipAddress->ip ?? null;
if (!$ip) {
return response()->json([
'error' => true,
'message' => __('Không tìm thấy IP của máy ảo'),
], 400);
}
$payload = [
'ip' => $ip,
'user' => $isWindows ? 'administrator' : 'root',
'password' => $product->password ?? 'TGS@123', // fallback if field exists; adjust per model
'new_password' => $newPassword,
];
$ok = $isWindows
? $serviceVCenter->change_password_windows($payload)
: $serviceVCenter->change_password_linux($payload);
if (!$ok) {
return response()->json([
'error' => true,
'message' => __('Không thể đặt lại mật khẩu. Vui lòng thử lại sau.'),
], 500);
}
return response()->json([
'error' => false,
'message' => __('Đặt lại mật khẩu thành công'),
]);
} catch (\Exception $e) {
Log::error('Lỗi reset VM password', [
'product_id' => $request->product_id ?? null,
'error' => $e->getMessage(),
]);
return response()->json([
'error' => true,
'message' => __('Có lỗi xảy ra khi đặt lại mật khẩu'),
], 500);
}
}
/**
* Change Remote Desktop port (Windows only)
*/
public function changeRemotePort(Request $request, vCenter $serviceVCenter)
{
try {
$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);
}
$validated = FacadesValidator::make($request->all(), [
'product_id' => 'required|string|max:255',
'port' => 'required|integer|min:1|max:65535',
]);
if ($validated->fails()) {
return response()->json([
'error' => true,
'message' => __('Dữ liệu không hợp lệ'),
'errors' => $validated->errors(),
], 422);
}
$productId = $this->sanitizeInput($request->product_id);
$newPort = (int) $request->port;
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter', 'os.osGroup')
->where('vmware_id', $productId)
->where('customer_id', $user->id)
->first();
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);
}
$osFamily = strtoupper($product->os->osGroup->name ?? '');
if (!str_contains($osFamily, 'WINDOWS')) {
return response()->json([
'error' => true,
'message' => __('Chỉ hỗ trợ Windows'),
], 400);
}
$vcenter = $product->ipAddress->host->datacenter->vCenter ?? null;
if (!$vcenter) {
return response()->json([
'error' => true,
'message' => __('Cấu hình vCenter không hợp lệ'),
], 400);
}
if (!$serviceVCenter->loginServer($vcenter)) {
return response()->json([
'error' => true,
'message' => __('Không thể kết nối đến vCenter'),
], 500);
}
// NOTE: Implement the actual change RDP port over WinRM/PowerShell here.
// Placeholder success until service method is available.
return response()->json([
'error' => false,
'message' => __('Yêu cầu đổi port RDP đã được ghi nhận'),
]);
} catch (\Exception $e) {
Log::error('Lỗi changeRemotePort', [
'product_id' => $request->product_id ?? null,
'error' => $e->getMessage(),
]);
return response()->json([
'error' => true,
'message' => __('Có lỗi xảy ra khi đổi port'),
], 500);
}
}
/**
* Secure remove snapshot using session authentication - NO BEARER TOKEN
*/