| 1 | package Net::OpenStack::Client::API::Magic; | ||||||||||||||||||||||||||||
| 2 | 10 | $Net::OpenStack::Client::API::Magic::VERSION = '0.1.4'; | |||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Net::OpenStack::Client::Request qw(@SUPPORTED_METHODS @METHODS_REQUIRE_OPTIONS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 10 | our @EXPORT_OK = qw(retrieve version); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # hashref to store cached command data | ||||||||||||||||||||||||||||
| 17 | 10 | my $cache; | |||||||||||||||||||||||||||
| 18 | # Init the cache | ||||||||||||||||||||||||||||
| 19 | 10 | $cache = flush_cache(); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head2 Public functions | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =over | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =item flush_cache | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Reset the cache | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub flush_cache | ||||||||||||||||||||||||||||
| 32 | 11 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
flush_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 11 | $cache = {cmd => {}, api => {}}; | |||||||||||||||||||||||||||
| 34 | 11 | return $cache; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =item cache | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Given C<data> command hashref, | ||||||||||||||||||||||||||||
| 40 | cache and return the relevant (filtered) command data. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | If C<api_service> is defined, store the data as | ||||||||||||||||||||||||||||
| 43 | the service C<API_DATA>. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub cache | ||||||||||||||||||||||||||||
| 48 | 40 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cache: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 40 | my ($data, $api_service) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 40 | ▶ | if ($api_service) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 9 | $cache->{api}->{$api_service} = $data; | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 31 | my $service = $data->{service}; | |||||||||||||||||||||||||||
| 55 | 31 | my $name = $data->{name}; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 31 | $cache->{cmd}->{$service}->{$name} = $data; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 40 | return $data; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item retrieve | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Retrieve the command data for service C<service>, name C<name> | ||||||||||||||||||||||||||||
| 66 | and version C<version>. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Returns the tuple with cache command hashref and undef errormessage on SUCCESS, | ||||||||||||||||||||||||||||
| 69 | an emptyhashref and actual errormessage otherwise. | ||||||||||||||||||||||||||||
| 70 | If the command is already in cache, returns the cached version | ||||||||||||||||||||||||||||
| 71 | (and undef errormessage). | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub retrieve | ||||||||||||||||||||||||||||
| 76 | 105 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=25 SCAR=33.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 105 | my ($service, $name, $version) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Return already cached data | ||||||||||||||||||||||||||||
| 80 | 105 | ▶ | return ($cache->{cmd}->{$service}->{$name}, undef) if defined(($cache->{cmd}->{$service} || {})->{$name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$cache->{'cmd'}{$service} || {}
MC/DC100%1/1
$cache->{'cmd'}{$service} || {}
$cache->{'cmd'}{$service}
Truth table100%2/2
A
A= $cache->{'cmd'}{$service}
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 33 | my $err_prefix = "retrieve name $name for service $service"; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 33 | ▶ | if ($version) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 33 | ▶ | if (ref($version) ne 'version') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 13 | ▶ | $version = "v$version" if $version !~ m/^v/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 13 | $version = version->new($version); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 0 | return {}, "$err_prefix no version defined"; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 33 | $err_prefix .= " version $version failed:"; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 33 | my $versionpackagename = "$version"; | |||||||||||||||||||||||||||
| 96 | 33 | $versionpackagename =~ s/[.]/DOT/g; # cannot have a . in the package name | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 33 | my $servicepackagename = ucfirst($service); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 33 | my $apidata = $cache->{api}->{$service}; | |||||||||||||||||||||||||||
| 101 | 33 | my $result; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 33 | ▶ | if (!$apidata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 10 | my $package = "Net::OpenStack::Client::API::${servicepackagename}::${versionpackagename}"; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 10 | local $@; | |||||||||||||||||||||||||||
| 107 | 10 | eval { | |||||||||||||||||||||||||||
| 108 | 10 | load $package; | |||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||
| 110 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 1 | return {}, "$err_prefix no API module $package: $@"; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 9 | my $varname = "${package}::API_DATA"; | |||||||||||||||||||||||||||
| 115 | eval { | ||||||||||||||||||||||||||||
| 116 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 9 | $apidata = ${$varname}; | |||||||||||||||||||||||||||
| 118 | 10 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 9 | }; | |||||||||||||||||||||||||||
| 120 | 9 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 121 | 0 | return {}, "$err_prefix somthing went wrong while looking for variable $varname: $@"; | |||||||||||||||||||||||||||
| 122 | } elsif (!defined $apidata) { | ||||||||||||||||||||||||||||
| 123 | 0 | return {}, "$err_prefix no variable $varname"; | |||||||||||||||||||||||||||
| 124 | } elsif (ref($apidata) ne 'HASH') { | ||||||||||||||||||||||||||||
| 125 | 0 | return {}, "$err_prefix variable $varname not a hash (got ".ref($apidata).")"; | |||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # cache this data | ||||||||||||||||||||||||||||
| 129 | 9 | cache($apidata, $service); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 32 | my $data = $apidata->{$name}; | |||||||||||||||||||||||||||
| 133 | 32 | ▶ | if (! $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | # Try custom functions | ||||||||||||||||||||||||||||
| 135 | 14 | my $package = "Net::OpenStack::Client::${servicepackagename}::${versionpackagename}"; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 14 | local $@; | |||||||||||||||||||||||||||
| 138 | 14 | eval { | |||||||||||||||||||||||||||
| 139 | 14 | load $package; | |||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 14 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | my $msg = "$err_prefix no API data or client module"; | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($@ !~ m/^can.*locate.*in.*INC/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | # if you can't locate the module, it's probably ok no to mention it | ||||||||||||||||||||||||||||
| 146 | # but anything else (eg syntax error) should be reported | ||||||||||||||||||||||||||||
| 147 | 0 | $msg .= " (client module load failed: $@)" | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 0 | return {}, $msg; | |||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||
| 151 | # Retrieve the function in the package | ||||||||||||||||||||||||||||
| 152 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | 14 | my %symbol_table = %{"${package}::"}; | |||||||||||||||||||||||||||
| 154 | 10 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 14 | my $something = $symbol_table{$name}; | |||||||||||||||||||||||||||
| 157 | 14 | ▶ | if (defined $something) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | # magic bits from Package::Stash list_all_symbols | ||||||||||||||||||||||||||||
| 159 | 13 | ▶ | if (ref \$something eq 'GLOB' && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref \$something eq "GLOB" and defined *$something{"CODE"}
MC/DC50%1/2
ref \$something eq "GLOB" and defined *$something{"CODE"}
ref \$something eq "GLOB"
defined *$something{"CODE"}
Truth table66%2/3
A and B
A= ref \$something eq "GLOB"B= defined *$something{"CODE"}
| |||||||||||||||||||||||||||||
| 160 | defined *$something{CODE}) { | ||||||||||||||||||||||||||||
| 161 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | 12 | my $function = \&{"${package}::$name"}; | |||||||||||||||||||||||||||
| 163 | 10 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 12 | $result = { | |||||||||||||||||||||||||||
| 166 | name => $name, # human readable function/method name | ||||||||||||||||||||||||||||
| 167 | service => $service, | ||||||||||||||||||||||||||||
| 168 | code => $function, | ||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||
| 171 | 1 | return {}, "$err_prefix found in client module, but not a function"; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | 1 | return {}, "$err_prefix no API data or function from client module"; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||
| 178 | # data is a hashref | ||||||||||||||||||||||||||||
| 179 | # sanity check | ||||||||||||||||||||||||||||
| 180 | 18 | ▶ | if (!exists($data->{endpoint})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | return {}, "$err_prefix data should at least contain the endpoint"; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 18 | ▶ | if (!exists($data->{method})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | return {}, "$err_prefix data should at least contain the method"; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 18 | my $method = $data->{method}; | |||||||||||||||||||||||||||
| 189 | 18 | ▶ | if (!grep {$_ eq $method} @SUPPORTED_METHODS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | return {}, "$err_prefix method $method is not supported"; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 18 | ▶ | if ((grep {$method eq $_} @METHODS_REQUIRE_OPTIONS) && !exists($data->{options})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
grep {$method eq $_;} @Net::OpenStack::Client::API::Magic::METHODS_REQUIRE_OPTIONS and not exists $data->{"options"}
MC/DC0%0/2
grep {$method eq $_;} @Net::OpenStack::Client::API::Magic::METHODS_REQUIRE_OPTIONS and not exists $data->{"options"}
grep {$method eq $_;} @Net::OpenStack::Client::API::Magic::METHODS_REQUIRE_OPTIONS
not exists $data->{"options"}
Truth table66%2/3
A and not(B)
A= grep {$method eq $_;} @Net::OpenStack::Client::API::Magic::METHODS_REQUIRE_OPTIONSB= not exists $data->{"options"}
| |||||||||||||||||||||||||||||
| 193 | 0 | return {}, "$err_prefix data should contain options for method $method"; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | $result = { | ||||||||||||||||||||||||||||
| 197 | name => $name, # human readable function/method name | ||||||||||||||||||||||||||||
| 198 | method => $method, # HTTP method | ||||||||||||||||||||||||||||
| 199 | service => $service, | ||||||||||||||||||||||||||||
| 200 | endpoint => $data->{endpoint}, | ||||||||||||||||||||||||||||
| 201 | 18 | version => $version, | |||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 18 | ▶ | $result->{result} = $data->{result} if defined($data->{result}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 18 | foreach my $k (qw(templates parameters options)) { | |||||||||||||||||||||||||||
| 207 | 54 | ▶ | $result->{$k} = $data->{$k} if exists($data->{$k}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 30 | return cache($result), undef; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =pod | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =back | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 10 | 1; | |||||||||||||||||||||||||||