feat: implement VM snapshot management, enhance client resource loading, and refactor deployment workflow for production
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Service;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Api\Service\Concerns\ResolvesVm;
|
||||
use App\Http\Services\vCenter;
|
||||
use App\Jobs\VirtualMachine\Snapshot\CreateSnapshot;
|
||||
use App\Jobs\VirtualMachine\Snapshot\RevertSnapshot;
|
||||
use App\Jobs\VirtualMachine\Snapshot\RemoveSnapshot;
|
||||
use App\Models\ModuleSnapshot;
|
||||
use App\Models\ModuleVmware;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SnapshotController extends Controller
|
||||
{
|
||||
use ResolvesVm;
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required'],
|
||||
'description' => ['nullable', 'string', 'max:1000'],
|
||||
]);
|
||||
|
||||
$vm = $this->resolveVmById($payload['product'], $request);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$today = now()->format('d-m-Y');
|
||||
$existingCount = ModuleSnapshot::where('module_vmware_id', $vm->id)
|
||||
->whereDate('created_at', today())
|
||||
->count();
|
||||
$snapshotName = sprintf('%s-%s-%d', $vm->name, $today, $existingCount + 1);
|
||||
|
||||
$snapshot = ModuleSnapshot::create([
|
||||
'name' => $snapshotName,
|
||||
'description' => $payload['description'] ?? null,
|
||||
'customer_id' => $customer->id,
|
||||
'module_vmware_id' => $vm->id,
|
||||
'status' => 'pending',
|
||||
'snapshot_ref' => null,
|
||||
]);
|
||||
|
||||
CreateSnapshot::dispatch($snapshot->id);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi yêu cầu tạo snapshot vào hàng đợi',
|
||||
'data' => [
|
||||
'id' => $snapshot->id,
|
||||
'name' => $snapshot->name,
|
||||
'description' => $snapshot->description,
|
||||
'status' => 'pending',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function index(Request $request, vCenter $serviceVCenter): JsonResponse
|
||||
{
|
||||
$productParam = $request->query('product');
|
||||
if (!$productParam) {
|
||||
return response()->json(['error' => true, 'message' => 'Thiếu product id'], 422);
|
||||
}
|
||||
|
||||
$vm = $this->resolveVmById($productParam, $request);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$remote = $serviceVCenter->getSnapShot($vm);
|
||||
if (is_string($remote) || ($remote['error'] ?? true)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => is_string($remote) ? $remote : ($remote['message'] ?? 'Không thể lấy danh sách snapshot'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
$remoteItems = $remote['data'] ?? [];
|
||||
foreach ($remoteItems as $item) {
|
||||
$ref = (string) ($item['id'] ?? $item['name'] ?? '');
|
||||
$name = (string) ($item['name'] ?? $ref);
|
||||
if ($name === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$snapshot = ModuleSnapshot::query()
|
||||
->where('module_vmware_id', $vm->id)
|
||||
->where(function ($q) use ($ref, $name) {
|
||||
$q->where('snapshot_ref', $ref)->orWhere('name', $name);
|
||||
})
|
||||
->first();
|
||||
|
||||
$payload = [
|
||||
'name' => $name,
|
||||
'description' => $item['description'] ?? null,
|
||||
'customer_id' => $request->user()->id,
|
||||
'module_vmware_id' => $vm->id,
|
||||
'status' => 'success',
|
||||
'snapshot_ref' => $ref ?: $name,
|
||||
'size_gb' => isset($item['size_gb']) ? (float) $item['size_gb'] : null,
|
||||
'size_last_synced_at' => now(),
|
||||
];
|
||||
|
||||
if ($snapshot) {
|
||||
$snapshot->update($payload);
|
||||
} else {
|
||||
ModuleSnapshot::create($payload);
|
||||
}
|
||||
}
|
||||
|
||||
$dbSnapshots = ModuleSnapshot::query()
|
||||
->where('module_vmware_id', $vm->id)
|
||||
->where('customer_id', $request->user()->id)
|
||||
->orderByDesc('created_at')
|
||||
->get()
|
||||
->map(function ($snap) {
|
||||
return [
|
||||
'id' => $snap->id,
|
||||
'name' => $snap->name,
|
||||
'description' => $snap->description,
|
||||
'snapshot_ref' => $snap->snapshot_ref,
|
||||
'status' => $snap->status,
|
||||
'size_gb' => $snap->size_gb,
|
||||
'created_at' => $snap->created_at?->toISOString(),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $dbSnapshots,
|
||||
]);
|
||||
}
|
||||
|
||||
public function revert(Request $request): JsonResponse
|
||||
{
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required'],
|
||||
'snapshot_id' => ['required', 'numeric'],
|
||||
]);
|
||||
|
||||
$vm = $this->resolveVmById($payload['product'], $request);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$snapshot = ModuleSnapshot::where('id', $payload['snapshot_id'])
|
||||
->where('module_vmware_id', $vm->id)
|
||||
->where('customer_id', $customer->id)
|
||||
->first();
|
||||
|
||||
if (!$snapshot) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy snapshot'], 404);
|
||||
}
|
||||
|
||||
RevertSnapshot::dispatch($vm->id, $snapshot->id);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi yêu cầu revert snapshot vào hàng đợi',
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(Request $request): JsonResponse
|
||||
{
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required'],
|
||||
'snapshot_id' => ['required', 'numeric'],
|
||||
]);
|
||||
|
||||
$vm = $this->resolveVmById($payload['product'], $request);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$snapshot = ModuleSnapshot::where('id', $payload['snapshot_id'])
|
||||
->where('module_vmware_id', $vm->id)
|
||||
->where('customer_id', $customer->id)
|
||||
->first();
|
||||
|
||||
if (!$snapshot) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy snapshot'], 404);
|
||||
}
|
||||
|
||||
RemoveSnapshot::dispatch($vm->id, $snapshot->id);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi yêu cầu xóa snapshot vào hàng đợi',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user