Skip to content

Commit aca0819

Browse files
committed
Add server build management to API
1 parent d3dba3f commit aca0819

File tree

8 files changed

+202
-128
lines changed

8 files changed

+202
-128
lines changed

app/Contracts/Repository/AllocationRepositoryInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,21 @@ public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): L
4040
* @return \Illuminate\Support\Collection
4141
*/
4242
public function getUniqueAllocationIpsForNode(int $node): Collection;
43+
44+
/**
45+
* Return all of the allocations that exist for a node that are not currently
46+
* allocated.
47+
*
48+
* @param int $node
49+
* @return array
50+
*/
51+
public function getUnassignedAllocationIds(int $node): array;
52+
53+
/**
54+
* Get an array of all allocations that are currently assigned to a given server.
55+
*
56+
* @param int $server
57+
* @return array
58+
*/
59+
public function getAssignedAllocationIds(int $server): array;
4360
}

app/Exceptions/DisplayException.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Exceptions;
114

@@ -66,7 +59,7 @@ public function render($request)
6659
if ($request->expectsJson()) {
6760
return response()->json(Handler::convertToArray($this, [
6861
'detail' => $this->getMessage(),
69-
]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR);
62+
]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_BAD_REQUEST);
7063
}
7164

7265
app()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash();

app/Exceptions/Handler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Handler extends ExceptionHandler
3636
* @var array
3737
*/
3838
protected $dontFlash = [
39+
'token',
40+
'secret',
3941
'password',
4042
'password_confirmation',
4143
];

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,42 @@
33
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
44

55
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Services\Servers\BuildModificationService;
67
use Pterodactyl\Services\Servers\DetailsModificationService;
78
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
89
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
910
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest;
11+
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerBuildConfigurationRequest;
1012

1113
class ServerDetailsController extends ApplicationApiController
1214
{
15+
/**
16+
* @var \Pterodactyl\Services\Servers\BuildModificationService
17+
*/
18+
private $buildModificationService;
19+
1320
/**
1421
* @var \Pterodactyl\Services\Servers\DetailsModificationService
1522
*/
16-
private $modificationService;
23+
private $detailsModificationService;
1724

1825
/**
1926
* ServerDetailsController constructor.
2027
*
21-
* @param \Pterodactyl\Services\Servers\DetailsModificationService $modificationService
28+
* @param \Pterodactyl\Services\Servers\BuildModificationService $buildModificationService
29+
* @param \Pterodactyl\Services\Servers\DetailsModificationService $detailsModificationService
2230
*/
23-
public function __construct(DetailsModificationService $modificationService)
31+
public function __construct(BuildModificationService $buildModificationService, DetailsModificationService $detailsModificationService)
2432
{
2533
parent::__construct();
2634

27-
$this->modificationService = $modificationService;
35+
$this->buildModificationService = $buildModificationService;
36+
$this->detailsModificationService = $detailsModificationService;
2837
}
2938

3039
/**
40+
* Update the details for a specific server.
41+
*
3142
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest $request
3243
* @param \Pterodactyl\Models\Server $server
3344
* @return array
@@ -38,7 +49,27 @@ public function __construct(DetailsModificationService $modificationService)
3849
*/
3950
public function details(UpdateServerDetailsRequest $request, Server $server): array
4051
{
41-
$server = $this->modificationService->returnUpdatedModel()->handle($server, $request->validated());
52+
$server = $this->detailsModificationService->returnUpdatedModel()->handle($server, $request->validated());
53+
54+
return $this->fractal->item($server)
55+
->transformWith($this->getTransformer(ServerTransformer::class))
56+
->toArray();
57+
}
58+
59+
/**
60+
* Update the build details for a specific server.
61+
*
62+
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerBuildConfigurationRequest $request
63+
* @param \Pterodactyl\Models\Server $server
64+
* @return array
65+
*
66+
* @throws \Pterodactyl\Exceptions\DisplayException
67+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
68+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
69+
*/
70+
public function build(UpdateServerBuildConfigurationRequest $request, Server $server): array
71+
{
72+
$server = $this->buildModificationService->handle($server, $request->validated());
4273

4374
return $this->fractal->item($server)
4475
->transformWith($this->getTransformer(ServerTransformer::class))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
4+
5+
use Pterodactyl\Models\Server;
6+
7+
class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
8+
{
9+
/**
10+
* Return the rules to validate this request aganist.
11+
*
12+
* @return array
13+
*/
14+
public function rules(): array
15+
{
16+
$rules = Server::getUpdateRulesForId($this->route()->parameter('server')->id);
17+
18+
return [
19+
'allocation' => $rules['allocation_id'],
20+
'memory' => $rules['memory'],
21+
'swap' => $rules['swap'],
22+
'io' => $rules['io'],
23+
'cpu' => $rules['cpu'],
24+
'disk' => $rules['disk'],
25+
'add_allocations' => 'bail|array',
26+
'add_allocations.*' => 'integer',
27+
'remove_allocations' => 'bail|array',
28+
'remove_allocations.*' => 'integer',
29+
];
30+
}
31+
32+
/**
33+
* Convert the allocation field into the expected format for the service handler.
34+
*
35+
* @return array
36+
*/
37+
public function validated()
38+
{
39+
$data = parent::validated();
40+
41+
$data['allocation_id'] = $data['allocation'];
42+
unset($data['allocation']);
43+
44+
return $data;
45+
}
46+
47+
/**
48+
* Custom attributes to use in error message responses.
49+
*
50+
* @return array
51+
*/
52+
public function attributes()
53+
{
54+
return [
55+
'add_allocations' => 'allocations to add',
56+
'remove_allocations' => 'allocations to remove',
57+
'add_allocations.*' => 'allocation to add',
58+
'remove_allocations.*' => 'allocation to remove',
59+
];
60+
}
61+
}

app/Repositories/Eloquent/AllocationRepository.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Pterodactyl\Repositories\Eloquent;
44

5-
use Pterodactyl\Models\Node;
65
use Illuminate\Support\Collection;
76
use Pterodactyl\Models\Allocation;
87
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@@ -68,4 +67,31 @@ public function getUniqueAllocationIpsForNode(int $node): Collection
6867
->orderByRaw('INET_ATON(ip) ASC')
6968
->get($this->getColumns());
7069
}
70+
71+
/**
72+
* Return all of the allocations that exist for a node that are not currently
73+
* allocated.
74+
*
75+
* @param int $node
76+
* @return array
77+
*/
78+
public function getUnassignedAllocationIds(int $node): array
79+
{
80+
$results = $this->getBuilder()->select('id')->whereNull('server_id')->where('node_id', $node)->get();
81+
82+
return $results->pluck('id')->toArray();
83+
}
84+
85+
/**
86+
* Get an array of all allocations that are currently assigned to a given server.
87+
*
88+
* @param int $server
89+
* @return array
90+
*/
91+
public function getAssignedAllocationIds(int $server): array
92+
{
93+
$results = $this->getBuilder()->select('id')->where('server_id', $server)->get();
94+
95+
return $results->pluck('id')->toArray();
96+
}
7197
}

0 commit comments

Comments
 (0)