add endpoints for filtering applications by domain and managing container labels

- Add /api/v1/domains?uuid={application_uuid} endpoint (GET) to filter applications by domains for a given project UUID
- Add /api/v1/domains endpoint (PUT) to update domains and regenerate container labels
- Add /api/v1/domains endpoint (DELETE) to delete domains and regenerate container labels
This commit is contained in:
samirimtiaz1996
2024-06-19 00:59:39 +06:00
parent de7380fb0c
commit d688244664
3 changed files with 141 additions and 32 deletions

View File

@@ -3,9 +3,11 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Application;
use App\Models\InstanceSettings;
use App\Models\Project as ModelsProject;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class Domains extends Controller
{
@@ -15,6 +17,15 @@ class Domains extends Controller
if (is_null($teamId)) {
return invalid_token();
}
$uuid = $request->query->get('uuid');
if ($uuid) {
$domains = Application::getDomainsByUuid($uuid);
return response()->json([
'uuid' => $uuid,
'domains' => $domains,
]);
}
$projects = ModelsProject::where('team_id', $teamId)->get();
$domains = collect();
$applications = $projects->pluck('applications')->flatten();
@@ -38,7 +49,7 @@ class Domains extends Controller
'ip' => $settings->public_ipv6,
]);
}
if (! $settings->public_ipv4 && ! $settings->public_ipv6) {
if (!$settings->public_ipv4 && !$settings->public_ipv6) {
$domains->push([
'domain' => $fqdn,
'ip' => $ip,
@@ -74,7 +85,7 @@ class Domains extends Controller
'ip' => $settings->public_ipv6,
]);
}
if (! $settings->public_ipv4 && ! $settings->public_ipv6) {
if (!$settings->public_ipv4 && !$settings->public_ipv6) {
$domains->push([
'domain' => $fqdn,
'ip' => $ip,
@@ -101,4 +112,90 @@ class Domains extends Controller
return response()->json($domains);
}
public function updateDomains(Request $request)
{
$teamId = get_team_id_from_token();
if (is_null($teamId)) {
return invalid_token();
}
$validator = Validator::make($request->all(), [
'uuid' => 'required|string|exists:applications,uuid',
'domains' => 'required|array',
'domains.*' => 'required|string|distinct',
]);
if ($validator->fails()) {
return response()->json([
'success' => false,
'message' => 'Validation failed',
'errors' => $validator->errors()
], 422);
}
$application = Application::where('uuid', $request->uuid)->first();
if (!$application) {
return response()->json([
'success' => false,
'message' => 'Application not found'
], 404);
}
$existingDomains = explode(',', $application->fqdn);
$newDomains = $request->domains;
$filteredNewDomains = array_filter($newDomains, function ($domain) use ($existingDomains) {
return !in_array($domain, $existingDomains);
});
$mergedDomains = array_unique(array_merge($existingDomains, $filteredNewDomains));
$application->fqdn = implode(',', $mergedDomains);
$application->custom_labels = base64_encode(implode("\n ", generateLabelsApplication($application)));
$application->save();
return response()->json([
'success' => true,
'message' => 'Domains updated successfully',
'application' => $application
]);
}
public function deleteDomains(Request $request)
{
$teamId = get_team_id_from_token();
if (is_null($teamId)) {
return invalid_token();
}
$validator = Validator::make($request->all(), [
'uuid' => 'required|string|exists:applications,uuid',
'domains' => 'required|array',
'domains.*' => 'required|string|distinct',
]);
if ($validator->fails()) {
return response()->json([
'success' => false,
'message' => 'Validation failed',
'errors' => $validator->errors()
], 422);
}
$application = Application::where('uuid', $request->uuid)->first();
if (!$application) {
return response()->json([
'success' => false,
'message' => 'Application not found'
], 404);
}
$existingDomains = explode(',', $application->fqdn);
$domainsToDelete = $request->domains;
$updatedDomains = array_diff($existingDomains, $domainsToDelete);
$application->fqdn = implode(',', $updatedDomains);
$application->save();
return response()->json([
'success' => true,
'message' => 'Domains updated successfully',
'application' => $application
]);
}
}