File Coverage

blib/lib/App/Manoc/DB.pm
Criterion Covered Total %
statement 9 45 20.0
branch 0 4 0.0
condition n/a
subroutine 3 12 25.0
pod n/a
total 12 61 19.6


line stmt bran cond sub pod time code
1             package App::Manoc::DB;
2             #ABSTRACT: Manoc DB Schema
3              
4 24     24   341590 use strict;
  24         61  
  24         567  
5 24     24   111 use warnings;
  24         49  
  24         1144  
6              
7             our $VERSION = '2.99.2'; ##TRIAL VERSION
8              
9              
10             our $SCHEMA_VERSION = 4;
11              
12             our $DEFAULT_ADMIN_PASSWORD = 'admin';
13              
14 24     24   7609 use parent 'DBIx::Class::Schema';
  24         5106  
  24         117  
15              
16             __PACKAGE__->load_components(
17             qw(
18             Helper::Schema::LintContents
19             Helper::Schema::QuoteNames
20             Helper::Schema::DidYouMean
21              
22             Helper::Schema::Verifier::ColumnInfo
23             Helper::Schema::Verifier::RelationshipColumnName
24             Helper::Schema::Verifier::Parent
25             )
26             );
27              
28             __PACKAGE__->load_namespaces( default_resultset_class => '+App::Manoc::DB::ResultSet', );
29              
30              
31             sub allowed_column_keys {
32 0     0     my $self = shift;
33 0           my @keys = $self->next::method;
34 0           push @keys, qw(encode_class encode_check_method encode_args encode_column
35             ipv4_address
36             extras
37             _inflate_info);
38 0           return @keys;
39             }
40              
41              
42 0     0     sub base_result { 'App::Manoc::DB::Result' }
43              
44              
45 0     0     sub base_resultset { 'App::Manoc::DB::ResultSet' }
46              
47              
48             sub get_version {
49 0     0     return $SCHEMA_VERSION;
50             }
51              
52             our $DEFAULT_CONFIG = {
53             connect_info => {
54             dsn => $ENV{MANOC_DB_DSN} || 'dbi:SQLite:manoc.db',
55             user => $ENV{MANOC_DB_USERNAME} || undef,
56             password => $ENV{MANOC_DB_PASSWORD} || undef,
57              
58             # dbi_attributes
59             quote_names => 1,
60              
61             # extra attributes
62             AutoCommit => 1,
63             },
64             };
65              
66              
67             sub init_admin {
68 0     0     my ($self) = @_;
69              
70 0           my $admin_user = $self->resultset('User')->update_or_create(
71             {
72             username => 'admin',
73             fullname => 'Administrator',
74             active => 1,
75             password => $DEFAULT_ADMIN_PASSWORD,
76             superadmin => 1,
77             agent => 0,
78             }
79             );
80             }
81              
82              
83             sub init_vlan {
84 0     0     my ($self) = @_;
85              
86 0           my $rs = $self->resultset('VlanRange');
87 0 0         if ( $rs->count() > 0 ) {
88 0           return;
89             }
90 0           my $vlan_range = $rs->update_or_create(
91             {
92             name => 'sample',
93             description => 'sample range',
94             start => 1,
95             end => 10,
96             }
97             );
98 0           $vlan_range->add_to_vlans( { name => 'native', id => 1 } );
99             }
100              
101              
102             sub init_ipnetwork {
103 0     0     my ($self) = @_;
104              
105 0           my $rs = $self->resultset('IPNetwork');
106              
107 0 0         $rs->count() > 0 and return;
108 0           $rs->update_or_create(
109             {
110             address => App::Manoc::IPAddress::IPv4->new("10.10.0.0"),
111             prefix => 16,
112             name => 'My Corp network'
113             }
114             );
115 0           $rs->update_or_create(
116             {
117             address => App::Manoc::IPAddress::IPv4->new("10.10.0.0"),
118             prefix => 22,
119             name => 'Server Farm'
120             }
121             );
122 0           $rs->update_or_create(
123             {
124             address => App::Manoc::IPAddress::IPv4->new("10.10.0.0"),
125             prefix => 24,
126             name => 'Yellow zone'
127             }
128             );
129 0           $rs->update_or_create(
130             {
131             address => App::Manoc::IPAddress::IPv4->new("10.10.0.0"),
132             prefix => 24,
133             name => 'Yellow zone'
134             }
135             );
136 0           $rs->update_or_create(
137             {
138             address => App::Manoc::IPAddress::IPv4->new("10.10.1.0"),
139             prefix => 24,
140             name => 'Green zone'
141             }
142             );
143 0           $rs->update_or_create(
144             {
145             address => App::Manoc::IPAddress::IPv4->new("10.10.5.0"),
146             prefix => 23,
147             name => 'Workstations'
148             }
149             );
150             }
151              
152              
153             sub init_roles {
154 0     0     my ( $self, $conf_roles ) = @_;
155              
156 0           my $rs = $self->resultset('Role');
157              
158 0           my $default_roles = \%App::Manoc::CatalystRole::Permission::DEFAULT_ROLES;
159 0           my $roles = Catalyst::Utils::merge_hashes( $default_roles, $conf_roles );
160              
161 0           foreach my $role ( keys %$roles ) {
162 0           $rs->update_or_create( { role => $role } );
163             }
164              
165             }
166              
167              
168             sub init_management_url {
169 0     0     my ($self) = @_;
170 0           my $rs = $self->resultset('MngUrlFormat');
171 0           $rs->update_or_create(
172             {
173             name => 'telnet',
174             format => 'telnet:%h',
175             }
176             );
177 0           $rs->update_or_create(
178             {
179             name => 'ssh',
180             format => 'ssh:%h',
181             }
182             );
183 0           $rs->update_or_create(
184             {
185             name => 'http',
186             format => 'http://:%h',
187             }
188             );
189 0           $rs->update_or_create(
190             {
191             name => 'https',
192             format => 'https://:%h',
193             }
194             );
195             }
196              
197             1;
198              
199             __END__
200              
201             =pod
202              
203             =head1 NAME
204              
205             App::Manoc::DB - Manoc DB Schema
206              
207             =head1 VERSION
208              
209             version 2.99.2
210              
211             =head1 DESCRIPTION
212              
213             Manoc DB Schema extends C<DBIx::Class::Schema>.
214              
215             It also loads the required L<DBIx::Class::Helper> components and
216             provides methods for schema configuration and initialization.
217              
218             =head1 METHODS
219              
220             =head2 allowed_column_keys
221              
222             This method is overriden from
223             C<DBIx::Class::Helper::Schema::Verifier::ColumnInfo> to add some
224             non-standard confg keys used by Manoc
225              
226             =head2 init_admin
227              
228             Create or reset admin user.
229              
230             =head2 init_vlan
231              
232             When there is no defined VlanRange create a sample range with a sample
233             vlan.
234              
235             =head2 init_ipnetwork
236              
237             Whene there is no defined IPNetwork rows create some sample networks
238             and subnetworks.
239              
240             =head2 init_roles
241              
242             Populate Role rows based on Manoc default roles defined in L<App::Manoc::CatalystRole::Permission>
243              
244             =head2 init_management_url
245              
246             Create some sample MngUrlFormat rows.
247              
248             =head1 FUNCTIONS
249              
250             =head2 base_result
251              
252             Return C<'App::Manoc::DB::Result'>
253              
254             =head2 base_resultset
255              
256             Return C<'App::Manoc::DB::ResultSet'>
257              
258             =head2 get_version
259              
260             Return the current schema version. Used tools like datadumper.
261              
262             =head1 AUTHORS
263              
264             =over 4
265              
266             =item *
267              
268             Gabriele Mambrini <gmambro@cpan.org>
269              
270             =item *
271              
272             Enrico Liguori
273              
274             =back
275              
276             =head1 COPYRIGHT AND LICENSE
277              
278             This software is copyright (c) 2017 by Gabriele Mambrini.
279              
280             This is free software; you can redistribute it and/or modify it under
281             the same terms as the Perl 5 programming language system itself.
282              
283             =cut