initial commit
This commit is contained in:
74
app/Models/IpPool.php
Normal file
74
app/Models/IpPool.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\IpPoolType;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class IpPool extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'type',
|
||||
'start_ip',
|
||||
'end_ip',
|
||||
'gateway',
|
||||
'cidr',
|
||||
'description',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => IpPoolType::class,
|
||||
'cidr' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function vms(): HasMany
|
||||
{
|
||||
return $this->hasMany(Customer::class, 'ip_pool_id');
|
||||
}
|
||||
|
||||
public function totalIps(): int
|
||||
{
|
||||
$start = ip2long($this->start_ip);
|
||||
$end = ip2long($this->end_ip);
|
||||
|
||||
if ($start === false || $end === false || $end < $start) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) ($end - $start + 1);
|
||||
}
|
||||
|
||||
public function usedIpsCount(): int
|
||||
{
|
||||
$query = Customer::query()
|
||||
->where('ip_pool_id', $this->id)
|
||||
->whereIn('status', ['pending', 'active']);
|
||||
|
||||
if ($this->type === IpPoolType::Public) {
|
||||
return $query->whereNotNull('public_ip')->count();
|
||||
}
|
||||
|
||||
return $query->whereNotNull('ip_address')->count();
|
||||
}
|
||||
|
||||
public function freeIpsCount(): int
|
||||
{
|
||||
return max(0, $this->totalIps() - $this->usedIpsCount());
|
||||
}
|
||||
|
||||
public function containsIp(string $ip): bool
|
||||
{
|
||||
$long = ip2long($ip);
|
||||
$start = ip2long($this->start_ip);
|
||||
$end = ip2long($this->end_ip);
|
||||
|
||||
return $long !== false && $long >= $start && $long <= $end;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user