Files
panel-pelican-dev/app/Http/Controllers/Api/Application/Servers/ServerManagementController.php

138 lines
3.8 KiB
PHP
Raw Normal View History

2018-01-20 13:48:02 -06:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Application\Servers;
2018-01-20 13:48:02 -06:00
use App\Enums\SuspendAction;
use App\Http\Controllers\Api\Application\ApplicationApiController;
use App\Http\Requests\Api\Application\Servers\ServerWriteRequest;
2024-03-12 22:39:16 -04:00
use App\Models\Server;
use App\Repositories\Daemon\DaemonServerRepository;
2024-03-12 22:39:16 -04:00
use App\Services\Servers\ReinstallServerService;
use App\Services\Servers\SuspensionService;
use App\Services\Servers\TransferServerService;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
2018-01-20 13:48:02 -06:00
#[Group('Server', weight: 4)]
2018-01-20 13:48:02 -06:00
class ServerManagementController extends ApplicationApiController
{
/**
* ServerManagementController constructor.
2018-01-20 13:48:02 -06:00
*/
public function __construct(
private ReinstallServerService $reinstallServerService,
private SuspensionService $suspensionService,
private TransferServerService $transferServerService,
private DaemonServerRepository $daemonServerRepository,
) {
2018-01-20 13:48:02 -06:00
parent::__construct();
}
/**
* Suspsend
*
2018-01-20 13:48:02 -06:00
* Suspend a server on the Panel.
*
2019-12-22 13:45:40 -08:00
* @throws \Throwable
2018-01-20 13:48:02 -06:00
*/
public function suspend(ServerWriteRequest $request, Server $server): Response
2018-01-20 13:48:02 -06:00
{
$this->suspensionService->handle($server, SuspendAction::Suspend);
2018-01-20 13:48:02 -06:00
return $this->returnNoContent();
}
/**
* Unsuspsend
*
2018-01-20 13:48:02 -06:00
* Unsuspend a server on the Panel.
*
2019-12-22 13:45:40 -08:00
* @throws \Throwable
2018-01-20 13:48:02 -06:00
*/
public function unsuspend(ServerWriteRequest $request, Server $server): Response
2018-01-20 13:48:02 -06:00
{
$this->suspensionService->handle($server, SuspendAction::Unsuspend);
2018-01-20 13:48:02 -06:00
return $this->returnNoContent();
}
/**
* Reinstall
*
2018-01-20 13:48:02 -06:00
* Mark a server as needing to be reinstalled.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\DisplayException
* @throws \App\Exceptions\Model\DataValidationException
2018-01-20 13:48:02 -06:00
*/
public function reinstall(ServerWriteRequest $request, Server $server): Response
2018-01-20 13:48:02 -06:00
{
2020-10-05 21:54:29 -07:00
$this->reinstallServerService->handle($server);
2018-01-20 13:48:02 -06:00
return $this->returnNoContent();
}
/**
* Start transfer
*
* Starts a transfer of a server to a new node.
*/
public function startTransfer(ServerWriteRequest $request, Server $server): Response
{
$validatedData = $request->validate([
'node_id' => 'required|exists:nodes,id',
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
'allocation_additional' => 'nullable|array',
'allocation_additional.*' => 'integer|exists:allocations,id',
]);
if ($this->transferServerService->handle($server, Arr::get($validatedData, 'node_id'), Arr::get($validatedData, 'allocation_id'), Arr::get($validatedData, 'allocation_additional', []))) {
/**
* Transfer started
*
* @status 204
*/
return $this->returnNoContent();
}
/**
* Node was not viable
*
* @status 406
*/
return $this->returnNotAcceptable();
}
/**
* Cancel transfer
*
* Cancels a transfer of a server to a new node.
*
* @throws ConnectionException
*/
public function cancelTransfer(ServerWriteRequest $request, Server $server): Response
{
if (!$transfer = $server->transfer) {
/**
* Server is not transferring
*
* @status 406
*/
return $this->returnNotAcceptable();
}
$transfer->successful = true;
$transfer->save();
$this->daemonServerRepository->setServer($server)->cancelTransfer();
/**
* Transfer cancelled
*
* @status 204
*/
return $this->returnNoContent();
}
2018-01-20 13:48:02 -06:00
}