Skip to content

Commit 42fb9fd

Browse files
committed
Add the ability for a node to fetch a list of all servers it has been assigned
1 parent 07d19ad commit 42fb9fd

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,16 @@ public function getSuspendedServersCount(): int;
171171
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
172172
*/
173173
public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator;
174+
175+
/**
176+
* Returns every server that exists for a given node.
177+
*
178+
* This is different from {@see loadAllServersForNode} because
179+
* it does not paginate the response.
180+
*
181+
* @param int $node
182+
*
183+
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
184+
*/
185+
public function loadEveryServerForNode(int $node);
174186
}

app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Http\JsonResponse;
77
use Pterodactyl\Http\Controllers\Controller;
8+
use Pterodactyl\Repositories\Eloquent\NodeRepository;
89
use Pterodactyl\Services\Eggs\EggConfigurationService;
910
use Pterodactyl\Repositories\Eloquent\ServerRepository;
1011
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
@@ -26,21 +27,29 @@ class ServerDetailsController extends Controller
2627
*/
2728
private $configurationStructureService;
2829

30+
/**
31+
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
32+
*/
33+
private $nodeRepository;
34+
2935
/**
3036
* ServerConfigurationController constructor.
3137
*
3238
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
3339
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
3440
* @param \Pterodactyl\Services\Eggs\EggConfigurationService $eggConfigurationService
41+
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
3542
*/
3643
public function __construct(
3744
ServerRepository $repository,
3845
ServerConfigurationStructureService $configurationStructureService,
39-
EggConfigurationService $eggConfigurationService
46+
EggConfigurationService $eggConfigurationService,
47+
NodeRepository $nodeRepository
4048
) {
4149
$this->eggConfigurationService = $eggConfigurationService;
4250
$this->repository = $repository;
4351
$this->configurationStructureService = $configurationStructureService;
52+
$this->nodeRepository = $nodeRepository;
4453
}
4554

4655
/**
@@ -62,4 +71,32 @@ public function __invoke(Request $request, $uuid)
6271
'process_configuration' => $this->eggConfigurationService->handle($server),
6372
]);
6473
}
74+
75+
/**
76+
* Lists all servers with their configurations that are assigned to the requesting node.
77+
*
78+
* @param \Illuminate\Http\Request $request
79+
*
80+
* @return \Illuminate\Http\JsonResponse
81+
*
82+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
83+
*/
84+
public function list(Request $request)
85+
{
86+
$authorization = substr($request->header('Authorization'), 7);
87+
88+
$node = $this->nodeRepository->findFirstWhere([ 'daemonSecret' => $authorization ]);
89+
$servers = $this->repository->loadEveryServerForNode($node->id);
90+
91+
$configurations = [];
92+
93+
foreach ($servers as $server) {
94+
$configurations[$server->uuid] = [
95+
'settings' => $this->configurationStructureService->handle($server),
96+
'process_configuration' => $this->eggConfigurationService->handle($server),
97+
];
98+
}
99+
100+
return JsonResponse::create($configurations);
101+
}
65102
}

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,21 @@ public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginat
378378
->where('node_id', '=', $node)
379379
->paginate($limit);
380380
}
381+
382+
/**
383+
* Returns every server that exists for a given node.
384+
*
385+
* This is different from {@see loadAllServersForNode} because
386+
* it does not paginate the response.
387+
*
388+
* @param int $node
389+
*
390+
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
391+
*/
392+
public function loadEveryServerForNode(int $node)
393+
{
394+
return $this->getBuilder()
395+
->with('nest')
396+
->where('node_id', '=', $node)->get();
397+
}
381398
}

routes/api-remote.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
// Routes for the Wings daemon.
99
Route::post('/sftp/auth', 'SftpAuthenticationController');
10+
11+
Route::get('/servers', 'Servers\ServerDetailsController@list');
12+
1013
Route::group(['prefix' => '/servers/{uuid}'], function () {
1114
Route::get('/', 'Servers\ServerDetailsController');
1215
Route::get('/install', 'Servers\ServerInstallController@index');

0 commit comments

Comments
 (0)