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 @@ public function domains(Request $request)
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();
@ -101,4 +112,90 @@ public function domains(Request $request)
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
]);
}
}

View File

@ -1167,4 +1167,15 @@ public function generate_preview_fqdn(int $pull_request_id)
return $preview;
}
public static function getDomainsByUuid(string $uuid): array
{
$application = self::where('uuid', $uuid)->first();
if ($application) {
return $application->fqdns;
}
return [];
}
}

View File

@ -39,7 +39,8 @@
Route::get('/resources', [Resources::class, 'resources']);
Route::get('/domains', [Domains::class, 'domains']);
Route::put('/domains', [Domains::class, 'updateDomains']);
Route::delete('/domains', [Domains::class, 'deleteDomains']);
Route::get('/teams', [Team::class, 'teams']);
Route::get('/team/current', [Team::class, 'current_team']);
Route::get('/team/current/members', [Team::class, 'current_team_members']);