add initial DBIC relations for vlan search

This commit is contained in:
Oliver Gorwits
2012-01-07 17:31:59 +00:00
parent 9213c013f8
commit e9470a14f3
6 changed files with 89 additions and 4 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
Netdisco/environments/development.yml
test.pl

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;