| 1 | package App::Netdisco::DB::ResultSet::NodeIp; | ||
| 2 | use base 'App::Netdisco::DB::ResultSet'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | __PACKAGE__->load_components(qw/ | ||
| 8 | +App::Netdisco::DB::ExplicitLocking | ||
| 9 | /); | ||
| 10 | |||
| 11 | my $order_by_time_last_and_join_manufacturer = { | ||
| 12 | order_by => {'-desc' => 'time_last'}, | ||
| 13 | '+columns' => [ | ||
| 14 | 'manufacturer.company', | ||
| 15 | 'manufacturer.abbrev', | ||
| 16 | { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" }, | ||
| 17 | { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" }, | ||
| 18 | ], | ||
| 19 | join => 'manufacturer' | ||
| 20 | }; | ||
| 21 | |||
| 22 | =head1 with_times | ||
| 23 | |||
| 24 | This is a modifier for any C<search()> (including the helpers below) which | ||
| 25 | will add the following additional synthesized columns to the result set: | ||
| 26 | |||
| 27 | =over 4 | ||
| 28 | |||
| 29 | =item time_first_stamp | ||
| 30 | |||
| 31 | =item time_last_stamp | ||
| 32 | |||
| 33 | =back | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub with_times { | ||
| 38 | my ($rs, $cond, $attrs) = @_; | ||
| 39 | |||
| 40 | return $rs | ||
| 41 | ->search_rs({}, $order_by_time_last_and_join_manufacturer) | ||
| 42 | ->search($cond, $attrs); | ||
| 43 | } | ||
| 44 | |||
| 45 | =head1 with_router | ||
| 46 | |||
| 47 | This is a modifier for any C<search()> (including the helpers below) which | ||
| 48 | will add the following additional synthesized column to the result set: | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item router_ip | ||
| 53 | |||
| 54 | =item router_name | ||
| 55 | |||
| 56 | =back | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub with_router { | ||
| 61 | my ($rs, $cond, $attrs) = @_; | ||
| 62 | |||
| 63 | return $rs | ||
| 64 | ->search_rs({}, { | ||
| 65 | '+columns' => [ | ||
| 66 | { router_ip => \q{(SELECT key FROM json_each_text(seen_on_router_last::json) ORDER BY value::timestamp DESC LIMIT 1)} }, | ||
| 67 | { router_name => \q{COALESCE(NULLIF(router.dns,''), NULLIF(router.name,''), '')} }, | ||
| 68 | ], | ||
| 69 | join => 'router' | ||
| 70 | }) | ||
| 71 | ->search($cond, $attrs); | ||
| 72 | } | ||
| 73 | |||
| 74 | =head2 with_l3_domain | ||
| 75 | |||
| 76 | Adds C<l3_domain_id> and C<l3_domain_name>, the L3 domain of each row's VRF. | ||
| 77 | They are plain columns so that results serialized with C<hri>, as the search | ||
| 78 | API does, carry the domain. A NULL id means the domain cannot be determined. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | my $l3_domain_id = q{CASE WHEN me.vrf = '' THEN 0 ELSE l3_domain_of(me.router_last, me.vrf) END}; | ||
| 83 | |||
| 84 | sub with_l3_domain { | ||
| 85 | my ($rs, $cond, $attrs) = @_; | ||
| 86 | |||
| 87 | return $rs | ||
| 88 | ->search_rs({}, { | ||
| 89 | '+columns' => [ | ||
| 90 | { l3_domain_id => \$l3_domain_id }, | ||
| 91 | { l3_domain_name => \"(SELECT ld.name FROM l3_domain ld WHERE ld.id = $l3_domain_id)" }, | ||
| 92 | ], | ||
| 93 | }) | ||
| 94 | ->search($cond, $attrs); | ||
| 95 | } | ||
| 96 | |||
| 97 | =head1 search_by_ip( \%cond, \%attrs? ) | ||
| 98 | |||
| 99 | my $set = $rs->search_by_ip({ip => '192.0.2.1', active => 1}); | ||
| 100 | |||
| 101 | Like C<search()>, this returns a ResultSet of matching rows from the | ||
| 102 | NodeIp table. | ||
| 103 | |||
| 104 | =over 4 | ||
| 105 | |||
| 106 | =item * | ||
| 107 | |||
| 108 | The C<cond> parameter must be a hashref containing a key C<ip> with the value | ||
| 109 | to search for. Value can either be a simple string of IPv4 or IPv6, or a | ||
| 110 | L<NetAddr::IP::Lite> object in which case all results within the CIDR/Prefix | ||
| 111 | will be retrieved. | ||
| 112 | |||
| 113 | =item * | ||
| 114 | |||
| 115 | Results are ordered by time last seen. | ||
| 116 | |||
| 117 | =item * | ||
| 118 | |||
| 119 | Additional columns C<time_first_stamp> and C<time_last_stamp> provide | ||
| 120 | preformatted timestamps of the C<time_first> and C<time_last> fields. | ||
| 121 | |||
| 122 | =item * | ||
| 123 | |||
| 124 | A JOIN is performed on the Manufacturer table and the Manufacturer C<company> column prefetched. | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | To limit results only to active IPs, set C<< {active => 1} >> in C<cond>. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub search_by_ip { | ||
| 133 | my ($rs, $cond, $attrs) = @_; | ||
| 134 | |||
| 135 | die "ip address required for search_by_ip\n" | ||
| 136 | if ref {} ne ref $cond or !exists $cond->{ip}; | ||
| 137 | |||
| 138 | # handle either plain text IP or NetAddr::IP (/32 or CIDR) | ||
| 139 | my ($op, $ip) = ('=', delete $cond->{ip}); | ||
| 140 | |||
| 141 | if ('NetAddr::IP::Lite' eq ref $ip and $ip->num > 1) { | ||
| 142 | $op = '<<='; | ||
| 143 | $ip = $ip->cidr; | ||
| 144 | } | ||
| 145 | $cond->{'me.ip'} = { $op => $ip }; | ||
| 146 | |||
| 147 | return $rs | ||
| 148 | ->search_rs({}, $order_by_time_last_and_join_manufacturer) | ||
| 149 | ->search($cond, $attrs); | ||
| 150 | } | ||
| 151 | |||
| 152 | =head1 search_by_dns( \%cond, \%attrs? ) | ||
| 153 | |||
| 154 | my $set = $rs->search_by_dns({ | ||
| 155 | dns => 'foo.example.com', | ||
| 156 | suffix => qr/(?:\.example\..com|\.local)$/, | ||
| 157 | active => 1 | ||
| 158 | }); | ||
| 159 | |||
| 160 | Like C<search()>, this returns a ResultSet of matching rows from the | ||
| 161 | NodeIp table. | ||
| 162 | |||
| 163 | =over 4 | ||
| 164 | |||
| 165 | =item * | ||
| 166 | |||
| 167 | The NodeIp table must have a C<dns> column for this search to work. Typically | ||
| 168 | this column is the IP's DNS PTR record, cached at the time of Netdisco Arpnip. | ||
| 169 | |||
| 170 | =item * | ||
| 171 | |||
| 172 | The C<cond> parameter must be a hashref containing a key C<dns> with the value | ||
| 173 | to search for. The value may optionally include SQL wildcard characters. | ||
| 174 | |||
| 175 | =item * | ||
| 176 | |||
| 177 | If C<dns> is a plain string, then the C<cond> parameter may optionally have a | ||
| 178 | C<suffix> parameter which is a regular expression of domain names - one of | ||
| 179 | which must match the results. | ||
| 180 | |||
| 181 | =item * | ||
| 182 | |||
| 183 | Results are ordered by time last seen. | ||
| 184 | |||
| 185 | =item * | ||
| 186 | |||
| 187 | Additional columns C<time_first_stamp> and C<time_last_stamp> provide | ||
| 188 | preformatted timestamps of the C<time_first> and C<time_last> fields. | ||
| 189 | |||
| 190 | =item * | ||
| 191 | |||
| 192 | A JOIN is performed on the Manufacturer table and the Manufacturer C<company> column prefetched. | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | To limit results only to active IPs, set C<< {active => 1} >> in C<cond>. | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | sub search_by_dns { | ||
| 201 | my ($rs, $cond, $attrs) = @_; | ||
| 202 | |||
| 203 | die "dns field required for search_by_dns\n" | ||
| 204 | if ref {} ne ref $cond or !exists $cond->{dns}; | ||
| 205 | |||
| 206 | my $dns_field = delete $cond->{dns}; | ||
| 207 | |||
| 208 | (my $suffix = ($cond->{suffix} || '')) | ||
| 209 | =~ s|\Q(?^\E[-xismu]*|(?|g; | ||
| 210 | |||
| 211 | if (q{} eq ref $dns_field and exists $cond->{suffix}) { | ||
| 212 | (my $stripped_dns_field = $dns_field) =~ s/\.\%$//; | ||
| 213 | (my $fqdn_field = $stripped_dns_field) .= '%'; | ||
| 214 | $stripped_dns_field =~ s/$cond->{suffix}$// if $cond->{suffix}; | ||
| 215 | $stripped_dns_field .= '.%'; | ||
| 216 | |||
| 217 | $cond->{'me.dns'} = [ -or => | ||
| 218 | [ -and => | ||
| 219 | { '-ilike' => $stripped_dns_field }, | ||
| 220 | { '~*' => "***:$suffix" }, | ||
| 221 | ], | ||
| 222 | [ -and => | ||
| 223 | { '-ilike' => $dns_field }, | ||
| 224 | { '~*' => "***:$suffix" }, | ||
| 225 | ], | ||
| 226 | { '-ilike' => $fqdn_field }, | ||
| 227 | ]; | ||
| 228 | } | ||
| 229 | elsif (q{} ne ref $dns_field) { | ||
| 230 | $cond->{'me.dns'} = $dns_field; | ||
| 231 | } | ||
| 232 | else { | ||
| 233 | $cond->{'me.dns'} = { '-ilike' => $dns_field }; | ||
| 234 | } | ||
| 235 | |||
| 236 | delete $cond->{suffix}; | ||
| 237 | return $rs | ||
| 238 | ->search_rs({}, $order_by_time_last_and_join_manufacturer) | ||
| 239 | ->search($cond, $attrs); | ||
| 240 | } | ||
| 241 | |||
| 242 | =head1 search_by_mac( \%cond, \%attrs? ) | ||
| 243 | |||
| 244 | my $set = $rs->search_by_mac({mac => '00:11:22:33:44:55', active => 1}); | ||
| 245 | |||
| 246 | Like C<search()>, this returns a ResultSet of matching rows from the | ||
| 247 | NodeIp table. | ||
| 248 | |||
| 249 | =over 4 | ||
| 250 | |||
| 251 | =item * | ||
| 252 | |||
| 253 | The C<cond> parameter must be a hashref containing a key C<mac> with the value | ||
| 254 | to search for. | ||
| 255 | |||
| 256 | =item * | ||
| 257 | |||
| 258 | Results are ordered by time last seen. | ||
| 259 | |||
| 260 | =item * | ||
| 261 | |||
| 262 | Additional columns C<time_first_stamp> and C<time_last_stamp> provide | ||
| 263 | preformatted timestamps of the C<time_first> and C<time_last> fields. | ||
| 264 | |||
| 265 | =item * | ||
| 266 | |||
| 267 | A JOIN is performed on the Manufacturer table and the Manufacturer C<company> column prefetched. | ||
| 268 | |||
| 269 | =back | ||
| 270 | |||
| 271 | To limit results only to active IPs, set C<< {active => 1} >> in C<cond>. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub search_by_mac { | ||
| 276 | my ($rs, $cond, $attrs) = @_; | ||
| 277 | |||
| 278 | die "mac address required for search_by_mac\n" | ||
| 279 | if ref {} ne ref $cond or !exists $cond->{mac}; | ||
| 280 | |||
| 281 | return $rs | ||
| 282 | ->search_rs({}, $order_by_time_last_and_join_manufacturer) | ||
| 283 | ->search($cond, $attrs); | ||
| 284 | } | ||
| 285 | |||
| 286 | =head2 ip_version( $version ) | ||
| 287 | |||
| 288 | my $rset = $rs->ip_version(4); | ||
| 289 | |||
| 290 | This predefined C<search()> returns a ResultSet of matching rows from the | ||
| 291 | NodeIp table of nodes with addresses of the supplied IP version. | ||
| 292 | |||
| 293 | =over 4 | ||
| 294 | |||
| 295 | =item * | ||
| 296 | |||
| 297 | The C<version> parameter must be an integer either 4 or 6. | ||
| 298 | |||
| 299 | =back | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub ip_version { | ||
| 304 | my ( $rs, $version ) = @_; | ||
| 305 | |||
| 306 | die "ip_version input must be either 4 or 6\n" | ||
| 307 | unless $version && ( $version == 4 || $version == 6 ); | ||
| 308 | |||
| 309 | return $rs->search_rs( \[ 'family(me.ip) = ?', $version ] ); | ||
| 310 | } | ||
| 311 | |||
| 312 | 1; |