JiffyBox.pm

filter: Statementstmt 41.6% 50 / 120 Branchbran 20.5% 7 / 34 Conditioncond 37.5% 6 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 70.0% 14 / 20 Podpod 88.8% 8 / 9 totaltotal 42.3% 88 / 208 CC 26 SCAR 33.0
CC
26
Cov
37%
get_id_from_name
90.0
get_vms
42.0
create_vm
11.1
CRAP 27.0
↑ Summary JiffyBox/Box.pm »
1 package VM::JiffyBox;
2 4 $VM::JiffyBox::VERSION = '0.032';
3 # The line below is recognised by Dist::Zilla and taken for CPAN packaging
4 # ABSTRACT: OO-API for JiffyBox Virtual Machine
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use VM::JiffyBox::Box;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶has domain_name => (is => 'rw', default => sub {'https://api.jiffybox.de'});
Subroutine __ANON__: called CC=1 SCAR=0.0
17 9 ▶has version => (is => 'rw', default => sub {'v1.0'});
Subroutine __ANON__: called CC=1 SCAR=0.0
18 4 has token => (is => 'rw', required => 1);
19  
20 9 ▶has ua => (is => 'rw', default => sub {LWP::UserAgent->new()});
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 2 ▶has test_mode => (is => 'rw', default => sub {'0'});
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 # should always keep the last message from the server
25 4 has last => (is => 'rw');
26 4 has details_cache => (is => 'rw');
27  
28 17 ▶sub base_url {
Subroutine base_url: called CC=1 SCAR=0.0
Pod undocumented
29 17 my $self = shift;
30  
31 17 return $self->domain_name . '/'
32 . $self->token . '/'
33 . $self->version ;
34 }
35  
36 0 ▶sub get_details {
Subroutine get_details: not called CC=2 SCAR=17.9
Pod documented
37 0 my $self = shift;
38
39 0 my $url = $self->base_url . '/jiffyBoxes';
40
41 0 my $response = $self->ua->get($url);
42  
43 # POSSIBLE EXIT
44 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
45 0 $self->last ($response->status_line);
46 0 return;
47 }
48  
49 0 my $details = from_json($response->decoded_content);
50  
51 0 $self->last ( $details );
52 0 $self->details_cache( $details );
53  
54 0 return $details;
55 }
56  
57 0 ▶sub get_id_from_name {
Subroutine get_id_from_name: not called CC=9 SCAR=45.0
Pod documented
58 0 my $self = shift;
59 0 ▶ my $box_name = shift || die 'box_name as argument needed';
Condition0%0/2
shift() || die('box_name as argument needed')
l!l
00
MC/DC0%0/1
shift() || die('box_name as argument needed')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
60
61 0 my $details = $self->get_details;
62  
63 # EXIT if no details
64 0 ▶ return unless $details;
Branch: 1 branch
BranchTrueFalseTotal
unless $details 0 0 0
65  
66 0 $self->last ( $details );
67  
68 # EXIT if no expected result
69 0 ▶ return unless (reftype $details eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($details) eq "HASH" 0 0 0
70 0 ▶ return unless (exists $details->{result});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $details->{"result"} 0 0 0
71 0 ▶ return unless (reftype $details->{result} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($details->{"result"}) eq "HASH" 0 0 0
72  
73 0 $self->details_cache( $details );
74
75 # look for a match in the results
76 0 foreach my $box (values %{$details->{result}}) {
77  
78 # EXIT if no expected result
79 0 ▶ return unless (reftype $box eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($box) eq "HASH" 0 0 0
80  
81 0 ▶ return $box->{id} if ($box->{name} eq $box_name);
Branch: 1 branch
BranchTrueFalseTotal
if $box->{"name"} eq $box_name 0 0 0
82 }
83  
84 # if we reach here, this means there was no match
85 0 return;
86 }
87  
88 9 ▶sub get_vm {
Subroutine get_vm: called CC=2 SCAR=6.9
Pod documented
89 9 my $self = shift;
90 9 ▶ my $box_id = shift || die 'box_id needed';
Condition100%2/2
shift() || die('box_id needed')
l!l
81
MC/DC100%1/1
shift() || die('box_id needed')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
91  
92 8 my $box = VM::JiffyBox::Box->new(id => $box_id, hypervisor => $self);
93  
94 8 return $box;
95 }
96  
97 7 ▶sub create_vm {
Subroutine create_vm: called CC=8 SCAR=24.0
Pod documented
98 7 my $self = shift;
99 7 my $args = {@_};
100  
101 # POSSIBLE EXIT (DIE)
102 7 ▶ die 'name needed' unless $args->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"name"} 2 5 7
103 5 ▶ die 'planid needed' unless $args->{planid};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"planid"} 1 4 5
104 die 'backupid or distribution needed' unless $args->{backupid}
105 4 ▶ xor $args->{distribution};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"backupid"} xor $args->{"distribution"} 2 2 4
Condition100%4/4
$args->{"backupid"} xor $args->{"distribution"}
l&&rl&&!r!l&&r!l&&!r
1111
MC/DC100%2/2
$args->{"backupid"} xor $args->{"distribution"}
$args->{"backupid"} $args->{"distribution"}
Truth table100%4/4
A xor B
A= $args->{"backupid"}B= $args->{"distribution"}
ABresult
110
101
011
000
106  
107 2 my $url = $self->base_url . '/jiffyBoxes';
108  
109 # transform args into JSON and pass them to API server
110 2 my $response = $self->ua->post($url, Content => to_json($args));
111  
112 # POSSIBLE EXIT
113 2 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 2 0 2
114 2 $self->last ($response->status_line);
115 2 return;
116 }
117  
118 0 $self->last(from_json($response->decoded_content));
119  
120 # POSSIBLE EXIT
121 # TODO: should check the array for more messages
122 0 ▶ if (exists $self->last->{messages}->[0]->{type}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->last->{"messages"}[0]{"type"} and $self->last->{"messages"}[0]{"type"} eq "error") 0 0 0
Condition0%0/3
exists $self->last->{"messages"}[0]{"type"} and $self->last->{"messages"}[0]{"type"} eq "error"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->last->{"messages"}[0]{"type"} and $self->last->{"messages"}[0]{"type"} eq "error"
exists $self->last->{"messages"}[0]{"type"} $self->last->{"messages"}[0]{"type"} eq "error"
Truth table0%0/3
A and B
A= exists $self->last->{"messages"}[0]{"type"}B= $self->last->{"messages"}[0]{"type"} eq "error"
ABresult
0X0
100
111
123 and $self->last->{messages}->[0]->{type} eq 'error') {
124 0 return;
125 }
126  
127 0 my $box_id = $self->last->{result}->{id};
128 0 my $box = VM::JiffyBox::Box->new(id => $box_id, hypervisor => $self, name => $args->{name});
129  
130 0 return $box;
131 }
132  
133 0 ▶sub get_vms {
Subroutine get_vms: not called CC=6 SCAR=37.4
Pod documented
134 0 my ($self) = shift;
135  
136 0 my $url = $self->base_url . '/jiffyBoxes';
137 0 my $response = $self->ua->get( $url );
138  
139 0 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
140 0 $self->last ($response->status_line);
141 0 return;
142 }
143  
144 0 $self->last( from_json( $response->decoded_content ) );
145  
146 0 ▶ if (exists $self->last->{messages}->[0]->{type}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->last->{"messages"}[0]{"type"} and $self->last->{"messages"}[0]{"type"} eq "error") 0 0 0
Condition0%0/3
exists $self->last->{"messages"}[0]{"type"} and $self->last->{"messages"}[0]{"type"} eq "error"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->last->{"messages"}[0]{"type"} and $self->last->{"messages"}[0]{"type"} eq "error"
exists $self->last->{"messages"}[0]{"type"} $self->last->{"messages"}[0]{"type"} eq "error"
Truth table0%0/3
A and B
A= exists $self->last->{"messages"}[0]{"type"}B= $self->last->{"messages"}[0]{"type"} eq "error"
ABresult
0X0
100
111
147 and $self->last->{messages}->[0]->{type} eq 'error') {
148 0 return;
149 }
150  
151 0 my @boxes;
152  
153 0 my $result = $self->last->{result};
154 0 ▶ for my $box_id ( keys %{ $result || {} } ) {
Condition0%0/2
$result || {}
l!l
00
MC/DC0%0/1
$result || {}
$result
Truth table0%0/2
A
A= $result
Aresult
11
00
155 0 my $name = $result->{$box_id}->{name};
156  
157 0 push @boxes, VM::JiffyBox::Box->new(
158 id => $box_id,
159 name => $name,
160 hypervisor => $self,
161 );
162 }
163  
164 0 return @boxes;
165 }
166  
167 0 ▶sub get_distributions {
Subroutine get_distributions: not called CC=2 SCAR=17.9
Pod documented
168 0 my ($self) = shift;
169  
170 0 my $url = $self->base_url . '/distributions';
171 0 my $response = $self->ua->get( $url );
172  
173 0 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
174 0 $self->last ($response->status_line);
175 0 return;
176 }
177  
178 0 $self->last( from_json( $response->decoded_content ) );
179  
180 0 return $self->last;
181 }
182  
183 0 ▶sub get_plans {
Subroutine get_plans: not called CC=2 SCAR=17.9
Pod documented
184 0 my ($self) = shift;
185  
186 0 my $url = $self->base_url . '/plans';
187 0 my $response = $self->ua->get( $url );
188  
189 0 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
190 0 $self->last ($response->status_line);
191 0 return;
192 }
193  
194 0 $self->last( from_json( $response->decoded_content ) );
195  
196 0 return $self->last;
197 }
198  
199 0 ▶sub get_plan_details {
Subroutine get_plan_details: not called CC=2 SCAR=17.9
Pod documented
200 0 my ($self, $id_or_name) = @_;
201  
202 0 my $url = $self->base_url . '/plans/' . $id_or_name;
203 0 my $response = $self->ua->get( $url );
204  
205 0 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
206 0 $self->last ($response->status_line);
207 0 return;
208 }
209  
210 0 $self->last( from_json( $response->decoded_content ) );
211  
212 0 return $self->last;
213 }
214  
215 4 1;
216  
217 __END__
↑ Summary JiffyBox/Box.pm »