database-host: reverse node relationship

This commit is contained in:
Matthew Penner 2022-12-14 18:53:31 -07:00
parent 0c5416ee27
commit 507a802dec
No known key found for this signature in database
5 changed files with 20 additions and 24 deletions

View file

@ -3,7 +3,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/** /**
* @property int $id * @property int $id
@ -13,7 +13,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property string $username * @property string $username
* @property string $password * @property string $password
* @property int|null $max_databases * @property int|null $max_databases
* @property int|null $node_id
* @property \Carbon\CarbonImmutable $created_at * @property \Carbon\CarbonImmutable $created_at
* @property \Carbon\CarbonImmutable $updated_at * @property \Carbon\CarbonImmutable $updated_at
*/ */
@ -41,7 +40,7 @@ class DatabaseHost extends Model
* Fields that are mass assignable. * Fields that are mass assignable.
*/ */
protected $fillable = [ protected $fillable = [
'name', 'host', 'port', 'username', 'password', 'max_databases', 'node_id', 'name', 'host', 'port', 'username', 'password', 'max_databases',
]; ];
/** /**
@ -50,7 +49,6 @@ class DatabaseHost extends Model
protected $casts = [ protected $casts = [
'id' => 'integer', 'id' => 'integer',
'max_databases' => 'integer', 'max_databases' => 'integer',
'node_id' => 'integer',
]; ];
/** /**
@ -62,17 +60,8 @@ class DatabaseHost extends Model
'port' => 'required|numeric|between:1,65535', 'port' => 'required|numeric|between:1,65535',
'username' => 'required|string|max:32', 'username' => 'required|string|max:32',
'password' => 'nullable|string', 'password' => 'nullable|string',
'node_id' => 'sometimes|nullable|integer|exists:nodes,id',
]; ];
/**
* Gets the node associated with a database host.
*/
public function node(): BelongsTo
{
return $this->belongsTo(Node::class);
}
/** /**
* Gets the databases associated with this host. * Gets the databases associated with this host.
*/ */
@ -80,4 +69,12 @@ class DatabaseHost extends Model
{ {
return $this->hasMany(Database::class); return $this->hasMany(Database::class);
} }
/**
* Returns the nodes that a database host is assigned to.
*/
public function nodes(): BelongsToMany
{
return $this->belongsToMany(Node::class);
}
} }

View file

@ -31,7 +31,6 @@ class DatabaseHostTransformer extends Transformer
'host' => $model->host, 'host' => $model->host,
'port' => $model->port, 'port' => $model->port,
'username' => $model->username, 'username' => $model->username,
'node_id' => $model->node_id,
'created_at' => self::formatTimestamp($model->created_at), 'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at), 'updated_at' => self::formatTimestamp($model->updated_at),
]; ];

View file

@ -24,7 +24,7 @@ class DatabaseAuthorizationTest extends ClientApiIntegrationTestCase
// And as no access to $server3. // And as no access to $server3.
$server3 = $this->createServerModel(); $server3 = $this->createServerModel();
$host = DatabaseHost::factory()->create([]); $host = DatabaseHost::factory()->create();
// Set the API $user as a subuser of server 2, but with no permissions // Set the API $user as a subuser of server 2, but with no permissions
// to do anything with the databases for that server. // to do anything with the databases for that server.

View file

@ -58,7 +58,7 @@ class DatabaseManagementServiceTest extends IntegrationTestCase
public function testDatabaseCannotBeCreatedIfServerHasReachedLimit() public function testDatabaseCannotBeCreatedIfServerHasReachedLimit()
{ {
$server = $this->createServerModel(['database_limit' => 2]); $server = $this->createServerModel(['database_limit' => 2]);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); $host = DatabaseHost::factory()->create();
Database::factory()->times(2)->create(['server_id' => $server->id, 'database_host_id' => $host->id]); Database::factory()->times(2)->create(['server_id' => $server->id, 'database_host_id' => $host->id]);
@ -90,8 +90,8 @@ class DatabaseManagementServiceTest extends IntegrationTestCase
$server = $this->createServerModel(); $server = $this->createServerModel();
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id); $name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); $host = DatabaseHost::factory()->create();
$host2 = DatabaseHost::factory()->create(['node_id' => $server->node_id]); $host2 = DatabaseHost::factory()->create();
Database::factory()->create([ Database::factory()->create([
'database' => $name, 'database' => $name,
'database_host_id' => $host->id, 'database_host_id' => $host->id,
@ -119,7 +119,7 @@ class DatabaseManagementServiceTest extends IntegrationTestCase
$server = $this->createServerModel(); $server = $this->createServerModel();
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id); $name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); $host = DatabaseHost::factory()->create();
$this->repository->expects('createDatabase')->with($name); $this->repository->expects('createDatabase')->with($name);
@ -177,7 +177,7 @@ class DatabaseManagementServiceTest extends IntegrationTestCase
$server = $this->createServerModel(); $server = $this->createServerModel();
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id); $name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); $host = DatabaseHost::factory()->create();
$this->repository->expects('createDatabase')->with($name)->andThrows(new \BadMethodCallException()); $this->repository->expects('createDatabase')->with($name)->andThrows(new \BadMethodCallException());
$this->repository->expects('dropDatabase')->with($name); $this->repository->expects('dropDatabase')->with($name);

View file

@ -62,7 +62,7 @@ class DeployServerDatabaseServiceTest extends IntegrationTestCase
$server = $this->createServerModel(); $server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]); $node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]); DatabaseHost::factory()->create();
config()->set('pterodactyl.client_features.databases.allow_random', false); config()->set('pterodactyl.client_features.databases.allow_random', false);
@ -97,8 +97,8 @@ class DeployServerDatabaseServiceTest extends IntegrationTestCase
$server = $this->createServerModel(); $server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]); $node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]); DatabaseHost::factory()->create();
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); $host = DatabaseHost::factory()->create();
$this->managementService->expects('create')->with($server, [ $this->managementService->expects('create')->with($server, [
'database_host_id' => $host->id, 'database_host_id' => $host->id,
@ -124,7 +124,7 @@ class DeployServerDatabaseServiceTest extends IntegrationTestCase
$server = $this->createServerModel(); $server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]); $node = Node::factory()->create(['location_id' => $server->location->id]);
$host = DatabaseHost::factory()->create(['node_id' => $node->id]); $host = DatabaseHost::factory()->create();
$this->managementService->expects('create')->with($server, [ $this->managementService->expects('create')->with($server, [
'database_host_id' => $host->id, 'database_host_id' => $host->id,