2017-12-17 20:57:05 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
|
|
|
use Pterodactyl\Models\Node;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-13 20:08:19 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-12 04:49:46 +00:00
|
|
|
class NodeTransformer extends BaseTransformer
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*/
|
2022-05-04 23:11:42 +00:00
|
|
|
protected array $availableIncludes = ['allocations', 'location', 'servers'];
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Node::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
2018-01-13 20:08:19 +00:00
|
|
|
* Return a node transformed into a format that can be consumed by the
|
|
|
|
* external administrative API.
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
|
|
|
public function transform(Node $node): array
|
|
|
|
{
|
2018-01-13 20:08:19 +00:00
|
|
|
$response = collect($node->toArray())->mapWithKeys(function ($value, $key) {
|
|
|
|
// I messed up early in 2016 when I named this column as poorly
|
|
|
|
// as I did. This is the tragic result of my mistakes.
|
|
|
|
$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;
|
|
|
|
|
|
|
|
return [snake_case($key) => $value];
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$response[$node->getUpdatedAtColumn()] = $this->formatTimestamp($node->updated_at);
|
|
|
|
$response[$node->getCreatedAtColumn()] = $this->formatTimestamp($node->created_at);
|
|
|
|
|
2020-09-18 03:44:24 +00:00
|
|
|
$resources = $node->servers()->select(['memory', 'disk'])->get();
|
|
|
|
|
|
|
|
$response['allocated_resources'] = [
|
|
|
|
'memory' => $resources->sum('memory'),
|
|
|
|
'disk' => $resources->sum('disk'),
|
|
|
|
];
|
|
|
|
|
2018-01-13 20:08:19 +00:00
|
|
|
return $response;
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nodes associated with this location.
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeAllocations(Node $node): Collection|NullResource
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
|
2018-01-13 20:08:19 +00:00
|
|
|
return $this->null();
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 20:08:19 +00:00
|
|
|
$node->loadMissing('allocations');
|
|
|
|
|
|
|
|
return $this->collection(
|
2021-01-23 20:33:34 +00:00
|
|
|
$node->getRelation('allocations'),
|
|
|
|
$this->makeTransformer(AllocationTransformer::class),
|
|
|
|
'allocation'
|
2018-01-13 20:08:19 +00:00
|
|
|
);
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nodes associated with this location.
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeLocation(Node $node): Item|NullResource
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
|
2018-01-13 20:08:19 +00:00
|
|
|
return $this->null();
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 20:08:19 +00:00
|
|
|
$node->loadMissing('location');
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-13 20:08:19 +00:00
|
|
|
return $this->item(
|
2021-01-23 20:33:34 +00:00
|
|
|
$node->getRelation('location'),
|
|
|
|
$this->makeTransformer(LocationTransformer::class),
|
|
|
|
'location'
|
2018-01-13 20:08:19 +00:00
|
|
|
);
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nodes associated with this location.
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeServers(Node $node): Collection|NullResource
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
2018-01-13 20:08:19 +00:00
|
|
|
return $this->null();
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 20:08:19 +00:00
|
|
|
$node->loadMissing('servers');
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-13 20:08:19 +00:00
|
|
|
return $this->collection(
|
2021-01-23 20:33:34 +00:00
|
|
|
$node->getRelation('servers'),
|
|
|
|
$this->makeTransformer(ServerTransformer::class),
|
|
|
|
'server'
|
2018-01-13 20:08:19 +00:00
|
|
|
);
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
}
|