130 lines
3.0 KiB
PHP
130 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Customer extends Model
|
|
{
|
|
protected $table = 'customers';
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'ip_pool_id',
|
|
'name',
|
|
'domain',
|
|
'vmid',
|
|
'ip_address',
|
|
'public_ip',
|
|
'behind_traefik',
|
|
'provision_mode',
|
|
'hosting_plan_id',
|
|
'whmcs_service_id',
|
|
'cpu',
|
|
'ram',
|
|
'disk',
|
|
'attached_iso',
|
|
'proxmox_status',
|
|
'proxmox_uptime',
|
|
'proxmox_status_at',
|
|
'status',
|
|
'provisioning_step',
|
|
'error_message',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'vmid' => 'integer',
|
|
'cpu' => 'integer',
|
|
'ram' => 'integer',
|
|
'disk' => 'integer',
|
|
'behind_traefik' => 'boolean',
|
|
'proxmox_uptime' => 'integer',
|
|
'proxmox_status_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function owner(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function ipPool(): BelongsTo
|
|
{
|
|
return $this->belongsTo(IpPool::class, 'ip_pool_id');
|
|
}
|
|
|
|
public function plan(): BelongsTo
|
|
{
|
|
return $this->belongsTo(HostingPlan::class, 'hosting_plan_id');
|
|
}
|
|
|
|
public function whmcsService(): BelongsTo
|
|
{
|
|
return $this->belongsTo(WhmcsService::class, 'whmcs_service_id');
|
|
}
|
|
|
|
public function vmidReservation(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
{
|
|
return $this->hasOne(VmidReservation::class);
|
|
}
|
|
|
|
public function devices(): HasMany
|
|
{
|
|
return $this->hasMany(VmDevice::class)->orderBy('sort_order');
|
|
}
|
|
|
|
public function snapshots(): HasMany
|
|
{
|
|
return $this->hasMany(VmSnapshot::class);
|
|
}
|
|
|
|
public function backups(): HasMany
|
|
{
|
|
return $this->hasMany(VmBackup::class);
|
|
}
|
|
|
|
public function firewallRules(): HasMany
|
|
{
|
|
return $this->hasMany(VmFirewallRule::class)->orderBy('sort_order');
|
|
}
|
|
|
|
public function metrics(): HasMany
|
|
{
|
|
return $this->hasMany(VmMetric::class)->orderByDesc('recorded_at');
|
|
}
|
|
|
|
public function activityLogs(): HasMany
|
|
{
|
|
return $this->hasMany(VmActivityLog::class)->latest();
|
|
}
|
|
|
|
public function isRunning(): bool
|
|
{
|
|
return $this->proxmox_status === 'running';
|
|
}
|
|
|
|
public function scopeForUser(Builder $query, User $user): Builder
|
|
{
|
|
if ($user->isAdmin()) {
|
|
return $query;
|
|
}
|
|
|
|
return $query->where('user_id', $user->id);
|
|
}
|
|
|
|
public function isProvisionable(): bool
|
|
{
|
|
return in_array($this->status, ['pending', 'failed'], true);
|
|
}
|
|
|
|
public function displayName(): string
|
|
{
|
|
return $this->name.($this->vmid ? " (#{$this->vmid})" : '');
|
|
}
|
|
}
|