deployments relationship

This commit is contained in:
2025-05-22 17:47:55 +01:00
parent 41c8521936
commit 9ce024d479

View File

@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Spatie\Ssh\Ssh; use Spatie\Ssh\Ssh;
class Server extends Model class Server extends Model
@@ -35,7 +36,7 @@ class Server extends Model
$server->internal_ip_ending = $existingServer $server->internal_ip_ending = $existingServer
? $existingServer->internal_ip_ending + 1 ? $existingServer->internal_ip_ending + 1
: 2; : 2;
$server->internal_ip = config('keystone.internal_ip_base').$server->internal_ip_ending; $server->internal_ip = config('keystone.internal_ip_base') . $server->internal_ip_ending;
}); });
} }
@@ -69,6 +70,26 @@ class Server extends Model
return $this->belongsTo(Provider::class); return $this->belongsTo(Provider::class);
} }
public function serviceDeployments(): HasManyThrough
{
return $this->hasManyThrough(
Deployment::class,
Service::class,
'server_id',
'target_id',
)->where('target_type', (new Service)->getMorphClass());
}
public function environmentDeployments(): HasManyThrough
{
return $this->hasManyThrough(
Deployment::class,
Environment::class,
'server_id',
'target_id',
)->where('target_type', (new Environment)->getMorphClass());
}
public function sshClient(string $user = 'root'): Ssh public function sshClient(string $user = 'root'): Ssh
{ {
return Ssh::create($user, $this->ipv4) return Ssh::create($user, $this->ipv4)