source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Nethost.pm @ 1374

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

Readd ip6 attribute

File size: 8.3 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Nethost;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Sql::objects);
8use LATMOS::Accounts::Log;
9
10our $VERSION = (q$Rev: 2104 $ =~ /^Rev: (\d+) /)[0];
11
12=head1 NAME
13
14LATMOS::Accounts::Bases::Sql::Nethost - A Network Host entry
15
16=cut
17
18sub _object_table { 'nethost' }
19
20sub _key_field { 'name' }
21
22sub _has_extended_attributes { 1 }
23
24sub _get_attr_schema {
25    my ($class, $base) = @_;
26
27    $class->SUPER::_get_attr_schema($base,
28        {
29            name    => { ro => 1, inline => 1, },
30            cn      => { ro => 1, inline => 1, iname => 'name' },
31            date    => { ro => 1, inline => 1, },
32            create  => { ro => 1, inline => 1, },
33            ip      => {
34                monitored => 1,
35                multiple => 1,
36                uniq => 1,
37                input => sub {
38                    my $ip = $_[0];
39                    $ip =~ s/(\D|^)0+/$1/g;
40                    $ip
41                },
42            },
43            ipFrom  => {
44                multiple => 1, 
45                managed => 1,
46                can_values => sub {
47                    $base->search_objects('netzone', 'type=dhcp')
48                },
49                set => sub {
50                    my ($self, $data) = @_;
51                    foreach (ref $data ? @{$data} : $data) {
52                        my $zone = $self->base->get_object('netzone', $_)
53                            or next;
54                        my @freeips = $zone->get_attributes('freeIP') or next;
55                        my $idx = rand(scalar(@freeips));
56                        $self->object->_addAttributeValue('ip', $freeips[$idx]);
57                        $self->base->log(LA_NOTICE, "Assigning ip %s to host %s",
58                            $freeips[$idx], $self->object->id);
59                    }
60                },
61            },
62            macaddr => {
63                monitored => 1,
64                multiple => 1,
65                uniq => 1,
66                input => sub {
67                    my @elem = split(/[:-]/, $_[0]);
68                    return join(':', map { sprintf("%02x", hex($_)) } @elem);
69                },
70                checkinput => sub {
71                    return $_[0] =~ /^([0-9a-f]{2}([:-]|$)){6}$/i ? 1 : undef;
72                },
73            },
74            cname   => {
75                monitored => 1,
76                multiple => 1, uniq => 1,
77                input => sub { lc($_[0]) } 
78            },
79            related   => {
80                multiple => 1, uniq => 1,
81                reference => 'nethost',
82            },
83            owner   => {
84                monitored => 1,
85                reference => 'user',
86                delayed => 1,
87            },
88            user    => {
89                reference => 'user',
90                delayed => 1,
91            },
92            netZone => {
93                multiple => 1, ro => 1, managed => 1,
94                reference => 'netzone',
95                get => sub {
96                    my ($self) = @_;
97                    my $find = $self->base->db->prepare_cached(q{
98                        select name from netzone where ikey in
99                        (
100                        select netzone_attributes.okey from netzone_attributes join
101                        nethost_attributes_ips on netzone_attributes.attr='net'
102                            and
103                        nethost_attributes_ips.value::inet <<=
104                        netzone_attributes.value::inet
105                        where nethost_attributes_ips.okey = $1
106                        except
107                        select netzone_attributes.okey from netzone_attributes join
108                        nethost_attributes_ips on netzone_attributes.attr='netExclude'
109                            and nethost_attributes_ips.value::inet <<= netzone_attributes.value::inet
110                        where nethost_attributes_ips.okey = $1
111                        )
112                        order by name
113                        });
114                    $find->execute($self->object->get_attributes('ikey'));
115                    my @zones;
116                    while (my $res = $find->fetchrow_hashref) {
117                        push(@zones, $res->{name});
118                    }
119                    return @zones ? [ @zones ] : undef;
120                },
121            },
122            puppetClass => { multiple => 1, },
123            netZoneExclude => {
124                multiple => 1, ro => 1, managed => 1,
125                reference => 'netzone',
126                get => sub {
127                    my ($self) = @_;
128                    my $find = $self->base->db->prepare_cached(q{
129                        select name from netzone where ikey in
130                        (
131                        select netzone_attributes.okey from netzone_attributes join
132                        nethost_attributes_ips on netzone_attributes.attr='netExclude'
133                            and
134                        nethost_attributes_ips.value::inet <<=
135                        netzone_attributes.value::inet
136                        where nethost_attributes_ips.okey = $1
137                        )
138                            order by name
139                        });
140                    $find->execute($self->object->get_attributes('ikey'));
141                    my @zones;
142                    while (my $res = $find->fetchrow_hashref) {
143                        push(@zones, $res->{name});
144                    }
145                    return @zones ? [ @zones ] : undef;
146                },
147            },
148            noDynamic => { 
149                formtype => 'CHECKBOX',
150            },
151            exported => { formtype => 'CHECKBOX', },
152            noInheritPuppet => { formtype => 'CHECKBOX', },
153            otherName => {
154                multiple => 1,
155                input => sub { lc($_[0]) },
156            },
157            sshfp => { multiple => 1, },
158            ip6 => { },
159            sshfpUpdate  => {
160                managed => 1,
161                iname => 'sshfp',
162                input => sub {
163                    my ($self, $data) = @_;
164                    open(my $h, '<', $data) or return;
165                    my @sshfps;
166                    while (my $line = <$h>) {
167                        my ($type, $mode, $key) = $line =~ /^\S+\s+IN\s+SSHFP\s+(\d)\s+(\d)\s+(\S+)/i
168                            or do {
169                            la_log(LA_ERR, "Seems to not be a ssh-keygen line: %s", $line);
170                            return;
171                        };
172                        la_log(LA_DEBUG, "found sshfp %s %s %s", $type, $mode, $key);
173                        push(@sshfps, "$type $mode $key");
174                    }
175                    close($h);
176                    \@sshfps;
177                }
178            },
179            comment => { },
180            description => { },
181            encryptKey => { },
182            reverse => { },
183            serialNumber => { },
184        }
185    )
186}
187
188sub set_fields {
189    my ($self, %data) = @_;
190
191    my $res = $self->SUPER::set_fields(%data);
192
193    # Post update related
194    if (exists $data{related}) {
195        my %exists;
196        if ($data{related}) {
197            my @related = ref $data{related} ? @{$data{related}} : $data{related};
198            foreach (@related) {
199                my $obj = $self->base->get_object('nethost', $_) or next;
200                $exists{$obj->id} = 1; 
201                my @current = grep { $_ } $obj->get_attributes('related');
202                grep { $_ eq $self->id } @current and next;
203                $obj->set_c_fields('related' => [ $self->id, @current ]);
204
205            }
206        }
207        foreach ($self->base->search_objects('nethost', 'related=' . $self->id)) {
208            my $obj = $self->base->get_object('nethost', $_) or next;
209            $exists{$_} and next;
210            my @current = grep { $_ eq $self->id } $obj->get_attributes('related');
211            $obj->set_c_fields('related' => [ @current ]);
212        }
213    }
214
215    $res
216}
217
2181;
219
220__END__
221
222=head1 SEE ALSO
223
224L<LATMOS::Accounts::Bases::Sql::Netzones>, L<LATMOS::Accounts::Bases::Sql>
225
226=head1 AUTHOR
227
228Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
229
230=head1 COPYRIGHT AND LICENSE
231
232Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
233
234This library is free software; you can redistribute it and/or modify
235it under the same terms as Perl itself, either Perl version 5.10.0 or,
236at your option, any later version of Perl 5 you may have available.
237
238
239=cut
Note: See TracBrowser for help on using the repository browser.