Link Model (#15611)

* Link Model
and relationships

* Some uses of the new model
This commit is contained in:
Tony Murray
2023-12-15 10:13:10 -06:00
committed by GitHub
parent 648a5ea7d3
commit c79d8665d8
4 changed files with 73 additions and 2 deletions

View File

@@ -26,13 +26,16 @@
namespace App\Http\Controllers\Device\Tabs;
use App\Models\Device;
use App\Models\Link;
use LibreNMS\Interfaces\UI\DeviceTab;
class NeighboursController implements DeviceTab
{
public function visible(Device $device): bool
{
return \DB::table('links')->where('local_device_id', $device->device_id)->exists();
return Link::where('local_device_id', $device->device_id)
->orWhere('remote_device_id', $device->device_id)
->exists();
}
public function slug(): string

View File

@@ -766,6 +766,21 @@ class Device extends BaseModel
return $this->hasMany(\App\Models\IsisAdjacency::class, 'device_id', 'device_id');
}
public function links(): HasMany
{
return $this->hasMany(\App\Models\Link::class, 'local_device_id');
}
public function remoteLinks(): HasMany
{
return $this->hasMany(\App\Models\Link::class, 'remote_device_id');
}
public function allLinks(): \Illuminate\Support\Collection
{
return $this->links->merge($this->remoteLinks);
}
public function location(): BelongsTo
{
return $this->belongsTo(\App\Models\Location::class, 'location_id', 'id');

37
app/Models/Link.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
class Link extends Model
{
use HasFactory;
public $timestamps = false;
// ---- Define Relationships ----
public function device(): BelongsTo
{
return $this->belongsTo(\App\Models\Device::class, 'local_device_id', 'device_id');
}
public function port(): BelongsTo
{
return $this->belongsTo(\App\Models\Port::class, 'local_port_id', 'port_id');
}
public function remoteDevice(): HasOne
{
return $this->hasOne(\App\Models\Device::class, 'device_id', 'remote_device_id');
}
public function remotePort(): HasOne
{
return $this->hasOne(\App\Models\Port::class, 'port_id', 'remote_port_id');
}
}

View File

@@ -42,11 +42,12 @@ class Port extends DeviceRelatedModel
$port->statistics()->delete();
$port->stp()->delete();
$port->vlans()->delete();
$port->links()->delete();
$port->remoteLinks()->delete();
// dont have relationships yet
DB::table('juniAtmVp')->where('port_id', $port->port_id)->delete();
DB::table('ports_perms')->where('port_id', $port->port_id)->delete();
DB::table('links')->where('local_port_id', $port->port_id)->orWhere('remote_port_id', $port->port_id)->delete();
DB::table('ports_stack')->where('port_id_low', $port->port_id)->orWhere('port_id_high', $port->port_id)->delete();
\Rrd::purge($port->device?->hostname, \Rrd::portName($port->port_id)); // purge all port rrd files
@@ -310,6 +311,21 @@ class Port extends DeviceRelatedModel
return $this->hasMany(\App\Models\Ipv6Address::class, 'port_id');
}
public function links(): HasMany
{
return $this->hasMany(\App\Models\Link::class, 'local_port_id');
}
public function remoteLinks(): HasMany
{
return $this->hasMany(\App\Models\Link::class, 'remote_port_id');
}
public function allLinks(): \Illuminate\Support\Collection
{
return $this->links->merge($this->remoteLinks);
}
public function macAccounting(): HasMany
{
return $this->hasMany(MacAccounting::class, 'port_id');