JiffyBox/Box.pm

filter: Statementstmt 46.8% 45 / 96 Branchbran 21.4% 6 / 28 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 76.9% 10 / 13 Podpod 100% 8 / 8 totaltotal 46.6% 69 / 148 SCAR 42.5
CC
16
Cov
40%
clone
42.0
_status_action
10.0
get_backups
6.5
CRAP 70.0
« JiffyBox.pm ↑ Summary
1 package VM::JiffyBox::Box;
2 4 $VM::JiffyBox::Box::VERSION = '0.031';
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 sub get_backups {
25 2 ▶ my $self = shift;
Subroutine get_backups: called CC=3 SCAR=18.8
Pod documented
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 sub get_details {
50 3 ▶ my $self = shift;
Subroutine get_details: called CC=3 SCAR=18.8
Pod documented
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 sub clone {
77 0 ▶ my $self = shift;
Subroutine clone: not called CC=6 SCAR=37.4
Pod documented
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 sub _status_action {
117 8 ▶ my $self = shift;
Subroutine _status_action: called CC=5 SCAR=23.0
118 8 my $action = shift;
119  
120 8 my $status = uc $action;
121 8 ▶ $status = 'SHUTDOWN' if 'stop' eq lc $action;
Branch: 1 branch
BranchTrueFalseTotal
if "stop" eq lc $action 4 4 8
122
123 8 my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id;
124 8 my $json = to_json( { status => $status } );
125
126 # POSSIBLE EXIT
127 return { url => $url, json => $json }
128 8 ▶ if ($self->{hypervisor}->test_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hypervisor"}->test_mode 8 0 8
129
130 # send the request with method specific json content
131 0 my $response = $self->{hypervisor}->ua->put( $url, Content => $json );
132  
133 # POSSIBLE EXIT
134 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
135  
136 0 $self->last ( $response->status_line );
137 0 return;
138 }
139  
140 0 my $status_info = from_json($response->decoded_content);
141  
142 0 my $cache_sub = $self->can( $action . '_cache' );
143 0 $self->last ($status_info);
144 0 ▶ $cache_sub->($status_info) if $cache_sub;
Branch: 1 branch
BranchTrueFalseTotal
if $cache_sub 0 0 0
145 0 return $status_info ;
146 }
147  
148 sub freeze {
149 0 ▶ my $self = shift;
Subroutine freeze: not called CC=1 SCAR=6.9
Pod documented
150  
151 0 return $self->_status_action( 'freeze' );
152 }
153  
154 sub thaw {
155 0 ▶ my $self = shift;
Subroutine thaw: not called CC=1 SCAR=6.9
Pod documented
156  
157 0 return $self->_status_action( 'thaw' );
158 }
159  
160 sub start {
161 4 ▶ my $self = shift;
Subroutine start: called CC=1 SCAR=0.0
Pod documented
162  
163 4 return $self->_status_action( 'start' );
164 }
165  
166 sub stop {
167 4 ▶ my $self = shift;
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
168  
169 4 return $self->_status_action( 'stop' );
170 }
171  
172 sub delete {
173 2 ▶ my $self = shift;
Subroutine delete: called CC=3 SCAR=18.8
Pod documented
174
175 2 my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id;
176
177 # POSSIBLE EXIT
178 2 ▶ return { url => $url } if ($self->{hypervisor}->test_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hypervisor"}->test_mode 2 0 2
179
180 0 my $response = $self->{hypervisor}->ua->delete($url);
181  
182 # POSSIBLE EXIT
183 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
184  
185 0 $self->last ( $response->status_line );
186 0 return;
187 }
188  
189 0 my $delete_info = from_json($response->decoded_content);
190  
191 0 $self->last ($delete_info);
192 0 $self->delete_cache ($delete_info);
193 0 return $delete_info ;
194 }
195  
196 4 1;
197  
198 __END__
« JiffyBox.pm ↑ Summary