|
25 | 25 | namespace Pterodactyl\Http\Controllers\API; |
26 | 26 |
|
27 | 27 | use Log; |
28 | | -use Pterodactyl\Models; |
29 | 28 | use Illuminate\Http\Request; |
| 29 | +use Pterodactyl\Models\Node; |
| 30 | +use Pterodactyl\Models\Allocation; |
30 | 31 | use Dingo\Api\Exception\ResourceException; |
31 | 32 | use Pterodactyl\Exceptions\DisplayException; |
32 | 33 | use Pterodactyl\Repositories\NodeRepository; |
|
35 | 36 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
36 | 37 | use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
37 | 38 |
|
38 | | -/** |
39 | | - * @Resource("Servers") |
40 | | - */ |
41 | 39 | class NodeController extends BaseController |
42 | 40 | { |
43 | | - public function __construct() |
44 | | - { |
45 | | - // |
46 | | - } |
47 | | - |
48 | 41 | /** |
49 | | - * List All Nodes. |
50 | | - * |
51 | 42 | * Lists all nodes currently on the system. |
52 | 43 | * |
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 |
59 | 46 | */ |
60 | | - public function lists(Request $request) |
| 47 | + public function index(Request $request) |
61 | 48 | { |
62 | | - return Models\Node::all()->toArray(); |
| 49 | + return Node::all()->toArray(); |
63 | 50 | } |
64 | 51 |
|
65 | 52 | /** |
66 | | - * Create a New Node. |
| 53 | + * Create a new node. |
67 | 54 | * |
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 |
96 | 60 | */ |
97 | 61 | public function create(Request $request) |
98 | 62 | { |
| 63 | + $repo = new NodeRepository; |
| 64 | + |
99 | 65 | 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]; |
109 | 78 | } catch (DisplayValidationException $ex) { |
110 | 79 | throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); |
111 | 80 | } catch (DisplayException $ex) { |
112 | 81 | throw new ResourceException($ex->getMessage()); |
113 | 82 | } catch (\Exception $ex) { |
114 | 83 | 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.'); |
116 | 85 | } |
117 | 86 | } |
118 | 87 |
|
119 | 88 | /** |
120 | | - * List Specific Node. |
121 | | - * |
122 | 89 | * Lists specific fields about a server or all fields pertaining to that node. |
123 | 90 | * |
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 |
131 | 95 | */ |
132 | 96 | public function view(Request $request, $id, $fields = null) |
133 | 97 | { |
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); |
138 | 99 |
|
139 | 100 | $node->allocations->transform(function ($item) { |
140 | 101 | return collect($item)->only([ |
141 | 102 | 'id', 'ip', 'ip_alias', 'port', 'server_id', |
142 | 103 | ]); |
143 | 104 | }); |
144 | 105 |
|
145 | | - if (! is_null($request->input('fields'))) { |
| 106 | + if (! empty($request->input('fields'))) { |
146 | 107 | $fields = explode(',', $request->input('fields')); |
147 | 108 | if (! empty($fields) && is_array($fields)) { |
148 | 109 | return collect($node)->only($fields); |
149 | 110 | } |
150 | 111 | } |
151 | 112 |
|
152 | | - return $node; |
| 113 | + return $node->toArray(); |
153 | 114 | } |
154 | 115 |
|
| 116 | + /** |
| 117 | + * Returns a configuration file for a given node. |
| 118 | + * |
| 119 | + * @param Request $request |
| 120 | + * @param int $id |
| 121 | + * @return array |
| 122 | + */ |
155 | 123 | public function config(Request $request, $id) |
156 | 124 | { |
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); |
161 | 126 |
|
162 | 127 | return $node->getConfigurationAsJson(); |
163 | 128 | } |
164 | 129 |
|
165 | 130 | /** |
166 | | - * List all Node Allocations. |
167 | | - * |
168 | 131 | * Returns a listing of all allocations for every node. |
169 | 132 | * |
170 | | - * @Get("/nodes/allocations") |
171 | | - * @Versions({"v1"}) |
172 | | - * @Response(200) |
| 133 | + * @param Request $request |
| 134 | + * @return array |
173 | 135 | */ |
174 | 136 | public function allocations(Request $request) |
175 | 137 | { |
176 | | - return Models\Allocation::all()->toArray(); |
| 138 | + return Allocation::all()->toArray(); |
177 | 139 | } |
178 | 140 |
|
179 | 141 | /** |
180 | | - * List Node Allocation based on assigned to ID. |
181 | | - * |
182 | 142 | * Returns a listing of the allocation for the specified server id. |
183 | 143 | * |
184 | | - * @Get("/nodes/allocations/{id}") |
185 | | - * @Versions({"v1"}) |
186 | | - * @Response(200) |
| 144 | + * @param Request $request |
| 145 | + * @return array |
187 | 146 | */ |
188 | 147 | public function allocationsView(Request $request, $id) |
189 | 148 | { |
190 | | - return Models\Allocation::where('server_id', $id)->get()->toArray(); |
| 149 | + return Allocation::where('server_id', $id)->get()->toArray(); |
191 | 150 | } |
192 | 151 |
|
193 | 152 | /** |
194 | | - * Delete Node. |
| 153 | + * Delete a node. |
195 | 154 | * |
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 |
202 | 158 | */ |
203 | 159 | public function delete(Request $request, $id) |
204 | 160 | { |
| 161 | + $repo = new NodeRepository; |
205 | 162 | try { |
206 | | - $node = new NodeRepository; |
207 | | - $node->delete($id); |
| 163 | + $repo->delete($id); |
208 | 164 |
|
209 | 165 | return $this->response->noContent(); |
210 | 166 | } catch (DisplayException $ex) { |
|
0 commit comments