Better middleware for routes, cleaned up API, removed old API calls
New API routes for Server allow specifying which fractal objects to load into the request, thus making it possible to fine-tune what data is returned.
This commit is contained in:
parent
ddb82ac3ca
commit
97773300ed
15 changed files with 304 additions and 747 deletions
63
app/Transformers/User/AllocationTransformer.php
Normal file
63
app/Transformers/User/AllocationTransformer.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?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\Transformers\User;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class AllocationTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Server eloquent model.
|
||||
*
|
||||
* @return \Pterodactyl\Models\Server
|
||||
*/
|
||||
protected $server;
|
||||
|
||||
/**
|
||||
* Setup allocation transformer with access to server data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Server $server)
|
||||
{
|
||||
$this->server = $server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a generic transformed allocation array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Allocation $allocation)
|
||||
{
|
||||
return [
|
||||
'ip' => $allocation->alias,
|
||||
'port' => $allocation->port,
|
||||
'default' => ($allocation->id === $this->server->allocation_id),
|
||||
];
|
||||
}
|
||||
}
|
50
app/Transformers/User/OverviewTransformer.php
Normal file
50
app/Transformers/User/OverviewTransformer.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?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\Transformers\User;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class OverviewTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Return a generic transformed server array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Server $server)
|
||||
{
|
||||
return [
|
||||
'uuidShort' => $server->uuidShort,
|
||||
'uuid' => $server->uuid,
|
||||
'name' => $server->name,
|
||||
'node' => $server->node->name,
|
||||
'ip' => $server->allocation->alias,
|
||||
'port' => $server->allocation->port,
|
||||
'service' => $server->service->name,
|
||||
'option' => $server->option->name,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -29,6 +29,17 @@ use League\Fractal\TransformerAbstract;
|
|||
|
||||
class ServerTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* List of resources that can be included.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = [
|
||||
'allocations',
|
||||
'subusers',
|
||||
'stats',
|
||||
];
|
||||
|
||||
/**
|
||||
* Return a generic transformed server array.
|
||||
*
|
||||
|
@ -37,14 +48,53 @@ class ServerTransformer extends TransformerAbstract
|
|||
public function transform(Server $server)
|
||||
{
|
||||
return [
|
||||
'short' => $server->uuidShort,
|
||||
'uuidShort' => $server->uuidShort,
|
||||
'uuid' => $server->uuid,
|
||||
'name' => $server->name,
|
||||
'description' => $server->description,
|
||||
'node' => $server->node->name,
|
||||
'ip' => $server->allocation->alias,
|
||||
'port' => $server->allocation->port,
|
||||
'service' => $server->service->name,
|
||||
'option' => $server->option->name,
|
||||
'limits' => [
|
||||
'memory' => $server->memory,
|
||||
'swap' => $server->swap,
|
||||
'disk' => $server->disk,
|
||||
'io' => $server->io,
|
||||
'cpu' => $server->cpu,
|
||||
'oom_disabled' => (bool) $server->oom_disabled,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a generic array of allocations for this server.
|
||||
*
|
||||
* @return \Leauge\Fractal\Resource\Collection
|
||||
*/
|
||||
public function includeAllocations(Server $server)
|
||||
{
|
||||
$allocations = $server->allocations;
|
||||
|
||||
return $this->collection($allocations, new AllocationTransformer($server));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a generic array of subusers for this server.
|
||||
*
|
||||
* @return \Leauge\Fractal\Resource\Collection
|
||||
*/
|
||||
public function includeSubusers(Server $server)
|
||||
{
|
||||
$server->load('subusers.permissions', 'subusers.user');
|
||||
|
||||
return $this->collection($server->subusers, new SubuserTransformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a generic array of allocations for this server.
|
||||
*
|
||||
* @return \Leauge\Fractal\Resource\Item
|
||||
*/
|
||||
public function includeStats(Server $server)
|
||||
{
|
||||
return $this->item($server->guzzleClient(), new StatsTransformer);
|
||||
}
|
||||
}
|
||||
|
|
62
app/Transformers/User/StatsTransformer.php
Normal file
62
app/Transformers/User/StatsTransformer.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?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\Transformers\User;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
|
||||
class StatsTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Return a generic transformed subuser array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Client $client)
|
||||
{
|
||||
try {
|
||||
$res = $client->request('GET', '/server', ['http_errors' => false]);
|
||||
|
||||
if ($res->getStatusCode() !== 200) {
|
||||
return [
|
||||
'error' => 'Error: HttpResponseException. Recieved non-200 HTTP status code from daemon: ' . $res->statusCode(),
|
||||
];
|
||||
}
|
||||
|
||||
$json = json_decode($res->getBody());
|
||||
|
||||
return [
|
||||
'status' => $json->status,
|
||||
'resources' => $json->proc,
|
||||
];
|
||||
} catch (ConnectException $ex) {
|
||||
return [
|
||||
'error' => 'Error: ConnectException. Unable to contact the daemon to request server status.',
|
||||
'exception' => (config('app.debug')) ? $ex->getMessage() : null,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
47
app/Transformers/User/SubuserTransformer.php
Normal file
47
app/Transformers/User/SubuserTransformer.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?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\Transformers\User;
|
||||
|
||||
use Pterodactyl\Models\Subuser;
|
||||
use Pterodactyl\Models\Permission;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class SubuserTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Return a generic transformed subuser array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Subuser $subuser)
|
||||
{
|
||||
return [
|
||||
'username' => $subuser->user->username,
|
||||
'email' => $subuser->user->email,
|
||||
'2fa' => (bool) $subuser->user->use_totp,
|
||||
'permissions' => $subuser->permissions->pluck('permission'),
|
||||
];
|
||||
}
|
||||
}
|
Reference in a new issue