package LATMOS::Accounts::Bases::Sql::Netzone; use 5.010000; use strict; use warnings; use base qw(LATMOS::Accounts::Bases::Sql::objects); our $VERSION = (q$Rev: 2104 $ =~ /^Rev: (\d+) /)[0]; =head1 NAME LATMOS::Accounts::Bases::Sql::Netzone - Network management =head1 DESCRIPTION The C object handle information for dns or dhcp management. =cut sub _object_table { 'netzone' } sub _key_field { 'name' } sub _has_extended_attributes { 1 } sub _get_attr_schema { my ($class, $base) = @_; $class->SUPER::_get_attr_schema($base, { name => { ro => 1, inline => 1, }, cn => { ro => 1, inline => 1, iname => 'name' }, date => { ro => 1, inline => 1, }, create => { ro => 1, inline => 1, }, net => { multiple => 1, monitored => 1, }, netExclude => { multiple => 1, monitored => 1, }, group => { hide => 1 }, allow_dyn => { formtype => 'CHECKBOX', ro => sub { $_[0] && ($_[0]->get_attributes('type') || '') eq 'dhcp' ? 0 : 1 }, }, output => {}, outputD => { hide => 1, ro => 1, managed => 1, }, template => {}, templateD => { hide => 1, ro => 1, managed => 1, }, type => { mandatory => 1, can_values => sub { qw(dhcp dns reverse puppet) }, }, site => { delayed => 1, reference => 'site', }, dynFrom => { delayed => 1, multiple => 1, reference => 'netzone', ro => sub { $_[0] && ($_[0]->get_attributes('type') || '') eq 'dhcp' ? 0 : 1 }, }, freeIP => { managed => 1, ro => 1, multiple => 1, }, freeIPCount => { managed => 1, ro => 1, }, ipCount => { managed => 1, ro => 1, }, hosts => { ro => 1, multiple => 1, reference => 'nethost', }, hostsExclude => { ro => 1, multiple => 1, reference => 'nethost' }, lastBuild => { hide => 1, }, lastUpdate => { ro => 1, iname => 'lastBuild' }, zoneRevision => { hide => 1, }, dnsRevision => { ro => 1, iname => 'zoneRevision' }, exported => { formtype => 'CHECKBOX', }, puppetClass => { multiple => 1, }, } ) } sub get_field { my ($self, $field) = @_; if ($field eq 'ipCount') { my $findip = $self->base->db->prepare(q{ select count(*) from ( select host(network(value::inet)+ (generate_series(1, broadcast(value::inet) - network(value::inet) -1))) from netzone_attributes where attr='net' and okey = $1 and family(value::inet) = 4 except ( select host(network(value::inet)+ (generate_series(0, broadcast(value::inet) - network(value::inet)))) from netzone_attributes where attr='netExclude' and okey = $1 ) ) as s }); $findip->execute($self->get_attributes('ikey')); my $res = $findip->fetchrow_hashref; $findip->finish; return $res->{count} } elsif ($field eq 'freeIP') { my @ips; my $findip = $self->base->db->prepare(q{ select * from ( select host(network(value::inet)+ (generate_series(1, broadcast(value::inet) - network(value::inet) -1))) from netzone_attributes where attr='net' and okey = $1 and family(value::inet) = 4 except ( select host(network(value::inet)+ (generate_series(0, broadcast(value::inet) - network(value::inet)))) from netzone_attributes where attr='netExclude' and okey = $1 union select nethost_attributes_ips.value from nethost_attributes_ips join netzone_attributes on netzone_attributes.attr='net' and nethost_attributes_ips.value::inet <<= netzone_attributes.value::inet where netzone_attributes.okey = $1 ) ) as s order by host::inet }); $findip->execute($self->get_attributes('ikey')); while (my $res = $findip->fetchrow_hashref) { push(@ips, $res->{host}); } return scalar(@ips) ? \@ips : undef; } elsif ($field eq 'freeIPCount') { return scalar(grep { $_ } $self->get_attributes('freeIP')); } elsif ($field eq 'hosts') { my $findhost = $self->base->db->prepare_cached(q{ select name from nethost join nethost_attributes_ips on nethost.ikey = nethost_attributes_ips.okey where value::inet <<= any(?) } . ($self->base->{wexported} ? '' : 'and exported = true') . q{ except select name from nethost join nethost_attributes_ips on nethost.ikey = nethost_attributes_ips.okey where value::inet <<= any(?) order by name } ); $findhost->execute( [ $self->get_attributes('net') ], [ $self->get_attributes('netExclude') ], ); my @hosts; while (my $res = $findhost->fetchrow_hashref) { push(@hosts, $res->{name}); } return scalar(@hosts) ? \@hosts : undef; } elsif ($field eq 'hostsExclude') { my $findhost = $self->base->db->prepare_cached(q{ select name from nethost join nethost_attributes_ips on nethost.ikey = nethost_attributes_ips.okey where value::inet <<= any(?) } . ($self->base->{wexported} ? '' : 'and exported = true') . q{ order by name } ); $findhost->execute( [ $self->get_attributes('netExclude') ], ); my @hosts; while (my $res = $findhost->fetchrow_hashref) { push(@hosts, $res->{name}); } return scalar(@hosts) ? \@hosts : undef; } elsif ($field eq 'templateD') { return $self->get_attributes('template') || ($self->id . '.in'); } elsif ($field eq 'outputD') { return $self->get_attributes('output') || $self->id; } else { return $self->SUPER::get_field($field); } } 1; __END__ =head1 SEE ALSO L L =head1 AUTHOR Olivier Thauvin, Eolivier.thauvin@latmos.ipsl.frE =head1 COPYRIGHT AND LICENSE Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available. =cut