MetaAPI/API/Images.pm

filter: Statementstmt 88.5% 31 / 35 Branchbran 83.3% 10 / 12 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 4 totaltotal 72.7% 48 / 66 CC 9 SCAR 15.1
CC
9
Cov
83%
image_from_name
7.4
image_from_uid
3.0
images
2.0
CRAP 4.5
« MetaAPI/API/Compute.pm ↑ Summary MetaAPI/API/Service.pm »
1 package OpenStack::MetaAPI::API::Images;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 extends 'OpenStack::MetaAPI::API::Service';
9  
10 # roles
11 5 with 'OpenStack::MetaAPI::Roles::Listable';
12  
13 5 has '+name' => (default => 'image');
14 5 has '+version_prefix' => (default => 'v2');
15  
16  
17 sub images {
18 0 ▶ my ($self, @args) = @_;
Subroutine images: not called CC=1 SCAR=6.9
Pod undocumented
19  
20 0 die "Please use image_from_uid, image_from_name or list_images";
21 }
22  
23 # Every image matching %query, pagination followed.
24 #
25 # The warning above is about listing images *unfiltered*, which really can take
26 # fifty requests -- but there was no way to ask for a filtered list either, and
27 # some questions have no single-image answer. "Which snapshots does this
28 # instance have" is one: Glance holds them as ordinary images, and the only way
29 # to find them is to ask for the ones matching.
30 #
31 # %query goes to Glance as request parameters, so the filtering happens there
32 # rather than here: name, owner, visibility, status, and any image property --
33 # Nova stamps its snapshots with image_type and instance_uuid, which is the
34 # narrow way to ask that question.
35 #
36 # Pagination is OpenStack::Client's; all() follows the 'next' link until there
37 # is not one.
38 sub list_images {
39 0 ▶ my ($self, %query) = @_;
Subroutine list_images: not called CC=1 SCAR=6.9
Pod undocumented
40  
41 0 return $self->client->all($self->root_uri('/images'), 'images', \%query);
42 }
43  
44 # API doc
45 # https://developer.openstack.org/api-ref/image/v2/?expanded=list-images-detail
46  
47 # FIXME: should be added to specs
48 sub image_from_uid {
49 17 ▶ my ($self, $uid) = @_;
Subroutine image_from_uid: called CC=3 SCAR=11.0
Pod undocumented
50  
51 17 ▶ die "image_from_uid: uid is required" unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 2 15 17
52 15 ▶ die "Invalid UUID format '$uid' - expected 8-4-4-4-12 hex format"
Branch: 1 branch
BranchTrueFalseTotal
unless $uid =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i 4 11 15
53 unless $uid =~ m{^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$}i;
54  
55 11 my $uri = $self->root_uri('/images/' . $uid);
56  
57 11 return $self->get($uri);
58 }
59  
60 sub image_from_name {
61 6 ▶ my ($self, $name) = @_;
Subroutine image_from_name: called CC=7 SCAR=20.0
Pod undocumented
62  
63 # v2/images?name=in:"glass,%20darkly"
64  
65 6 ▶ die "image_from_name: name is required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 4 6
66  
67 4 my $uri = $self->root_uri('/images');
68  
69 4 my $reply = $self->get($uri, name => qq{in:"$name"});
70  
71 4 ▶ return unless ref $reply && $reply->{images};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $reply and $reply->{"images"} 0 4 4
Condition33%1/3
ref $reply and $reply->{"images"}
!ll&&!rl&&r
004
MC/DC0%0/2
ref $reply and $reply->{"images"}
ref $reply $reply->{"images"}
Truth table33%1/3
A and B
A= ref $replyB= $reply->{"images"}
ABresult
0X0
100
111
72  
73 4 my $images = $reply->{images};
74  
75 4 ▶ return unless ref $images;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $images 0 4 4
76  
77 4 ▶ if (scalar @$images > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$images > 1) 2 2 4
78 2 ▶ my @ids = map { $_->{id} // 'unknown' } @$images;
Condition50%1/2
$_->{"id"} // "unknown"
l!l
40
MC/DC0%0/1
$_->{"id"} // "unknown"
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
Aresult
11
00
79 2 die "image_from_name: multiple images found for name '$name'"
80 . " (ids: " . join(', ', @ids) . ")."
81 . " Use image_from_uid to select a specific image";
82 }
83  
84 2 return $images->[0];
85 }
86  
87 ### helpers
88  
89 5 1;
90  
91 __END__
92  
93 =pod
94  
95 =encoding UTF-8
96  
97 =head1 NAME
98  
99 OpenStack::MetaAPI::API::Images
100  
101 =head1 VERSION
102  
103 version 0.004
104  
105 Note loading all images can be very slow
106 as we have to use multiple requests (kind of pagination)...
107 and can result to require more than 50 requests...
108  
109 For this reason we would prefer selecting one image
110 either by its 'exact name' or its 'UID'
111  
112 =head1 AUTHOR
113  
114 Nicolas R <atoomic@cpan.org>
115  
116 =head1 COPYRIGHT AND LICENSE
117  
118 This software is copyright (c) 2019 by cPanel, Inc.
119  
120 This is free software; you can redistribute it and/or modify it under
121 the same terms as the Perl 5 programming language system itself.
122  
123 =cut
« MetaAPI/API/Compute.pm ↑ Summary MetaAPI/API/Service.pm »