JiffyBox/Box.pm

filter: Statementstmt 48.5% 49 / 101 Branchbran 25.0% 8 / 32 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 76.9% 10 / 13 Podpod 100% 8 / 8 totaltotal 47.7% 75 / 157 CC 18 SCAR 26.3
CC
18
Cov
42%
clone
42.0
_status_action
13.7
get_backups
6.5
CRAP 13.9
« JiffyBox.pm ↑ Summary
1 package VM::JiffyBox::Box;
2 4 $VM::JiffyBox::Box::VERSION = '0.032';
3 # ABSTRACT: Representation of a Virtual Machine in JiffyBox
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 has id => (is => 'ro', required => 1);
12 4 has hypervisor => (is => 'ro', required => 1);
13 4 has name => (is => 'rwp');
14  
15 4 has last => (is => 'rw');
16 4 has backup_cache => (is => 'rw');
17 4 has details_cache => (is => 'rw');
18 4 has start_cache => (is => 'rw');
19 4 has stop_cache => (is => 'rw');
20 4 has delete_cache => (is => 'rw');
21 4 has freeze_cache => (is => 'rw');
22 4 has thaw_cache => (is => 'rw');
23  
24 2 ▶sub get_backups {
Subroutine get_backups: called CC=3 SCAR=18.8
Pod documented
25 2 my $self = shift;
26
27 2 my $url = $self->{hypervisor}->base_url . '/backups/' . $self->id;
28
29 # POSSIBLE EXIT
30 2 ▶ return { url => $url } if ($self->{hypervisor}->test_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hypervisor"}->test_mode 2 0 2
31
32 0 my $response = $self->{hypervisor}->ua->get($url);
33  
34 # POSSIBLE EXIT
35 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
36  
37 0 $self->last ( $response->status_line );
38 0 return;
39 }
40  
41 0 my $backup_info = from_json($response->decoded_content);
42  
43 0 $self->last ($backup_info);
44 0 $self->backup_cache ($backup_info);
45 0 return $backup_info ;
46  
47 }
48  
49 3 ▶sub get_details {
Subroutine get_details: called CC=3 SCAR=18.8
Pod documented
50 3 my $self = shift;
51
52 # add method specific stuff to the URL
53 3 my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id;
54
55 # POSSIBLE EXIT
56 # return the URL if we are using test_mode
57 3 ▶ return { url => $url } if ($self->{hypervisor}->test_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hypervisor"}->test_mode 3 0 3
58
59 # send the request and return the response
60 0 my $response = $self->{hypervisor}->ua->get($url);
61  
62 # POSSIBLE EXIT
63 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
64  
65 0 $self->last ( $response->status_line );
66 0 return;
67 }
68  
69 0 my $details = from_json($response->decoded_content);
70  
71 0 $self->last ($details);
72 0 $self->details_cache ($details);
73 0 return $details ;
74 }
75  
76 0 ▶sub clone {
Subroutine clone: not called CC=6 SCAR=37.4
Pod documented
77 0 my $self = shift;
78 0 my $args = shift;
79
80 # POSSIBLE EXIT (DIE)
81 0 ▶ die 'name needed' unless $args->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"name"} 0 0 0
82 0 ▶ die 'planid needed' unless $args->{planid};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"planid"} 0 0 0
83  
84 0 my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id;
85 my $json = to_json( {
86 name => $args->{name},
87 planid => $args->{planid},
88 metadata => $args->{metadata} || {},
89 0 ▶ });
Condition0%0/2
$args->{'metadata'} || {}
l!l
00
MC/DC0%0/1
$args->{'metadata'} || {}
$args->{'metadata'}
Truth table0%0/2
A
A= $args->{'metadata'}
Aresult
11
00
90  
91 # POSSIBLE EXIT
92 return { url => $url, json => $json }
93 0 ▶ if ($self->{hypervisor}->test_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hypervisor"}->test_mode 0 0 0
94
95 # send the request with method specific json content
96 0 my $response = $self->{hypervisor}->ua->post( $url, Content => $json );
97  
98 # POSSIBLE EXIT
99 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
100  
101 0 $self->last ( $response->status_line );
102 0 return;
103 }
104  
105 0 my $clone_info = from_json($response->decoded_content);
106  
107 my $clone_box = __PACKAGE__->new(
108 id => $clone_info->{result}->{id},
109 name => $args->{name},
110 0 hypervisor => $self,
111 );
112  
113 0 return $clone_box;
114 }
115  
116 8 ▶sub _status_action {
Subroutine _status_action: called CC=7 SCAR=26.2
117 8 my $self = shift;
118 8 my $action = shift;
119 8 my $params = shift;
120  
121 8 ▶ return if !$action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 8 8
122  
123 8 my $status = uc $action;
124 8 ▶ $status = 'SHUTDOWN' if 'stop' eq lc $action;
Branch: 1 branch
BranchTrueFalseTotal
if "stop" eq lc $action 4 4 8
125  
126 8 my %opts;
127 8 ▶ if ( $status eq 'THAW' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "THAW") 0 8 8
128 0 $opts{planid} = $params->{planid};
129 }
130
131 8 my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id;
132 8 my $json = to_json( { %opts, status => $status } );
133
134 # POSSIBLE EXIT
135 return { url => $url, json => $json }
136 8 ▶ if ($self->{hypervisor}->test_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hypervisor"}->test_mode 8 0 8
137
138 # send the request with method specific json content
139 0 my $response = $self->{hypervisor}->ua->put( $url, Content => $json );
140  
141 # POSSIBLE EXIT
142 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
143  
144 0 $self->last ( $response->status_line );
145 0 return;
146 }
147  
148 0 my $status_info = from_json($response->decoded_content);
149  
150 0 my $cache_sub = $self->can( $action . '_cache' );
151 0 $self->last ($status_info);
152 0 ▶ $cache_sub->($status_info) if $cache_sub;
Branch: 1 branch
BranchTrueFalseTotal
if $cache_sub 0 0 0
153 0 return $status_info ;
154 }
155  
156 0 ▶sub freeze {
Subroutine freeze: not called CC=1 SCAR=6.9
Pod documented
157 0 my $self = shift;
158  
159 0 return $self->_status_action( 'freeze' );
160 }
161  
162 0 ▶sub thaw {
Subroutine thaw: not called CC=1 SCAR=6.9
Pod documented
163 0 my $self = shift;
164  
165 0 return $self->_status_action( 'thaw', shift );
166 }
167  
168 4 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
169 4 my $self = shift;
170  
171 4 return $self->_status_action( 'start' );
172 }
173  
174 4 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
175 4 my $self = shift;
176  
177 4 return $self->_status_action( 'stop' );
178 }
179  
180 2 ▶sub delete {
Subroutine delete: called CC=3 SCAR=18.8
Pod documented
181 2 my $self = shift;
182
183 2 my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id;
184
185 # POSSIBLE EXIT
186 2 ▶ return { url => $url } if ($self->{hypervisor}->test_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hypervisor"}->test_mode 2 0 2
187
188 0 my $response = $self->{hypervisor}->ua->delete($url);
189  
190 # POSSIBLE EXIT
191 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
192  
193 0 $self->last ( $response->status_line );
194 0 return;
195 }
196  
197 0 my $delete_info = from_json($response->decoded_content);
198  
199 0 $self->last ($delete_info);
200 0 $self->delete_cache ($delete_info);
201 0 return $delete_info ;
202 }
203  
204 4 1;
205  
206 __END__
« JiffyBox.pm ↑ Summary