- Implemented DomainCollection, DomainDetailResource, DomainNSCollection, DomainNSResource, and DomainResource for handling DNS domain and record data. - Created VpsServiceCollection and VpsServiceResource for VPS service management. - Added DnsManagerNS and DnsManagerUser models to represent DNS records and user associations. - Developed AdminZoneDnsSqlImporter for importing DNS data from SQL dumps, including customer and DNS domain records. - Added migration scripts to update the customers table with new fields and sync foreign key references for DNS tables. - Created a migration preview document outlining the mapping and schema changes for the admin zone DNS import.
370 lines
13 KiB
PHP
370 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Client\Service;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\Api\Service\VpsServiceCollection;
|
|
use App\Http\Resources\Api\Client\Service\ServiceClientDetailResource;
|
|
use App\Http\Services\vCenter;
|
|
use App\Models\Order;
|
|
use App\Models\Service;
|
|
use App\Repositories\Api\ServiceClient\ServiceClientRepository;
|
|
use App\Services\ActivityLogger;
|
|
use App\Services\Api\ServiceClient\ServiceClientService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ServiceController extends Controller
|
|
{
|
|
protected $serviceClientService;
|
|
protected $serviceClientRepository;
|
|
protected ActivityLogger $activityLogger;
|
|
|
|
public function __construct(
|
|
ServiceClientService $serviceClientService,
|
|
ServiceClientRepository $serviceClientRepository,
|
|
ActivityLogger $activityLogger,
|
|
) {
|
|
$this->serviceClientService = $serviceClientService;
|
|
$this->serviceClientRepository = $serviceClientRepository;
|
|
$this->activityLogger = $activityLogger;
|
|
}
|
|
|
|
public function indexVps(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
$customer = $request->user();
|
|
|
|
$services = $this->serviceClientService->getActiveServices($customer->id, 'vps', $perPage);
|
|
|
|
return (new VpsServiceCollection($services))
|
|
->additional([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service_vps',
|
|
])
|
|
->response();
|
|
}
|
|
|
|
public function indexHosting(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service_hosting',
|
|
'data' => [
|
|
'items' => [],
|
|
'pagination' => [
|
|
'total' => 0,
|
|
'per_page' => $perPage,
|
|
'current_page' => 1,
|
|
'last_page' => 1,
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function indexWebDesign(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service_web_design',
|
|
'data' => [
|
|
'items' => [],
|
|
'pagination' => [
|
|
'total' => 0,
|
|
'per_page' => $perPage,
|
|
'current_page' => 1,
|
|
'last_page' => 1,
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function getDetail(Request $request, string $uuid)
|
|
{
|
|
$service = $this->serviceClientService->getDetailService($uuid, $request->user()->id);
|
|
|
|
if (!$service) {
|
|
return response()->json(
|
|
[
|
|
'error' => true,
|
|
'message' => 'Không tìm thấy dịch vụ',
|
|
],
|
|
404,
|
|
);
|
|
}
|
|
|
|
return (new ServiceClientDetailResource($service))->additional([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service',
|
|
]);
|
|
}
|
|
|
|
public function getConsoleTicket(Request $request, string $uuid, vCenter $serviceVCenter)
|
|
{
|
|
$customer = $request->user();
|
|
|
|
$service = Service::with(['orderItem.order.vmWare.vCenter'])
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$service) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy dịch vụ'], 404);
|
|
}
|
|
|
|
$vm = $service->orderItem?->order?->vmWare?->first();
|
|
|
|
if (!$vm) {
|
|
return response()->json(['error' => true, 'message' => 'Dịch vụ chưa được cấp phát 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']]);
|
|
}
|
|
|
|
$ticket = basename(parse_url((string) $consoleTicket['data'], PHP_URL_PATH));
|
|
|
|
$this->activityLogger->log(
|
|
event: 'console_access',
|
|
logName: 'VM',
|
|
description: "Customer {$customer->email} truy cập console VM: {$vm->name} (service: {$uuid})",
|
|
subject: $vm,
|
|
newValues: ['service_uuid' => $uuid, 'vm_name' => $vm->name],
|
|
);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => $ticket,
|
|
]);
|
|
}
|
|
|
|
public function getTicketByOrderNum(Request $request, vCenter $serviceVCenter): JsonResponse
|
|
{
|
|
$orderNum = (string) $request->query('order_num', '');
|
|
if ($orderNum === '') {
|
|
return response()->json(['error' => true, 'message' => 'Thiếu order_num'], 422);
|
|
}
|
|
|
|
$customer = $request->user();
|
|
$order = 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']]);
|
|
}
|
|
|
|
$this->activityLogger->log(
|
|
event: 'console_access',
|
|
logName: 'VM',
|
|
description: "Customer {$customer->email} truy cập console VM: {$vm->name} (order: {$order->order_num})",
|
|
subject: $vm,
|
|
newValues: ['order_num' => $order->order_num, 'vm_name' => $vm->name],
|
|
);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $consoleTicket['data'],
|
|
'order_num' => $order->order_num,
|
|
'vm_id' => $vm->id,
|
|
]);
|
|
}
|
|
|
|
public function getAvailableOs(Request $request, string $uuid): JsonResponse
|
|
{
|
|
$customer = $request->user();
|
|
|
|
$service = Service::with([
|
|
'product.osVersions' => fn($q) => $q->where('status', 1)->with('osGroup'),
|
|
])
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$service) {
|
|
return response()->json(['success' => false, 'message' => 'Không tìm thấy dịch vụ'], 404);
|
|
}
|
|
|
|
if (!in_array($service->status, ['active', 'suspended'], true)) {
|
|
return response()->json(
|
|
['success' => false, 'message' => 'Dịch vụ không ở trạng thái hợp lệ để xem OS'],
|
|
422,
|
|
);
|
|
}
|
|
|
|
if (!$service->product) {
|
|
return response()->json(['success' => false, 'message' => 'Không tìm thấy product của dịch vụ'], 404);
|
|
}
|
|
|
|
$grouped = $service->product->osVersions
|
|
->groupBy(fn($os) => $os->osGroup?->id ?? 0)
|
|
->map(
|
|
fn($items) => [
|
|
'group' => [
|
|
'id' => $items->first()->osGroup?->id,
|
|
'name' => $items->first()->osGroup?->name,
|
|
'thumbnail' => $items->first()->osGroup?->thumbnail,
|
|
],
|
|
'items' => $items
|
|
->map(
|
|
fn($os) => [
|
|
'id' => $os->id,
|
|
'version' => $os->version,
|
|
'template_id' => $os->template_id,
|
|
],
|
|
)
|
|
->values(),
|
|
],
|
|
)
|
|
->values();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $grouped,
|
|
]);
|
|
}
|
|
|
|
public function rebuild(Request $request, string $uuid, vCenter $serviceVCenter): JsonResponse
|
|
{
|
|
$customer = $request->user();
|
|
$payload = $request->validate([
|
|
'os_id' => ['required', 'integer', 'exists:os,id'],
|
|
'password' => ['nullable', 'string', 'max:255'],
|
|
]);
|
|
|
|
$service = Service::with(['product.osVersions', 'vmware'])
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$service) {
|
|
return response()->json(['success' => false, 'message' => 'Không tìm thấy dịch vụ'], 404);
|
|
}
|
|
|
|
if ($service->status === 'suspended') {
|
|
return response()->json(
|
|
['success' => false, 'message' => 'Dịch vụ đang bị tạm ngưng, không thể rebuild'],
|
|
422,
|
|
);
|
|
}
|
|
|
|
if (!$service->isActive()) {
|
|
return response()->json(['success' => false, 'message' => 'Dịch vụ không ở trạng thái active'], 422);
|
|
}
|
|
|
|
$allowedOsIds =
|
|
$service->product?->osVersions()->where('status', 1)->pluck('os.id')->map('intval')->toArray() ?? [];
|
|
|
|
if (!in_array((int) $payload['os_id'], $allowedOsIds, true)) {
|
|
return response()->json(['success' => false, 'message' => 'OS không hợp lệ cho sản phẩm hiện tại'], 422);
|
|
}
|
|
|
|
$vm = $service->vmware;
|
|
if (!$vm) {
|
|
return response()->json(['success' => false, 'message' => 'Dịch vụ chưa được cấp phát VM'], 404);
|
|
}
|
|
|
|
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 rebuild via service UUID: ' . $uuid,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$result = $serviceVCenter->reBuildVPS((string) $vm->name);
|
|
if (is_string($result) || ($result['error'] ?? false)) {
|
|
return response()->json(
|
|
[
|
|
'success' => false,
|
|
'message' => is_string($result) ? $result : $result['message'] ?? 'Không thể rebuild VM',
|
|
],
|
|
400,
|
|
);
|
|
}
|
|
|
|
$this->activityLogger->log(
|
|
event: 'rebuild',
|
|
logName: 'VM',
|
|
description: "Customer {$customer->email} rebuild VM: {$vm->name} với OS ID: {$payload['os_id']} qua service UUID: {$uuid}",
|
|
subject: $vm,
|
|
newValues: ['os_id' => $payload['os_id'], 'service_uuid' => $uuid],
|
|
);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Đã gửi yêu cầu rebuild VM',
|
|
]);
|
|
}
|
|
}
|