File Coverage

blib/lib/OpenStack/MetaAPI/Roles/Listable.pm
Criterion Covered Total %
statement 36 38 94.7
branch 13 20 65.0
condition 5 11 45.4
subroutine 4 4 100.0
pod n/a
total 58 73 79.4


line stmt bran cond sub pod time code
1             package OpenStack::MetaAPI::Roles::Listable;
2              
3 5     5   2831 use strict;
  5         19  
  5         156  
4 5     5   27 use warnings;
  5         10  
  5         126  
5              
6 5     5   24 use Moo::Role;
  5         9  
  5         35  
7              
8             sub _list {
9 20     20   43 my ($self, $all_args, $caller_args) = @_;
10              
11             # all_args are arguments from the internal OpenStack::MetaAPI::API
12             # caller_args are coming from the user to filter the results
13             # if some filters are also arguments to the request
14             # then appending them to the query will shorten the output and run it faster
15              
16 20         37 my @all;
17             {
18 20         30 my ($uri, @extra) = @$all_args;
  20         53  
19 20 100       95 $uri = $self->root_uri($uri)
20             if $uri !~ m{^/v}; # can be removed once dynmaic methods are used
21              
22 20         378 my $extra_filters =
23             $self->api_specs()->query_filters_for('/get', $uri, $caller_args);
24              
25 20 100       57 if ($extra_filters) {
26 2 50       6 if (scalar @extra == 1) {
    0          
27 2         4 push @extra, {};
28             } elsif (scalar @extra > 1) {
29 0         0 die "Too many args when calling _list for all...";
30             }
31 2         5 $extra[-1] = {%{$extra[-1]}, %$extra_filters};
  2         6  
32             }
33              
34 20         409 @all = $self->client->all($uri, @extra);
35             }
36              
37 20         49386 my @args = @$caller_args;
38              
39             # apply our filters to the raw results
40 20         45 my $nargs = scalar @args;
41 20 100 66     113 if ($nargs && $nargs % 2 == 0) {
42 17         53 my %opts = @args;
43 17         56 foreach my $filter (sort keys %opts) {
44 17         27 my @keep;
45 17   50     57 my $filter_isa = ref $opts{$filter} // '';
46 17         33 foreach my $candidate (@all) {
47 43 50       91 next unless ref $candidate;
48 43 50       87 if ($filter_isa eq 'Regexp') {
49              
50             # can use a regexp as a filter
51             next
52             unless $candidate->{$filter}
53 0 0 0     0 && $candidate->{$filter} =~ $opts{$filter};
54             } else {
55              
56             # otherwise do one 'eq' check
57             next
58             unless $candidate->{$filter}
59 43 100 66     167 && $candidate->{$filter} eq $opts{$filter};
60             }
61              
62 15         32 push @keep, $candidate;
63             }
64              
65 17         94 @all = @keep;
66              
67             }
68             }
69              
70             # avoid to return a list when possible
71 20 100       279 return $all[0] if scalar @all <= 1;
72              
73             # return a list
74 2         38 return @all;
75             }
76              
77             1;
78              
79             __END__