diff --git a/.gitignore b/.gitignore index 0bb2db9e..fdd66ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ Netdisco/environments/development.yml +test.pl diff --git a/Netdisco/lib/Netdisco/DB/Result/Device.pm b/Netdisco/lib/Netdisco/DB/Result/Device.pm index 11577d48..b1f330fa 100644 --- a/Netdisco/lib/Netdisco/DB/Result/Device.pm +++ b/Netdisco/lib/Netdisco/DB/Result/Device.pm @@ -82,6 +82,14 @@ __PACKAGE__->set_primary_key("ip"); # Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:671/XuuvsO2aMB1+IRWFjg +__PACKAGE__->has_many( vlans => 'Netdisco::DB::Result::DeviceVlan', 'ip' ); +__PACKAGE__->has_many( ports => 'Netdisco::DB::Result::DevicePort', 'ip' ); +__PACKAGE__->has_many( + port_vlans => 'Netdisco::DB::Result::DevicePortVlan', + 'ip', { join_type => 'RIGHT' } +); + +# helper which assumes we've just RIGHT JOINed to Vlans table +sub vlan { return (shift)->vlans->first } -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm b/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm index fc72a63e..28ea4a0d 100644 --- a/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm +++ b/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm @@ -64,6 +64,31 @@ __PACKAGE__->set_primary_key("port", "ip"); # Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:lcbweb0loNwHoWUuxTN/hA +__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip' ); +__PACKAGE__->has_many( port_vlans_tagged => 'Netdisco::DB::Result::DevicePortVlan', + sub { + my $args = shift; + return { + "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" }, + "$args->{foreign_alias}.port" => { -ident => "$args->{self_alias}.port" }, + -not_bool => "$args->{foreign_alias}.native", + }; + } +); +__PACKAGE__->many_to_many( tagged_vlans => 'port_vlans_tagged', 'vlan' ); +__PACKAGE__->might_have( native_port_vlan => 'Netdisco::DB::Result::DevicePortVlan', + sub { + my $args = shift; + return { + "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" }, + "$args->{foreign_alias}.port" => { -ident => "$args->{self_alias}.port" }, + -bool => "$args->{foreign_alias}.native", + }; + } +); +sub native_vlan { + my $row = shift; + return eval { $row->native_port_vlan->vlan || undef }; +}; -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Netdisco/lib/Netdisco/DB/Result/DevicePortVlan.pm b/Netdisco/lib/Netdisco/DB/Result/DevicePortVlan.pm index 725cb803..12046cad 100644 --- a/Netdisco/lib/Netdisco/DB/Result/DevicePortVlan.pm +++ b/Netdisco/lib/Netdisco/DB/Result/DevicePortVlan.pm @@ -39,6 +39,12 @@ __PACKAGE__->set_primary_key("ip", "port", "vlan"); # Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:/3KLjJ3D18pGaPEaw9EU5w +__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip' ); +__PACKAGE__->belongs_to( port => 'Netdisco::DB::Result::DevicePort', { + 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port', +}); +__PACKAGE__->belongs_to( vlan => 'Netdisco::DB::Result::DeviceVlan', { + 'foreign.ip' => 'self.ip', 'foreign.vlan' => 'self.vlan', +}); -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Netdisco/lib/Netdisco/DB/Result/DeviceVlan.pm b/Netdisco/lib/Netdisco/DB/Result/DeviceVlan.pm index efac70e4..7e065ff4 100644 --- a/Netdisco/lib/Netdisco/DB/Result/DeviceVlan.pm +++ b/Netdisco/lib/Netdisco/DB/Result/DeviceVlan.pm @@ -37,6 +37,28 @@ __PACKAGE__->set_primary_key("ip", "vlan"); # Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hBJRcdzOic4d3u4pD1m8iA +__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip' ); +__PACKAGE__->has_many( port_vlans_tagged => 'Netdisco::DB::Result::DevicePortVlan', + sub { + my $args = shift; + return { + "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" }, + "$args->{foreign_alias}.vlan" => { -ident => "$args->{self_alias}.vlan" }, + -not_bool => "$args->{foreign_alias}.native", + }; + } +); +__PACKAGE__->has_many( port_vlans_native => 'Netdisco::DB::Result::DevicePortVlan', + sub { + my $args = shift; + return { + "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" }, + "$args->{foreign_alias}.vlan" => { -ident => "$args->{self_alias}.vlan" }, + -bool => "$args->{foreign_alias}.native", + }; + } +); +__PACKAGE__->many_to_many( tagging_ports => 'port_vlans_tagged', 'port' ); +__PACKAGE__->many_to_many( native_ports => 'port_vlans_native', 'port' ); -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm b/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm new file mode 100644 index 00000000..9c6cad72 --- /dev/null +++ b/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm @@ -0,0 +1,23 @@ +package Netdisco::DB::ResultSet::Device; +use base 'DBIx::Class::ResultSet'; + +sub carrying_vlan { + my ($set, $vlan) = @_; + return $set unless $vlan and $vlan =~ m/^\d+$/; + + return $set->search( + { + 'vlans.vlan' => $vlan, + 'port_vlans.vlan' => $vlan, + }, + { + join => [qw/ port_vlans vlans /], + prefetch => 'vlans', + order_by => [qw/ me.dns me.ip /], + columns => [qw/ me.ip me.dns me.model me.os me.vendor /], + }, + ); +} + + +1;