2424
2525namespace Pterodactyl \Http \Controllers \API ;
2626
27+ use Log ;
2728use Pterodactyl \Models ;
2829use Illuminate \Http \Request ;
2930use Dingo \Api \Exception \ResourceException ;
@@ -96,15 +97,21 @@ public function lists(Request $request)
9697 public function create (Request $ request )
9798 {
9899 try {
99- $ node = new NodeRepository ;
100- $ new = $ node ->create ($ request ->all ());
101-
102- return ['id ' => $ new ];
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 ];
103109 } catch (DisplayValidationException $ ex ) {
104110 throw new ResourceException ('A validation error occured. ' , json_decode ($ ex ->getMessage (), true ));
105111 } catch (DisplayException $ ex ) {
106112 throw new ResourceException ($ ex ->getMessage ());
107- } catch (\Exception $ e ) {
113+ } catch (\Exception $ ex ) {
114+ Log::error ($ ex );
108115 throw new BadRequestHttpException ('There was an error while attempting to add this node to the system. ' );
109116 }
110117 }
@@ -124,88 +131,35 @@ public function create(Request $request)
124131 */
125132 public function view (Request $ request , $ id , $ fields = null )
126133 {
127- $ node = Models \Node::where ('id ' , $ id );
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+ });
128144
129145 if (! is_null ($ request ->input ('fields ' ))) {
130- foreach (explode (', ' , $ request ->input ('fields ' )) as $ field ) {
131- if (! empty ($ field )) {
132- $ node ->addSelect ($ field );
133- }
146+ $ fields = explode (', ' , $ request ->input ('fields ' ));
147+ if (! empty ($ fields ) && is_array ($ fields )) {
148+ return collect ($ node )->only ($ fields );
134149 }
135150 }
136151
137- try {
138- if (! $ node ->first ()) {
139- throw new NotFoundHttpException ('No node by that ID was found. ' );
140- }
141-
142- return [
143- 'node ' => $ node ->first (),
144- 'allocations ' => [
145- 'assigned ' => Models \Allocation::where ('node ' , $ id )->whereNotNull ('assigned_to ' )->get (),
146- 'unassigned ' => Models \Allocation::where ('node ' , $ id )->whereNull ('assigned_to ' )->get (),
147- ],
148- ];
149- } catch (NotFoundHttpException $ ex ) {
150- throw $ ex ;
151- } catch (\Exception $ ex ) {
152- throw new BadRequestHttpException ('There was an issue with the fields passed in the request. ' );
153- }
152+ return $ node ;
154153 }
155154
156155 public function config (Request $ request , $ id )
157156 {
158- if (! $ request ->secure ()) {
159- throw new BadRequestHttpException ('This API route can only be accessed using a secure connection. ' );
160- }
161-
162157 $ node = Models \Node::where ('id ' , $ id )->first ();
163158 if (! $ node ) {
164159 throw new NotFoundHttpException ('No node by that ID was found. ' );
165160 }
166161
167- return [
168- 'web ' => [
169- 'listen ' => $ node ->daemonListen ,
170- 'host ' => '0.0.0.0 ' ,
171- 'ssl ' => [
172- 'enabled ' => ($ node ->scheme === 'https ' ),
173- 'certificate ' => '/etc/certs/ ' . $ node ->fqdn . '/fullchain.pem ' ,
174- 'key ' => '/etc/certs/ ' . $ node ->fqdn . '/privkey.pem ' ,
175- ],
176- ],
177- 'docker ' => [
178- 'socket ' => '/var/run/docker.sock ' ,
179- 'autoupdate_images ' => true ,
180- ],
181- 'sftp ' => [
182- 'path ' => $ node ->daemonBase ,
183- 'port ' => (int ) $ node ->daemonSFTP ,
184- 'container ' => 'ptdl-sftp ' ,
185- ],
186- 'query ' => [
187- 'kill_on_fail ' => true ,
188- 'fail_limit ' => 5 ,
189- ],
190- 'logger ' => [
191- 'path ' => 'logs/ ' ,
192- 'src ' => false ,
193- 'level ' => 'info ' ,
194- 'period ' => '1d ' ,
195- 'count ' => 3 ,
196- ],
197- 'remote ' => [
198- 'base ' => config ('app.url ' ),
199- 'download ' => route ('remote.download ' ),
200- 'installed ' => route ('remote.install ' ),
201- ],
202- 'uploads ' => [
203- 'size_limit ' => $ node ->upload_size ,
204- ],
205- 'keys ' => [
206- $ node ->daemonSecret ,
207- ],
208- ];
162+ return $ node ->getConfigurationAsJson ();
209163 }
210164
211165 /**
@@ -219,12 +173,7 @@ public function config(Request $request, $id)
219173 */
220174 public function allocations (Request $ request )
221175 {
222- $ allocations = Models \Allocation::all ();
223- if ($ allocations ->count () < 1 ) {
224- throw new NotFoundHttpException ('No allocations have been created. ' );
225- }
226-
227- return $ allocations ;
176+ return Models \Allocation::all ()->toArray ();
228177 }
229178
230179 /**
@@ -238,18 +187,7 @@ public function allocations(Request $request)
238187 */
239188 public function allocationsView (Request $ request , $ id )
240189 {
241- $ query = Models \Allocation::where ('assigned_to ' , $ id )->get ();
242- try {
243- if (empty ($ query )) {
244- throw new NotFoundHttpException ('No allocations for that server were found. ' );
245- }
246-
247- return $ query ;
248- } catch (NotFoundHttpException $ ex ) {
249- throw $ ex ;
250- } catch (\Exception $ ex ) {
251- throw new BadRequestHttpException ('There was an issue with the fields passed in the request. ' );
252- }
190+ return Models \Allocation::where ('assigned_to ' , $ id )->get ()->toArray ();
253191 }
254192
255193 /**
0 commit comments