2929use Alert ;
3030use Carbon ;
3131use Validator ;
32+ use Javascript ;
3233use Pterodactyl \Models ;
3334use Illuminate \Http \Request ;
3435use Pterodactyl \Exceptions \DisplayException ;
@@ -55,9 +56,13 @@ public function getScript(Request $request, $id)
5556
5657 public function getIndex (Request $ request )
5758 {
58- return view ('admin.nodes.index ' , [
59- 'nodes ' => Models \Node::with ('location ' )->withCount ('servers ' )->paginate (20 ),
60- ]);
59+ $ nodes = Models \Node::with ('location ' )->withCount ('servers ' );
60+
61+ if (! is_null ($ request ->input ('query ' ))) {
62+ $ nodes ->search ($ request ->input ('query ' ));
63+ }
64+
65+ return view ('admin.nodes.index ' , ['nodes ' => $ nodes ->paginate (25 )]);
6166 }
6267
6368 public function getNew (Request $ request )
@@ -117,6 +122,105 @@ public function getView(Request $request, $id)
117122 ]);
118123 }
119124
125+ /**
126+ * Shows the index overview page for a specific node.
127+ *
128+ * @param Request $request
129+ * @param int $id The ID of the node to display information for.
130+ *
131+ * @return \Illuminate\View\View
132+ */
133+ public function viewIndex (Request $ request , $ id )
134+ {
135+ $ node = Models \Node::with ('location ' )->withCount ('servers ' )->findOrFail ($ id );
136+ $ stats = collect (
137+ Models \Server::select (
138+ DB ::raw ('SUM(memory) as memory, SUM(disk) as disk ' )
139+ )->where ('node_id ' , $ node ->id )->first ()
140+ )->mapWithKeys (function ($ item , $ key ) use ($ node ) {
141+ $ percent = ($ item / $ node ->{$ key }) * 100 ;
142+
143+ return [$ key => [
144+ 'value ' => $ item ,
145+ 'percent ' => $ percent ,
146+ 'css ' => ($ percent <= 75 ) ? 'green ' : (($ percent > 90 ) ? 'red ' : 'yellow ' ),
147+ ]];
148+ })->toArray ();
149+
150+ return view ('admin.nodes.view.index ' , ['node ' => $ node , 'stats ' => $ stats ]);
151+ }
152+
153+ /**
154+ * Shows the settings page for a specific node.
155+ *
156+ * @param Request $request
157+ * @param int $id The ID of the node to display information for.
158+ *
159+ * @return \Illuminate\View\View
160+ */
161+ public function viewSettings (Request $ request , $ id )
162+ {
163+ return view ('admin.nodes.view.settings ' , [
164+ 'node ' => Models \Node::findOrFail ($ id ),
165+ 'locations ' => Models \Location::all (),
166+ ]);
167+ }
168+
169+ /**
170+ * Shows the configuration page for a specific node.
171+ *
172+ * @param Request $request
173+ * @param int $id The ID of the node to display information for.
174+ *
175+ * @return \Illuminate\View\View
176+ */
177+ public function viewConfiguration (Request $ request , $ id )
178+ {
179+ return view ('admin.nodes.view.configuration ' , [
180+ 'node ' => Models \Node::findOrFail ($ id ),
181+ ]);
182+ }
183+
184+ /**
185+ * Shows the allocation page for a specific node.
186+ *
187+ * @param Request $request
188+ * @param int $id The ID of the node to display information for.
189+ *
190+ * @return \Illuminate\View\View
191+ */
192+ public function viewAllocation (Request $ request , $ id )
193+ {
194+ $ node = Models \Node::findOrFail ($ id );
195+ $ node ->setRelation ('allocations ' , $ node ->allocations ()->orderBy ('ip ' , 'asc ' )->orderBy ('port ' , 'asc ' )->with ('server ' )->paginate (50 ));
196+
197+ Javascript::put ([
198+ 'node ' => collect ($ node )->only (['id ' ]),
199+ ]);
200+
201+ return view ('admin.nodes.view.allocation ' , ['node ' => $ node ]);
202+ }
203+
204+ /**
205+ * Shows the server listing page for a specific node.
206+ *
207+ * @param Request $request
208+ * @param int $id The ID of the node to display information for.
209+ *
210+ * @return \Illuminate\View\View
211+ */
212+ public function viewServers (Request $ request , $ id )
213+ {
214+ $ node = Models \Node::with ('servers.user ' , 'servers.service ' , 'servers.allocations ' )->findOrFail ($ id );
215+ Javascript::put ([
216+ 'node ' => collect ($ node ->makeVisible ('daemonSecret ' ))->only (['scheme ' , 'fqdn ' , 'daemonListen ' , 'daemonSecret ' ]),
217+ ]);
218+
219+ return view ('admin.nodes.view.servers ' , [
220+ 'node ' => $ node ,
221+ ]);
222+ }
223+
120224 public function postView (Request $ request , $ id )
121225 {
122226 try {
@@ -149,10 +253,18 @@ public function postView(Request $request, $id)
149253 ])->withInput ();
150254 }
151255
152- public function deallocateSingle (Request $ request , $ node , $ allocation )
256+ /**
257+ * Removes a single allocation from a node.
258+ *
259+ * @param Request $request
260+ * @param integer $node
261+ * @param integer $allocation [description]
262+ * @return mixed
263+ */
264+ public function allocationRemoveSingle (Request $ request , $ node , $ allocation )
153265 {
154266 $ query = Models \Allocation::where ('node_id ' , $ node )->whereNull ('server_id ' )->where ('id ' , $ allocation )->delete ();
155- if (( int ) $ query === 0 ) {
267+ if ($ query < 1 ) {
156268 return response ()->json ([
157269 'error ' => 'Unable to find an allocation matching those details to delete. ' ,
158270 ], 400 );
@@ -161,33 +273,40 @@ public function deallocateSingle(Request $request, $node, $allocation)
161273 return response ('' , 204 );
162274 }
163275
164- public function deallocateBlock (Request $ request , $ node )
276+ /**
277+ * Remove all allocations for a specific IP at once on a node.
278+ *
279+ * @param Request $request
280+ * @param integer $node
281+ * @return mixed
282+ */
283+ public function allocationRemoveBlock (Request $ request , $ node )
165284 {
166285 $ query = Models \Allocation::where ('node_id ' , $ node )->whereNull ('server_id ' )->where ('ip ' , $ request ->input ('ip ' ))->delete ();
167- if (( int ) $ query === 0 ) {
286+ if ($ query < 1 ) {
168287 Alert::danger ('There was an error while attempting to delete allocations on that IP. ' )->flash ();
169-
170- return redirect ()->route ('admin.nodes.view ' , [
171- 'id ' => $ node ,
172- 'tab ' => 'tab_allocations ' ,
173- ]);
288+ } else {
289+ Alert::success ('Deleted all unallocated ports for <code> ' . $ request ->input ('ip ' ) . '</code>. ' )->flash ();
174290 }
175- Alert::success ('Deleted all unallocated ports for <code> ' . $ request ->input ('ip ' ) . '</code>. ' )->flash ();
176291
177- return redirect ()->route ('admin.nodes.view ' , [
178- 'id ' => $ node ,
179- 'tab ' => 'tab_allocation ' ,
180- ]);
292+ return redirect ()->route ('admin.nodes.view.allocation ' , $ node );
181293 }
182294
183- public function setAlias (Request $ request , $ node )
295+ /**
296+ * Sets an alias for a specific allocation on a node.
297+ *
298+ * @param Request $request
299+ * @param integer $node
300+ * @return mixed
301+ */
302+ public function allocationSetAlias (Request $ request , $ node )
184303 {
185- if (! $ request ->input ('allocation ' )) {
304+ if (! $ request ->input ('allocation_id ' )) {
186305 return response ('Missing required parameters. ' , 422 );
187306 }
188307
189308 try {
190- $ update = Models \Allocation::findOrFail ($ request ->input ('allocation ' ));
309+ $ update = Models \Allocation::findOrFail ($ request ->input ('allocation_id ' ));
191310 $ update ->ip_alias = (empty ($ request ->input ('alias ' ))) ? null : $ request ->input ('alias ' );
192311 $ update ->save ();
193312
@@ -197,60 +316,37 @@ public function setAlias(Request $request, $node)
197316 }
198317 }
199318
200- public function getAllocationsJson ( Request $ request , $ id )
201- {
202- $ allocations = Models \Allocation:: select ( ' ip ' )-> where ( ' node_id ' , $ id )-> groupBy ( ' ip ' )-> get ();
203-
204- return response ()-> json ( $ allocations );
205- }
206-
207- public function postAllocations (Request $ request , $ id )
319+ /**
320+ * Creates new allocations on a node.
321+ *
322+ * @param Request $request
323+ * @param integer $node
324+ * @return \Illuminate\Http\RedirectResponse
325+ */
326+ public function createAllocation (Request $ request , $ node )
208327 {
209- $ validator = Validator::make ($ request ->all (), [
210- 'allocate_ip.* ' => 'required|string ' ,
211- 'allocate_port.* ' => 'required ' ,
212- ]);
328+ $ repo = new NodeRepository ;
213329
214- if ($ validator ->fails ()) {
215- return redirect ()->route ('admin.nodes.view ' , [
216- 'id ' => $ id ,
217- 'tab ' => 'tab_allocation ' ,
218- ])->withErrors ($ validator ->errors ())->withInput ();
330+ try {
331+ $ repo ->addAllocations ($ node , $ request ->intersect (['allocation_ip ' , 'allocation_alias ' , 'allocation_ports ' ]));
332+ Alert::success ('Successfully added new allocations! ' )->flash ();
333+ } catch (DisplayValidationException $ ex ) {
334+ return redirect ()->route ('admin.nodes.view.allocation ' , $ node )->withErrors (json_decode ($ ex ->getMessage ()))->withInput ();
335+ } catch (DisplayException $ ex ) {
336+ Alert::danger ($ ex ->getMessage ())->flash ();
337+ } catch (\Exception $ ex ) {
338+ Log::error ($ ex );
339+ Alert::danger ('An unhandled exception occured while attempting to add allocations this node. This error has been logged. ' )->flash ();
219340 }
220341
221- $ processedData = [];
222- foreach ($ request ->input ('allocate_ip ' ) as $ ip ) {
223- if (! array_key_exists ($ ip , $ processedData )) {
224- $ processedData [$ ip ] = [];
225- }
226- }
342+ return redirect ()->route ('admin.nodes.view.allocation ' , $ node );
343+ }
227344
228- foreach ($ request ->input ('allocate_port ' ) as $ portid => $ ports ) {
229- if (array_key_exists ($ portid , $ request ->input ('allocate_ip ' ))) {
230- $ json = json_decode ($ ports );
231- if (json_last_error () === 0 && ! empty ($ json )) {
232- foreach ($ json as &$ parsed ) {
233- array_push ($ processedData [$ request ->input ('allocate_ip ' )[$ portid ]], $ parsed ->value );
234- }
235- }
236- }
237- }
345+ public function getAllocationsJson (Request $ request , $ id )
346+ {
347+ $ allocations = Models \Allocation::select ('ip ' )->where ('node_id ' , $ id )->groupBy ('ip ' )->get ();
238348
239- try {
240- $ node = new NodeRepository ;
241- $ node ->addAllocations ($ id , $ processedData );
242- Alert::success ('Successfully added new allocations to this node. ' )->flash ();
243- } catch (DisplayException $ e ) {
244- Alert::danger ($ e ->getMessage ())->flash ();
245- } catch (\Exception $ e ) {
246- Log::error ($ e );
247- Alert::danger ('An unhandled exception occured while attempting to add allocations this node. Please try again. ' )->flash ();
248- } finally {
249- return redirect ()->route ('admin.nodes.view ' , [
250- 'id ' => $ id ,
251- 'tab ' => 'tab_allocation ' ,
252- ]);
253- }
349+ return response ()->json ($ allocations );
254350 }
255351
256352 public function deleteNode (Request $ request , $ id )
0 commit comments