Skip to content

Commit

Permalink
fix: api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Jul 3, 2024
1 parent 4459c9f commit b24a489
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 328 deletions.
145 changes: 31 additions & 114 deletions app/Http/Controllers/Api/ApplicationsController.php

Large diffs are not rendered by default.

78 changes: 23 additions & 55 deletions app/Http/Controllers/Api/DatabasesController.php

Large diffs are not rendered by default.

44 changes: 16 additions & 28 deletions app/Http/Controllers/Api/DeployController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public function deployments(Request $request)
'status',
])->sortBy('id')->toArray();

return response()->json([
'success' => true,
'data' => serializeApiResponse($deployments_per_server),
]);
return response()->json(serializeApiResponse($deployments_per_server));
}

public function deployment_by_uuid(Request $request)
Expand All @@ -59,17 +56,14 @@ public function deployment_by_uuid(Request $request)
}
$uuid = $request->route('uuid');
if (! $uuid) {
return response()->json(['success' => false, 'message' => 'UUID is required.'], 400);
return response()->json(['message' => 'UUID is required.'], 400);
}
$deployment = ApplicationDeploymentQueue::where('deployment_uuid', $uuid)->first();
if (! $deployment) {
return response()->json(['success' => false, 'message' => 'Deployment not found.'], 404);
return response()->json(['message' => 'Deployment not found.'], 404);
}

return response()->json([
'success' => true,
'data' => $this->removeSensitiveData($deployment),
]);
return response()->json($this->removeSensitiveData($deployment));
}

public function deploy(Request $request)
Expand All @@ -80,7 +74,7 @@ public function deploy(Request $request)
$force = $request->query->get('force') ?? false;

if ($uuids && $tags) {
return response()->json(['success' => false, 'message' => 'You can only use uuid or tag, not both.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
return response()->json(['message' => 'You can only use uuid or tag, not both.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
}
if (is_null($teamId)) {
return invalidTokenResponse();
Expand All @@ -91,7 +85,7 @@ public function deploy(Request $request)
return $this->by_uuids($uuids, $teamId, $force);
}

return response()->json(['success' => false, 'message' => 'You must provide uuid or tag.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
return response()->json(['message' => 'You must provide uuid or tag.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
}

private function by_uuids(string $uuid, int $teamId, bool $force = false)
Expand All @@ -100,7 +94,7 @@ private function by_uuids(string $uuid, int $teamId, bool $force = false)
$uuids = collect(array_filter($uuids));

if (count($uuids) === 0) {
return response()->json(['success' => false, 'message' => 'No UUIDs provided.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
return response()->json(['message' => 'No UUIDs provided.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
}
$deployments = collect();
$payload = collect();
Expand All @@ -109,22 +103,19 @@ private function by_uuids(string $uuid, int $teamId, bool $force = false)
if ($resource) {
['message' => $return_message, 'deployment_uuid' => $deployment_uuid] = $this->deploy_resource($resource, $force);
if ($deployment_uuid) {
$deployments->push(['success' => true, 'message' => $return_message, 'resource_uuid' => $uuid, 'deployment_uuid' => $deployment_uuid->toString()]);
$deployments->push(['message' => $return_message, 'resource_uuid' => $uuid, 'deployment_uuid' => $deployment_uuid->toString()]);
} else {
$deployments->push(['success' => true, 'message' => $return_message, 'resource_uuid' => $uuid]);
$deployments->push(['message' => $return_message, 'resource_uuid' => $uuid]);
}
}
}
if ($deployments->count() > 0) {
$payload->put('deployments', $deployments->toArray());

return response()->json([
'success' => true,
'data' => serializeApiResponse($payload->toArray()),
]);
return response()->json(serializeApiResponse($payload->toArray()));
}

return response()->json(['success' => false, 'message' => 'No resources found.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 404);
return response()->json(['message' => 'No resources found.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 404);
}

public function by_tags(string $tags, int $team_id, bool $force = false)
Expand All @@ -133,7 +124,7 @@ public function by_tags(string $tags, int $team_id, bool $force = false)
$tags = collect(array_filter($tags));

if (count($tags) === 0) {
return response()->json(['success' => false, 'message' => 'No TAGs provided.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
return response()->json(['message' => 'No TAGs provided.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
}
$message = collect([]);
$deployments = collect();
Expand Down Expand Up @@ -169,21 +160,18 @@ public function by_tags(string $tags, int $team_id, bool $force = false)
$payload->put('details', $deployments->toArray());
}

return response()->json([
'success' => true,
'data' => serializeApiResponse($payload->toArray()),
]);
return response()->json(serializeApiResponse($payload->toArray()));
}

return response()->json(['success' => false, 'message' => 'No resources found with this tag.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 404);
return response()->json(['message' => 'No resources found with this tag.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 404);
}

public function deploy_resource($resource, bool $force = false): array
{
$message = null;
$deployment_uuid = null;
if (gettype($resource) !== 'object') {
return ['success' => false, 'message' => "Resource ($resource) not found.", 'deployment_uuid' => $deployment_uuid];
return ['message' => "Resource ($resource) not found.", 'deployment_uuid' => $deployment_uuid];
}
switch ($resource?->getMorphClass()) {
case 'App\Models\Application':
Expand All @@ -209,6 +197,6 @@ public function deploy_resource($resource, bool $force = false): array
break;
}

return ['success' => true, 'message' => $message, 'deployment_uuid' => $deployment_uuid];
return ['message' => $message, 'deployment_uuid' => $deployment_uuid];
}
}
3 changes: 0 additions & 3 deletions app/Http/Controllers/Api/EnvironmentVariablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ public function delete_env_by_uuid(Request $request)
$env = EnvironmentVariable::where('uuid', $request->env_uuid)->first();
if (! $env) {
return response()->json([
'success' => false,
'message' => 'Environment variable not found.',
], 404);
}
$found_app = $env->resource()->whereRelation('environment.project.team', 'id', $teamId)->first();
if (! $found_app) {
return response()->json([
'success' => false,
'message' => 'Environment variable not found.',
], 404);
}
$env->delete();

return response()->json([
'success' => true,
'message' => 'Environment variable deleted.',
]);
}
Expand Down
22 changes: 8 additions & 14 deletions app/Http/Controllers/Api/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ public function projects(Request $request)
}
$projects = Project::whereTeamId($teamId)->select('id', 'name', 'uuid')->get();

return response()->json([
'success' => true,
'data' => serializeApiResponse($projects),
]);
return response()->json(serializeApiResponse($projects),
);
}

public function project_by_uuid(Request $request)
Expand All @@ -30,13 +28,12 @@ public function project_by_uuid(Request $request)
}
$project = Project::whereTeamId($teamId)->whereUuid(request()->uuid)->first()->load(['environments']);
if (! $project) {
return response()->json(['success' => false, 'message' => 'Project not found.'], 404);
return response()->json(['message' => 'Project not found.'], 404);
}

return response()->json([
'success' => true,
'data' => serializeApiResponse($project),
]);
return response()->json(
serializeApiResponse($project),
);
}

public function environment_details(Request $request)
Expand All @@ -48,13 +45,10 @@ public function environment_details(Request $request)
$project = Project::whereTeamId($teamId)->whereUuid(request()->uuid)->first();
$environment = $project->environments()->whereName(request()->environment_name)->first();
if (! $environment) {
return response()->json(['success' => false, 'message' => 'Environment not found.'], 404);
return response()->json(['message' => 'Environment not found.'], 404);
}
$environment = $environment->load(['applications', 'postgresqls', 'redis', 'mongodbs', 'mysqls', 'mariadbs', 'services']);

return response()->json([
'success' => true,
'data' => serializeApiResponse($environment),
]);
return response()->json(serializeApiResponse($environment));
}
}
5 changes: 1 addition & 4 deletions app/Http/Controllers/Api/ResourcesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public function resources(Request $request)
return $payload;
});

return response()->json([
'success' => true,
'data' => serializeApiResponse($resources),
]);
return response()->json(serializeApiResponse($resources));
}
}
39 changes: 13 additions & 26 deletions app/Http/Controllers/Api/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public function keys(Request $request)
}
$keys = PrivateKey::where('team_id', $teamId)->get();

return response()->json([
'success' => true,
'data' => serializeApiResponse($keys),
]);
return response()->json(serializeApiResponse($keys));
}

public function key_by_uuid(Request $request)
Expand All @@ -33,15 +30,11 @@ public function key_by_uuid(Request $request)

if (is_null($key)) {
return response()->json([
'success' => false,
'message' => 'Key not found.',
'message' => 'Private Key not found.',
], 404);
}

return response()->json([
'success' => true,
'data' => serializeApiResponse($key),
]);
return response()->json(serializeApiResponse($key));
}

public function create_key(Request $request)
Expand All @@ -64,7 +57,6 @@ public function create_key(Request $request)
$errors = $validator->errors();

return response()->json([
'success' => false,
'message' => 'Validation failed.',
'errors' => $errors,
], 422);
Expand All @@ -82,10 +74,9 @@ public function create_key(Request $request)
'private_key' => $request->private_key,
]);

return response()->json([
'success' => true,
'data' => serializeApiResponse($key),
]);
return response()->json(serializeApiResponse([
'uuid' => $key->uuid,
]))->setStatusCode(201);
}

public function update_key(Request $request)
Expand Down Expand Up @@ -116,24 +107,21 @@ public function update_key(Request $request)
}

return response()->json([
'success' => false,
'message' => 'Validation failed.',
'errors' => $errors,
], 422);
}
$foundKey = PrivateKey::where('team_id', $teamId)->where('uuid', $request->uuid)->first();
if (is_null($foundKey)) {
return response()->json([
'success' => false,
'message' => 'Key not found.',
'message' => 'Private Key not found.',
], 404);
}
$foundKey->update($request->all());

return response()->json([
'success' => true,
'data' => serializeApiResponse($foundKey),
])->setStatusCode(201);
return response()->json(serializeApiResponse([
'uuid' => $foundKey->uuid,
]))->setStatusCode(201);
}

public function delete_key(Request $request)
Expand All @@ -143,18 +131,17 @@ public function delete_key(Request $request)
return invalidTokenResponse();
}
if (! $request->uuid) {
return response()->json(['success' => false, 'message' => 'UUID is required.'], 422);
return response()->json(['message' => 'UUID is required.'], 422);
}

$key = PrivateKey::where('team_id', $teamId)->where('uuid', $request->uuid)->first();
if (is_null($key)) {
return response()->json(['success' => false, 'message' => 'Key not found.'], 404);
return response()->json(['message' => 'Private Key not found.'], 404);
}
$key->forceDelete();

return response()->json([
'success' => true,
'message' => 'Key deleted.',
'message' => 'Private Key deleted.',
]);
}
}
Loading

0 comments on commit b24a489

Please sign in to comment.