File Coverage

blib/lib/API/Vultr.pm
Criterion Covered Total %
statement 46 89 51.6
branch 9 32 28.1
condition n/a
subroutine 13 29 44.8
pod 18 21 85.7
total 86 171 50.2


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