1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Http\Controllers\API; |
26
|
|
|
|
27
|
|
|
use Log; |
28
|
|
|
use Pterodactyl\Models; |
29
|
|
|
use Illuminate\Http\Request; |
30
|
|
|
use Dingo\Api\Exception\ResourceException; |
31
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
32
|
|
|
use Pterodactyl\Repositories\NodeRepository; |
33
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
34
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
35
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
36
|
|
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @Resource("Servers") |
40
|
|
|
*/ |
41
|
|
|
class NodeController extends BaseController |
42
|
|
|
{ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
// |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* List All Nodes. |
50
|
|
|
* |
51
|
|
|
* Lists all nodes currently on the system. |
52
|
|
|
* |
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) |
59
|
|
|
*/ |
60
|
|
|
public function lists(Request $request) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return Models\Node::all()->toArray(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create a New Node. |
67
|
|
|
* |
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
|
|
|
* }) |
96
|
|
|
*/ |
97
|
|
|
public function create(Request $request) |
98
|
|
|
{ |
99
|
|
|
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]; |
|
|
|
|
109
|
|
|
} catch (DisplayValidationException $ex) { |
110
|
|
|
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); |
111
|
|
|
} catch (DisplayException $ex) { |
112
|
|
|
throw new ResourceException($ex->getMessage()); |
113
|
|
|
} catch (\Exception $ex) { |
114
|
|
|
Log::error($ex); |
115
|
|
|
throw new BadRequestHttpException('There was an error while attempting to add this node to the system.'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* List Specific Node. |
121
|
|
|
* |
122
|
|
|
* Lists specific fields about a server or all fields pertaining to that node. |
123
|
|
|
* |
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) |
131
|
|
|
*/ |
132
|
|
|
public function view(Request $request, $id, $fields = null) |
|
|
|
|
133
|
|
|
{ |
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
|
|
|
} |
138
|
|
|
|
139
|
|
|
$node->allocations->transform(function ($item) { |
140
|
|
|
return collect($item)->only([ |
141
|
|
|
'id', 'ip', 'ip_alias', 'port', 'server_id', |
142
|
|
|
]); |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
View Code Duplication |
if (! is_null($request->input('fields'))) { |
|
|
|
|
146
|
|
|
$fields = explode(',', $request->input('fields')); |
147
|
|
|
if (! empty($fields) && is_array($fields)) { |
148
|
|
|
return collect($node)->only($fields); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $node; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function config(Request $request, $id) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$node = Models\Node::where('id', $id)->first(); |
158
|
|
|
if (! $node) { |
159
|
|
|
throw new NotFoundHttpException('No node by that ID was found.'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $node->getConfigurationAsJson(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* List all Node Allocations. |
167
|
|
|
* |
168
|
|
|
* Returns a listing of all allocations for every node. |
169
|
|
|
* |
170
|
|
|
* @Get("/nodes/allocations") |
171
|
|
|
* @Versions({"v1"}) |
172
|
|
|
* @Response(200) |
173
|
|
|
*/ |
174
|
|
|
public function allocations(Request $request) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
return Models\Allocation::all()->toArray(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* List Node Allocation based on assigned to ID. |
181
|
|
|
* |
182
|
|
|
* Returns a listing of the allocation for the specified server id. |
183
|
|
|
* |
184
|
|
|
* @Get("/nodes/allocations/{id}") |
185
|
|
|
* @Versions({"v1"}) |
186
|
|
|
* @Response(200) |
187
|
|
|
*/ |
188
|
|
|
public function allocationsView(Request $request, $id) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
return Models\Allocation::where('assigned_to', $id)->get()->toArray(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Delete Node. |
195
|
|
|
* |
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) |
202
|
|
|
*/ |
203
|
|
View Code Duplication |
public function delete(Request $request, $id) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
try { |
206
|
|
|
$node = new NodeRepository; |
207
|
|
|
$node->delete($id); |
208
|
|
|
|
209
|
|
|
return $this->response->noContent(); |
210
|
|
|
} catch (DisplayException $ex) { |
211
|
|
|
throw new ResourceException($ex->getMessage()); |
212
|
|
|
} catch (\Exception $e) { |
213
|
|
|
throw new ServiceUnavailableHttpException('An error occured while attempting to delete this node.'); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.