2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Http\Controllers\Api\Admin\DatabaseHosts ;
use Exception ;
use Illuminate\Http\JsonResponse ;
use Illuminate\Http\Response ;
use Knuckles\Scribe\Attributes\Endpoint ;
use Knuckles\Scribe\Attributes\Group ;
use Knuckles\Scribe\Attributes\QueryParam ;
use Knuckles\Scribe\Attributes\Response as ScribeResponse ;
use Knuckles\Scribe\Attributes\Subgroup ;
use PDOException ;
use Pterodactyl\Contracts\Databases\CreatesDatabaseHosts ;
use Pterodactyl\Contracts\Databases\DeletesDatabaseHosts ;
use Pterodactyl\Contracts\Databases\UpdatesDatabaseHosts ;
use Pterodactyl\Exceptions\DisplayException ;
2026-10-09 06:26:45 +01:00
use Pterodactyl\Extensions\Scribe\Attributes\ExtensionFieldsParam ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseField ;
2026-10-11 21:40:18 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromResource ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Activity ;
use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController ;
use Pterodactyl\Http\Requests\Api\Admin\DatabaseHosts\DeleteDatabaseHostRequest ;
use Pterodactyl\Http\Requests\Api\Admin\DatabaseHosts\GetDatabaseHostRequest ;
use Pterodactyl\Http\Requests\Api\Admin\DatabaseHosts\GetDatabaseHostsRequest ;
use Pterodactyl\Http\Requests\Api\Admin\DatabaseHosts\StoreDatabaseHostRequest ;
use Pterodactyl\Http\Requests\Api\Admin\DatabaseHosts\UpdateDatabaseHostRequest ;
2026-10-11 21:40:18 -04:00
use Pterodactyl\Http\Resources\Api\Admin\DatabaseHostResource ;
use Pterodactyl\Http\Resources\Api\ApiResourceCollection ;
use Pterodactyl\Http\Resources\Api\CallbackResource ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Database ;
use Pterodactyl\Models\DatabaseHost ;
use Pterodactyl\Models\Server ;
use Spatie\QueryBuilder\QueryBuilder ;
use Throwable ;
use UnexpectedValueException ;
#[ Group ( 'Admin API' , 'Root administrator endpoints for managing panel configuration and resources.' )]
#[ Subgroup ( 'Database Hosts' , 'Create and manage database hosts available to servers.' )]
class DatabaseHostController extends AdminApiController
{
private const array DATABASE_HOST_DATABASE_EXAMPLE = [
'object' => 'database' ,
'attributes' => [
'server' => [
'id' => 1 ,
'name' => 'Survival' ,
],
'name' => 's1_database' ,
'username' => 'u1_database' ,
'connections_from' => '%' ,
'max_connections' => 0 ,
],
];
private const array DATABASE_LIST_EXAMPLE = [
'object' => 'list' ,
'data' => [
self :: DATABASE_HOST_DATABASE_EXAMPLE ,
],
'meta' => [
'pagination' => [
'total' => 1 ,
'count' => 1 ,
'per_page' => 50 ,
'current_page' => 1 ,
'total_pages' => 1 ,
'links' => [],
],
],
];
private const array CONNECTION_ERROR = [
'errors' => [
[
'code' => 'DisplayException' ,
'status' => '400' ,
'detail' => 'There was an error while trying to connect to the host or while executing a query: "connection refused"' ,
],
],
];
/**
* List database hosts.
*/
#[ Endpoint ( 'List database hosts' , 'Returns a paginated list of database hosts.' )]
#[ QueryParam ( 'page' , 'integer' , 'Page number to retrieve.' , required : false , example : 1 )]
#[ QueryParam ( 'per_page' , 'integer' , 'Results to return per page.' , required : false , example : 50 )]
#[ QueryParam ( 'filter[name]' , 'string' , 'Filter hosts by name.' , required : false , example : 'Primary' )]
#[ QueryParam ( 'filter[host]' , 'string' , 'Filter hosts by address or hostname.' , required : false , example : '127.0.0.1' )]
#[ QueryParam ( 'sort' , 'string' , 'Sort hosts by ID, name, or creation date. Prefix with a hyphen for descending order.' , required : false , example : '-created_at' )]
#[ QueryParam ( 'include' , 'string' , 'Comma-separated relationships to include. Supports node and databases.' , required : false , example : 'node' )]
2026-10-11 21:40:18 -04:00
#[ ResponseFromResource ( DatabaseHostResource :: class , DatabaseHost :: class , description : 'Database hosts returned.' , collection : true , resourceKey : 'database_host' , paginate : 50 )]
public function index ( GetDatabaseHostsRequest $request ) : ApiResourceCollection
2026-10-05 18:10:18 -04:00
{
$query = QueryBuilder :: for ( DatabaseHost :: query ())
-> allowedFilters ([ 'name' , 'host' ])
-> allowedSorts ([ 'id' , 'name' , 'created_at' ]);
$hosts = $query -> getEloquentBuilder ()
-> with ( 'node' )
-> withCount ( 'databases' )
-> paginate ( $request -> perPage ());
2026-10-11 21:40:18 -04:00
return DatabaseHostResource :: collection ( $hosts );
2026-10-05 18:10:18 -04:00
}
/**
* Show database host.
*/
#[ Endpoint ( 'Get database host' , 'Returns a single database host by ID.' )]
#[ QueryParam ( 'include' , 'string' , 'Comma-separated relationships to include. Supports node and databases.' , required : false , example : 'node' )]
2026-10-11 21:40:18 -04:00
#[ ResponseFromResource ( DatabaseHostResource :: class , DatabaseHost :: class , description : 'Database host returned.' , resourceKey : 'database_host' , include : [ 'node' , 'databases' ])]
public function show ( GetDatabaseHostRequest $request , DatabaseHost $databaseHost ) : DatabaseHostResource
2026-10-05 18:10:18 -04:00
{
$databaseHost -> loadCount ( 'databases' );
$databaseHost -> load ( 'node' );
2026-10-11 21:40:18 -04:00
return DatabaseHostResource :: make ( $databaseHost ) -> withExtensionFields ();
2026-10-05 18:10:18 -04:00
}
/**
* List databases associated with a database host.
*/
#[ Endpoint ( 'List database host databases' , 'Returns a paginated list of server databases assigned to a database host.' )]
#[ QueryParam ( 'page' , 'integer' , 'Page number to retrieve.' , required : false , example : 1 )]
#[ QueryParam ( 'per_page' , 'integer' , 'Results to return per page.' , required : false , example : 50 )]
#[ ScribeResponse ( self :: DATABASE_LIST_EXAMPLE , description : 'Databases returned.' )]
#[ ResponseField ( 'attributes.max_connections' , 'integer' , example : 0 , nullable : true )]
2026-10-11 21:40:18 -04:00
public function databases ( GetDatabaseHostRequest $request , DatabaseHost $databaseHost ) : ApiResourceCollection
2026-10-05 18:10:18 -04:00
{
$databases = $databaseHost -> databases ()
-> with ( 'server' )
-> paginate ( $request -> perPage ());
2026-10-11 21:40:18 -04:00
return CallbackResource :: list ( $databases , function ( Database $database ) : array {
$server = $database -> getRelation ( 'server' );
throw_unless ( $server instanceof Server , UnexpectedValueException :: class , 'A database response requires its server relation.' );
return [
'server' => [
'id' => $server -> id ,
'name' => $server -> name ,
],
'name' => $database -> database ,
'username' => $database -> username ,
'connections_from' => $database -> remote ,
'max_connections' => $database -> max_connections ,
];
}, 'database' );
2026-10-05 18:10:18 -04:00
}
/**
* Create database host.
*/
#[ Endpoint ( 'Create database host' , 'Creates a database host after validating that the panel can connect to it.' )]
2026-10-11 21:40:18 -04:00
#[ ResponseFromResource ( DatabaseHostResource :: class , DatabaseHost :: class , status : 201 , description : 'Database host created.' , resourceKey : 'database_host' , meta : [ 'resource' => 'https://panel.example.com/api/admin/database-hosts/1' ])]
2026-10-05 18:10:18 -04:00
#[ ScribeResponse ( self :: CONNECTION_ERROR , status : 400 , description : 'The panel could not connect to the database host.' )]
2026-10-09 06:26:45 +01:00
#[ ExtensionFieldsParam ]
2026-10-05 18:10:18 -04:00
public function store ( StoreDatabaseHostRequest $request , CreatesDatabaseHosts $createHost ) : JsonResponse
{
try {
2026-10-11 07:27:47 -04:00
$host = $createHost -> create ( $request -> actionInput ());
2026-10-05 18:10:18 -04:00
} catch ( Exception $exception ) {
$this -> handleConnectionException ( $exception );
}
$host -> load ( 'node' ) -> loadCount ( 'databases' );
Activity :: event ( 'admin:database-host.create' )
-> subject ( $host )
-> property ( 'name' , $host -> name )
-> log ();
2026-10-11 21:40:18 -04:00
return DatabaseHostResource :: make ( $host )
-> withExtensionFields ()
-> withMeta ([
2026-10-05 18:10:18 -04:00
'resource' => route ( 'api.admin.database-hosts.view' , [
'databaseHost' => $host -> id ,
]),
])
-> respond ( Response :: HTTP_CREATED );
}
/**
* Update database host.
*/
#[ Endpoint ( 'Update database host' , 'Updates a database host after validating that the panel can connect to the supplied host details.' )]
2026-10-11 21:40:18 -04:00
#[ ResponseFromResource ( DatabaseHostResource :: class , DatabaseHost :: class , description : 'Database host updated.' , resourceKey : 'database_host' )]
2026-10-05 18:10:18 -04:00
#[ ScribeResponse ( self :: CONNECTION_ERROR , status : 400 , description : 'The panel could not connect to the database host.' )]
2026-10-09 06:26:45 +01:00
#[ ExtensionFieldsParam ]
2026-10-11 21:40:18 -04:00
public function update ( UpdateDatabaseHostRequest $request , UpdatesDatabaseHosts $updateHost , DatabaseHost $databaseHost ) : DatabaseHostResource
2026-10-05 18:10:18 -04:00
{
try {
2026-10-11 07:27:47 -04:00
$host = $updateHost -> update ( $databaseHost , $request -> actionInput ());
2026-10-05 18:10:18 -04:00
} catch ( Exception $exception ) {
$this -> handleConnectionException ( $exception );
}
$host -> load ( 'node' ) -> loadCount ( 'databases' );
Activity :: event ( 'admin:database-host.update' )
-> subject ( $host )
-> property ( 'name' , $host -> name )
-> log ();
2026-10-11 21:40:18 -04:00
return DatabaseHostResource :: make ( $host ) -> withExtensionFields ();
2026-10-05 18:10:18 -04:00
}
/**
* Delete database host.
*/
#[ Endpoint ( 'Delete database host' , 'Deletes a database host when no server databases depend on it.' )]
#[ ScribeResponse ( status : 204 , description : 'Database host deleted.' )]
public function destroy ( DeleteDatabaseHostRequest $request , DeletesDatabaseHosts $deleteHost , DatabaseHost $databaseHost ) : Response
{
2026-10-11 02:53:16 -04:00
$deleteHost -> delete ( $databaseHost );
2026-10-05 18:10:18 -04:00
Activity :: event ( 'admin:database-host.delete' )
-> subject ( $databaseHost )
-> property ( 'name' , $databaseHost -> name )
-> log ();
return $this -> returnNoContent ();
}
private function handleConnectionException ( Throwable $exception ) : never
{
if ( $exception instanceof PDOException || $exception -> getPrevious () instanceof PDOException ) {
2026-10-11 15:53:06 -04:00
report ( $exception );
2026-10-05 18:10:18 -04:00
throw new DisplayException ( sprintf ( 'There was an error while trying to connect to the host or while executing a query: "%s"' , $exception -> getMessage ()), $exception );
}
throw $exception ;
}
}