Skip to content

Commit 1a421e4

Browse files
committed
Fix up getIdFromIp
1 parent fbcc9c1 commit 1a421e4

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

LibreNMS/Cache/Port.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LibreNMS\Cache;
44

5+
use App\Facades\DeviceCache;
56
use App\Models\Ipv4Address;
67
use App\Models\Ipv6Address;
78
use Illuminate\Support\Facades\Log;
@@ -106,25 +107,26 @@ public function getIdFromIp(string|IP $ip, ?string $context_name = null, \App\Mo
106107

107108
$device_id = $this->deviceToId($device);
108109
$ip_string = $ip->uncompressed();
110+
$context_name = (string) $context_name;
109111

110112
if (! array_key_exists($device_id, $this->ipMaps) || ! array_key_exists($ip_string, $this->ipMaps[$device_id])) {
111113
if ($ip->getFamily() == 'ipv4') {
112-
$this->ipMaps[$device_id][$ip_string] = Ipv4Address::query()
113-
->when($device, fn ($q) => $q->where('device_id', $device_id))
114+
$query = $device ? DeviceCache::get($device_id)->ipv4() : Ipv4Address::query();
115+
$this->ipMaps[$device_id][$context_name][$ip_string] = $query
114116
->where('ipv4_address', $ip_string)
115117
->where('context_name', $context_name)
116-
->value('port_id');
118+
->value('ipv4_addresses.port_id');
117119
} else {
118-
$this->ipMaps[$device_id][$ip_string] = Ipv6Address::query()
119-
->when($device, fn ($q) => $q->where('device_id', $device_id))
120+
$query = $device ? DeviceCache::get($device_id)->ipv6() : Ipv6Address::query();
121+
$this->ipMaps[$device_id][$context_name][$ip_string] = $query
120122
->where('ipv6_address', $ip_string)
121123
->where('context_name', $context_name)
122-
->value('port_id');
124+
->value('ipv6_addresses.port_id');
123125
}
124126
}
125127

126-
if (isset($this->ipMaps[$device_id][$ip_string])) {
127-
return $this->ipMaps[$device_id][$ip_string];
128+
if (isset($this->ipMaps[$device_id][$context_name][$ip_string])) {
129+
return $this->ipMaps[$device_id][$context_name][$ip_string];
128130
}
129131

130132
return null;

0 commit comments

Comments
 (0)