From fb1b2406b55c6ce79df938dc841bedeeae54fbc7 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 24 Feb 2018 14:09:09 -0600 Subject: [PATCH] Add API endpoint to get a server by external ID --- CHANGELOG.md | 1 + .../Servers/ExternalServerController.php | 23 ++++++++ .../Servers/GetExternalServerRequest.php | 57 +++++++++++++++++++ routes/api-application.php | 1 + 4 files changed, 82 insertions(+) create mode 100644 app/Http/Controllers/Api/Application/Servers/ExternalServerController.php create mode 100644 app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b4edd9765..104256f65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Adds ability to include egg variables on an API request. * Added `external_id` column to servers that allows for easier linking with external services such as WHMCS. * Added back the sidebar when viewing servers that allows for quick-switching to a different server. +* Added API endpoint to get a server by external ID. ## v0.7.1 (Derelict Dermodactylus) ### Fixed diff --git a/app/Http/Controllers/Api/Application/Servers/ExternalServerController.php b/app/Http/Controllers/Api/Application/Servers/ExternalServerController.php new file mode 100644 index 000000000..391c5645c --- /dev/null +++ b/app/Http/Controllers/Api/Application/Servers/ExternalServerController.php @@ -0,0 +1,23 @@ +fractal->item($request->getServerModel()) + ->transformWith($this->getTransformer(ServerTransformer::class)) + ->toArray(); + } +} diff --git a/app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php b/app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php new file mode 100644 index 000000000..f783dc715 --- /dev/null +++ b/app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php @@ -0,0 +1,57 @@ +container->make(ServerRepositoryInterface::class); + + try { + $this->serverModel = $repository->findFirstWhere([ + ['external_id', '=', $this->route()->parameter('external_id')], + ]); + } catch (RecordNotFoundException $exception) { + return false; + } + + return true; + } + + /** + * Return the server model for the requested external server. + * + * @return \Pterodactyl\Models\Server + */ + public function getServerModel(): Server + { + return $this->serverModel; + } +} diff --git a/routes/api-application.php b/routes/api-application.php index 900de8c6a..9b3b4f073 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -74,6 +74,7 @@ Route::group(['prefix' => '/locations'], function () { Route::group(['prefix' => '/servers'], function () { Route::get('/', 'Servers\ServerController@index')->name('api.application.servers'); Route::get('/{server}', 'Servers\ServerController@view')->name('api.application.servers.view'); + Route::get('/external/{external_id}', 'Servers\ExternalServerController@index')->name('api.application.servers.external'); Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details'); Route::patch('/{server}/build', 'Servers\ServerDetailsController@build')->name('api.application.servers.build');