Add server details modification endpoint to API.
This commit is contained in:
parent
3e327b8b0e
commit
17f6f3eeb6
7 changed files with 160 additions and 267 deletions
|
@ -401,7 +401,7 @@ class ServersController extends Controller
|
|||
*/
|
||||
public function setDetails(Request $request, Server $server)
|
||||
{
|
||||
$this->detailsModificationService->edit($server, $request->only([
|
||||
$this->detailsModificationService->handle($server, $request->only([
|
||||
'owner_id', 'name', 'description',
|
||||
]));
|
||||
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Services\Servers\DetailsModificationService;
|
||||
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
|
||||
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest;
|
||||
|
||||
class ServerDetailsController extends ApplicationApiController
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\DetailsModificationService
|
||||
*/
|
||||
private $modificationService;
|
||||
|
||||
/**
|
||||
* ServerDetailsController constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Services\Servers\DetailsModificationService $modificationService
|
||||
*/
|
||||
public function __construct(DetailsModificationService $modificationService)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->modificationService = $modificationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest $request
|
||||
* @param \Pterodactyl\Models\Server $server
|
||||
* @return array
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function details(UpdateServerDetailsRequest $request, Server $server): array
|
||||
{
|
||||
$server = $this->modificationService->returnUpdatedModel()->handle($server, $request->validated());
|
||||
|
||||
return $this->fractal->item($server)
|
||||
->transformWith($this->getTransformer(ServerTransformer::class))
|
||||
->toArray();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
|
||||
class UpdateServerDetailsRequest extends ServerWriteRequest
|
||||
{
|
||||
/**
|
||||
* Rules to apply to a server details update request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = Server::getUpdateRulesForId($this->route()->parameter('server')->id);
|
||||
|
||||
return [
|
||||
'name' => $rules['name'],
|
||||
'user' => $rules['owner_id'],
|
||||
'description' => $rules['description'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the posted data into the correct format that is expected
|
||||
* by the application.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validated(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->input('name'),
|
||||
'owner_id' => $this->input('user'),
|
||||
'description' => $this->input('description'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename some of the attributes in error messages to clarify the field
|
||||
* being discussed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'user' => 'User ID',
|
||||
'name' => 'Server Name',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -80,7 +80,7 @@ class Server extends Model implements CleansAttributes, ValidableContract
|
|||
*/
|
||||
protected static $dataIntegrityRules = [
|
||||
'owner_id' => 'exists:users,id',
|
||||
'name' => 'regex:/^([\w .-]{1,200})$/',
|
||||
'name' => 'string|min:1|max:255',
|
||||
'node_id' => 'exists:nodes,id',
|
||||
'description' => 'string',
|
||||
'memory' => 'numeric|min:0',
|
||||
|
|
|
@ -1,55 +1,37 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Services\Servers;
|
||||
|
||||
use Illuminate\Log\Writer;
|
||||
use Pterodactyl\Models\Server;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Traits\Services\ReturnsUpdatedModels;
|
||||
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService;
|
||||
use Pterodactyl\Repositories\Daemon\ServerRepository as DaemonServerRepository;
|
||||
|
||||
class DetailsModificationService
|
||||
{
|
||||
use ReturnsUpdatedModels;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Daemon\ServerRepository
|
||||
*/
|
||||
protected $daemonServerRepository;
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService
|
||||
*/
|
||||
protected $keyCreationService;
|
||||
private $keyCreationService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService
|
||||
*/
|
||||
protected $keyDeletionService;
|
||||
private $keyDeletionService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Log\Writer
|
||||
*/
|
||||
protected $writer;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* DetailsModificationService constructor.
|
||||
|
@ -57,92 +39,48 @@ class DetailsModificationService
|
|||
* @param \Illuminate\Database\ConnectionInterface $connection
|
||||
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService $keyCreationService
|
||||
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService $keyDeletionService
|
||||
* @param \Pterodactyl\Repositories\Daemon\ServerRepository $daemonServerRepository
|
||||
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
|
||||
* @param \Illuminate\Log\Writer $writer
|
||||
*/
|
||||
public function __construct(
|
||||
ConnectionInterface $connection,
|
||||
DaemonKeyCreationService $keyCreationService,
|
||||
DaemonKeyDeletionService $keyDeletionService,
|
||||
DaemonServerRepository $daemonServerRepository,
|
||||
ServerRepository $repository,
|
||||
Writer $writer
|
||||
ServerRepository $repository
|
||||
) {
|
||||
$this->connection = $connection;
|
||||
$this->daemonServerRepository = $daemonServerRepository;
|
||||
$this->keyCreationService = $keyCreationService;
|
||||
$this->keyDeletionService = $keyDeletionService;
|
||||
$this->repository = $repository;
|
||||
$this->writer = $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the details for a single server instance.
|
||||
*
|
||||
* @param int|\Pterodactyl\Models\Server $server
|
||||
* @param array $data
|
||||
* @param \Pterodactyl\Models\Server $server
|
||||
* @param array $data
|
||||
* @return bool|\Pterodactyl\Models\Server
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function edit($server, array $data)
|
||||
public function handle(Server $server, array $data)
|
||||
{
|
||||
if (! $server instanceof Server) {
|
||||
$server = $this->repository->find($server);
|
||||
}
|
||||
|
||||
$this->connection->beginTransaction();
|
||||
$this->repository->withoutFreshModel()->update($server->id, [
|
||||
|
||||
$response = $this->repository->setFreshModel($this->getUpdatedModel())->update($server->id, [
|
||||
'owner_id' => array_get($data, 'owner_id'),
|
||||
'name' => array_get($data, 'name'),
|
||||
'description' => array_get($data, 'description', ''),
|
||||
], true, true);
|
||||
|
||||
if (array_get($data, 'owner_id') != $server->owner_id) {
|
||||
if ((int) array_get($data, 'owner_id', 0) !== (int) $server->owner_id) {
|
||||
$this->keyDeletionService->handle($server, $server->owner_id);
|
||||
$this->keyCreationService->handle($server->id, array_get($data, 'owner_id'));
|
||||
}
|
||||
|
||||
$this->connection->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the docker container for a specified server.
|
||||
*
|
||||
* @param int|\Pterodactyl\Models\Server $server
|
||||
* @param string $image
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function setDockerImage($server, $image)
|
||||
{
|
||||
if (! $server instanceof Server) {
|
||||
$server = $this->repository->find($server);
|
||||
}
|
||||
|
||||
$this->connection->beginTransaction();
|
||||
$this->repository->withoutFreshModel()->update($server->id, ['image' => $image]);
|
||||
|
||||
try {
|
||||
$this->daemonServerRepository->setServer($server)->update([
|
||||
'build' => [
|
||||
'image' => $image,
|
||||
],
|
||||
]);
|
||||
} catch (RequestException $exception) {
|
||||
$this->connection->rollBack();
|
||||
$response = $exception->getResponse();
|
||||
$this->writer->warning($exception);
|
||||
|
||||
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
|
||||
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
|
||||
]));
|
||||
}
|
||||
|
||||
$this->connection->commit();
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue