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   2784 use strict;
  5         10  
  5         138  
4 5     5   23 use warnings;
  5         9  
  5         112  
5              
6 5     5   21 use Moo::Role;
  5         10  
  5         28  
7              
8             sub _list {
9 20     20   42 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         30 my @all;
17             {
18 20         29 my ($uri, @extra) = @$all_args;
  20         55  
19 20 100       92 $uri = $self->root_uri($uri)
20             if $uri !~ m{^/v}; # can be removed once dynmaic methods are used
21              
22 20         330 my $extra_filters =
23             $self->api_specs()->query_filters_for('/get', $uri, $caller_args);
24              
25 20 100       51 if ($extra_filters) {
26 2 50       7 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         3 $extra[-1] = {%{$extra[-1]}, %$extra_filters};
  2         7  
32             }
33              
34 20         326 @all = $self->client->all($uri, @extra);
35             }
36              
37 20         43874 my @args = @$caller_args;
38              
39             # apply our filters to the raw results
40 20         36 my $nargs = scalar @args;
41 20 100 66     102 if ($nargs && $nargs % 2 == 0) {
42 17         44 my %opts = @args;
43 17         48 foreach my $filter (sort keys %opts) {
44 17         30 my @keep;
45 17   50     43 my $filter_isa = ref $opts{$filter} // '';
46 17         27 foreach my $candidate (@all) {
47 43 50       93 next unless ref $candidate;
48 43 50       71 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     143 && $candidate->{$filter} eq $opts{$filter};
60             }
61              
62 15         41 push @keep, $candidate;
63             }
64              
65 17         78 @all = @keep;
66              
67             }
68             }
69              
70             # avoid to return a list when possible
71 20 100       193 return $all[0] if scalar @all <= 1;
72              
73             # return a list
74 2         33 return @all;
75             }
76              
77             1;
78              
79             __END__