Skip to content

Commit 63f377a

Browse files
committed
Add more API routes
Servers: list all, list single Nodes: list all, list single, list single allocations, add node Locations: list all
1 parent 0ccaa16 commit 63f377a

File tree

7 files changed

+368
-6
lines changed

7 files changed

+368
-6
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\API;
4+
5+
use Illuminate\Http\Request;
6+
use Pterodactyl\Models\Location;
7+
8+
/**
9+
* @Resource("Servers")
10+
*/
11+
class LocationController extends BaseController
12+
{
13+
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* List All Locations
21+
*
22+
* Lists all locations currently on the system.
23+
*
24+
* @Get("/locations")
25+
* @Versions({"v1"})
26+
* @Response(200)
27+
*/
28+
public function getLocations(Request $request)
29+
{
30+
return Location::all();
31+
}
32+
33+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\API;
4+
5+
use Illuminate\Http\Request;
6+
7+
use Pterodactyl\Models;
8+
use Pterodactyl\Transformers\NodeTransformer;
9+
use Pterodactyl\Repositories\NodeRepository;
10+
11+
use Pterodactyl\Exceptions\DisplayValidationException;
12+
use Pterodactyl\Exceptions\DisplayException;
13+
use Dingo\Api\Exception\ResourceException;
14+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16+
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
17+
18+
/**
19+
* @Resource("Servers")
20+
*/
21+
class NodeController extends BaseController
22+
{
23+
24+
public function __construct()
25+
{
26+
//
27+
}
28+
29+
/**
30+
* List All Nodes
31+
*
32+
* Lists all nodes currently on the system.
33+
*
34+
* @Get("/nodes/{?page}")
35+
* @Versions({"v1"})
36+
* @Parameters({
37+
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
38+
* })
39+
* @Response(200)
40+
*/
41+
public function getNodes(Request $request)
42+
{
43+
$nodes = Models\Node::paginate(15);
44+
return $this->response->paginator($nodes, new NodeTransformer);
45+
}
46+
47+
/**
48+
* Create a New Node
49+
*
50+
* @Post("/nodes")
51+
* @Versions({"v1"})
52+
* @Transaction({
53+
* @Request({
54+
* 'name' => 'My API Node',
55+
* 'location' => 1,
56+
* 'public' => 1,
57+
* 'fqdn' => 'daemon.wuzzle.woo',
58+
* 'scheme' => 'https',
59+
* 'memory' => 10240,
60+
* 'memory_overallocate' => 100,
61+
* 'disk' => 204800,
62+
* 'disk_overallocate' => -1,
63+
* 'daemonBase' => '/srv/daemon-data',
64+
* 'daemonSFTP' => 2022,
65+
* 'daemonListen' => 8080
66+
* }, headers={"Authorization": "Bearer <jwt-token>"}),
67+
* @Response(201),
68+
* @Response(422, body={
69+
* "message": "A validation error occured.",
70+
* "errors": {},
71+
* "status_code": 422
72+
* }),
73+
* @Response(503, body={
74+
* "message": "There was an error while attempting to add this node to the system.",
75+
* "status_code": 503
76+
* })
77+
* })
78+
*/
79+
public function postNode(Request $request)
80+
{
81+
try {
82+
$node = new NodeRepository;
83+
$new = $node->create($request->all());
84+
return $this->response->created(route('api.nodes.view', [
85+
'id' => $new
86+
]));
87+
} catch (DisplayValidationException $ex) {
88+
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
89+
} catch (DisplayException $ex) {
90+
throw new ResourceException($ex->getMessage());
91+
} catch (\Exception $e) {
92+
throw new BadRequestHttpException('There was an error while attempting to add this node to the system.');
93+
}
94+
}
95+
96+
/**
97+
* List Specific Node
98+
*
99+
* Lists specific fields about a server or all fields pertaining to that node.
100+
*
101+
* @Get("/nodes/{id}/{?fields}")
102+
* @Versions({"v1"})
103+
* @Parameters({
104+
* @Parameter("id", type="integer", required=true, description="The ID of the node to get information on."),
105+
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
106+
* })
107+
* @Response(200)
108+
*/
109+
public function getNode(Request $request, $id, $fields = null)
110+
{
111+
$query = Models\Node::where('id', $id);
112+
113+
if (!is_null($request->input('fields'))) {
114+
foreach(explode(',', $request->input('fields')) as $field) {
115+
if (!empty($field)) {
116+
$query->addSelect($field);
117+
}
118+
}
119+
}
120+
121+
try {
122+
if (!$query->first()) {
123+
throw new NotFoundHttpException('No node by that ID was found.');
124+
}
125+
return $query->first();
126+
} catch (NotFoundHttpException $ex) {
127+
throw $ex;
128+
} catch (\Exception $ex) {
129+
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
130+
}
131+
}
132+
133+
/**
134+
* List Node Allocations
135+
*
136+
* Returns a listing of all node allocations.
137+
*
138+
* @Get("/nodes/{id}/allocations")
139+
* @Versions({"v1"})
140+
* @Parameters({
141+
* @Parameter("id", type="integer", required=true, description="The ID of the node to get allocations for."),
142+
* })
143+
* @Response(200)
144+
*/
145+
public function getNodeAllocations(Request $request, $id)
146+
{
147+
$allocations = Models\Allocation::select('ip', 'port', 'assigned_to')->where('node', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get();
148+
if ($allocations->count() < 1) {
149+
throw new NotFoundHttpException('No allocations where found for the requested node.');
150+
}
151+
return $allocations;
152+
}
153+
154+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\API;
4+
5+
use Illuminate\Http\Request;
6+
7+
use Pterodactyl\Models;
8+
use Pterodactyl\Transformers\ServerTransformer;
9+
use Pterodactyl\Repositories\ServerRepository;
10+
11+
use Pterodactyl\Exceptions\DisplayValidationException;
12+
use Pterodactyl\Exceptions\DisplayException;
13+
use Dingo\Api\Exception\ResourceException;
14+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16+
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
17+
18+
/**
19+
* @Resource("Servers")
20+
*/
21+
class ServerController extends BaseController
22+
{
23+
24+
public function __construct()
25+
{
26+
//
27+
}
28+
29+
/**
30+
* List All Servers
31+
*
32+
* Lists all servers currently on the system.
33+
*
34+
* @Get("/servers/{?page}")
35+
* @Versions({"v1"})
36+
* @Parameters({
37+
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
38+
* })
39+
* @Response(200)
40+
*/
41+
public function getServers(Request $request)
42+
{
43+
$servers = Models\Server::paginate(15);
44+
return $this->response->paginator($servers, new ServerTransformer);
45+
}
46+
47+
/**
48+
* List Specific Server
49+
*
50+
* Lists specific fields about a server or all fields pertaining to that server.
51+
*
52+
* @Get("/servers/{id}/{fields}")
53+
* @Versions({"v1"})
54+
* @Parameters({
55+
* @Parameter("id", type="integer", required=true, description="The ID of the server to get information on."),
56+
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
57+
* })
58+
* @Response(200)
59+
*/
60+
public function getServer(Request $request, $id)
61+
{
62+
$query = Models\Server::where('id', $id);
63+
64+
if (!is_null($request->input('fields'))) {
65+
foreach(explode(',', $request->input('fields')) as $field) {
66+
if (!empty($field)) {
67+
$query->addSelect($field);
68+
}
69+
}
70+
}
71+
72+
try {
73+
if (!$query->first()) {
74+
throw new NotFoundHttpException('No server by that ID was found.');
75+
}
76+
return $query->first();
77+
} catch (NotFoundHttpException $ex) {
78+
throw $ex;
79+
} catch (\Exception $ex) {
80+
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
81+
}
82+
}
83+
84+
}

app/Http/Controllers/API/UserController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function getUsers(Request $request)
5353
* })
5454
* @Response(200)
5555
*/
56-
public function getUser(Request $request, $id, $fields = null)
56+
public function getUser(Request $request, $id)
5757
{
5858
$query = Models\User::where('id', $id);
5959

60-
if (!is_null($fields)) {
61-
foreach(explode(',', $fields) as $field) {
60+
if (!is_null($request->input('fields'))) {
61+
foreach(explode(',', $request->input('fields')) as $field) {
6262
if (!empty($field)) {
6363
$query->addSelect($field);
6464
}

app/Http/Routes/APIRoutes.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function map(Router $router) {
3030
});
3131

3232
$api->version('v1', ['middleware' => 'api.auth'], function ($api) {
33+
34+
/**
35+
* User Routes
36+
*/
3337
$api->get('users', [
3438
'as' => 'api.users',
3539
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@getUsers'
@@ -40,20 +44,65 @@ public function map(Router $router) {
4044
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@postUser'
4145
]);
4246

43-
$api->get('users/{id}/{fields?}', [
47+
$api->get('users/{id}', [
4448
'as' => 'api.users.view',
4549
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@getUser'
4650
]);
4751

48-
$api->patch('users/{id}/', [
52+
$api->patch('users/{id}', [
4953
'as' => 'api.users.patch',
5054
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@patchUser'
5155
]);
5256

53-
$api->delete('users/{id}/', [
57+
$api->delete('users/{id}', [
5458
'as' => 'api.users.delete',
5559
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@deleteUser'
5660
]);
61+
62+
/**
63+
* Server Routes
64+
*/
65+
$api->get('servers', [
66+
'as' => 'api.servers',
67+
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@getServers'
68+
]);
69+
70+
$api->get('servers/{id}', [
71+
'as' => 'api.servers.view',
72+
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@getServer'
73+
]);
74+
75+
/**
76+
* Node Routes
77+
*/
78+
$api->get('nodes', [
79+
'as' => 'api.nodes',
80+
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@getNodes'
81+
]);
82+
83+
$api->post('nodes', [
84+
'as' => 'api.nodes.post',
85+
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@postNode'
86+
]);
87+
88+
$api->get('nodes/{id}', [
89+
'as' => 'api.nodes.view',
90+
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@getNode'
91+
]);
92+
93+
$api->get('nodes/{id}/allocations', [
94+
'as' => 'api.nodes.view',
95+
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@getNodeAllocations'
96+
]);
97+
98+
/**
99+
* Location Routes
100+
*/
101+
$api->get('locations', [
102+
'as' => 'api.locations',
103+
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@getLocations'
104+
]);
105+
57106
});
58107
}
59108

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Pterodactyl\Transformers;
4+
5+
use Pterodactyl\Models\Node;
6+
use League\Fractal\TransformerAbstract;
7+
8+
class NodeTransformer extends TransformerAbstract
9+
{
10+
11+
/**
12+
* Turn this item object into a generic array
13+
*
14+
* @return array
15+
*/
16+
public function transform(Node $node)
17+
{
18+
return $node;
19+
}
20+
21+
}

0 commit comments

Comments
 (0)