File Coverage

lib/Rex/Helper/Rexfile/ParamLookup.pm
Criterion Covered Total %
statement 50 50 100.0
branch 21 24 87.5
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 78 82 95.1


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Helper::Rexfile::ParamLookup;
6              
7 32     32   517 use v5.12.5;
  32         121  
8 32     32   183 use warnings;
  32         70  
  32         1414  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 32     32   397 use Devel::Caller;
  32         87395  
  32         1588  
13 32     32   257 use Data::Dumper;
  32         77  
  32         1857  
14             require Rex::Exporter;
15             require Rex::Commands;
16              
17 32     32   211 use base qw(Rex::Exporter);
  32         111  
  32         2125  
18 32     32   272 use vars qw (@EXPORT);
  32         114  
  32         15397  
19              
20             @EXPORT = qw(param_lookup);
21              
22             sub param_lookup {
23 10     10 0 175 my ( $key, $default ) = @_;
24              
25 10         24 my $ret;
26              
27 10         123 my ($caller_pkg) = caller(0);
28              
29 10         83 my @args = Devel::Caller::caller_args(1);
30 10 100       278 if ( ref $args[0] eq "HASH" ) {
31 7 100       31 if ( exists $args[0]->{$key} ) {
32 5         21 $ret = $args[0]->{$key};
33             }
34             }
35              
36 10 100       53 if ( !$ret ) {
37              
38             # check if cmdb is loaded
39 5         1496 my ($use_cmdb) = grep { m/CMDB\.pm/ } keys %INC;
  1342         2185  
40 5 50       73 if ($use_cmdb) {
41              
42             # look inside cmdb
43 5         22 my $cmdb_key = "${caller_pkg}::$key";
44 5         36 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
45              
46 5 50       39 if ( !$ret ) {
47              
48             # check in resource
49 5 100       41 if ( Rex::Resource->is_inside_resource ) {
50 2         17 $cmdb_key =
51             Rex::Resource->get_current_resource->display_name . "::$key";
52 2         16 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
53             }
54              
55 5 100       53 if ( !$ret ) {
56              
57             # check in task
58 4         16 my $task = Rex::Commands::task();
59 4 50       23 if ($task) {
60 4         22 my $task_name = $task->{name};
61 4         27 $task_name =~ s/:/::/;
62 4         35 $cmdb_key = $task_name . "::$key";
63 4         18 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
64             }
65             }
66             }
67              
68 5 100       59 if ( !$ret ) {
69              
70             # check in global namespace
71 4         31 $ret = Rex::Commands::get( Rex::CMDB::cmdb($key) );
72             }
73             }
74             }
75              
76 10 100       75 if ( !$ret ) {
77 3         15 $ret = $default;
78             }
79              
80 10 100       86 if ( Rex::Resource->is_inside_resource ) {
81 4         15 Rex::Resource->get_current_resource()->set_parameter( $key => $ret );
82             }
83              
84 10 100       58 if ( !Rex::Resource->is_inside_resource ) {
85 6         30 Rex::Commands::task()->set_opt( $key => $ret );
86             }
87              
88 10         112 return $ret;
89             }
90              
91             1;
92              
93             =pod
94              
95             =head1 NAME
96              
97             Rex::Helper::Rexfile::ParamLookup - A command to manage task parameters.
98              
99             A command to manage task parameters. Additionally it register the parameters as template values.
100              
101             This module also looks inside a CMDB (if present) for a valid key.
102              
103              
104             =head1 SYNOPSIS
105              
106             task "setup", sub {
107             my $var = param_lookup "param_name", "default_value";
108             };
109              
110             =head1 LOOKUP
111              
112             First I checks the task parameters for a valid parameter. If none is found and if a CMDB is used, it will look inside the cmdb.
113              
114             If your module is named "Rex::NTP" than it will first look if the key "Rex::NTP::param_name" exists. If it doesn't exists it checks for the key "param_name".
115              
116             =cut