2017-12-17 14:57:05 -06:00
|
|
|
<?php
|
|
|
|
|
2018-01-19 21:47:06 -06:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-12-17 14:57:05 -06:00
|
|
|
|
|
|
|
use Pterodactyl\Models\Node;
|
2022-10-14 10:59:20 -06:00
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-13 14:08:19 -06:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2022-12-14 17:05:46 -07:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2017-12-17 14:57:05 -06:00
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
class NodeTransformer extends Transformer
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*/
|
2022-05-04 19:11:42 -04:00
|
|
|
protected array $availableIncludes = ['allocations', 'location', 'servers'];
|
2017-12-17 14:57:05 -06:00
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Node::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-12-17 14:57:05 -06:00
|
|
|
/**
|
2018-01-13 14:08:19 -06:00
|
|
|
* Return a node transformed into a format that can be consumed by the
|
|
|
|
* external administrative API.
|
2017-12-17 14:57:05 -06:00
|
|
|
*/
|
2022-12-14 17:05:46 -07:00
|
|
|
public function transform(Node $model): array
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2022-12-14 18:08:50 -07:00
|
|
|
$response = $model->toArray();
|
2018-01-13 14:08:19 -06:00
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
$response['created_at'] = self::formatTimestamp($model->created_at);
|
|
|
|
$response['updated_at'] = self::formatTimestamp($model->updated_at);
|
2018-01-13 14:08:19 -06:00
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
$resources = $model->servers()->select(['memory', 'disk'])->get();
|
2020-09-17 20:44:24 -07:00
|
|
|
|
|
|
|
$response['allocated_resources'] = [
|
|
|
|
'memory' => $resources->sum('memory'),
|
|
|
|
'disk' => $resources->sum('disk'),
|
|
|
|
];
|
|
|
|
|
2018-01-13 14:08:19 -06:00
|
|
|
return $response;
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-14 17:05:46 -07:00
|
|
|
* Return the allocations associated with this node.
|
2017-12-17 14:57:05 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeAllocations(Node $node): Collection|NullResource
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
|
2018-01-13 14:08:19 -06:00
|
|
|
return $this->null();
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
return $this->collection($node->allocations, new AllocationTransformer());
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-14 17:05:46 -07:00
|
|
|
* Return the location associated with this node.
|
2017-12-17 14:57:05 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeLocation(Node $node): Item|NullResource
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
|
2018-01-13 14:08:19 -06:00
|
|
|
return $this->null();
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
return $this->item($node->location, new LocationTransformer());
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-14 17:05:46 -07:00
|
|
|
* Return the servers associated with this node.
|
2017-12-17 14:57:05 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeServers(Node $node): Collection|NullResource
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
2018-01-13 14:08:19 -06:00
|
|
|
return $this->null();
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
return $this->collection($node->servers, new ServerTransformer());
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
}
|