MetaAPI.pm

filter: Statementstmt 100% 109 / 109 Branchbran 90.7% 49 / 54 Conditioncond 61.5% 24 / 39 MC/DCmcdc 37.5% 9 / 24 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 85.0% 205 / 241 CC 43 SCAR 30.8
CC
43
Cov
90%
create_vm
34.9
__ANON__
6.0
look_by_id_or_name
5.0
CRAP 21.8
↑ Summary MetaAPI/Roles/Listable.pm »
1 package OpenStack::MetaAPI;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use OpenStack::Client::Auth ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use OpenStack::MetaAPI::Routes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use OpenStack::MetaAPI::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Scalar::Util qw/blessed weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Perl5 OpenStack API abstraction on top of OpenStack::Client
15  
16 18 our $VERSION = '0.005'; # VERSION: generated by DZP::OurPkgVersion
17  
18 18 has 'debug' => (is => 'rw', default => 0);
19 18 has 'auth' => (is => 'ro', required => 1, handles => [qw/services/]);
20  
21 has 'route' => (
22 is => 'ro',
23 28 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 28 my ($self) = @_;
25  
26 # weaken our circular dependency
27 28 my $auth = $self->auth;
28 28 weaken($auth);
29  
30 28 weaken($self);
31  
32 28 return OpenStack::MetaAPI::Routes->new(
33 auth => $auth,
34 api => $self);
35 },
36 18 handles => [OpenStack::MetaAPI::Routes->list_all()],
37 );
38  
39 # for create server
40 18 has 'create_max_timeout' => (is => 'rw', default => 5 * 60);
41 18 has 'create_loop_sleep' => (is => 'rw', default => 5);
42  
43 32 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
44 32 my ($orig, $class, @args) = @_;
45  
46 32 ▶ die "Missing arguments to create Auth object" unless scalar @args;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @args 2 30 32
47  
48 # A caller that has already built its auth object hands it straight over.
49 #
50 # Keystone can be talked to by methods OpenStack::Client::Auth does not
51 # implement -- an application credential, for one -- and the only way to use
52 # one is to construct the auth object yourself. Building a fresh one from
53 # the args regardless left no way to do that, so such a caller could not use
54 # this module at all.
55 #
56 # Normal construction is new($endpoint, %args), whose first argument is a
57 # URL rather than a reference, so it does not match this.
58 #
59 # The 'auth' has to be a blessed object, not merely a reference: a
60 # clouds.yaml cloud entry is a plain hashref with an 'auth' key of its own
61 # (auth_url, username, password, ...), and letting that through would store
62 # an unblessed hash as the auth object -- construction would succeed and the
63 # first delegated call would die far from here on an unblessed reference.
64 return $args[0]
65 if scalar @args == 1
66 && ref $args[0] eq 'HASH'
67 30 ▶ && blessed($args[0]->{'auth'});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args == 1 and ref $args[0] eq "HASH" and builtin::blessed($args[0]{"auth"}) 1 29 30
Condition83%5/6
scalar @args == 1 and ref $args[0] eq "HASH" and builtin::blessed($args[0]{"auth"})
!ll&&!rl&&r
2721
scalar @args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
2703
MC/DC66%2/3
scalar @args == 1 and ref $args[0] eq "HASH" and builtin::blessed($args[0]{"auth"})
scalar @args == 1 ref $args[0] eq "HASH" builtin::blessed($args[0]{"auth"})
Truth table75%3/4
A and B and C
A= scalar @args == 1B= ref $args[0] eq "HASH"C= builtin::blessed($args[0]{"auth"})
ABCresult
0XX0
10X0
1100
1111
68  
69 # OpenStack::Client turns off the check of the server's hostname unless
70 # its user agent class puts it back. See OpenStack::MetaAPI::UserAgent.
71 29 my ($endpoint, %args) = @args;
72 29 ▶ $args{package_ua} //= 'OpenStack::MetaAPI::UserAgent';
Condition100%2/2
$args{"package_ua"} //= "OpenStack::MetaAPI::UserAgent"
l!l
128
MC/DC100%1/1
$args{"package_ua"} //= "OpenStack::MetaAPI::UserAgent"
$args{"package_ua"}
Truth table100%2/2
A
A= $args{"package_ua"}
Aresult
11
00
73  
74 29 return {auth => OpenStack::Client::Auth->new($endpoint, %args)};
75 18 };
76  
77 15 ▶sub create_vm {
Subroutine create_vm: called CC=32 SCAR=35.5
Pod documented
78 15 my ($self, %opts) = @_;
79  
80 die "'flavor' name or id is required by create_vm"
81 15 ▶ unless defined $opts{flavor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"flavor"} 1 14 15
82 die "'network' name or id is required by create_vm"
83 14 ▶ unless defined $opts{network};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"network"} 1 13 14
84 die "'image' name or id is required by create_vm"
85 13 ▶ unless defined $opts{image};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"image"} 1 12 13
86 12 ▶ die "'name' field is required by create_vm" unless defined $opts{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"name"} 1 11 12
87  
88 $opts{security_group} //=
89 11 ▶ 'default'; # optional argument fallback to 'default'
Condition50%1/2
$opts{"security_group"} //= "default"
l!l
011
MC/DC0%0/1
$opts{"security_group"} //= "default"
$opts{"security_group"}
Truth table50%1/2
A
A= $opts{"security_group"}
Aresult
11
00
90  
91 # get the flavor by id or name
92 11 my $flavor = $self->look_by_id_or_name(flavors => $opts{flavor});
93  
94 # get the network by id or name
95 11 my $network = $self->look_by_id_or_name(networks => $opts{network});
96  
97 # Optional: not every cloud has a tenant network to escape from. Where the
98 # only network is external and shared, a server on it is given a routable
99 # address directly -- reported as 'fixed' -- and there is no floating IP to
100 # attach. Requiring one made such a cloud impossible to build on at all.
101 11 my $network_for_floating_ip;
102 $network_for_floating_ip =
103 $self->look_by_id_or_name(networks => $opts{network_for_floating_ip})
104 11 ▶ if defined $opts{network_for_floating_ip};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"network_for_floating_ip"} 9 2 11
105  
106 10 my $image;
107 10 ▶ if (_looks_valid_id($opts{image})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_looks_valid_id($opts{"image"})) 10 0 10
108 10 $image = $self->image_from_uid($opts{image});
109 }
110 10 ▶ $image //= $self->image_from_name($opts{image});
Condition33%1/3
$image //= $self->image_from_name($opts{"image"})
l!l&&r!l&&!r
1000
MC/DC0%0/2
$image //= $self->image_from_name($opts{"image"})
$image $self->image_from_name($opts{"image"})
Truth table33%1/3
A //= B
A= $imageB= $self->image_from_name($opts{"image"})
ABresult
1X1
011
000
111  
112 die "Cannot find image for id/name '$opts{image}'"
113 10 ▶ unless ref $image eq 'HASH' && _looks_valid_id($image->{id});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $image eq "HASH" and _looks_valid_id($image->{"id"}) 1 9 10
Condition66%2/3
ref $image eq "HASH" and _looks_valid_id($image->{"id"})
!ll&&!rl&&r
019
MC/DC50%1/2
ref $image eq "HASH" and _looks_valid_id($image->{"id"})
ref $image eq "HASH" _looks_valid_id($image->{"id"})
Truth table66%2/3
A and B
A= ref $image eq "HASH"B= _looks_valid_id($image->{"id"})
ABresult
0X0
100
111
114  
115 my $security_group =
116 9 $self->look_by_id_or_name(security_groups => $opts{security_group});
117  
118 9 my @extra;
119 9 ▶ if (defined $opts{key_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"key_name"}) 3 6 9
120 3 push @extra, (key_name => $opts{key_name});
121 }
122  
123 # Nova wants user_data base64 encoded, and every caller has cloud-config
124 # rather than base64, so encode it here instead of making each of them
125 # remember to. Without this there is no way to hand a new server its
126 # cloud-init payload at all, which is most of the point of creating one.
127 9 ▶ if (defined $opts{user_data} && length $opts{user_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"user_data"} and length $opts{"user_data"}) 1 8 9
Condition66%2/3
defined $opts{"user_data"} and length $opts{"user_data"}
!ll&&!rl&&r
801
MC/DC50%1/2
defined $opts{"user_data"} and length $opts{"user_data"}
defined $opts{"user_data"} length $opts{"user_data"}
Truth table66%2/3
A and B
A= defined $opts{"user_data"}B= length $opts{"user_data"}
ABresult
0X0
100
111
128 push @extra,
129 1 (user_data => MIME::Base64::encode_base64($opts{user_data}, ''));
130 }
131  
132 # Pass-throughs. Each is a plain server-create field that had no way of
133 # being set: which availability zone to build in, the metadata a later
134 # lookup finds the server by, and the volumes it boots or carries.
135 9 foreach my $field (qw{availability_zone metadata block_device_mapping_v2}) {
136 27 ▶ push @extra, ($field => $opts{$field}) if defined $opts{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$field} 2 25 27
137 }
138  
139 my $server = $self->create_server(
140 name => $opts{name},
141 imageRef => $image->{id},
142 flavorRef => $flavor->{id},
143 min_count => 1,
144 max_count => 1,
145 # By name, which is what the field is. This passed the id, which
146 # some clouds resolve anyway and others do not.
147 security_groups => [{name => $security_group->{name} // $security_group->{id}}],
148 9 ▶ networks => [{uuid => $network->{id}}],
Condition33%1/3
$security_group->{"name"} // $security_group->{"id"}
l!l&&r!l&&!r
900
MC/DC0%0/2
$security_group->{"name"} // $security_group->{"id"}
$security_group->{"name"} $security_group->{"id"}
Truth table33%1/3
A // B
A= $security_group->{"name"}B= $security_group->{"id"}
ABresult
1X1
011
000
149 @extra,
150 );
151  
152 9 my $server_uid = $server->{id};
153 9 ▶ die "Failed to create server" unless _looks_valid_id($server_uid);
Branch: 1 branch
BranchTrueFalseTotal
unless _looks_valid_id($server_uid) 0 9 9
154  
155 # we are going to wait for 5 minutes for the server
156 9 ▶ my $wait_time_limit = $opts{wait_time_limit} // $self->create_max_timeout;
Condition33%1/3
$opts{"wait_time_limit"} // $self->create_max_timeout
l!l&&r!l&&!r
090
MC/DC0%0/2
$opts{"wait_time_limit"} // $self->create_max_timeout
$opts{"wait_time_limit"} $self->create_max_timeout
Truth table33%1/3
A // B
A= $opts{"wait_time_limit"}B= $self->create_max_timeout
ABresult
1X1
011
000
157  
158 9 my $now = time();
159 9 my $max_time = $now + $wait_time_limit;
160 9 my $server_is_ready = 0;
161  
162 9 my $server_status;
163  
164 # TODO: maybe add one alarm...
165 9 ▶ while (time() < $max_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $max_time) 12 1 13
166  
167 12 $server_status = $self->server_from_uid($server_uid);
168  
169 12 ▶ if (ref $server_status && $server_status->{status}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $server_status and $server_status->{"status"}) 12 0 12
Condition33%1/3
ref $server_status and $server_status->{"status"}
!ll&&!rl&&r
0012
MC/DC0%0/2
ref $server_status and $server_status->{"status"}
ref $server_status $server_status->{"status"}
Truth table33%1/3
A and B
A= ref $server_statusB= $server_status->{"status"}
ABresult
0X0
100
111
170 12 my $status = lc($server_status->{status});
171  
172 12 ▶ if ($status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "active") 4 8 12
173 4 $server_is_ready = 1;
174 4 last;
175 }
176  
177 # Fail fast on ERROR — no point waiting out the timeout.
178 8 ▶ if ($status eq 'error') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "error") 4 4 8
179 4 ▶ my $fault = $server_status->{fault} || {};
Condition100%2/2
$server_status->{'fault'} || {}
l!l
22
MC/DC100%1/1
$server_status->{'fault'} || {}
$server_status->{'fault'}
Truth table100%2/2
A
A= $server_status->{'fault'}
Aresult
11
00
180 4 my $msg
181 = "Failed to create server $server_uid: status=ERROR";
182 4 ▶ $msg .= " code=$fault->{code}" if $fault->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $fault->{"code"} 2 2 4
183 4 ▶ $msg .= " message=$fault->{message}" if $fault->{message};
Branch: 1 branch
BranchTrueFalseTotal
if $fault->{"message"} 2 2 4
184 4 die "$msg\n";
185 }
186 }
187 4 ▶ sleep $self->create_loop_sleep if $self->create_loop_sleep;
Branch: 1 branch
BranchTrueFalseTotal
if $self->create_loop_sleep 2 2 4
188 }
189  
190 5 ▶ die "Failed to create server: never came back as active"
Branch: 1 branch
BranchTrueFalseTotal
unless $server_is_ready 1 4 5
191 unless $server_is_ready;
192  
193 # now add one IP to the server
194 4 ▶ if ($network_for_floating_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($network_for_floating_ip) 2 2 4
195 # create a floating IP
196 my $floating_ip =
197 2 $self->create_floating_ip($network_for_floating_ip->{id});
198  
199 die "Failed to create floating ip"
200 2 ▶ unless ref $floating_ip && _looks_valid_id($floating_ip->{id});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $floating_ip and _looks_valid_id($floating_ip->{"id"}) 0 2 2
Condition33%1/3
ref $floating_ip and _looks_valid_id($floating_ip->{"id"})
!ll&&!rl&&r
002
MC/DC0%0/2
ref $floating_ip and _looks_valid_id($floating_ip->{"id"})
ref $floating_ip _looks_valid_id($floating_ip->{"id"})
Truth table33%1/3
A and B
A= ref $floating_ipB= _looks_valid_id($floating_ip->{"id"})
ABresult
0X0
100
111
201  
202 # add the floating IP to the server
203 my $added =
204 $self->add_floating_ip_to_server($floating_ip->{id}, $server_uid,
205 2 network_id => $network->{id});
206  
207 $server_status->{floating_ip_address} =
208 2 $floating_ip->{floating_ip_address};
209 2 $server_status->{floating_ip_id} = $floating_ip->{id};
210 }
211  
212 4 return $server_status;
213 }
214  
215 44 ▶sub look_by_id_or_name {
Subroutine look_by_id_or_name: called CC=5 SCAR=16.1
Pod undocumented
216 44 my ($self, $helper, $id_or_name) = @_;
217  
218 44 my $entry;
219 44 ▶ if (_looks_valid_id($id_or_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_looks_valid_id($id_or_name)) 1 43 44
220 1 $entry = $self->can($helper)->($self, id => $id_or_name);
221 }
222 44 ▶ $entry //= $self->can($helper)->($self, name => $id_or_name);
Condition100%3/3
$entry //= $self->can($helper)->($self, "name", $id_or_name)
l!l&&r!l&&!r
1403
MC/DC100%2/2
$entry //= $self->can($helper)->($self, "name", $id_or_name)
$entry $self->can($helper)->($self, "name", $id_or_name)
Truth table100%3/3
A //= B
A= $entryB= $self->can($helper)->($self, "name", $id_or_name)
ABresult
1X1
011
000
223  
224 44 ▶ if (ref $entry ne 'HASH' || !_looks_valid_id($entry->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry ne "HASH" or not _looks_valid_id($entry->{"id"})) 3 41 44
Condition66%2/3
ref $entry ne "HASH" or not _looks_valid_id($entry->{"id"})
l!l&&r!l&&!r
3041
MC/DC50%1/2
ref $entry ne "HASH" or not _looks_valid_id($entry->{"id"})
ref $entry ne "HASH" not _looks_valid_id($entry->{"id"})
Truth table66%2/3
A or not(B)
A= ref $entry ne "HASH"B= not _looks_valid_id($entry->{"id"})
ABresult
1X1
011
000
225 3 die "Cannot find '$helper' for id/name '$id_or_name'";
226 }
227  
228 41 return $entry;
229 }
230  
231 116 ▶sub _looks_valid_id {
Subroutine _looks_valid_id: called CC=3 SCAR=11.0
232 116 my ($id) = @_;
233  
234 116 ▶ return unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 115 116
235 115 ▶ return if ref $id;
Branch: 1 branch
BranchTrueFalseTotal
if ref $id 0 115 115
236  
237 115 my $VALID_ID = qr{^[a-f0-9\-]+$}i;
238  
239 115 return $id =~ $VALID_ID;
240 }
241  
242 18 1;
243  
244 __END__
↑ Summary MetaAPI/Roles/Listable.pm »