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   465 use v5.12.5;
  32         129  
8 32     32   299 use warnings;
  32         94  
  32         1674  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 32     32   412 use Devel::Caller;
  32         87649  
  32         1612  
13 32     32   382 use Data::Dumper;
  32         89  
  32         1820  
14             require Rex::Exporter;
15             require Rex::Commands;
16              
17 32     32   216 use base qw(Rex::Exporter);
  32         55  
  32         2154  
18 32     32   222 use vars qw (@EXPORT);
  32         81  
  32         14591  
19              
20             @EXPORT = qw(param_lookup);
21              
22             sub param_lookup {
23 10     10 0 195 my ( $key, $default ) = @_;
24              
25 10         30 my $ret;
26              
27 10         134 my ($caller_pkg) = caller(0);
28              
29 10         121 my @args = Devel::Caller::caller_args(1);
30 10 100       313 if ( ref $args[0] eq "HASH" ) {
31 7 100       34 if ( exists $args[0]->{$key} ) {
32 5         16 $ret = $args[0]->{$key};
33             }
34             }
35              
36 10 100       54 if ( !$ret ) {
37              
38             # check if cmdb is loaded
39 5         1398 my ($use_cmdb) = grep { m/CMDB\.pm/ } keys %INC;
  1342         2174  
40 5 50       76 if ($use_cmdb) {
41              
42             # look inside cmdb
43 5         37 my $cmdb_key = "${caller_pkg}::$key";
44 5         44 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
45              
46 5 50       41 if ( !$ret ) {
47              
48             # check in resource
49 5 100       48 if ( Rex::Resource->is_inside_resource ) {
50 2         13 $cmdb_key =
51             Rex::Resource->get_current_resource->display_name . "::$key";
52 2         14 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
53             }
54              
55 5 100       68 if ( !$ret ) {
56              
57             # check in task
58 4         377 my $task = Rex::Commands::task();
59 4 50       59 if ($task) {
60 4         26 my $task_name = $task->{name};
61 4         46 $task_name =~ s/:/::/;
62 4         30 $cmdb_key = $task_name . "::$key";
63 4         25 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
64             }
65             }
66             }
67              
68 5 100       50 if ( !$ret ) {
69              
70             # check in global namespace
71 4         37 $ret = Rex::Commands::get( Rex::CMDB::cmdb($key) );
72             }
73             }
74             }
75              
76 10 100       80 if ( !$ret ) {
77 3         8 $ret = $default;
78             }
79              
80 10 100       91 if ( Rex::Resource->is_inside_resource ) {
81 4         38 Rex::Resource->get_current_resource()->set_parameter( $key => $ret );
82             }
83              
84 10 100       60 if ( !Rex::Resource->is_inside_resource ) {
85 6         27 Rex::Commands::task()->set_opt( $key => $ret );
86             }
87              
88 10         85 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