1 | package LATMOS::Accounts::Bases::Sql::Netzone; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
8 | |
---|
9 | our $VERSION = (q$Rev: 2104 $ =~ /^Rev: (\d+) /)[0]; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::Accounts::Bases::Sql::Netzone - Network management |
---|
14 | |
---|
15 | =head1 DESCRIPTION |
---|
16 | |
---|
17 | The C<Netzone> object handle information for dns or dhcp management. |
---|
18 | |
---|
19 | =cut |
---|
20 | |
---|
21 | sub _object_table { 'netzone' } |
---|
22 | |
---|
23 | sub _key_field { 'name' } |
---|
24 | |
---|
25 | sub _has_extended_attributes { 1 } |
---|
26 | |
---|
27 | sub _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, notify => 1, }, |
---|
37 | netExclude => { multiple => 1, notify => 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 => { hide => 1, ro => 1, managed => 1, }, |
---|
47 | template => {}, |
---|
48 | templateD => { hide => 1, ro => 1, managed => 1, }, |
---|
49 | type => { |
---|
50 | mandatory => 1, |
---|
51 | can_values => sub { qw(dhcp dns reverse puppet) }, |
---|
52 | }, |
---|
53 | site => { |
---|
54 | delayed => 1, |
---|
55 | reference => 'site', |
---|
56 | }, |
---|
57 | dynFrom => { |
---|
58 | delayed => 1, |
---|
59 | multiple => 1, |
---|
60 | reference => 'netzone', |
---|
61 | ro => sub { |
---|
62 | $_[0] && ($_[0]->get_attributes('type') || '') eq 'dhcp' ? 0 : 1 |
---|
63 | }, |
---|
64 | }, |
---|
65 | freeIP => { managed => 1, ro => 1, multiple => 1, }, |
---|
66 | freeIPCount => { managed => 1, ro => 1, }, |
---|
67 | ipCount => { managed => 1, ro => 1, }, |
---|
68 | hosts => { ro => 1, multiple => 1, reference => 'nethost', }, |
---|
69 | hostsExclude => { ro => 1, multiple => 1, reference => 'nethost' }, |
---|
70 | lastBuild => { hide => 1, }, |
---|
71 | lastUpdate => { ro => 1, iname => 'lastBuild' }, |
---|
72 | zoneRevision => { hide => 1, }, |
---|
73 | dnsRevision => { ro => 1, iname => 'zoneRevision' }, |
---|
74 | exported => { formtype => 'CHECKBOX', }, |
---|
75 | puppetClass => { multiple => 1, }, |
---|
76 | } |
---|
77 | ) |
---|
78 | } |
---|
79 | |
---|
80 | sub get_field { |
---|
81 | my ($self, $field) = @_; |
---|
82 | if ($field eq 'ipCount') { |
---|
83 | my $findip = $self->base->db->prepare(q{ |
---|
84 | select count(*) from ( |
---|
85 | select host(network(value::inet)+ (generate_series(1, broadcast(value::inet) - network(value::inet) -1))) from netzone_attributes where |
---|
86 | attr='net' and okey = $1 and family(value::inet) = 4 |
---|
87 | except |
---|
88 | ( |
---|
89 | select host(network(value::inet)+ (generate_series(0, broadcast(value::inet) - network(value::inet)))) from |
---|
90 | netzone_attributes where attr='netExclude' and okey = $1 |
---|
91 | ) |
---|
92 | ) as s |
---|
93 | }); |
---|
94 | $findip->execute($self->get_attributes('ikey')); |
---|
95 | my $res = $findip->fetchrow_hashref; |
---|
96 | $findip->finish; |
---|
97 | return $res->{count} |
---|
98 | } elsif ($field eq 'freeIP') { |
---|
99 | my @ips; |
---|
100 | my $findip = $self->base->db->prepare(q{ |
---|
101 | select * from ( |
---|
102 | select host(network(value::inet)+ (generate_series(1, broadcast(value::inet) - network(value::inet) -1))) from netzone_attributes where |
---|
103 | attr='net' and okey = $1 and family(value::inet) = 4 |
---|
104 | except |
---|
105 | ( |
---|
106 | select host(network(value::inet)+ (generate_series(0, broadcast(value::inet) - network(value::inet)))) from |
---|
107 | netzone_attributes where attr='netExclude' and okey = $1 |
---|
108 | union |
---|
109 | select nethost_attributes_ips.value from nethost_attributes_ips join netzone_attributes on |
---|
110 | netzone_attributes.attr='net' and nethost_attributes_ips.value::inet <<= netzone_attributes.value::inet |
---|
111 | where netzone_attributes.okey = $1 |
---|
112 | ) |
---|
113 | ) as s |
---|
114 | order by host::inet |
---|
115 | }); |
---|
116 | $findip->execute($self->get_attributes('ikey')); |
---|
117 | while (my $res = $findip->fetchrow_hashref) { |
---|
118 | push(@ips, $res->{host}); |
---|
119 | } |
---|
120 | return scalar(@ips) ? \@ips : undef; |
---|
121 | } elsif ($field eq 'freeIPCount') { |
---|
122 | return scalar(grep { $_ } $self->get_attributes('freeIP')); |
---|
123 | } elsif ($field eq 'hosts') { |
---|
124 | my $findhost = $self->base->db->prepare_cached(q{ |
---|
125 | select name from nethost join nethost_attributes_ips on |
---|
126 | nethost.ikey = nethost_attributes_ips.okey |
---|
127 | where value::inet <<= any(?) |
---|
128 | } . ($self->base->{wexported} ? '' : 'and exported = true') . |
---|
129 | q{ except |
---|
130 | select name from nethost join nethost_attributes_ips on |
---|
131 | nethost.ikey = nethost_attributes_ips.okey |
---|
132 | where value::inet <<= any(?) |
---|
133 | order by name |
---|
134 | } |
---|
135 | ); |
---|
136 | $findhost->execute( |
---|
137 | [ $self->get_attributes('net') ], |
---|
138 | [ $self->get_attributes('netExclude') ], |
---|
139 | ); |
---|
140 | my @hosts; |
---|
141 | while (my $res = $findhost->fetchrow_hashref) { |
---|
142 | push(@hosts, $res->{name}); |
---|
143 | } |
---|
144 | return scalar(@hosts) ? \@hosts : undef; |
---|
145 | } elsif ($field eq 'hostsExclude') { |
---|
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{ |
---|
152 | order by name |
---|
153 | } |
---|
154 | ); |
---|
155 | $findhost->execute( |
---|
156 | [ $self->get_attributes('netExclude') ], |
---|
157 | ); |
---|
158 | my @hosts; |
---|
159 | while (my $res = $findhost->fetchrow_hashref) { |
---|
160 | push(@hosts, $res->{name}); |
---|
161 | } |
---|
162 | return scalar(@hosts) ? \@hosts : undef; |
---|
163 | } elsif ($field eq 'templateD') { |
---|
164 | return $self->get_attributes('template') || ($self->id . '.in'); |
---|
165 | } elsif ($field eq 'outputD') { |
---|
166 | return $self->get_attributes('output') || $self->id; |
---|
167 | } else { |
---|
168 | return $self->SUPER::get_field($field); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | 1; |
---|
173 | |
---|
174 | __END__ |
---|
175 | |
---|
176 | =head1 SEE ALSO |
---|
177 | |
---|
178 | L<LATMOS::Accounts::Bases::Sql> |
---|
179 | L<LATMOS::Accounts::Bases::Sql::Nethost> |
---|
180 | |
---|
181 | =head1 AUTHOR |
---|
182 | |
---|
183 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
184 | |
---|
185 | =head1 COPYRIGHT AND LICENSE |
---|
186 | |
---|
187 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
188 | |
---|
189 | This library is free software; you can redistribute it and/or modify |
---|
190 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
191 | at your option, any later version of Perl 5 you may have available. |
---|
192 | |
---|
193 | |
---|
194 | =cut |
---|