blib/lib/API/Vultr.pm

filter: Statementstmt 18.1% 16 / 88 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 17.8% 5 / 28 Podpod 85.7% 18 / 21 totaltotal 23.0% 39 / 169 CC 17 SCAR 16.3
CC
17
Cov
13%
attach_iso_to_instance
12.0
_make_uri
6.0
_request
6.0
CRAP 5.1
↑ Summary
1 package API::Vultr;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.001';
12  
13 sub _make_uri {
14 0 ▶ my ( $self, $path, %query ) = @_;
Subroutine _make_uri: not called CC=2 SCAR=17.9
15  
16 0 my $uri = URI->new( 'https://api.vultr.com/v2' . $path );
17 0 ▶ if (%query) {
Branch: 1 branch
BranchTrueFalseTotal
if (%query) 0 0 0
18 0 $uri->query_form(%query);
19 }
20  
21 0 return $uri->as_string;
22 }
23  
24 sub _request {
25 0 ▶ my ( $self, $method, $uri, $body ) = @_;
Subroutine _request: not called CC=2 SCAR=17.9
26  
27 0 ▶ if ( not( defined $body ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $body) { } 0 0 0
28 0 my $lc_method = lc $method;
29 return $self->ua->$lc_method( $uri,
30 0 Authorization => 'Bearer ' . $self->{api_key} );
31 }
32 else {
33 0 my $request = HTTP::Request->new( uc $method, $uri );
34 0 $request->header( 'Content-Type' => 'application/json' );
35 0 $request->content($body);
36 0 return $self->ua->request($request);
37 }
38 }
39  
40 sub api_key {
41 0 ▶ my ( $self, $setter ) = @_;
Subroutine api_key: not called CC=2 SCAR=17.9
Pod documented
42  
43 0 ▶ if ( defined $setter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setter) 0 0 0
44 0 return $self->{api_key} = $setter;
45 }
46  
47 0 return $self->{api_key};
48 }
49  
50 sub ua {
51 0 ▶ my ( $self, $setter ) = @_;
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
52  
53 0 ▶ if ( defined $setter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setter) 0 0 0
54 0 return $self->{ua} = $setter;
55 }
56  
57 0 return $self->{ua};
58 }
59  
60 sub new {
61 0 ▶ my ( $class, %args ) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
62  
63 croak
64 qq{You must specify an API key when creating an instance of API::Vultr}
65 0 ▶ unless exists $args{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"api_key"} 0 0 0
66  
67 0 my $self = { %args, ua => LWP::UserAgent->new( timeout => 10 ) };
68  
69 0 return bless( $self, __PACKAGE__ );
70 }
71  
72 # ACCOUNT #
73  
74 sub get_account_info {
75 0 ▶ my $self = shift;
Subroutine get_account_info: not called CC=1 SCAR=6.9
Pod documented
76 0 return $self->_request( 'get', $self->_make_uri('/account') );
77 }
78  
79 # APPLICATIONS #
80  
81 sub get_applications {
82 0 ▶ my $self = shift;
Subroutine get_applications: not called CC=1 SCAR=6.9
Pod documented
83 0 return $self->_request( 'get', $self->_make_uri('/applications') );
84 }
85  
86 # BACKUPS #
87  
88 sub get_backups {
89 0 ▶ my ( $self, %query ) = @_;
Subroutine get_backups: not called CC=1 SCAR=6.9
Pod documented
90 0 return $self->_request( 'get', $self->_make_uri( '/backups', %query ) );
91 }
92  
93 sub get_backup_by_id {
94 0 ▶ my ( $self, $id ) = @_;
Subroutine get_backup_by_id: not called CC=1 SCAR=6.9
Pod documented
95 0 return $self->_request( 'get', $self->_make_uri( '/backups/' . $id ) );
96 }
97  
98 # INSTANCES #
99  
100 sub list_instances {
101 0 ▶ my ( $self, %query ) = @_;
Subroutine list_instances: not called CC=1 SCAR=6.9
Pod documented
102 0 return $self->_request( 'get', $self->_make_uri( '/instances', %query ) );
103 }
104  
105 sub create_instance {
106 0 ▶ my ( $self, %body ) = @_;
Subroutine create_instance: not called CC=1 SCAR=6.9
Pod documented
107 0 return $self->_request( 'post', $self->_make_uri('/instances'), {%body} );
108 }
109  
110 sub get_instance_by_id {
111 0 ▶ my ( $self, $id ) = @_;
Subroutine get_instance_by_id: not called CC=2 SCAR=17.9
Pod documented
112  
113 0 ▶ croak qq{ID cannot be undefined when calling get_instance_by_id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
114 unless defined $id;
115  
116 0 return $self->_request( 'get', $self->_make_uri( '/instances/' . $id ) );
117 }
118  
119 sub delete_instance_by_id {
120 0 ▶ my ( $self, $id ) = @_;
Subroutine delete_instance_by_id: not called CC=2 SCAR=17.9
Pod documented
121  
122 0 ▶ croak qq{ID cannot be undefined when calling get_instance_by_id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
123 unless defined $id;
124  
125 0 return $self->_request( 'delete', $self->_make_uri( '/instances/' . $id ) );
126 }
127  
128 sub halt_instances {
129 0 ▶ my ( $self, @ids ) = @_;
Subroutine halt_instances: not called CC=2 SCAR=17.9
Pod documented
130  
131 0 ▶ croak qq{Expected list of ids, instead got undef.}
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 0 0
132 unless @ids;
133  
134 0 return $self->_request(
135 'post',
136 $self->_make_uri('/instances/halt'),
137 { instance_ids => [@ids] }
138 );
139 }
140  
141 sub reboot_instances {
142 0 ▶ my ( $self, @ids ) = @_;
Subroutine reboot_instances: not called CC=2 SCAR=17.9
Pod documented
143  
144 0 ▶ croak qq{Expected list of ids, instead got undef.}
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 0 0
145 unless @ids;
146  
147 0 return $self->_request(
148 'post',
149 $self->_make_uri('/instances/reboot'),
150 { instance_ids => [@ids] }
151 );
152 }
153  
154 sub start_instances {
155 0 ▶ my ( $self, @ids ) = @_;
Subroutine start_instances: not called CC=2 SCAR=17.9
Pod documented
156  
157 0 ▶ croak qq{Expected list of ids, instead got undef.}
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 0 0
158 unless @ids;
159  
160 0 return $self->_request(
161 'post',
162 $self->_make_uri('/instances/start'),
163 { instance_ids => [@ids] }
164 );
165 }
166  
167 sub get_instance_bandwidth {
168 0 ▶ my ( $self, $id, %query ) = @_;
Subroutine get_instance_bandwidth: not called CC=2 SCAR=17.9
Pod documented
169  
170 0 ▶ croak qq{Expected scalar id as second argument, instead got $id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
171 unless defined $id;
172  
173 0 return $self->_request( 'get',
174 $self->_make_uri( '/instances/' . $id . '/bandwidth', %query ) );
175 }
176  
177 sub get_instance_neighbours {
178 0 ▶ my ( $self, $id ) = @_;
Subroutine get_instance_neighbours: not called CC=2 SCAR=17.9
Pod documented
179  
180 0 ▶ croak qq{Expected scalar id as second argument, instead got $id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
181 unless defined $id;
182  
183 0 return $self->_request( 'get',
184 $self->_make_uri( '/instances/' . $id . '/neighbours' ) );
185 }
186  
187 sub get_instance_iso_status {
188 0 ▶ my ( $self, $id ) = @_;
Subroutine get_instance_iso_status: not called CC=2 SCAR=17.9
Pod documented
189  
190 0 ▶ croak qq{Expected scalar id as second argument, instead got $id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
191 unless defined $id;
192  
193 0 return $self->_request( 'get',
194 $self->_make_uri( '/instances/' . $id . '/iso' ) );
195 }
196  
197 sub detach_iso_from_instance {
198 0 ▶ my ( $self, $id ) = @_;
Subroutine detach_iso_from_instance: not called CC=2 SCAR=17.9
Pod documented
199  
200 0 ▶ croak qq{Expected scalar id as second argument, instead got $id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
201 unless defined $id;
202  
203 0 return $self->_request( 'post',
204 $self->_make_uri( '/instances/' . $id . '/iso/detach' ) );
205 }
206  
207 sub attach_iso_to_instance {
208 0 ▶ my ( $self, $id, $iso_id ) = @_;
Subroutine attach_iso_to_instance: not called CC=3 SCAR=24.8
Pod documented
209  
210 0 ▶ croak qq{Expected scalar id as second argument, instead got $id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
211 unless defined $id;
212  
213 0 ▶ croak qq{Expected scalar iso_id as second argument, instead got $iso_id.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iso_id 0 0 0
214 unless defined $iso_id;
215  
216 0 return $self->_request(
217 'post',
218 $self->_make_uri( '/instances/' . $id . '/iso/attach' ),
219 { iso_id => $iso_id }
220 );
221 }
222  
223 # ISO #
224  
225 sub get_isos {
226 0 ▶ my ( $self, %query ) = @_;
Subroutine get_isos: not called CC=1 SCAR=6.9
Pod undocumented
227 0 return $self->_request( $self->_make_uri( '/iso', %query ) );
228 }
229  
230 sub create_iso {
231 0 ▶ my ( $self, %body ) = @_;
Subroutine create_iso: not called CC=1 SCAR=6.9
Pod undocumented
232 0 return $self->_request( $self->_make_uri('/iso'), {%body} );
233 }
234  
235 1 1;
236  
237 =encoding utf8
238  
239 =head1 Name
240  
241 API::Vultr
242  
243 =head1 Synopsis
244  
245 A simple, and inuitive interface to the L<Vultr Api|https://https://www.vultr.com/api> using L<LWP::UserAgent>.
246  
247 This does not cover the entire Vultr API, but instead intends to be very
248 extendible allowing for easy contributions. Basically, I have what I need,
249 if you need more feel free to add it!
250  
251 =head1 Example
252  
253 use API::Vultr;
254 use Data::Dumper qw(Dumper);
255  
256 my $vultr_api = API::Vultr->new(api_key => $ENV{VULTR_API_KEY});
257  
258 my $create_response = $vultr_api->create_instance(
259 region => 'ewr',
260 plan => 'vc2-6c-16gb',
261 label => 'My Instance',
262 os_id => 215,
263 user_data => 'QmFzZTY4EVsw32WfsGGHsjKJI',
264 backups => 'enabled',
265 hostname => 'hostname'
266 );
267  
268 if ($create_response->is_success) {
269 print Dumper($create_response->decoded_content);
270 }
271 else {
272 die $create_response->status_line;
273 }
274  
275 =head1 API
276  
277 =head2 ua
278  
279 Set, or get the L<LWP::UserAgent> associated L<API::Vultr> instance.
280  
281 =head2 api_key
282  
283 Set, or get the Vultr API key associated with the L<API::Vultr> instance.
284  
285 =head2 get_account_info
286  
287 Retrieve the account information associated with your API key.
288  
289 L<Vultr API Reference|https://www.vultr.com/api/#tag/account/operation/get-account>
290  
291 =head2 get_applications
292  
293 Retrieve applications associated with your API key.
294  
295 L<Vultr API Reference|https://www.vultr.com/api/#tag/application/operation/list-applications>
296  
297 =head2 get_backups
298  
299 Get a list of all backups associated with your API key.
300  
301 L<Vultr API Reference|https://www.vultr.com/api/#tag/backup/operation/list-backups>
302  
303 =head2 get_backup_by_id
304  
305 Get information on a specific backup by its id.
306  
307 L<Vultr API Reference|https://www.vultr.com/api/#tag/backup/operation/get-backup>
308  
309 =head2 list_instances
310  
311 Get a list of all instances associated with your API key.
312  
313 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/list-instances>
314  
315 =head2 create_instance
316  
317 Create a Vultr instance.
318  
319 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/create-instance>
320  
321 =head2 get_instance_by_id
322  
323 Find an instance by its id.
324  
325 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/get-instance>
326  
327 =head2 delete_instance_by_id
328  
329 Delete an instance by its id.
330  
331 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/delete-instance>
332  
333 =head2 halt_instances
334  
335 Halt a list of instances by their ids.
336  
337 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/halt-instances>
338  
339 =head2 reboot_instances
340  
341 Reboot a list of instances by their ids.
342  
343 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/reboot-instances>
344  
345 =head2 start_instances
346  
347 Reboot a list of instances by their ids.
348  
349 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/start-instances>
350  
351 =head2 get_instance_bandwidth
352  
353 Get the remaining bandwidth of a given instance.
354  
355 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/get-instance-bandwidth>
356  
357 =head2 get_instance_neighbours
358  
359 Get a list of instances in the same location as the specified instance.
360  
361 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/get-instance-neighbors>
362  
363 =head2 get_instance_iso_status
364  
365 Get the ISO status for an instance.
366  
367 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/get-instance-iso-status>
368  
369 =head2 detach_iso_from_instance
370  
371 Detach an ISO from a specified instance.
372  
373 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/detach-instance-iso>
374  
375 =head2 attach_iso_to_instance
376  
377 Attach an ISO to a specified instance.
378  
379 L<Vultr API Reference|https://www.vultr.com/api/#tag/instances/operation/attach-instance-iso>
380  
381 =cut
↑ Summary