Files
baodev0andClaude Sonnet 5.5 6315a392c0 feat(service): custom product, lịch sử giám sát VM và mã hoá response theo phiên
- Dịch vụ custom: bảng custom_products + services.custom_product_id; createService nhận
  custom{} thay product_id, giá = đơn giá tháng x số tháng chu kỳ (1/3/6/12/24/36),
  gia hạn tính theo cùng đơn giá, ServiceClientDetailResource trả custom_product và specs.
- Giám sát VM: GET v1/system/vm-metrics/history gom lịch sử về tối đa 240 điểm;
  lỗi lấy dung lượng đĩa không còn làm hỏng cả thẻ metrics.
- SealResponse: AES-256-GCM theo khoá phiên do proxy Next cấp qua header x-seal-key
  (HMAC của session id). Client opt-in bằng x-seal; chi tiết dịch vụ, mật khẩu VM và
  console ticket luôn được mã hoá. Body gửi lên dạng {enc} được giải mã trước validate;
  khoá lệch trả 428 để client lấy khoá mới. Đây là che mắt DevTools, không phải bảo mật
  chống chính chủ tài khoản.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 13:58:50 +07:00

73 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Service\Concerns;
use App\Models\VirtualMachines;
use Illuminate\Support\Carbon;
use App\Services\VmMetricsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use RuntimeException;
trait ManagesVmMetrics
{
public function metrics(Request $request, VmMetricsService $metricsService): JsonResponse
{
$productId = $request->input('product');
if (!$productId) {
return response()->json(['success' => false, 'message' => 'Thiếu product id.'], 422);
}
$vm = $this->resolveVmById($productId, $request, requireActiveService: false);
if ($vm instanceof JsonResponse) {
return $vm;
}
try {
$inventory = VirtualMachines::where('vm_id', $vm->vmware_id)->first(['ram_mb', 'cpu']);
$metrics = $metricsService->get((string) $vm->vmware_id, $inventory?->ram_mb, $inventory?->cpu);
} catch (RuntimeException $exception) {
$status = $exception->getCode() === 404 ? 404 : 502;
return response()->json([
'success' => false,
'message' => $status === 404
? 'Chưa có dữ liệu giám sát cho VM.'
: 'Không thể tải dữ liệu giám sát VM.',
], $status);
}
return response()->json(['success' => true, 'data' => $metrics]);
}
public function metricsHistory(Request $request, VmMetricsService $metricsService): JsonResponse
{
$data = $request->validate([
'product' => 'required|string',
'from' => 'nullable|date',
'to' => 'nullable|date|after:from',
]);
$to = isset($data['to']) ? Carbon::parse($data['to']) : now();
$from = isset($data['from']) ? Carbon::parse($data['from']) : $to->copy()->subDay();
if ($from->diffInDays($to) > 31) {
return response()->json(['success' => false, 'message' => 'Chỉ xem được tối đa 31 ngày mỗi lần.'], 422);
}
$vm = $this->resolveVmById($data['product'], $request, requireActiveService: false);
if ($vm instanceof JsonResponse) {
return $vm;
}
try {
$points = $metricsService->history((string) $vm->vmware_id, $from, $to);
} catch (RuntimeException $exception) {
$status = $exception->getCode() === 404 ? 404 : 502;
return response()->json(['success' => false, 'message' => 'Không thể tải lịch sử giám sát VM.'], $status);
}
return response()->json(['success' => true, 'data' => $points]);
}
}