Very rough go at getting API back into operational state.

Not spending a lot of time on this as its a pre-release and I have
plans to overhaul the API to actually work and be easy to maintain.
This commit is contained in:
Dane Everitt 2017-03-19 13:20:33 -04:00
parent 4e916cbf08
commit 5e27772fef
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
7 changed files with 189 additions and 333 deletions

View file

@ -27,26 +27,15 @@ namespace Pterodactyl\Http\Controllers\API;
use Illuminate\Http\Request;
use Pterodactyl\Models\Location;
/**
* @Resource("Servers")
*/
class LocationController extends BaseController
{
public function __construct()
{
//
}
/**
* List All Locations.
*
* Lists all locations currently on the system.
*
* @Get("/locations")
* @Versions({"v1"})
* @Response(200)
* @param Request $request
* @return array
*/
public function lists(Request $request)
public function index(Request $request)
{
return Location::with('nodes')->get()->map(function ($item) {
$item->nodes->transform(function ($item) {

View file

@ -25,8 +25,9 @@
namespace Pterodactyl\Http\Controllers\API;
use Log;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Allocation;
use Dingo\Api\Exception\ResourceException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\NodeRepository;
@ -35,106 +36,66 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
/**
* @Resource("Servers")
*/
class NodeController extends BaseController
{
public function __construct()
{
//
}
/**
* List All Nodes.
*
* Lists all nodes currently on the system.
*
* @Get("/nodes/{?page}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
* })
* @Response(200)
* @param Request $request
* @return array
*/
public function lists(Request $request)
public function index(Request $request)
{
return Models\Node::all()->toArray();
return Node::all()->toArray();
}
/**
* Create a New Node.
* Create a new node.
*
* @Post("/nodes")
* @Versions({"v1"})
* @Transaction({
* @Request({
* 'name' => 'My API Node',
* 'location' => 1,
* 'public' => 1,
* 'fqdn' => 'daemon.wuzzle.woo',
* 'scheme' => 'https',
* 'memory' => 10240,
* 'memory_overallocate' => 100,
* 'disk' => 204800,
* 'disk_overallocate' => -1,
* 'daemonBase' => '/srv/daemon-data',
* 'daemonSFTP' => 2022,
* 'daemonListen' => 8080
* }, headers={"Authorization": "Bearer <jwt-token>"}),
* @Response(200),
* @Response(422, body={
* "message": "A validation error occured.",
* "errors": {},
* "status_code": 422
* }),
* @Response(503, body={
* "message": "There was an error while attempting to add this node to the system.",
* "status_code": 503
* })
* })
* @param Request $request
* @return array
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function create(Request $request)
{
try {
$repo = new NodeRepository;
$node = $repo->create($request->only([
'name', 'location_id', 'public', 'fqdn',
'scheme', 'memory', 'memory_overallocate',
'disk', 'disk_overallocate', 'daemonBase',
'daemonSFTP', 'daemonListen',
]));
$repo = new NodeRepository;
return ['id' => $repo->id];
try {
$node = $repo->create(array_merge(
$request->only([
'public', 'disk_overallocate', 'memory_overallocate',
]),
$request->intersect([
'name', 'location_id', 'fqdn',
'scheme', 'memory', 'disk',
'daemonBase', 'daemonSFTP', 'daemonListen',
])
));
return ['id' => $node->id];
} catch (DisplayValidationException $ex) {
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
} catch (DisplayException $ex) {
throw new ResourceException($ex->getMessage());
} catch (\Exception $ex) {
Log::error($ex);
throw new BadRequestHttpException('There was an error while attempting to add this node to the system.');
throw new BadRequestHttpException('There was an error while attempting to add this node to the system. This error has been logged.');
}
}
/**
* List Specific Node.
*
* Lists specific fields about a server or all fields pertaining to that node.
*
* @Get("/nodes/{id}/{?fields}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the node to get information on."),
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
* })
* @Response(200)
* @param Request $request
* @param int $id
* @param string $fields
* @return array
*/
public function view(Request $request, $id, $fields = null)
{
$node = Models\Node::with('allocations')->where('id', $id)->first();
if (! $node) {
throw new NotFoundHttpException('No node by that ID was found.');
}
$node = Node::with('allocations')->findOrFail($id);
$node->allocations->transform(function ($item) {
return collect($item)->only([
@ -142,69 +103,64 @@ class NodeController extends BaseController
]);
});
if (! is_null($request->input('fields'))) {
if (! empty($request->input('fields'))) {
$fields = explode(',', $request->input('fields'));
if (! empty($fields) && is_array($fields)) {
return collect($node)->only($fields);
}
}
return $node;
return $node->toArray();
}
/**
* Returns a configuration file for a given node.
*
* @param Request $request
* @param int $id
* @return array
*/
public function config(Request $request, $id)
{
$node = Models\Node::where('id', $id)->first();
if (! $node) {
throw new NotFoundHttpException('No node by that ID was found.');
}
$node = Node::findOrFail($id);
return $node->getConfigurationAsJson();
}
/**
* List all Node Allocations.
*
* Returns a listing of all allocations for every node.
*
* @Get("/nodes/allocations")
* @Versions({"v1"})
* @Response(200)
* @param Request $request
* @return array
*/
public function allocations(Request $request)
{
return Models\Allocation::all()->toArray();
return Allocation::all()->toArray();
}
/**
* List Node Allocation based on assigned to ID.
*
* Returns a listing of the allocation for the specified server id.
*
* @Get("/nodes/allocations/{id}")
* @Versions({"v1"})
* @Response(200)
* @param Request $request
* @return array
*/
public function allocationsView(Request $request, $id)
{
return Models\Allocation::where('server_id', $id)->get()->toArray();
return Allocation::where('server_id', $id)->get()->toArray();
}
/**
* Delete Node.
* Delete a node.
*
* @Delete("/nodes/{id}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the node."),
* })
* @Response(204)
* @param Request $request
* @param int $id
* @return void
*/
public function delete(Request $request, $id)
{
$repo = new NodeRepository;
try {
$node = new NodeRepository;
$node->delete($id);
$repo->delete($id);
return $this->response->noContent();
} catch (DisplayException $ex) {

View file

@ -25,8 +25,8 @@
namespace Pterodactyl\Http\Controllers\API;
use Log;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use Dingo\Api\Exception\ResourceException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\ServerRepository;
@ -35,44 +35,30 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
/**
* @Resource("Servers")
*/
class ServerController extends BaseController
{
public function __construct()
{
//
}
/**
* List All Servers.
*
* Lists all servers currently on the system.
*
* @Get("/servers/{?page}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
* })
* @Response(200)
* @param Request $request
* @return array
*/
public function lists(Request $request)
public function index(Request $request)
{
return Models\Server::all()->toArray();
return Server::all()->toArray();
}
/**
* Create Server.
*
* @Post("/servers")
* @Versions({"v1"})
* @Response(201)
* @param Request $request
* @return array
*/
public function create(Request $request)
{
$repo = new ServerRepository;
try {
$repo = new ServerRepository;
$server = $repo->create($request->all());
return ['id' => $server->id];
@ -89,22 +75,13 @@ class ServerController extends BaseController
/**
* List Specific Server.
*
* Lists specific fields about a server or all fields pertaining to that server.
*
* @Get("/servers/{id}{?fields}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the server to get information on."),
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
* })
* @Response(200)
* @param Request $request
* @param int $id
* @return array
*/
public function view(Request $request, $id)
{
$server = Models\Server::with('node', 'allocations', 'pack')->where('id', $id)->first();
if (! $server) {
throw new NotFoundHttpException('No server by that ID was found.');
}
$server = Server::with('node', 'allocations', 'pack')->where('id', $id)->firstOrFail();
if (! is_null($request->input('fields'))) {
$fields = explode(',', $request->input('fields'));
@ -138,32 +115,20 @@ class ServerController extends BaseController
/**
* Update Server configuration.
*
* Updates display information on panel.
*
* @Patch("/servers/{id}/config")
* @Versions({"v1"})
* @Transaction({
* @Request({
* "owner": "new@email.com",
* "name": "New Name",
* "reset_token": true
* }, headers={"Authorization": "Bearer <token>"}),
* @Response(200, body={"name": "New Name"}),
* @Response(422)
* })
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the server to modify.")
* })
* @param Request $request
* @param int $id
* @return array
*/
public function config(Request $request, $id)
{
$repo = new ServerRepository;
try {
$server = new ServerRepository;
$server->updateDetails($id, $request->only([
'owner', 'name', 'reset_token',
$server = $repo->updateDetails($id, $request->intersect([
'owner_id', 'name', 'reset_token',
]));
return Models\Server::findOrFail($id);
return ['id' => $id];
} catch (DisplayValidationException $ex) {
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
} catch (DisplayException $ex) {
@ -176,42 +141,21 @@ class ServerController extends BaseController
/**
* Update Server Build Configuration.
*
* Updates server build information on panel and on node.
*
* @Patch("/servers/{id}/build")
* @Versions({"v1"})
* @Transaction({
* @Request({
* "default": "192.168.0.1:25565",
* "add_additional": [
* "192.168.0.1:25566",
* "192.168.0.1:25567",
* "192.168.0.1:25568"
* ],
* "remove_additional": [],
* "memory": 1024,
* "swap": 0,
* "io": 500,
* "cpu": 0,
* "disk": 1024
* }, headers={"Authorization": "Bearer <token>"}),
* @Response(200, body={"name": "New Name"}),
* @Response(422)
* })
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the server to modify.")
* })
* @param Request $request
* @param int $id
* @return array
*/
public function build(Request $request, $id)
{
$repo = new ServerRepository;
try {
$server = new ServerRepository;
$server->changeBuild($id, $request->only([
'default', 'add_additional', 'remove_additional',
'memory', 'swap', 'io', 'cpu', 'disk',
$server = $repo->changeBuild($id, $request->intersect([
'allocation_id', 'add_allocations', 'remove_allocations',
'memory', 'swap', 'io', 'cpu',
]));
return Models\Server::findOrFail($id);
return ['id' => $id];
} catch (DisplayValidationException $ex) {
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
} catch (DisplayException $ex) {
@ -224,18 +168,15 @@ class ServerController extends BaseController
/**
* Suspend Server.
*
* @Post("/servers/{id}/suspend")
* @Versions({"v1"})
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the server."),
* })
* @Response(204)
* @param Request $request
* @param int $id
* @return void
*/
public function suspend(Request $request, $id)
{
try {
$server = new ServerRepository;
$server->suspend($id);
$repo = new ServerRepository;
$repo->suspend($id);
return $this->response->noContent();
} catch (DisplayException $ex) {
@ -248,18 +189,15 @@ class ServerController extends BaseController
/**
* Unsuspend Server.
*
* @Post("/servers/{id}/unsuspend")
* @Versions({"v1"})
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the server."),
* })
* @Response(204)
* @param Request $request
* @param int $id
* @return void
*/
public function unsuspend(Request $request, $id)
{
try {
$server = new ServerRepository;
$server->unsuspend($id);
$repo = new ServerRepository;
$repo->unsuspend($id);
return $this->response->noContent();
} catch (DisplayException $ex) {
@ -272,19 +210,17 @@ class ServerController extends BaseController
/**
* Delete Server.
*
* @Delete("/servers/{id}/{force}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the server."),
* @Parameter("force", type="string", required=false, description="Use 'force' if the server should be removed regardless of daemon response."),
* })
* @Response(204)
* @param Request $request
* @param int $id
* @param string|null $force
* @return void
*/
public function delete(Request $request, $id, $force = null)
{
$repo = new ServerRepository;
try {
$server = new ServerRepository;
$server->deleteServer($id, $force);
$repo->deleteServer($id, $force);
return $this->response->noContent();
} catch (DisplayException $ex) {

View file

@ -24,31 +24,20 @@
namespace Pterodactyl\Http\Controllers\API;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Models\Service;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @Resource("Services")
*/
class ServiceController extends BaseController
{
public function __construct()
public function index(Request $request)
{
//
}
public function lists(Request $request)
{
return Models\Service::all()->toArray();
return Service::all()->toArray();
}
public function view(Request $request, $id)
{
$service = Models\Service::with('options.variables', 'options.packs')->find($id);
if (! $service) {
throw new NotFoundHttpException('No service by that ID was found.');
}
$service = Service::with('options.variables', 'options.packs')->findOrFail($id);
return $service->toArray();
}

View file

@ -25,15 +25,15 @@
namespace Pterodactyl\Http\Controllers\API\User;
use Log;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use Pterodactyl\Http\Controllers\API\BaseController;
class ServerController extends BaseController
{
public function info(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid)->load('allocations');
$server = Server::byUuid($uuid)->load('allocations');
try {
$response = $server->guzzleClient()->request('GET', '/server');
@ -82,8 +82,14 @@ class ServerController extends BaseController
public function power(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
Auth::user()->can('power-' . $request->input('action'), $server);
$server = Server::byUuid($uuid);
$request->user()->can('power-' . $request->input('action'), $server);
if (empty($request->input('action'))) {
return $this->response()->error([
'error' => 'An action must be passed to this request.',
], 422);
}
$res = $server->guzzleClient()->request('PUT', '/server/power', [
'exceptions' => false,
@ -98,4 +104,29 @@ class ServerController extends BaseController
return $this->response->noContent();
}
public function command(Request $request, $uuid)
{
$server = Server::byUuid($uuid);
$request->user()->can('send-command', $server);
if (empty($request->input('command'))) {
return $this->response()->error([
'error' => 'A command must be passed to this request.',
], 422);
}
$res = $server->guzzleClient()->request('POST', '/server/command', [
'exceptions' => false,
'json' => [
'command' => $request->input('command'),
],
]);
if ($res->getStatusCode() !== 204) {
return $this->response->error(json_decode($res->getBody())->error, $res->getStatusCode());
}
return $this->response->noContent();
}
}

View file

@ -24,8 +24,8 @@
namespace Pterodactyl\Http\Controllers\API;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Dingo\Api\Exception\ResourceException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\UserRepository;
@ -33,51 +33,29 @@ use Pterodactyl\Exceptions\DisplayValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
/**
* @Resource("Users")
*/
class UserController extends BaseController
{
public function __construct()
{
}
/**
* List All Users.
*
* Lists all users currently on the system.
*
* @Get("/users/{?page}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
* })
* @Response(200)
* @param Request $request
* @return array
*/
public function lists(Request $request)
public function index(Request $request)
{
return Models\User::all()->toArray();
return User::all()->toArray();
}
/**
* List Specific User.
*
* Lists specific fields about a user or all fields pertaining to that user.
*
* @Get("/users/{id}/{fields}")
* @Versions({"v1"})
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the user to get information on."),
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
* })
* @Response(200)
* @param Request $request
* @param int $id
* @return array
*/
public function view(Request $request, $id)
{
$user = Models\User::with('servers')->where((is_numeric($id) ? 'id' : 'email'), $id)->first();
if (! $user->first()) {
throw new NotFoundHttpException('No user by that ID was found.');
}
$user = User::with('servers')->where((is_numeric($id) ? 'id' : 'email'), $id)->firstOrFail();
$user->servers->transform(function ($item) {
return collect($item)->only([
@ -100,31 +78,20 @@ class UserController extends BaseController
/**
* Create a New User.
*
* @Post("/users")
* @Versions({"v1"})
* @Transaction({
* @Request({
* "email": "foo@example.com",
* "password": "foopassword",
* "admin": false,
* "custom_id": 123
* }, headers={"Authorization": "Bearer <token>"}),
* @Response(201),
* @Response(422)
* })
* @param Request $request
* @return array
*/
public function create(Request $request)
{
try {
$user = new UserRepository;
$create = $user->create($request->only([
'email', 'username', 'name_first',
'name_last', 'password',
'root_admin', 'custom_id',
]));
$create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id'));
$repo = new UserRepository;
return ['id' => $create];
try {
$user = $user->create($request->only([
'email', 'password', 'name_first',
'name_last', 'username', 'root_admin',
]));
return ['id' => $user->id];
} catch (DisplayValidationException $ex) {
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
} catch (DisplayException $ex) {
@ -137,32 +104,21 @@ class UserController extends BaseController
/**
* Update an Existing User.
*
* The data sent in the request will be used to update the existing user on the system.
*
* @Patch("/users/{id}")
* @Versions({"v1"})
* @Transaction({
* @Request({
* "email": "new@email.com"
* }, headers={"Authorization": "Bearer <token>"}),
* @Response(200, body={"email": "new@email.com"}),
* @Response(422)
* })
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the user to modify.")
* })
* @param Request $request
* @param int $id
* @return array
*/
public function update(Request $request, $id)
{
$repo = new UserRepository;
try {
$user = new UserRepository;
$user->update($id, $request->only([
'username', 'email', 'name_first',
'name_last', 'password',
'root_admin', 'language',
$user = $repo->update($id, $request->only([
'email', 'password', 'name_first',
'name_last', 'username', 'root_admin',
]));
return Models\User::findOrFail($id);
return ['id' => $id];
} catch (DisplayValidationException $ex) {
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
} catch (DisplayException $ex) {
@ -175,22 +131,16 @@ class UserController extends BaseController
/**
* Delete a User.
*
* @Delete("/users/{id}")
* @Versions({"v1"})
* @Transaction({
* @Request(headers={"Authorization": "Bearer <token>"}),
* @Response(204),
* @Response(422)
* })
* @Parameters({
* @Parameter("id", type="integer", required=true, description="The ID of the user to delete.")
* })
* @param Request $request
* @param int $id
* @return void
*/
public function delete(Request $request, $id)
{
$repo = new UserRepository;
try {
$user = new UserRepository;
$user->delete($id);
$repo->delete($id);
return $this->response->noContent();
} catch (DisplayException $ex) {

View file

@ -42,10 +42,15 @@ class APIRoutes
'uses' => 'Pterodactyl\Http\Controllers\API\User\ServerController@info',
]);
$api->put('/server/{uuid}', [
$api->post('/server/{uuid}/power', [
'as' => 'api.user.server.power',
'uses' => 'Pterodactyl\Http\Controllers\API\User\ServerController@power',
]);
$api->post('/server/{uuid}/command', [
'as' => 'api.user.server.command',
'uses' => 'Pterodactyl\Http\Controllers\API\User\ServerController@command',
]);
});
$api->version('v1', ['prefix' => 'api', 'middleware' => 'api.auth'], function ($api) {
@ -55,7 +60,7 @@ class APIRoutes
*/
$api->get('users', [
'as' => 'api.admin.users.list',
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@lists',
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@index',
]);
$api->post('users', [
@ -83,7 +88,7 @@ class APIRoutes
*/
$api->get('servers', [
'as' => 'api.admin.servers.list',
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@lists',
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@index',
]);
$api->post('servers', [
@ -126,7 +131,7 @@ class APIRoutes
*/
$api->get('nodes', [
'as' => 'api.admin.nodes.list',
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@lists',
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@index',
]);
$api->post('nodes', [
@ -150,7 +155,7 @@ class APIRoutes
]);
$api->get('nodes/{id}/config', [
'as' => 'api.admin.nodes.view',
'as' => 'api.admin.nodes.view.config',
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@config',
]);
@ -164,7 +169,7 @@ class APIRoutes
*/
$api->get('locations', [
'as' => 'api.admin.locations.list',
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@lists',
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@index',
]);
/*
@ -172,7 +177,7 @@ class APIRoutes
*/
$api->get('services', [
'as' => 'api.admin.services.list',
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@lists',
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@index',
]);
$api->get('services/{id}', [