Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Nodes/NodeController.php
10277 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
4
5
use Pterodactyl\Models\Node;
6
use Illuminate\Http\JsonResponse;
7
use Spatie\QueryBuilder\QueryBuilder;
8
use Pterodactyl\Services\Nodes\NodeUpdateService;
9
use Pterodactyl\Services\Nodes\NodeCreationService;
10
use Pterodactyl\Services\Nodes\NodeDeletionService;
11
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
12
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest;
13
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest;
14
use Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest;
15
use Pterodactyl\Http\Requests\Api\Application\Nodes\DeleteNodeRequest;
16
use Pterodactyl\Http\Requests\Api\Application\Nodes\UpdateNodeRequest;
17
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
18
19
class NodeController extends ApplicationApiController
20
{
21
/**
22
* NodeController constructor.
23
*/
24
public function __construct(
25
private NodeCreationService $creationService,
26
private NodeDeletionService $deletionService,
27
private NodeUpdateService $updateService,
28
) {
29
parent::__construct();
30
}
31
32
/**
33
* Return all the nodes currently available on the Panel.
34
*/
35
public function index(GetNodesRequest $request): array
36
{
37
$nodes = QueryBuilder::for(Node::query())
38
->allowedFilters(['uuid', 'name', 'fqdn', 'daemon_token_id'])
39
->allowedSorts(['id', 'uuid', 'memory', 'disk'])
40
->paginate($request->query('per_page') ?? 50);
41
42
return $this->fractal->collection($nodes)
43
->transformWith($this->getTransformer(NodeTransformer::class))
44
->toArray();
45
}
46
47
/**
48
* Return data for a single instance of a node.
49
*/
50
public function view(GetNodeRequest $request, Node $node): array
51
{
52
return $this->fractal->item($node)
53
->transformWith($this->getTransformer(NodeTransformer::class))
54
->toArray();
55
}
56
57
/**
58
* Create a new node on the Panel. Returns the created node and an HTTP/201
59
* status response on success.
60
*
61
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
62
*/
63
public function store(StoreNodeRequest $request): JsonResponse
64
{
65
$node = $this->creationService->handle($request->validated());
66
67
return $this->fractal->item($node)
68
->transformWith($this->getTransformer(NodeTransformer::class))
69
->addMeta([
70
'resource' => route('api.application.nodes.view', [
71
'node' => $node->id,
72
]),
73
])
74
->respond(201);
75
}
76
77
/**
78
* Update an existing node on the Panel.
79
*
80
* @throws \Throwable
81
*/
82
public function update(UpdateNodeRequest $request, Node $node): array
83
{
84
$node = $this->updateService->handle(
85
$node,
86
$request->validated(),
87
$request->input('reset_secret') === true
88
);
89
90
return $this->fractal->item($node)
91
->transformWith($this->getTransformer(NodeTransformer::class))
92
->toArray();
93
}
94
95
/**
96
* Deletes a given node from the Panel as long as there are no servers
97
* currently attached to it.
98
*
99
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
100
*/
101
public function delete(DeleteNodeRequest $request, Node $node): JsonResponse
102
{
103
$this->deletionService->handle($node);
104
105
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
106
}
107
}
108
109