feat: Implement VM restart, auto-renew invoice functionality, and client invoice detail API, alongside admin UI improvements.
This commit is contained in:
@@ -15,6 +15,7 @@ Route::prefix('v1')->as('api.')->group(function () {
|
||||
Route::controller(ClientController::class)->group(function () {
|
||||
Route::post('login', 'login')->name('login');
|
||||
Route::post('register', 'register')->name('register');
|
||||
Route::post('refresh', 'refresh')->name('refresh');
|
||||
|
||||
Route::group(['middleware' => ['api.auth']], function () {
|
||||
Route::get('me', 'me')->name('me');
|
||||
@@ -28,8 +29,66 @@ Route::prefix('v1')->as('api.')->group(function () {
|
||||
Route::get('orders/{id}', 'getClientOrderDetail')->name('orders.detail');
|
||||
Route::get('invoices', 'getClientInvoices')->name('invoices.list');
|
||||
Route::get('invoices/{id}', 'getClientInvoiceDetail')->name('invoices.detail');
|
||||
Route::get('invoices/order/{orderNum}', 'getClientInvoiceDetailByOrderNum')->name('invoices.detailByOrderNum');
|
||||
Route::get('dich-vu', 'getClientServices')->name('services.list');
|
||||
Route::patch('dich-vu/{id}/auto-renew', 'updateClientServiceAutoRenew')->name('services.autoRenew.update');
|
||||
});
|
||||
|
||||
Route::middleware(['api.auth'])->get('get-ticket-console', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$orderNum = (string)$request->query('order_num', '');
|
||||
if ($orderNum === '') {
|
||||
return response()->json(['error' => true, 'message' => 'Thiếu order_num'], 422);
|
||||
}
|
||||
|
||||
$customer = $request->user();
|
||||
$order = \App\Models\Order::with(['vmWare.vCenter'])
|
||||
->where('customer_id', $customer->id)
|
||||
->where('order_num', $orderNum)
|
||||
->first();
|
||||
|
||||
if (!$order) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy đơn hàng'], 404);
|
||||
}
|
||||
|
||||
$vm = $order->vmWare->first();
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'Đơn hàng chưa có VM'], 404);
|
||||
}
|
||||
|
||||
$vcenter = $vm->vCenter;
|
||||
if (!$vcenter) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy thông tin vCenter'], 404);
|
||||
}
|
||||
|
||||
if (!$serviceVCenter->loginServer($vcenter)) {
|
||||
return response()->json(['error' => true, 'message' => 'Không thể đăng nhập vào vCenter']);
|
||||
}
|
||||
|
||||
$consoleTicket = $serviceVCenter->GetConsoleTickets($vm->vmware_id);
|
||||
|
||||
if ($consoleTicket === 'Login_Again') {
|
||||
if ($serviceVCenter->loginServer($vcenter)) {
|
||||
$consoleTicket = $serviceVCenter->GetConsoleTickets($vm->vmware_id);
|
||||
} else {
|
||||
return response()->json(['error' => true, 'message' => 'Không thể đăng nhập lại vào vCenter']);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_string($consoleTicket)) {
|
||||
return response()->json(['error' => true, 'message' => "Failed to get console ticket: $consoleTicket"]);
|
||||
}
|
||||
|
||||
if ($consoleTicket['error']) {
|
||||
return response()->json(['error' => true, 'message' => $consoleTicket['data']]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $consoleTicket['data'],
|
||||
'order_num' => $order->order_num,
|
||||
'vm_id' => $vm->id,
|
||||
]);
|
||||
})->name('client.console-ticket');
|
||||
});
|
||||
|
||||
// Services (Products) - Public & Protected Routes
|
||||
@@ -126,6 +185,434 @@ Route::prefix('v1/system')->middleware(['api.auth'])->group(function () {
|
||||
'data' => $ConsoleTicket['data'], // wss://10.10.10.120:443/ticket/...
|
||||
]);
|
||||
});
|
||||
|
||||
Route::post('vm-restart', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$productId = $request->input('product');
|
||||
$customer = $request->user();
|
||||
|
||||
if (!$productId) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Thiếu product id',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$product = \App\Models\ModuleVmware::with('ipAddress.host.datacenter.vCenter', 'vCenter')->find($productId);
|
||||
if (!$product) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không tìm thấy VM',
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ((int)$product->customer_id !== (int)$customer->id) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Bạn không có quyền thao tác VM này',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$result = $serviceVCenter->restartVM($product, $product->vmware_id);
|
||||
|
||||
if (is_string($result) || ($result['error'] ?? true)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => is_string($result) ? $result : ($result['data'] ?? 'Không thể restart VM'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi lệnh restart VM',
|
||||
'data' => $result['data'] ?? 'success',
|
||||
]);
|
||||
});
|
||||
|
||||
Route::post('vm-create-snapshot', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required', 'integer'],
|
||||
'name' => ['nullable', 'string', 'max:191'],
|
||||
'description' => ['nullable', 'string', 'max:1000'],
|
||||
]);
|
||||
|
||||
$product = \App\Models\ModuleVmware::with('ipAddress.host.datacenter.vCenter', 'vCenter')->find($payload['product']);
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$product->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$result = $serviceVCenter->CreateSnapShot($product, $payload['name'] ?? null);
|
||||
if (is_string($result) || ($result['error'] ?? true)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => is_string($result) ? $result : ($result['message'] ?? 'Không thể tạo snapshot'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
$snapshotName = $result['data'] ?? ($payload['name'] ?? null);
|
||||
$snapshot = \App\Models\ModuleSnapshot::create([
|
||||
'name' => $snapshotName,
|
||||
'description' => $payload['description'] ?? null,
|
||||
'customer_id' => $customer->id,
|
||||
'module_vmware_id' => $product->id,
|
||||
'status' => 'success',
|
||||
'snapshot_ref' => $snapshotName,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã tạo snapshot thành công',
|
||||
'data' => [
|
||||
'id' => $snapshot->id,
|
||||
'name' => $snapshot->name,
|
||||
'description' => $snapshot->description,
|
||||
'snapshot_ref' => $snapshot->snapshot_ref,
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
Route::get('vm-snapshots', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$customer = $request->user();
|
||||
$productId = (int)$request->query('product');
|
||||
if (!$productId) {
|
||||
return response()->json(['error' => true, 'message' => 'Thiếu product id'], 422);
|
||||
}
|
||||
|
||||
$product = \App\Models\ModuleVmware::with('ipAddress.host.datacenter.vCenter', 'vCenter')->find($productId);
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$product->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$remote = $serviceVCenter->getSnapShot($product);
|
||||
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 = \App\Models\ModuleSnapshot::query()
|
||||
->where('module_vmware_id', $product->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' => $customer->id,
|
||||
'module_vmware_id' => $product->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 {
|
||||
\App\Models\ModuleSnapshot::create($payload);
|
||||
}
|
||||
}
|
||||
|
||||
$dbSnapshots = \App\Models\ModuleSnapshot::query()
|
||||
->where('module_vmware_id', $product->id)
|
||||
->where('customer_id', $customer->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,
|
||||
]);
|
||||
});
|
||||
|
||||
Route::post('vm-revert-snapshot', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required', 'integer'],
|
||||
'snapshot' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
$product = \App\Models\ModuleVmware::with('ipAddress.host.datacenter.vCenter', 'vCenter')->find($payload['product']);
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$product->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$result = $serviceVCenter->RevertSnapShot($product, $payload['snapshot']);
|
||||
if (is_string($result) || ($result['error'] ?? true)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => is_string($result) ? $result : ($result['message'] ?? 'Không thể revert snapshot'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi lệnh revert snapshot',
|
||||
]);
|
||||
});
|
||||
|
||||
Route::post('vm-delete-snapshot', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required', 'integer'],
|
||||
'snapshot' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
$product = \App\Models\ModuleVmware::with('ipAddress.host.datacenter.vCenter', 'vCenter')->find($payload['product']);
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$product->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$result = $serviceVCenter->RemoveSnapShot($product, $payload['snapshot']);
|
||||
if (is_string($result) || ($result['error'] ?? true)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => is_string($result) ? $result : ($result['message'] ?? 'Không thể xoá snapshot'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
\App\Models\ModuleSnapshot::query()
|
||||
->where('module_vmware_id', $product->id)
|
||||
->where('customer_id', $customer->id)
|
||||
->where(function ($q) use ($payload) {
|
||||
$q->where('snapshot_ref', $payload['snapshot'])->orWhere('name', $payload['snapshot']);
|
||||
})
|
||||
->delete();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã xoá snapshot thành công',
|
||||
]);
|
||||
});
|
||||
|
||||
Route::get('vm-rebuild-options', function (\Illuminate\Http\Request $request) {
|
||||
$customer = $request->user();
|
||||
$productId = (int)$request->query('product');
|
||||
if (!$productId) {
|
||||
return response()->json(['error' => true, 'message' => 'Thiếu product id'], 422);
|
||||
}
|
||||
|
||||
$vm = \App\Models\ModuleVmware::with(['order.orderItems.product.osVersions.osGroup'])
|
||||
->find($productId);
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$vm->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$product = optional($vm->order?->orderItems?->first())->product;
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy product của dịch vụ hiện tại'], 404);
|
||||
}
|
||||
|
||||
$osOptions = $product->osVersions
|
||||
->where('status', 1)
|
||||
->map(fn($os) => [
|
||||
'id' => $os->id,
|
||||
'name' => $os->name,
|
||||
'version' => $os->version,
|
||||
'group' => $os->osGroup?->name,
|
||||
])
|
||||
->values();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'product_id' => $product->id,
|
||||
'product_uuid' => $product->uuid,
|
||||
'product_name' => $product->name,
|
||||
'os_options' => $osOptions,
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
Route::post('vm-rebuild', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required', 'integer'],
|
||||
'os_id' => ['required', 'integer', 'exists:os,id'],
|
||||
]);
|
||||
|
||||
$vm = \App\Models\ModuleVmware::with(['order.orderItems.product', 'vCenter', 'ipAddress.host.datacenter.vCenter'])
|
||||
->find($payload['product']);
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$vm->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$product = optional($vm->order?->orderItems?->first())->product;
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy product của dịch vụ hiện tại'], 404);
|
||||
}
|
||||
|
||||
$allowedOs = $product->osVersions()->where('status', 1)->pluck('id')->toArray();
|
||||
if (!in_array((int)$payload['os_id'], array_map('intval', $allowedOs), true)) {
|
||||
return response()->json(['error' => true, 'message' => 'OS không hợp lệ cho product hiện tại'], 422);
|
||||
}
|
||||
|
||||
\Illuminate\Support\Facades\DB::table('reinstall_logs')->insert([
|
||||
'product_id' => $vm->id,
|
||||
'product_type' => 4,
|
||||
'custome_id' => $customer->id,
|
||||
'os_id' => $payload['os_id'],
|
||||
'status' => 'pending',
|
||||
'initiator' => 'client',
|
||||
'note' => 'Client request rebuild from portal-client',
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$result = $serviceVCenter->reBuildVPS((string)$vm->name);
|
||||
if (is_string($result) || ($result['error'] ?? false)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => is_string($result) ? $result : ($result['message'] ?? 'Không thể rebuild VM'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi yêu cầu rebuild VM',
|
||||
'data' => $result,
|
||||
]);
|
||||
});
|
||||
|
||||
Route::get('vm-addon-options', function (\Illuminate\Http\Request $request) {
|
||||
$customer = $request->user();
|
||||
$productId = (int)$request->query('product');
|
||||
if (!$productId) {
|
||||
return response()->json(['error' => true, 'message' => 'Thiếu product id'], 422);
|
||||
}
|
||||
|
||||
$vm = \App\Models\ModuleVmware::with(['order.orderItems.product.addons'])
|
||||
->find($productId);
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$vm->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$product = optional($vm->order?->orderItems?->first())->product;
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy product của dịch vụ hiện tại'], 404);
|
||||
}
|
||||
|
||||
$addons = $product->addons()->where('status', true)->get()->map(function ($addon) {
|
||||
return [
|
||||
'id' => $addon->id,
|
||||
'code' => strtolower((string)$addon->code),
|
||||
'name' => $addon->name,
|
||||
'price' => (float)$addon->price,
|
||||
'min' => (int)($addon->min ?? 0),
|
||||
'max' => (int)($addon->max ?? 0),
|
||||
'step' => (int)($addon->step ?? 1),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'product_id' => $product->id,
|
||||
'product_name' => $product->name,
|
||||
'base_specs' => [
|
||||
'cpu' => (int)($product->cpu ?? 0),
|
||||
'ram' => (int)($product->ram ?? 0),
|
||||
'disk' => (int)($product->disk ?? 0),
|
||||
'bandwidth' => (int)($product->bandwidth ?? 0),
|
||||
'gpu' => $product->gpu,
|
||||
],
|
||||
'addons' => $addons->values(),
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
Route::post('vm-upgrade-addons', function (\Illuminate\Http\Request $request, \App\Http\Services\vCenter $serviceVCenter) {
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'product' => ['required', 'integer'],
|
||||
'cpu' => ['nullable', 'integer', 'min:0'],
|
||||
'ram' => ['nullable', 'integer', 'min:0'],
|
||||
'disk' => ['nullable', 'integer', 'min:0'],
|
||||
'bandwidth' => ['nullable', 'integer', 'min:0'],
|
||||
'gpu' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$vm = \App\Models\ModuleVmware::with(['vCenter', 'ipAddress.host.datacenter.vCenter'])
|
||||
->find($payload['product']);
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy VM'], 404);
|
||||
}
|
||||
if ((int)$vm->customer_id !== (int)$customer->id) {
|
||||
return response()->json(['error' => true, 'message' => 'Bạn không có quyền thao tác VM này'], 403);
|
||||
}
|
||||
|
||||
$vcenter = $vm->vCenter ?? $vm->ipAddress?->host?->datacenter?->vCenter;
|
||||
if (!$vcenter || !$serviceVCenter->loginServer($vcenter)) {
|
||||
return response()->json(['error' => true, 'message' => 'Không thể đăng nhập vCenter'], 400);
|
||||
}
|
||||
|
||||
$applied = [];
|
||||
|
||||
if (isset($payload['cpu']) && $payload['cpu'] > 0) {
|
||||
$res = $serviceVCenter->UpgradeCPUVirtualMachine($vm->vmware_id, (int)$payload['cpu']);
|
||||
if (is_array($res) && !($res['error'] ?? true)) $applied[] = 'cpu';
|
||||
}
|
||||
if (isset($payload['ram']) && $payload['ram'] > 0) {
|
||||
$res = $serviceVCenter->UpgradeMemoryVirtualMachine($vm->vmware_id, (int)$payload['ram']);
|
||||
if (is_array($res) && !($res['error'] ?? true)) $applied[] = 'ram';
|
||||
}
|
||||
if (isset($payload['disk']) && $payload['disk'] > 0) {
|
||||
$res = $serviceVCenter->UpgradeDiskVirtualMachine($vm, (int)$payload['disk']);
|
||||
if (is_array($res) && !($res['error'] ?? true)) $applied[] = 'disk';
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi yêu cầu nâng cấp addons',
|
||||
'data' => [
|
||||
'applied' => $applied,
|
||||
'note' => 'bandwidth/gpu hiện được ghi nhận yêu cầu, chưa có luồng tự động áp vào VM',
|
||||
],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user