Implement initial server and location API routes.
Also fixes a few exception handler issues causing incorrect HTTP status codes on authorization errors.
This commit is contained in:
parent
463f465dea
commit
c492446513
12 changed files with 639 additions and 45 deletions
51
app/Http/Controllers/API/Admin/LocationController.php
Normal file
51
app/Http/Controllers/API/Admin/LocationController.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\API\Admin;
|
||||
|
||||
use Fractal;
|
||||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Transformers\Admin\LocationTransformer;
|
||||
|
||||
class LocationController extends Controller
|
||||
{
|
||||
/**
|
||||
* Controller to handle returning all locations on the system.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('location-list', $request->apiKey());
|
||||
|
||||
return Fractal::create()
|
||||
->collection(Location::all())
|
||||
->transformWith(new LocationTransformer($request))
|
||||
->withResourceName('location')
|
||||
->toArray();
|
||||
}
|
||||
}
|
|
@ -27,8 +27,12 @@ namespace Pterodactyl\Http\Controllers\API\Admin;
|
|||
use Fractal;
|
||||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\Server;
|
||||
use GuzzleHttp\Exception\TransferException;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Repositories\ServerRepository;
|
||||
use Pterodactyl\Transformers\Admin\ServerTransformer;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
|
||||
class ServerController extends Controller
|
||||
|
@ -41,6 +45,8 @@ class ServerController extends Controller
|
|||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('server-list', $request->apiKey());
|
||||
|
||||
$servers = Server::paginate(20);
|
||||
|
||||
return Fractal::create()
|
||||
|
@ -59,6 +65,8 @@ class ServerController extends Controller
|
|||
*/
|
||||
public function view(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-view', $request->apiKey());
|
||||
|
||||
$server = Server::findOrFail($id);
|
||||
$fractal = Fractal::create()->item($server);
|
||||
|
||||
|
@ -70,4 +78,332 @@ class ServerController extends Controller
|
|||
->withResourceName('server')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new server on the system.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse|array
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->authorize('server-create', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
try {
|
||||
$server = $repo->create($request->all());
|
||||
|
||||
$fractal = Fractal::create()->item($server)->transformWith(new ServerTransformer($request));
|
||||
if ($request->input('include')) {
|
||||
$fractal->parseIncludes(explode(',', $request->input('include')));
|
||||
}
|
||||
|
||||
return $fractal->withResourceName('server')->toArray();
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return response()->json([
|
||||
'error' => json_decode($ex->getMessage()),
|
||||
], 400);
|
||||
} catch (DisplayException $ex) {
|
||||
return response()->json([
|
||||
'error' => $ex->getMessage(),
|
||||
], 400);
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
return response()->json([
|
||||
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.',
|
||||
], 504);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to add this server. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a server from the system.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function delete(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-delete', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
try {
|
||||
$repo->delete($id, $request->has('force_delete'));
|
||||
|
||||
return response('', 204);
|
||||
} catch (DisplayException $ex) {
|
||||
return response()->json([
|
||||
'error' => $ex->getMessage(),
|
||||
], 400);
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
return response()->json([
|
||||
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.',
|
||||
], 504);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to add this server. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the details for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\JsonResponse|array
|
||||
*/
|
||||
public function details(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-edit-details', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
try {
|
||||
$server = $repo->updateDetails($id, $request->intersect([
|
||||
'owner_id', 'name', 'description', 'reset_token',
|
||||
]));
|
||||
|
||||
$fractal = Fractal::create()->item($server)->transformWith(new ServerTransformer($request));
|
||||
if ($request->input('include')) {
|
||||
$fractal->parseIncludes(explode(',', $request->input('include')));
|
||||
}
|
||||
|
||||
return $fractal->withResourceName('server')->toArray();
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return response()->json([
|
||||
'error' => json_decode($ex->getMessage()),
|
||||
], 400);
|
||||
} catch (DisplayException $ex) {
|
||||
return response()->json([
|
||||
'error' => $ex->getMessage(),
|
||||
], 400);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to modify this server. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the new docker container for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\RedirectResponse|array
|
||||
*/
|
||||
public function container(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-edit-container', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
try {
|
||||
$server = $repo->updateContainer($id, $request->intersect('docker_image'));
|
||||
|
||||
$fractal = Fractal::create()->item($server)->transformWith(new ServerTransformer($request));
|
||||
if ($request->input('include')) {
|
||||
$fractal->parseIncludes(explode(',', $request->input('include')));
|
||||
}
|
||||
|
||||
return $fractal->withResourceName('server')->toArray();
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return response()->json([
|
||||
'error' => json_decode($ex->getMessage()),
|
||||
], 400);
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
return response()->json([
|
||||
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.',
|
||||
], 504);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to modify this server container. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles the install status for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function install(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-install', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
try {
|
||||
$repo->toggleInstall($id);
|
||||
|
||||
return response('', 204);
|
||||
} catch (DisplayException $ex) {
|
||||
return response()->json([
|
||||
'error' => $ex->getMessage(),
|
||||
], 400);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to toggle the install status for this server. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup a server to have a container rebuild.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function rebuild(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-rebuild', $request->apiKey());
|
||||
$server = Server::with('node')->findOrFail($id);
|
||||
|
||||
try {
|
||||
$server->node->guzzleClient([
|
||||
'X-Access-Server' => $server->uuid,
|
||||
'X-Access-Token' => $server->node->daemonSecret,
|
||||
])->request('POST', '/server/rebuild');
|
||||
|
||||
return response('', 204);
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
return response()->json([
|
||||
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.',
|
||||
], 504);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage the suspension status for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function suspend(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-suspend', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
$action = $request->input('action');
|
||||
if (! in_array($action, ['suspend', 'unsuspend'])) {
|
||||
return response()->json([
|
||||
'error' => 'The action provided was invalid. Action should be one of: suspend, unsuspend.',
|
||||
], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$repo->$action($id);
|
||||
|
||||
return response('', 204);
|
||||
} catch (DisplayException $ex) {
|
||||
return response()->json([
|
||||
'error' => $ex->getMessage(),
|
||||
], 400);
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
return response()->json([
|
||||
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.',
|
||||
], 504);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to ' . $action . ' this server. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the build configuration for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\JsonResponse|array
|
||||
*/
|
||||
public function build(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-edit-build', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
try {
|
||||
$server = $repo->changeBuild($id, $request->intersect([
|
||||
'allocation_id', 'add_allocations', 'remove_allocations',
|
||||
'memory', 'swap', 'io', 'cpu',
|
||||
]));
|
||||
|
||||
$fractal = Fractal::create()->item($server)->transformWith(new ServerTransformer($request));
|
||||
if ($request->input('include')) {
|
||||
$fractal->parseIncludes(explode(',', $request->input('include')));
|
||||
}
|
||||
|
||||
return $fractal->withResourceName('server')->toArray();
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return response()->json([
|
||||
'error' => json_decode($ex->getMessage()),
|
||||
], 400);
|
||||
} catch (DisplayException $ex) {
|
||||
return response()->json([
|
||||
'error' => $ex->getMessage(),
|
||||
], 400);
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
return response()->json([
|
||||
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.',
|
||||
], 504);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to modify the build settings for this server. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the startup command as well as variables.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function startup(Request $request, $id)
|
||||
{
|
||||
$this->authorize('server-edit-startup', $request->apiKey());
|
||||
|
||||
$repo = new ServerRepository;
|
||||
try {
|
||||
$repo->updateStartup($id, $request->all(), true);
|
||||
|
||||
return response('', 204);
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return response()->json([
|
||||
'error' => json_decode($ex->getMessage()),
|
||||
], 400);
|
||||
} catch (DisplayException $ex) {
|
||||
return response()->json([
|
||||
'error' => $ex->getMessage(),
|
||||
], 400);
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
return response()->json([
|
||||
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.',
|
||||
], 504);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An unhandled exception occured while attemping to modify the startup settings for this server. Please try again.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,6 +39,8 @@ class CoreController extends Controller
|
|||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('user-server-list', $request->apiKey());
|
||||
|
||||
$servers = $request->user()->access('service', 'node', 'allocation', 'option')->get();
|
||||
|
||||
return Fractal::collection($servers)
|
||||
|
|
|
@ -43,6 +43,8 @@ class ServerController extends Controller
|
|||
*/
|
||||
public function index(Request $request, $uuid)
|
||||
{
|
||||
$this->authorize('user-server-view', $request->apiKey());
|
||||
|
||||
$server = Server::byUuid($uuid);
|
||||
$fractal = Fractal::create()->item($server);
|
||||
|
||||
|
@ -64,6 +66,8 @@ class ServerController extends Controller
|
|||
*/
|
||||
public function power(Request $request, $uuid)
|
||||
{
|
||||
$this->authorize('user-server-power', $request->apiKey());
|
||||
|
||||
$server = Server::byUuid($uuid);
|
||||
$request->user()->can('power-' . $request->input('action'), $server);
|
||||
|
||||
|
@ -82,6 +86,8 @@ class ServerController extends Controller
|
|||
*/
|
||||
public function command(Request $request, $uuid)
|
||||
{
|
||||
$this->authorize('user-server-command', $request->apiKey());
|
||||
|
||||
$server = Server::byUuid($uuid);
|
||||
$request->user()->can('send-command', $server);
|
||||
|
||||
|
|
|
@ -97,6 +97,9 @@ class ServersController extends Controller
|
|||
return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
|
||||
|
@ -284,8 +287,9 @@ class ServersController extends Controller
|
|||
Alert::success('Successfully updated this server\'s docker image.')->flash();
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.servers.view.details', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
Alert::danger('A TransferException occured while attempting to update the container image. Is the daemon online? This error has been logged.');
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. This error has been logged.')->flash();
|
||||
|
@ -366,8 +370,9 @@ class ServersController extends Controller
|
|||
$repo->$action($id);
|
||||
|
||||
Alert::success('Server has been ' . $action . 'ed.');
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception occured while attemping to ' . $action . ' this server. This error has been logged.')->flash();
|
||||
|
@ -398,6 +403,9 @@ class ServersController extends Controller
|
|||
return redirect()->route('admin.servers.view.build', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (TransferException $ex) {
|
||||
Log::warning($ex);
|
||||
Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception occured while attemping to add this server. This error has been logged.')->flash();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue