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

Last change on this file since 1427 was 1361, checked in by nanardon, 9 years ago

Fix templateD attribute default value

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