File Coverage

lib/Rex/CMDB/Base.pm
Criterion Covered Total %
statement 37 42 88.1
branch 8 8 100.0
condition 3 8 37.5
subroutine 10 11 90.9
pod 0 1 0.0
total 58 70 82.8


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::CMDB::Base;
6              
7 6     6   104 use v5.12.5;
  6         22  
8 6     6   35 use warnings;
  6         25  
  6         307  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 6     6   41 use Rex::Helper::Path;
  6         22  
  6         569  
13 6     6   47 use Rex::Hardware;
  6         24  
  6         79  
14 6     6   197 use Rex::Hardware::Host;
  6         40  
  6         58  
15              
16             sub new {
17 0     0 0 0 my $that = shift;
18 0   0     0 my $proto = ref($that) || $that;
19 0         0 my $self = {@_};
20              
21 0         0 bless( $self, $proto );
22              
23 0         0 return $self;
24             }
25              
26             sub _parse_path {
27 156     156   835 my ( $self, $path, $mapping ) = @_;
28              
29 156         782 return parse_path( $path, $mapping );
30             }
31              
32             sub __get_hostname_for {
33 107     107   293 my ( $self, $server ) = @_;
34              
35 107   66     481 my $hostname = $server // Rex::get_current_connection()->{conn}->server->to_s;
36              
37 107 100       319 if ( $hostname eq '' ) {
38 29         519 my %hw_info = Rex::Hardware->get('Host');
39 29         447 $hostname = $hw_info{Host}{hostname};
40             }
41              
42 107         696 return $hostname;
43             }
44              
45             sub __warm_up_cache_for {
46 25     25   96 my ( $self, $server ) = @_;
47              
48 25         110 $server = $self->__get_hostname_for($server);
49 25         464 my $cache_key = $self->__cache_key("cmdb/$self/$server");
50              
51 25 100       207 if ( !$self->__cache->valid($cache_key) ) {
52 18   50     244 my $cmdb = $self->get( undef, $server ) || undef;
53 18         265 $self->__cache->set( $cache_key, $cmdb );
54             }
55              
56 25         121 return $self->__cache;
57             }
58              
59             sub __cache_key {
60 78     78   245 my ( $self, $key ) = @_;
61              
62 78 100       206 if ( defined $key ) {
63 25         92 $self->{__cache_key} = $key;
64             }
65              
66 78         352 return $self->{__cache_key};
67             }
68              
69             sub __cache {
70 121     121   301 my ($self) = @_;
71              
72 121 100       368 if ( !defined $self->{__cache} ) {
73 6         34 $self->{__cache} = Rex::get_cache();
74             }
75              
76 121         877 return $self->{__cache};
77             }
78              
79             1;