update
This commit is contained in:
@@ -3,17 +3,32 @@
|
||||
namespace App\Http\Controllers\Api\Admin\DnsManager;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Jobs\DnsManager\SyncDnsFromPlesk;
|
||||
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
|
||||
use App\Repositories\DnsManager\Record\DnsRecordRepositoryInterface;
|
||||
use App\Services\ServerService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DomainController extends Controller
|
||||
{
|
||||
private const RECORD_RULES = [
|
||||
'type' => 'required|string|in:A,AAAA,CNAME,TXT,MX,NS,SRV',
|
||||
'name' => 'required|string|max:255',
|
||||
'value' => 'required|string',
|
||||
'ttl' => 'nullable|integer|min:60',
|
||||
'priority' => 'nullable|integer|min:0',
|
||||
'weight' => 'nullable|integer|min:0',
|
||||
'port' => 'nullable|integer|min:0',
|
||||
'service_name' => 'nullable|string|max:255',
|
||||
'protocol' => 'nullable|string|max:50',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly DnsDomainRepositoryInterface $domainRepository,
|
||||
private readonly DnsRecordRepositoryInterface $recordRepository,
|
||||
private readonly ServerService $serverService,
|
||||
) {}
|
||||
|
||||
// ─── Domains ─────────────────────────────────────────────────────────────
|
||||
@@ -192,6 +207,18 @@ class DomainController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
// ─── Sync from Plesk ─────────────────────────────────────────────────────
|
||||
|
||||
public function syncDns(): JsonResponse
|
||||
{
|
||||
dispatch(new SyncDnsFromPlesk());
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã đưa lệnh đồng bộ DNS vào hàng đợi, quá trình sẽ chạy nền.',
|
||||
]);
|
||||
}
|
||||
|
||||
// ─── DNS Records ─────────────────────────────────────────────────────────
|
||||
|
||||
public function dnsIndex(int $domainId): JsonResponse
|
||||
@@ -218,21 +245,33 @@ class DomainController extends Controller
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$domainId"], 404);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'type' => 'required|string|in:A,AAAA,CNAME,TXT,MX,NS,SRV',
|
||||
'name' => 'required|string|max:255',
|
||||
'value' => 'required|string',
|
||||
'ttl' => 'nullable|integer|min:60',
|
||||
'priority' => 'nullable|integer|min:0',
|
||||
]);
|
||||
$validated = $request->validate(self::RECORD_RULES);
|
||||
|
||||
if (!$this->serverService->addServerById($domain->dns_manager_server_id)) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy server DNS'], 404);
|
||||
}
|
||||
|
||||
$result = $this->serverService
|
||||
->handlePayload(['domain' => $domain->domain_name, ...$validated])
|
||||
->addRecord();
|
||||
|
||||
if ($result['error']) {
|
||||
return response()->json(['error' => true, 'message' => $result['message'] ?? 'Tạo bản ghi DNS trên Plesk thất bại'], 422);
|
||||
}
|
||||
|
||||
$record = $this->recordRepository->create([
|
||||
'dns_manager_domain_id' => $domainId,
|
||||
'type' => $request->type,
|
||||
'name' => $request->name,
|
||||
'value' => $request->value,
|
||||
'ttl' => $request->ttl ?? 300,
|
||||
'priority' => $request->priority,
|
||||
'type' => $validated['type'],
|
||||
'type_valid' => $validated['type'],
|
||||
'name' => $validated['name'],
|
||||
'value' => $validated['value'],
|
||||
'ttl' => $validated['ttl'] ?? 300,
|
||||
'priority' => $validated['priority'] ?? null,
|
||||
'weight' => $validated['weight'] ?? null,
|
||||
'port' => $validated['port'] ?? null,
|
||||
'service_name' => $validated['service_name'] ?? null,
|
||||
'protocol' => $validated['protocol'] ?? null,
|
||||
'plesk_id' => $result['data']['id'] ?? null,
|
||||
]);
|
||||
|
||||
return response()->json(['error' => false, 'data' => $record], 201);
|
||||
@@ -240,6 +279,12 @@ class DomainController extends Controller
|
||||
|
||||
public function dnsUpdate(int $domainId, int $recordId, Request $request): JsonResponse
|
||||
{
|
||||
$domain = $this->domainRepository->find($domainId);
|
||||
|
||||
if (!$domain) {
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$domainId"], 404);
|
||||
}
|
||||
|
||||
$record = $this->recordRepository->model
|
||||
->where('dns_manager_domain_id', $domainId)
|
||||
->find($recordId);
|
||||
@@ -248,21 +293,47 @@ class DomainController extends Controller
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy bản ghi DNS #$recordId"], 404);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'type' => 'required|string|in:A,AAAA,CNAME,TXT,MX,NS,SRV',
|
||||
'name' => 'required|string|max:255',
|
||||
'value' => 'required|string',
|
||||
'ttl' => 'nullable|integer|min:60',
|
||||
'priority' => 'nullable|integer|min:0',
|
||||
]);
|
||||
$validated = $request->validate(self::RECORD_RULES);
|
||||
|
||||
$this->recordRepository->update($recordId, $request->only(['type', 'name', 'value', 'ttl', 'priority']));
|
||||
// ponytail: records created before Plesk sync was wired up have no plesk_id, update locally only
|
||||
if ($record->plesk_id) {
|
||||
if (!$this->serverService->addServerById($domain->dns_manager_server_id)) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy server DNS'], 404);
|
||||
}
|
||||
|
||||
$result = $this->serverService
|
||||
->handlePayload(['domain' => $domain->domain_name, ...$validated])
|
||||
->updateRecord($record->plesk_id);
|
||||
|
||||
if ($result['error']) {
|
||||
return response()->json(['error' => true, 'message' => $result['message'] ?? 'Cập nhật bản ghi DNS trên Plesk thất bại'], 422);
|
||||
}
|
||||
}
|
||||
|
||||
$this->recordRepository->update($recordId, [
|
||||
'type' => $validated['type'],
|
||||
'type_valid' => $validated['type'],
|
||||
'name' => $validated['name'],
|
||||
'value' => $validated['value'],
|
||||
'ttl' => $validated['ttl'] ?? 300,
|
||||
'priority' => $validated['priority'] ?? null,
|
||||
'weight' => $validated['weight'] ?? null,
|
||||
'port' => $validated['port'] ?? null,
|
||||
'service_name' => $validated['service_name'] ?? null,
|
||||
'protocol' => $validated['protocol'] ?? null,
|
||||
]);
|
||||
|
||||
return response()->json(['error' => false, 'data' => $this->recordRepository->find($recordId)]);
|
||||
}
|
||||
|
||||
public function dnsDestroy(int $domainId, int $recordId): JsonResponse
|
||||
{
|
||||
$domain = $this->domainRepository->find($domainId);
|
||||
|
||||
if (!$domain) {
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$domainId"], 404);
|
||||
}
|
||||
|
||||
$record = $this->recordRepository->model
|
||||
->where('dns_manager_domain_id', $domainId)
|
||||
->find($recordId);
|
||||
@@ -271,6 +342,18 @@ class DomainController extends Controller
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy bản ghi DNS #$recordId"], 404);
|
||||
}
|
||||
|
||||
if ($record->plesk_id) {
|
||||
if (!$this->serverService->addServerById($domain->dns_manager_server_id)) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy server DNS'], 404);
|
||||
}
|
||||
|
||||
$result = $this->serverService->deleteRecord($record->plesk_id);
|
||||
|
||||
if ($result['error']) {
|
||||
return response()->json(['error' => true, 'message' => $result['message'] ?? 'Xoá bản ghi DNS trên Plesk thất bại'], 422);
|
||||
}
|
||||
}
|
||||
|
||||
$this->recordRepository->delete($recordId);
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã xoá bản ghi DNS']);
|
||||
|
||||
Reference in New Issue
Block a user