source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Netzone.pm @ 2569

Last change on this file since 2569 was 2569, checked in by nanardon, 13 months ago

Manage par host radius parameters

File size: 8.7 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Netzone;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Sql::objects);
8use LATMOS::Accounts::I18N;
9
10our $VERSION = (q$Rev: 2104 $ =~ /^Rev: (\d+) /)[0];
11
12=head1 NAME
13
14LATMOS::Accounts::Bases::Sql::Netzone - Network management
15
16=head1 DESCRIPTION
17
18The C<Netzone> object handle information for dns or dhcp management.
19
20=cut
21
22sub _object_table { 'netzone' }
23
24sub _key_field { 'name' }
25
26sub _has_extended_attributes { 1 }
27
28sub _get_attr_schema {
29    my ($class, $base) = @_;
30
31    $class->SUPER::_get_attr_schema($base,
32        {
33            name    => { ro => 1, inline => 1, },
34            cn      => { ro => 1, inline => 1, iname => 'name' },
35            date    => { ro => 1, inline => 1, },
36            create  => { ro => 1, inline => 1, },
37            net     => { multiple => 1, monitored => 1, },
38            netExclude => { multiple => 1, monitored => 1, },
39            group   => { hide => 1 },
40            allow_dyn => {
41                formtype => 'CHECKBOX',
42                ro => sub { 
43                    $_[0] && ($_[0]->get_attributes('type') || '') =~ /dhcp|radius/ ? 0 : 1
44                },
45            },
46            output => {},
47            outputD => {
48                hide => 1,
49                ro => 1,
50                managed => 1,
51                get => sub {
52                    $_[0]->object->_get_c_field('output') ||
53                    ($_[0]->object->id . (($_[0]->object->get_attributes('type') || '') eq 'puppet' ? '.pp' : ''))
54                },
55            },
56            template => {},
57            templateD => {
58                hide => 1,
59                ro => 1,
60                managed => 1,
61                get => sub {
62                    $_[0]->object->_get_c_field('template') ||
63                    $_[0]->object->id . '.in'
64                },
65            },
66            type    => {
67                mandatory => 1,
68                can_values => sub { qw(dhcp dns reverse puppet radius) },
69            },
70            site   => {
71                delayed => 1,
72                reference => 'site',
73            },
74            dynFrom   => {
75                delayed => 1,
76                multiple => 1,
77                reference => 'netzone',
78                ro => sub { 
79                    $_[0] && ($_[0]->get_attributes('type') || '') =~ /^(dhcp|radius)$/ ? 0 : 1
80                },
81            },
82            freeIP => {
83                managed => 1,
84                ro => 1,
85                multiple => 1,
86                get => sub {
87                    my $self = $_[0]->object;
88                    my @ips;
89                    my $findip = $self->base->db->prepare(q{
90                        select * from (
91                        select host(network(value::inet)+ (generate_series(1, broadcast(value::inet) - network(value::inet) -1))) from netzone_attributes where
92                        attr='net' and okey = $1 and family(value::inet) = 4
93                        except
94                        (
95                        select host(network(value::inet)+ (generate_series(0, broadcast(value::inet) - network(value::inet)))) from
96                        netzone_attributes where attr='netExclude' and okey = $1 and family(netzone_attributes.value::inet) = 4
97                        union
98                        select nethost_attributes_ips.value from nethost_attributes_ips join netzone_attributes on
99                        netzone_attributes.attr='net' and nethost_attributes_ips.value::inet <<= netzone_attributes.value::inet
100                        where netzone_attributes.okey = $1 and family(netzone_attributes.value::inet) = 4
101                        )
102                        ) as s
103                        order by host::inet
104                        });
105                    $findip->execute($self->get_attributes('ikey'));
106                    while (my $res = $findip->fetchrow_hashref) {
107                        push(@ips, $res->{host});
108                    }
109                    return scalar(@ips) ? \@ips : undef;
110                },
111            },
112            freeIPCount => {
113                managed => 1,
114                ro => 1,
115                get => sub {
116                    return scalar(grep { $_ } $_[0]->object->get_attributes('freeIP'));
117                },
118            },
119            ipCount => {
120                managed => 1,
121                ro => 1,
122                get => sub {
123                    my ($self) = $_[0]->object;
124                    my $findip = $self->base->db->prepare(q{
125                        select count(*) from (
126                        select host(network(value::inet)+ (generate_series(1, broadcast(value::inet) - network(value::inet) -1))) from netzone_attributes where
127                        attr='net' and okey = $1 and family(value::inet) = 4
128                        except
129                        (
130                        select host(network(value::inet)+ (generate_series(0, broadcast(value::inet) - network(value::inet)))) from
131                        netzone_attributes where attr='netExclude' and okey = $1 and family(netzone_attributes.value::inet) = 4
132                        )
133                        ) as s
134                        });
135                    $findip->execute($self->get_attributes('ikey'));
136                    my $res = $findip->fetchrow_hashref;
137                    $findip->finish;
138                    return $res->{count}
139                },
140            },
141            hosts =>  {
142                ro => 1,
143                multiple => 1,
144                managed => 1,
145                reference => 'nethost',
146                get => sub {
147                    my ($self) = $_[0]->object;
148                    my $findhost = $self->base->db->prepare_cached(q{
149                        select name from nethost join nethost_attributes_ips on
150                        nethost.ikey = nethost_attributes_ips.okey
151                        where value::inet <<= any(?)
152                        } . ($self->base->{wexported} ? '' : 'and exported = true') .
153                        q{ except
154                        select name from nethost join nethost_attributes_ips on
155                        nethost.ikey = nethost_attributes_ips.okey
156                        where value::inet <<= any(?)
157                        order by name
158                        }
159                    );
160                    $findhost->execute(
161                        [ $self->get_attributes('net') ],
162                        [ $self->get_attributes('netExclude') ],
163                    );
164                    my @hosts;
165                    while (my $res = $findhost->fetchrow_hashref) {
166                        push(@hosts, $res->{name});
167                    }
168                    return scalar(@hosts) ? \@hosts : undef;
169                },
170            },
171            hostsExclude =>  {
172                ro => 1,
173                multiple => 1,
174                managed => 1,
175                reference => 'nethost',
176                get => sub {
177                    my $self = $_[0]->object;
178                    my $findhost = $self->base->db->prepare_cached(q{
179                        select name from nethost join nethost_attributes_ips on
180                        nethost.ikey = nethost_attributes_ips.okey
181                        where value::inet <<= any(?)
182                        } . ($self->base->{wexported} ? '' : 'and exported = true') .
183                        q{
184                        order by name
185                        }
186                    );
187                    $findhost->execute(
188                        [ $self->get_attributes('netExclude') ],
189                    );
190                    my @hosts;
191                    while (my $res = $findhost->fetchrow_hashref) {
192                        push(@hosts, $res->{name});
193                    }
194                    return scalar(@hosts) ? \@hosts : undef;
195                },
196            },
197            lastBuild => { hide => 1, },
198            lastUpdate => { ro => 1, iname => 'lastBuild' },
199            zoneRevision => { hide => 1, },
200            dnsRevision =>  { ro => 1, iname => 'zoneRevision' },
201            exported => { formtype => 'CHECKBOX', },
202            puppetClass =>  { multiple => 1, },
203            domain => { },
204            description => { },
205            hostParams => { multiple => 1 }, 
206            radiusParams => { iname => 'hostParams', ro => 1 },
207            vlan => { },
208        }
209    )
210}
211
2121;
213
214__END__
215
216=head1 SEE ALSO
217
218L<LATMOS::Accounts::Bases::Sql>
219L<LATMOS::Accounts::Bases::Sql::Nethost>
220
221=head1 AUTHOR
222
223Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
224
225=head1 COPYRIGHT AND LICENSE
226
227Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
228
229This library is free software; you can redistribute it and/or modify
230it under the same terms as Perl itself, either Perl version 5.10.0 or,
231at your option, any later version of Perl 5 you may have available.
232
233
234=cut
Note: See TracBrowser for help on using the repository browser.