Skip to content

Commit 5e27772

Browse files
committed
Very rough go at getting API back into operational state.
Not spending a lot of time on this as its a pre-release and I have plans to overhaul the API to actually work and be easy to maintain.
1 parent 4e916cb commit 5e27772

File tree

7 files changed

+187
-331
lines changed

7 files changed

+187
-331
lines changed

app/Http/Controllers/API/LocationController.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,15 @@
2727
use Illuminate\Http\Request;
2828
use Pterodactyl\Models\Location;
2929

30-
/**
31-
* @Resource("Servers")
32-
*/
3330
class LocationController extends BaseController
3431
{
35-
public function __construct()
36-
{
37-
//
38-
}
39-
4032
/**
41-
* List All Locations.
42-
*
4333
* Lists all locations currently on the system.
4434
*
45-
* @Get("/locations")
46-
* @Versions({"v1"})
47-
* @Response(200)
35+
* @param Request $request
36+
* @return array
4837
*/
49-
public function lists(Request $request)
38+
public function index(Request $request)
5039
{
5140
return Location::with('nodes')->get()->map(function ($item) {
5241
$item->nodes->transform(function ($item) {

app/Http/Controllers/API/NodeController.php

Lines changed: 54 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
namespace Pterodactyl\Http\Controllers\API;
2626

2727
use Log;
28-
use Pterodactyl\Models;
2928
use Illuminate\Http\Request;
29+
use Pterodactyl\Models\Node;
30+
use Pterodactyl\Models\Allocation;
3031
use Dingo\Api\Exception\ResourceException;
3132
use Pterodactyl\Exceptions\DisplayException;
3233
use Pterodactyl\Repositories\NodeRepository;
@@ -35,176 +36,131 @@
3536
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
3637
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
3738

38-
/**
39-
* @Resource("Servers")
40-
*/
4139
class NodeController extends BaseController
4240
{
43-
public function __construct()
44-
{
45-
//
46-
}
47-
4841
/**
49-
* List All Nodes.
50-
*
5142
* Lists all nodes currently on the system.
5243
*
53-
* @Get("/nodes/{?page}")
54-
* @Versions({"v1"})
55-
* @Parameters({
56-
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
57-
* })
58-
* @Response(200)
44+
* @param Request $request
45+
* @return array
5946
*/
60-
public function lists(Request $request)
47+
public function index(Request $request)
6148
{
62-
return Models\Node::all()->toArray();
49+
return Node::all()->toArray();
6350
}
6451

6552
/**
66-
* Create a New Node.
53+
* Create a new node.
6754
*
68-
* @Post("/nodes")
69-
* @Versions({"v1"})
70-
* @Transaction({
71-
* @Request({
72-
* 'name' => 'My API Node',
73-
* 'location' => 1,
74-
* 'public' => 1,
75-
* 'fqdn' => 'daemon.wuzzle.woo',
76-
* 'scheme' => 'https',
77-
* 'memory' => 10240,
78-
* 'memory_overallocate' => 100,
79-
* 'disk' => 204800,
80-
* 'disk_overallocate' => -1,
81-
* 'daemonBase' => '/srv/daemon-data',
82-
* 'daemonSFTP' => 2022,
83-
* 'daemonListen' => 8080
84-
* }, headers={"Authorization": "Bearer <jwt-token>"}),
85-
* @Response(200),
86-
* @Response(422, body={
87-
* "message": "A validation error occured.",
88-
* "errors": {},
89-
* "status_code": 422
90-
* }),
91-
* @Response(503, body={
92-
* "message": "There was an error while attempting to add this node to the system.",
93-
* "status_code": 503
94-
* })
95-
* })
55+
* @param Request $request
56+
* @return array
57+
*
58+
* @throws \Pterodactyl\Exceptions\DisplayException
59+
* @throws \Pterodactyl\Exceptions\DisplayValidationException
9660
*/
9761
public function create(Request $request)
9862
{
63+
$repo = new NodeRepository;
64+
9965
try {
100-
$repo = new NodeRepository;
101-
$node = $repo->create($request->only([
102-
'name', 'location_id', 'public', 'fqdn',
103-
'scheme', 'memory', 'memory_overallocate',
104-
'disk', 'disk_overallocate', 'daemonBase',
105-
'daemonSFTP', 'daemonListen',
106-
]));
107-
108-
return ['id' => $repo->id];
66+
$node = $repo->create(array_merge(
67+
$request->only([
68+
'public', 'disk_overallocate', 'memory_overallocate',
69+
]),
70+
$request->intersect([
71+
'name', 'location_id', 'fqdn',
72+
'scheme', 'memory', 'disk',
73+
'daemonBase', 'daemonSFTP', 'daemonListen',
74+
])
75+
));
76+
77+
return ['id' => $node->id];
10978
} catch (DisplayValidationException $ex) {
11079
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
11180
} catch (DisplayException $ex) {
11281
throw new ResourceException($ex->getMessage());
11382
} catch (\Exception $ex) {
11483
Log::error($ex);
115-
throw new BadRequestHttpException('There was an error while attempting to add this node to the system.');
84+
throw new BadRequestHttpException('There was an error while attempting to add this node to the system. This error has been logged.');
11685
}
11786
}
11887

11988
/**
120-
* List Specific Node.
121-
*
12289
* Lists specific fields about a server or all fields pertaining to that node.
12390
*
124-
* @Get("/nodes/{id}/{?fields}")
125-
* @Versions({"v1"})
126-
* @Parameters({
127-
* @Parameter("id", type="integer", required=true, description="The ID of the node to get information on."),
128-
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
129-
* })
130-
* @Response(200)
91+
* @param Request $request
92+
* @param int $id
93+
* @param string $fields
94+
* @return array
13195
*/
13296
public function view(Request $request, $id, $fields = null)
13397
{
134-
$node = Models\Node::with('allocations')->where('id', $id)->first();
135-
if (! $node) {
136-
throw new NotFoundHttpException('No node by that ID was found.');
137-
}
98+
$node = Node::with('allocations')->findOrFail($id);
13899

139100
$node->allocations->transform(function ($item) {
140101
return collect($item)->only([
141102
'id', 'ip', 'ip_alias', 'port', 'server_id',
142103
]);
143104
});
144105

145-
if (! is_null($request->input('fields'))) {
106+
if (! empty($request->input('fields'))) {
146107
$fields = explode(',', $request->input('fields'));
147108
if (! empty($fields) && is_array($fields)) {
148109
return collect($node)->only($fields);
149110
}
150111
}
151112

152-
return $node;
113+
return $node->toArray();
153114
}
154115

116+
/**
117+
* Returns a configuration file for a given node.
118+
*
119+
* @param Request $request
120+
* @param int $id
121+
* @return array
122+
*/
155123
public function config(Request $request, $id)
156124
{
157-
$node = Models\Node::where('id', $id)->first();
158-
if (! $node) {
159-
throw new NotFoundHttpException('No node by that ID was found.');
160-
}
125+
$node = Node::findOrFail($id);
161126

162127
return $node->getConfigurationAsJson();
163128
}
164129

165130
/**
166-
* List all Node Allocations.
167-
*
168131
* Returns a listing of all allocations for every node.
169132
*
170-
* @Get("/nodes/allocations")
171-
* @Versions({"v1"})
172-
* @Response(200)
133+
* @param Request $request
134+
* @return array
173135
*/
174136
public function allocations(Request $request)
175137
{
176-
return Models\Allocation::all()->toArray();
138+
return Allocation::all()->toArray();
177139
}
178140

179141
/**
180-
* List Node Allocation based on assigned to ID.
181-
*
182142
* Returns a listing of the allocation for the specified server id.
183143
*
184-
* @Get("/nodes/allocations/{id}")
185-
* @Versions({"v1"})
186-
* @Response(200)
144+
* @param Request $request
145+
* @return array
187146
*/
188147
public function allocationsView(Request $request, $id)
189148
{
190-
return Models\Allocation::where('server_id', $id)->get()->toArray();
149+
return Allocation::where('server_id', $id)->get()->toArray();
191150
}
192151

193152
/**
194-
* Delete Node.
153+
* Delete a node.
195154
*
196-
* @Delete("/nodes/{id}")
197-
* @Versions({"v1"})
198-
* @Parameters({
199-
* @Parameter("id", type="integer", required=true, description="The ID of the node."),
200-
* })
201-
* @Response(204)
155+
* @param Request $request
156+
* @param int $id
157+
* @return void
202158
*/
203159
public function delete(Request $request, $id)
204160
{
161+
$repo = new NodeRepository;
205162
try {
206-
$node = new NodeRepository;
207-
$node->delete($id);
163+
$repo->delete($id);
208164

209165
return $this->response->noContent();
210166
} catch (DisplayException $ex) {

0 commit comments

Comments
 (0)