| 1 | package OpenStack::MetaAPI::Roles::Listable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 85 | ▶ | sub _list { | ||||||||||||||||||||||||||
|
Subroutine
_list: called CC=17 SCAR=28.6
| |||||||||||||||||||||||||||||
| 9 | 85 | 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 | 85 | my @all; | |||||||||||||||||||||||||||
| 17 | { | ||||||||||||||||||||||||||||
| 18 | 85 | my ($uri, @extra) = @$all_args; | |||||||||||||||||||||||||||
| 19 | 85 | ▶ | $uri = $self->root_uri($uri) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | if $uri !~ m{^/v}; # can be removed once dynmaic methods are used | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 85 | my $extra_filters = | |||||||||||||||||||||||||||
| 23 | $self->api_specs()->query_filters_for('/get', $uri, $caller_args); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 85 | ▶ | if ($extra_filters) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | if (scalar @extra == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 27 | 4 | push @extra, {}; | |||||||||||||||||||||||||||
| 28 | } elsif (scalar @extra > 1) { | ||||||||||||||||||||||||||||
| 29 | 0 | die "Too many args when calling _list for all..."; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 4 | $extra[-1] = {%{$extra[-1]}, %$extra_filters}; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 85 | @all = $self->client->all($uri, @extra); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 85 | my @args = @$caller_args; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # apply our filters to the raw results | ||||||||||||||||||||||||||||
| 40 | 85 | my $nargs = scalar @args; | |||||||||||||||||||||||||||
| 41 | 85 | ▶ | if ($nargs && $nargs % 2 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$nargs and $nargs % 2 == 0
MC/DC50%1/2
$nargs and $nargs % 2 == 0
$nargs
$nargs % 2 == 0
Truth table66%2/3
A and B
A= $nargsB= $nargs % 2 == 0
| |||||||||||||||||||||||||||||
| 42 | 75 | my %opts = @args; | |||||||||||||||||||||||||||
| 43 | 75 | foreach my $filter (sort keys %opts) { | |||||||||||||||||||||||||||
| 44 | 77 | my @keep; | |||||||||||||||||||||||||||
| 45 | 77 | ▶ | my $filter_isa = ref $opts{$filter} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
ref $opts{$filter} // ""
MC/DC0%0/1
ref $opts{$filter} // ""
ref $opts{$filter}
Truth table50%1/2
A
A= ref $opts{$filter}
| |||||||||||||||||||||||||||||
| 46 | 77 | foreach my $candidate (@all) { | |||||||||||||||||||||||||||
| 47 | 193 | ▶ | next unless ref $candidate; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 193 | ▶ | if ($filter_isa eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # can use a regexp as a filter | ||||||||||||||||||||||||||||
| 51 | next | ||||||||||||||||||||||||||||
| 52 | unless defined $candidate->{$filter} | ||||||||||||||||||||||||||||
| 53 | 31 | ▶ | && $candidate->{$filter} =~ $opts{$filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $candidate->{$filter} and $candidate->{$filter} =~ /$opts{$filter}/
MC/DC100%2/2
defined $candidate->{$filter} and $candidate->{$filter} =~ /$opts{$filter}/
defined $candidate->{$filter}
$candidate->{$filter} =~ /$opts{$filter}/
Truth table100%3/3
A and B
A= defined $candidate->{$filter}B= $candidate->{$filter} =~ /$opts{$filter}/
| |||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # otherwise do one 'eq' check | ||||||||||||||||||||||||||||
| 57 | next | ||||||||||||||||||||||||||||
| 58 | unless defined $candidate->{$filter} | ||||||||||||||||||||||||||||
| 59 | 162 | ▶ | && $candidate->{$filter} eq $opts{$filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $candidate->{$filter} and $candidate->{$filter} eq $opts{$filter}
MC/DC100%2/2
defined $candidate->{$filter} and $candidate->{$filter} eq $opts{$filter}
defined $candidate->{$filter}
$candidate->{$filter} eq $opts{$filter}
Truth table100%3/3
A and B
A= defined $candidate->{$filter}B= $candidate->{$filter} eq $opts{$filter}
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 72 | push @keep, $candidate; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 77 | @all = @keep; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # avoid to return a list when possible | ||||||||||||||||||||||||||||
| 71 | 85 | ▶ | return $all[0] if scalar @all <= 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # return a list | ||||||||||||||||||||||||||||
| 74 | 10 | return @all; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 16 | 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||