[861] | 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); |
---|
[1551] | 8 | use LATMOS::Accounts::I18N; |
---|
[861] | 9 | |
---|
| 10 | our $VERSION = (q$Rev: 2104 $ =~ /^Rev: (\d+) /)[0]; |
---|
| 11 | |
---|
| 12 | =head1 NAME |
---|
| 13 | |
---|
[1023] | 14 | LATMOS::Accounts::Bases::Sql::Netzone - Network management |
---|
[861] | 15 | |
---|
| 16 | =head1 DESCRIPTION |
---|
| 17 | |
---|
[1023] | 18 | The C<Netzone> object handle information for dns or dhcp management. |
---|
[861] | 19 | |
---|
| 20 | =cut |
---|
| 21 | |
---|
[1014] | 22 | sub _object_table { 'netzone' } |
---|
[861] | 23 | |
---|
[1014] | 24 | sub _key_field { 'name' } |
---|
[861] | 25 | |
---|
[1014] | 26 | sub _has_extended_attributes { 1 } |
---|
[861] | 27 | |
---|
| 28 | sub _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, }, |
---|
[1297] | 37 | net => { multiple => 1, monitored => 1, }, |
---|
| 38 | netExclude => { multiple => 1, monitored => 1, }, |
---|
[861] | 39 | group => { hide => 1 }, |
---|
| 40 | allow_dyn => { |
---|
| 41 | formtype => 'CHECKBOX', |
---|
| 42 | ro => sub { |
---|
[2056] | 43 | $_[0] && ($_[0]->get_attributes('type') || '') =~ /dhcp|radius/ ? 0 : 1 |
---|
[861] | 44 | }, |
---|
| 45 | }, |
---|
| 46 | output => {}, |
---|
[1315] | 47 | outputD => { |
---|
| 48 | hide => 1, |
---|
| 49 | ro => 1, |
---|
| 50 | managed => 1, |
---|
| 51 | get => sub { |
---|
| 52 | $_[0]->object->_get_c_field('output') || |
---|
[1525] | 53 | ($_[0]->object->id . (($_[0]->object->get_attributes('type') || '') eq 'puppet' ? '.pp' : '')) |
---|
[1315] | 54 | }, |
---|
| 55 | }, |
---|
[861] | 56 | template => {}, |
---|
[1315] | 57 | templateD => { |
---|
| 58 | hide => 1, |
---|
| 59 | ro => 1, |
---|
| 60 | managed => 1, |
---|
| 61 | get => sub { |
---|
| 62 | $_[0]->object->_get_c_field('template') || |
---|
[1361] | 63 | $_[0]->object->id . '.in' |
---|
[1315] | 64 | }, |
---|
| 65 | }, |
---|
[861] | 66 | type => { |
---|
| 67 | mandatory => 1, |
---|
[2056] | 68 | can_values => sub { qw(dhcp dns reverse puppet radius) }, |
---|
[861] | 69 | }, |
---|
| 70 | site => { |
---|
| 71 | delayed => 1, |
---|
| 72 | reference => 'site', |
---|
| 73 | }, |
---|
| 74 | dynFrom => { |
---|
| 75 | delayed => 1, |
---|
| 76 | multiple => 1, |
---|
| 77 | reference => 'netzone', |
---|
| 78 | ro => sub { |
---|
[2056] | 79 | $_[0] && ($_[0]->get_attributes('type') || '') =~ /^(dhcp|radius)$/ ? 0 : 1 |
---|
[861] | 80 | }, |
---|
| 81 | }, |
---|
[1315] | 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 |
---|
[2187] | 96 | netzone_attributes where attr='netExclude' and okey = $1 and family(netzone_attributes.value::inet) = 4 |
---|
[1315] | 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 |
---|
[2187] | 100 | where netzone_attributes.okey = $1 and family(netzone_attributes.value::inet) = 4 |
---|
[1315] | 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 |
---|
[2187] | 131 | netzone_attributes where attr='netExclude' and okey = $1 and family(netzone_attributes.value::inet) = 4 |
---|
[1315] | 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, |
---|
[1461] | 144 | managed => 1, |
---|
[1315] | 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, |
---|
[1461] | 174 | managed => 1, |
---|
[1315] | 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 | }, |
---|
[861] | 197 | lastBuild => { hide => 1, }, |
---|
| 198 | lastUpdate => { ro => 1, iname => 'lastBuild' }, |
---|
| 199 | zoneRevision => { hide => 1, }, |
---|
| 200 | dnsRevision => { ro => 1, iname => 'zoneRevision' }, |
---|
| 201 | exported => { formtype => 'CHECKBOX', }, |
---|
[1064] | 202 | puppetClass => { multiple => 1, }, |
---|
[1315] | 203 | domain => { }, |
---|
| 204 | description => { }, |
---|
[2056] | 205 | hostParams => { multiple => 1 }, |
---|
[2569] | 206 | radiusParams => { iname => 'hostParams', ro => 1 }, |
---|
[2565] | 207 | vlan => { }, |
---|
[861] | 208 | } |
---|
| 209 | ) |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | 1; |
---|
| 213 | |
---|
| 214 | __END__ |
---|
| 215 | |
---|
| 216 | =head1 SEE ALSO |
---|
| 217 | |
---|
[1023] | 218 | L<LATMOS::Accounts::Bases::Sql> |
---|
| 219 | L<LATMOS::Accounts::Bases::Sql::Nethost> |
---|
| 220 | |
---|
[861] | 221 | =head1 AUTHOR |
---|
| 222 | |
---|
| 223 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 224 | |
---|
| 225 | =head1 COPYRIGHT AND LICENSE |
---|
| 226 | |
---|
| 227 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
| 228 | |
---|
| 229 | This library is free software; you can redistribute it and/or modify |
---|
| 230 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 231 | at your option, any later version of Perl 5 you may have available. |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | =cut |
---|