Skip to content

Commit ac65d5f

Browse files
committed
Finish base API.
Making PR, any additional API functions or modifications can be done within the repository now.
1 parent 77e3744 commit ac65d5f

File tree

8 files changed

+249
-6
lines changed

8 files changed

+249
-6
lines changed

app/Http/Controllers/API/LocationController.php

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

33
namespace Pterodactyl\Http\Controllers\API;
44

5+
use DB;
56
use Illuminate\Http\Request;
67
use Pterodactyl\Models\Location;
78

@@ -27,7 +28,16 @@ public function __construct()
2728
*/
2829
public function getLocations(Request $request)
2930
{
30-
return Location::all();
31+
$locations = Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes'))
32+
->join('nodes', 'locations.id', '=', 'nodes.location')
33+
->groupBy('locations.id')
34+
->get();
35+
36+
foreach($locations as &$location) {
37+
$location->nodes = explode(',', $location->nodes);
38+
}
39+
40+
return $locations;
3141
}
3242

3343
}

app/Http/Controllers/API/NodeController.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct()
4040
*/
4141
public function getNodes(Request $request)
4242
{
43-
$nodes = Models\Node::paginate(15);
43+
$nodes = Models\Node::paginate(50);
4444
return $this->response->paginator($nodes, new NodeTransformer);
4545
}
4646

@@ -151,4 +151,27 @@ public function getNodeAllocations(Request $request, $id)
151151
return $allocations;
152152
}
153153

154+
/**
155+
* Delete Node
156+
*
157+
* @Delete("/nodes/{id}")
158+
* @Versions({"v1"})
159+
* @Parameters({
160+
* @Parameter("id", type="integer", required=true, description="The ID of the node."),
161+
* })
162+
* @Response(204)
163+
*/
164+
public function deleteNode(Request $request, $id)
165+
{
166+
try {
167+
$node = new NodeRepository;
168+
$node->delete($id);
169+
return $this->response->noContent();
170+
} catch (DisplayException $ex) {
171+
throw new ResourceException($ex->getMessage());
172+
} catch(\Exception $e) {
173+
throw new ServiceUnavailableHttpException('An error occured while attempting to delete this node.');
174+
}
175+
}
176+
154177
}

app/Http/Controllers/API/ServerController.php

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,43 @@ public function __construct()
4040
*/
4141
public function getServers(Request $request)
4242
{
43-
$servers = Models\Server::paginate(15);
43+
$servers = Models\Server::paginate(50);
4444
return $this->response->paginator($servers, new ServerTransformer);
4545
}
4646

47+
/**
48+
* Create Server
49+
*
50+
* @Post("/servers")
51+
* @Versions({"v1"})
52+
* @Parameters({
53+
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
54+
* })
55+
* @Response(201)
56+
*/
57+
public function postServer(Request $request)
58+
{
59+
try {
60+
$server = new ServerRepository;
61+
$new = $server->create($request->all());
62+
return $this->response->created(route('api.servers.view', [
63+
'id' => $new
64+
]));
65+
} catch (DisplayValidationException $ex) {
66+
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
67+
} catch (DisplayException $ex) {
68+
throw new ResourceException($ex->getMessage());
69+
} catch (\Exception $e) {
70+
throw new BadRequestHttpException('There was an error while attempting to add this server to the system.');
71+
}
72+
}
73+
4774
/**
4875
* List Specific Server
4976
*
5077
* Lists specific fields about a server or all fields pertaining to that server.
5178
*
52-
* @Get("/servers/{id}/{fields}")
79+
* @Get("/servers/{id}{?fields}")
5380
* @Versions({"v1"})
5481
* @Parameters({
5582
* @Parameter("id", type="integer", required=true, description="The ID of the server to get information on."),
@@ -81,4 +108,72 @@ public function getServer(Request $request, $id)
81108
}
82109
}
83110

111+
/**
112+
* Suspend Server
113+
*
114+
* @Post("/servers/{id}/suspend")
115+
* @Versions({"v1"})
116+
* @Parameters({
117+
* @Parameter("id", type="integer", required=true, description="The ID of the server."),
118+
* })
119+
* @Response(204)
120+
*/
121+
public function postServerSuspend(Request $request, $id)
122+
{
123+
try {
124+
$server = new ServerRepository;
125+
$server->suspend($id);
126+
} catch (DisplayException $ex) {
127+
throw new ResourceException($ex->getMessage());
128+
} catch (\Exception $ex) {
129+
throw new ServiceUnavailableHttpException('An error occured while attempting to suspend this server instance.');
130+
}
131+
}
132+
133+
/**
134+
* Unsuspend Server
135+
*
136+
* @Post("/servers/{id}/unsuspend")
137+
* @Versions({"v1"})
138+
* @Parameters({
139+
* @Parameter("id", type="integer", required=true, description="The ID of the server."),
140+
* })
141+
* @Response(204)
142+
*/
143+
public function postServerUnsuspend(Request $request, $id)
144+
{
145+
try {
146+
$server = new ServerRepository;
147+
$server->unsuspend($id);
148+
} catch (DisplayException $ex) {
149+
throw new ResourceException($ex->getMessage());
150+
} catch (\Exception $ex) {
151+
throw new ServiceUnavailableHttpException('An error occured while attempting to unsuspend this server instance.');
152+
}
153+
}
154+
155+
/**
156+
* Delete Server
157+
*
158+
* @Delete("/servers/{id}/{force}")
159+
* @Versions({"v1"})
160+
* @Parameters({
161+
* @Parameter("id", type="integer", required=true, description="The ID of the server."),
162+
* @Parameter("force", type="string", required=false, description="Use 'force' if the server should be removed regardless of daemon response."),
163+
* })
164+
* @Response(204)
165+
*/
166+
public function deleteServer(Request $request, $id, $force = null)
167+
{
168+
try {
169+
$server = new ServerRepository;
170+
$server->deleteServer($id, $force);
171+
return $this->response->noContent();
172+
} catch (DisplayException $ex) {
173+
throw new ResourceException($ex->getMessage());
174+
} catch(\Exception $e) {
175+
throw new ServiceUnavailableHttpException('An error occured while attempting to delete this server.');
176+
}
177+
}
178+
84179
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\API;
4+
5+
use Illuminate\Http\Request;
6+
7+
use Pterodactyl\Models;
8+
use Pterodactyl\Transformers\ServiceTransformer;
9+
10+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11+
12+
/**
13+
* @Resource("Services")
14+
*/
15+
class ServiceController extends BaseController
16+
{
17+
18+
public function __construct()
19+
{
20+
//
21+
}
22+
23+
public function getServices(Request $request)
24+
{
25+
return Models\Service::all();
26+
}
27+
28+
public function getService(Request $request, $id)
29+
{
30+
$service = Models\Service::find($id);
31+
if (!$service) {
32+
throw new NotFoundHttpException('No service by that ID was found.');
33+
}
34+
35+
$options = Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')->where('parent_service', $service->id)->get();
36+
foreach($options as &$opt) {
37+
$opt->variables = Models\ServiceVariables::where('option_id', $opt->id)->get();
38+
}
39+
40+
return [
41+
'service' => $service,
42+
'options' => $options
43+
];
44+
45+
}
46+
47+
}

app/Http/Controllers/API/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class UserController extends BaseController
3636
*/
3737
public function getUsers(Request $request)
3838
{
39-
$users = Models\User::paginate(15);
39+
$users = Models\User::paginate(50);
4040
return $this->response->paginator($users, new UserTransformer);
4141
}
4242

app/Http/Routes/APIRoutes.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,31 @@ public function map(Router $router) {
4949
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@getServers'
5050
]);
5151

52+
$api->post('servers', [
53+
'as' => 'api.servers.post',
54+
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@postServer'
55+
]);
56+
5257
$api->get('servers/{id}', [
5358
'as' => 'api.servers.view',
5459
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@getServer'
5560
]);
5661

62+
$api->post('servers/{id}/suspend', [
63+
'as' => 'api.servers.suspend',
64+
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@postServerSuspend'
65+
]);
66+
67+
$api->post('servers/{id}/unsuspend', [
68+
'as' => 'api.servers.unsuspend',
69+
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@postServerUnsuspend'
70+
]);
71+
72+
$api->delete('servers/{id}/{force?}', [
73+
'as' => 'api.servers.delete',
74+
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@deleteServer'
75+
]);
76+
5777
/**
5878
* Node Routes
5979
*/
@@ -73,10 +93,15 @@ public function map(Router $router) {
7393
]);
7494

7595
$api->get('nodes/{id}/allocations', [
76-
'as' => 'api.nodes.view',
96+
'as' => 'api.nodes.view_allocations',
7797
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@getNodeAllocations'
7898
]);
7999

100+
$api->delete('nodes/{id}', [
101+
'as' => 'api.nodes.view',
102+
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@deleteNode'
103+
]);
104+
80105
/**
81106
* Location Routes
82107
*/
@@ -85,6 +110,19 @@ public function map(Router $router) {
85110
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@getLocations'
86111
]);
87112

113+
/**
114+
* Service Routes
115+
*/
116+
$api->get('services', [
117+
'as' => 'api.services',
118+
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@getServices'
119+
]);
120+
121+
$api->get('services/{id}', [
122+
'as' => 'api.services.view',
123+
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@getService'
124+
]);
125+
88126
});
89127
}
90128

app/Repositories/NodeRepository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,10 @@ public function addAllocations($id, array $allocations)
183183
}
184184
}
185185

186+
public function delete($id)
187+
{
188+
// @TODO: add logic;
189+
return true;
190+
}
191+
186192
}

app/Repositories/ServerRepository.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,4 +685,28 @@ public function toggleInstall($id)
685685
return $server->save();
686686
}
687687

688+
/**
689+
* Suspends a server instance making it unable to be booted or used by a user.
690+
* @param integer $id
691+
* @return boolean
692+
*/
693+
public function suspend($id)
694+
{
695+
// @TODO: Implement logic; not doing it now since that is outside of the
696+
// scope of this API brance.
697+
return true;
698+
}
699+
700+
/**
701+
* Unsuspends a server instance.
702+
* @param integer $id
703+
* @return boolean
704+
*/
705+
public function unsuspend($id)
706+
{
707+
// @TODO: Implement logic; not doing it now since that is outside of the
708+
// scope of this API brance.
709+
return true;
710+
}
711+
688712
}

0 commit comments

Comments
 (0)