line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package VM::JiffyBox; |
2
|
|
|
|
|
|
|
$VM::JiffyBox::VERSION = '0.031'; |
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
|
|
|
4
|
|
57413
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
104
|
|
7
|
4
|
|
|
4
|
|
13
|
use warnings; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
80
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
1751
|
use Moo; |
|
4
|
|
|
|
|
37786
|
|
|
4
|
|
|
|
|
23
|
|
10
|
4
|
|
|
4
|
|
5566
|
use JSON; |
|
4
|
|
|
|
|
21783
|
|
|
4
|
|
|
|
|
20
|
|
11
|
4
|
|
|
4
|
|
2595
|
use LWP::UserAgent; |
|
4
|
|
|
|
|
121332
|
|
|
4
|
|
|
|
|
130
|
|
12
|
4
|
|
|
4
|
|
29
|
use Scalar::Util qw( reftype ); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
343
|
|
13
|
|
|
|
|
|
|
|
14
|
4
|
|
|
4
|
|
1763
|
use VM::JiffyBox::Box; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
3320
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has domain_name => (is => 'rw', default => sub {'https://api.jiffybox.de'}); |
17
|
|
|
|
|
|
|
has version => (is => 'rw', default => sub {'v1.0'}); |
18
|
|
|
|
|
|
|
has token => (is => 'rw', required => 1); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has ua => (is => 'rw', default => sub {LWP::UserAgent->new()}); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has test_mode => (is => 'rw', default => sub {'0'}); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# should always keep the last message from the server |
25
|
|
|
|
|
|
|
has last => (is => 'rw'); |
26
|
|
|
|
|
|
|
has details_cache => (is => 'rw'); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub base_url { |
29
|
17
|
|
|
17
|
0
|
15
|
my $self = shift; |
30
|
|
|
|
|
|
|
|
31
|
17
|
|
|
|
|
109
|
return $self->domain_name . '/' |
32
|
|
|
|
|
|
|
. $self->token . '/' |
33
|
|
|
|
|
|
|
. $self->version ; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get_details { |
37
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
0
|
my $url = $self->base_url . '/jiffyBoxes'; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
0
|
my $response = $self->ua->get($url); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# POSSIBLE EXIT |
44
|
0
|
0
|
|
|
|
0
|
unless ($response->is_success) { |
45
|
0
|
|
|
|
|
0
|
$self->last ($response->status_line); |
46
|
0
|
|
|
|
|
0
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
0
|
my $details = from_json($response->decoded_content); |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
0
|
$self->last ( $details ); |
52
|
0
|
|
|
|
|
0
|
$self->details_cache( $details ); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
0
|
return $details; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub get_id_from_name { |
58
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
59
|
0
|
|
0
|
|
|
0
|
my $box_name = shift || die 'box_name as argument needed'; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
0
|
my $details = $self->get_details; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# EXIT if no details |
64
|
0
|
0
|
|
|
|
0
|
return unless $details; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
$self->last ( $details ); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# EXIT if no expected result |
69
|
0
|
0
|
|
|
|
0
|
return unless (reftype $details eq 'HASH'); |
70
|
0
|
0
|
|
|
|
0
|
return unless (exists $details->{result}); |
71
|
0
|
0
|
|
|
|
0
|
return unless (reftype $details->{result} eq 'HASH'); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
$self->details_cache( $details ); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# look for a match in the results |
76
|
0
|
|
|
|
|
0
|
foreach my $box (values %{$details->{result}}) { |
|
0
|
|
|
|
|
0
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# EXIT if no expected result |
79
|
0
|
0
|
|
|
|
0
|
return unless (reftype $box eq 'HASH'); |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
0
|
return $box->{id} if ($box->{name} eq $box_name); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# if we reach here, this means there was no match |
85
|
0
|
|
|
|
|
0
|
return; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub get_vm { |
89
|
9
|
|
|
9
|
1
|
1177
|
my $self = shift; |
90
|
9
|
|
100
|
|
|
39
|
my $box_id = shift || die 'box_id needed'; |
91
|
|
|
|
|
|
|
|
92
|
8
|
|
|
|
|
89
|
my $box = VM::JiffyBox::Box->new(id => $box_id, hypervisor => $self); |
93
|
|
|
|
|
|
|
|
94
|
8
|
|
|
|
|
2285
|
return $box; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub create_vm { |
98
|
7
|
|
|
7
|
1
|
2552
|
my $self = shift; |
99
|
7
|
|
|
|
|
20
|
my $args = {@_}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# POSSIBLE EXIT (DIE) |
102
|
7
|
100
|
|
|
|
35
|
die 'name needed' unless $args->{name}; |
103
|
5
|
100
|
|
|
|
18
|
die 'planid needed' unless $args->{planid}; |
104
|
|
|
|
|
|
|
die 'backupid or distribution needed' unless $args->{backupid} |
105
|
4
|
100
|
100
|
|
|
36
|
xor $args->{distribution}; |
106
|
|
|
|
|
|
|
|
107
|
2
|
|
|
|
|
7
|
my $url = $self->base_url . '/jiffyBoxes'; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# transform args into JSON and pass them to API server |
110
|
2
|
|
|
|
|
9
|
my $response = $self->ua->post($url, Content => to_json($args)); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# POSSIBLE EXIT |
113
|
2
|
50
|
|
|
|
638405
|
unless ($response->is_success) { |
114
|
0
|
|
|
|
|
0
|
$self->last ($response->status_line); |
115
|
0
|
|
|
|
|
0
|
return; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
34
|
$self->last(from_json($response->decoded_content)); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# POSSIBLE EXIT |
121
|
|
|
|
|
|
|
# TODO: should check the array for more messages |
122
|
2
|
50
|
33
|
|
|
306
|
if (exists $self->last->{messages}->[0]->{type} |
123
|
|
|
|
|
|
|
and $self->last->{messages}->[0]->{type} eq 'error') { |
124
|
2
|
|
|
|
|
54
|
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
|
|
|
|
|
|
|
sub get_vms { |
134
|
0
|
|
|
0
|
1
|
|
my ($self) = shift; |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $url = $self->base_url . '/jiffyBoxes'; |
137
|
0
|
|
|
|
|
|
my $response = $self->ua->get( $url ); |
138
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
unless ( $response->is_success ) { |
140
|
0
|
|
|
|
|
|
$self->last ($response->status_line); |
141
|
0
|
|
|
|
|
|
return; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
$self->last( from_json( $response->decoded_content ) ); |
145
|
|
|
|
|
|
|
|
146
|
0
|
0
|
0
|
|
|
|
if (exists $self->last->{messages}->[0]->{type} |
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
|
0
|
|
|
|
|
for my $box_id ( keys %{ $result || {} } ) { |
|
0
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub get_distributions { |
168
|
0
|
|
|
0
|
1
|
|
my ($self) = shift; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $url = $self->base_url . '/distributions'; |
171
|
0
|
|
|
|
|
|
my $response = $self->ua->get( $url ); |
172
|
|
|
|
|
|
|
|
173
|
0
|
0
|
|
|
|
|
unless ( $response->is_success ) { |
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
|
|
|
|
|
|
|
sub get_plans { |
184
|
0
|
|
|
0
|
1
|
|
my ($self) = shift; |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
my $url = $self->base_url . '/plans'; |
187
|
0
|
|
|
|
|
|
my $response = $self->ua->get( $url ); |
188
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
unless ( $response->is_success ) { |
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
|
|
|
|
|
|
|
sub get_plan_details { |
200
|
0
|
|
|
0
|
1
|
|
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
|
0
|
|
|
|
|
unless ( $response->is_success ) { |
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
|
|
|
|
|
|
|
1; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
__END__ |