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   84 use v5.12.5;
  6         26  
8 6     6   47 use warnings;
  6         15  
  6         279  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 6     6   37 use Rex::Helper::Path;
  6         16  
  6         566  
13 6     6   45 use Rex::Hardware;
  6         16  
  6         64  
14 6     6   251 use Rex::Hardware::Host;
  6         12  
  6         48  
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   873 my ( $self, $path, $mapping ) = @_;
28              
29 156         782 return parse_path( $path, $mapping );
30             }
31              
32             sub __get_hostname_for {
33 107     107   335 my ( $self, $server ) = @_;
34              
35 107   66     595 my $hostname = $server // Rex::get_current_connection()->{conn}->server->to_s;
36              
37 107 100       336 if ( $hostname eq '' ) {
38 29         546 my %hw_info = Rex::Hardware->get('Host');
39 29         395 $hostname = $hw_info{Host}{hostname};
40             }
41              
42 107         720 return $hostname;
43             }
44              
45             sub __warm_up_cache_for {
46 25     25   109 my ( $self, $server ) = @_;
47              
48 25         108 $server = $self->__get_hostname_for($server);
49 25         434 my $cache_key = $self->__cache_key("cmdb/$self/$server");
50              
51 25 100       229 if ( !$self->__cache->valid($cache_key) ) {
52 18   50     224 my $cmdb = $self->get( undef, $server ) || undef;
53 18         222 $self->__cache->set( $cache_key, $cmdb );
54             }
55              
56 25         137 return $self->__cache;
57             }
58              
59             sub __cache_key {
60 78     78   236 my ( $self, $key ) = @_;
61              
62 78 100       198 if ( defined $key ) {
63 25         93 $self->{__cache_key} = $key;
64             }
65              
66 78         312 return $self->{__cache_key};
67             }
68              
69             sub __cache {
70 121     121   308 my ($self) = @_;
71              
72 121 100       355 if ( !defined $self->{__cache} ) {
73 6         38 $self->{__cache} = Rex::get_cache();
74             }
75              
76 121         725 return $self->{__cache};
77             }
78              
79             1;