blib/lib/Net/Fastly.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Net/Fastly/Origin.pm ↑ Summary blib/lib/Net/Fastly/Client.pm »
1 package Net::Fastly;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Net::Fastly::Client;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Net::Fastly::Invoice;
8 use Net::Fastly::Settings;
9  
10 our $VERSION = "1.02";
11  
12 BEGIN {
13 no strict 'refs';
14 our @CLASSES = qw(Net::Fastly::User Net::Fastly::Customer
15 Net::Fastly::Backend Net::Fastly::Director
16 Net::Fastly::Domain Net::Fastly::Healthcheck
17 Net::Fastly::Match Net::Fastly::Origin
18 Net::Fastly::Service Net::Fastly::Syslog
19 Net::Fastly::VCL Net::Fastly::Version
20 Net::Fastly::Condition);
21  
22 foreach my $class (@CLASSES) {
23 my $file = $class . '.pm';
24 $file =~ s{::}{/}g;
25 CORE::require($file);
26 $class->import;
27
28 my $name = $class->_path;
29
30 foreach my $method (qw(get create update delete list)) {
31 my $code = "sub { shift->_$method('$class', \@_) }";
32 my $glob = "${method}_${name}";
33 $glob .= "s" if $method eq 'list';
34 # don't create this if it's a list and something isn't listable ...
35 next if $method eq 'list' && $class->_skip_list;
36 # or if it already exists (i.e it's been overidden)
37 next if defined *$glob;
38 *$glob = eval "$code";
39 }
40 }
41 };
42  
43 =head1 NAME
44  
45 Net::Fastly - client library for interacting with the Fastly web acceleration service
46  
47 =head1 SYNOPSIS
48  
49 my $fastly = Net::Fastly->new(%login_opts);
50
51 my $current_user = $fastly->current_user;
52 my $current_customer = $fastly->current_customer;
53
54 my $user = $fastly->get_user($current_user->id);
55 my $customer = $fastly->get_customer($current_customer->id);
56
57 print "Name: ".$user->name."\n";
58 print "Works for ".$user->customer->name."\n";
59 print "Which is the same as ".$customer->name."\n";
60 print "Which has the owner ".$customer->owner->name."\n";
61
62 # Let's see which services we have defined
63 foreach my $service ($fastly->list_services) {
64 print $service->name." (".$service->id.")\n";
65 foreach my $version ($service->versions) {
66 print "\t".$version->number."\n";
67 }
68 }
69
70 my $service = $fastly->create_service(name => "MyFirstService");
71 my $latest_version = $service->version;
72
73 # Create a domain and a backend for the service ...
74 my $domain = $fastly->create_domain(service_id => $service->id, version => $latest_version->number, name => "www.example.com");
75 my $backend = $fastly->create_backend(service_id => $service->id, version => $latest_version->number, ipv4 => "127.0.0.1", port => 80);
76
77 # ... and activate it. You're now hosted on Fastly.
78 $latest_version->activate;
79
80 # Let's take a peek at the VCL that Fastly generated for us
81 my $vcl = $latest_version->generated_vcl;
82 print "Generated VCL file is:\n".$vcl->content."\n";
83
84 # Now let's create a new version ...
85 my $new_version = $latest_version->clone;
86 # ... add a new backend ...
87 my $new_backend = $fastly->create_backend(service_id => $service->id, version => $new_version->number, ipv4 => "192.0.0.1", port => 8080);
88 # ... and upload some custome vcl (presuming we have permissions)
89 $new_version->upload_vcl($vcl_name, slurp($vcl_file));
90
91 $new_version->activate;
92  
93  
94 =head1 DESCRIPTION
95  
96 =head1 METHODS
97  
98 =cut
99  
100  
101 =head2 new <opt[s]>
102  
103 Create a new Fastly client. Options are
104  
105 =over 4
106  
107 =item user - your Fastly login
108  
109 =item password - your Fastly password
110  
111 =item api_key - your Fastly api key
112  
113 =back
114  
115 You only need to pass in C<api_key> OR C<user> and C<password>.
116  
117 Some methods require full username and password rather than just auth token.
118  
119 =cut
120 sub new {
121 my $class = shift;
122 my %opts = @_;
123 my ($client, $user, $customer) = Net::Fastly::Client->new(%opts);
124 my $self = bless { _client => $client, _current_customer => undef, _current_user => undef}, $class;
125 if ($user && $customer) {
126 $self->{_current_user} = Net::Fastly::User->new($self, %$user);
127 $self->{_current_customer} = Net::Fastly::Customer->new($self, %$customer);
128 }
129 return $self;
130 }
131  
132 =head2 client
133  
134 Get the current Net::Fastly::Client
135  
136 =cut
137 sub client { shift->{_client} }
138  
139 =head2 set_customer <customer id>
140  
141 Set the current customer to act as.
142  
143 B<NOTE>: this will only work if you're an admin
144  
145 =cut
146 sub set_customer {
147 my $self = shift;
148 my $id = shift;
149 die "You must be fully authed to set the customer" unless $self->fully_authed;
150 die "You must be an admin to set the customer" unless $self->current_user->can_do('admin');
151 delete $self->{_current_customer};
152 $self->client->set_customer($id);
153 }
154  
155 =head2 authed
156  
157 Whether or not we're authed at all by either username & password or API key
158  
159 =cut
160 sub authed { shift->client->authed }
161  
162 =head2 fully_authed
163  
164 Whether or not we're fully (username and password) authed
165  
166 =cut
167 sub fully_authed { shift->client->fully_authed }
168  
169 =head2 current_user
170  
171 Return a User object representing the current logged in user.
172  
173 This will not work if you're logged in with an API key.
174  
175 =cut
176 sub current_user {
177 my $self = shift;
178 die "You must be fully authed to get the current user" unless $self->fully_authed;
179 $self->{_current_user} ||= $self->_get("Net::Fastly::User");
180 }
181  
182 =head2 current_customer
183  
184 Return a Customer object representing the customer of the current logged in user.
185  
186 =cut
187 sub current_customer {
188 my $self = shift;
189 die "You must be authed to get the current customer" unless $self->authed;
190 $self->{_current_customer} ||= $self->_get("Net::Fastly::Customer");
191 }
192  
193 =head2 commands
194  
195 Return a hash representing all commands available.
196  
197 Useful for information.
198  
199 =cut
200 sub commands {
201 my $self = shift;
202 return $self->{__cache_commands} if ($self->{__cache_commands});
203 return eval { $self->{__cache_commands} = $self->client->_get('/commands') };
204 }
205  
206 =head2 purge <path>
207  
208 Purge the specified path from your cache.
209  
210 =cut
211 sub purge {
212 my $self = shift;
213 my $path = shift;
214 $self->client->_post("/purge/$path");
215 }
216  
217 =head2 stats [opt[s]]
218  
219 Fetches historical stats for each of your fastly services and groups the results by service id.
220  
221 If you pass in a C<field> opt then fetches only the specified field.
222  
223 If you pass in a C<service> opt then fetches only the specified service.
224  
225 The C<field> and C<service> opts can be combined.
226  
227 If you pass in an C<aggregate> flag then fetches historical stats information aggregated across all of your Fastly services. This cannot be combined with C<field> and C<service>.
228  
229 Other options available are:
230  
231 =over 4
232  
233 =item from & to
234  
235 =item by
236  
237 =item region
238  
239 =back
240  
241 See http://docs.fastly.com/docs/stats for details.
242  
243 =cut
244 sub stats {
245 my $self = shift;
246 my %opts = @_;
247
248 die "You can't specify a field or a service for an aggregate request" if $opts{aggregate} && ($opts{field} || $opts{service});
249
250 my $url = "/stats";
251  
252 if (delete $opts{aggregate}) {
253 $url .= "/aggregate";
254 }
255
256 if (my $service = delete $opts{service}) {
257 $url .= "/service/$service";
258 }
259
260 if (my $field = delete $opts{field}) {
261 $url .= "/field/$field";
262 }
263
264 $self->client->_get_stats($url, %opts);
265 }
266  
267  
268 =head2 usage [opt[s]]
269  
270 Returns usage information aggregated across all Fastly services and grouped by region.
271  
272 If the C<by_service> flag is passed then teturns usage information aggregated by service and grouped by service & region.
273  
274 Other options available are:
275  
276 =over 4
277  
278 =item from & to
279  
280 =item by
281  
282 =item region
283  
284 =back
285  
286 See http://docs.fastly.com/docs/stats for details.
287  
288 =cut
289 sub usage {
290 my $self = shift;
291 my %opts = @_;
292
293 my $url = "/stats/usage";
294 $url .= "_by_service" if delete $opts{by_service};
295
296 $self->client->_get_stats($url, %opts);
297 }
298  
299  
300 =head2 regions
301  
302 Fetches the list of codes for regions that are covered by the Fastly CDN service.
303  
304 =cut
305 sub regions {
306 my $self = shift;
307 $self->client->_get_stats("/stats/regions");
308 }
309  
310  
311 =head2 create_user <opts>
312  
313 =head2 create_customer <opts>
314  
315 =head2 create_service <opts>
316  
317 =head2 create_version service_id => <service id>, [opts]
318  
319 =head2 create_backend service_id => <service id>, version => <version number>, name => <name> <opts>
320  
321 =head2 create_director service_id => <service id>, version => <version number>, name => <name> <opts>
322  
323 =head2 create_domain service_id => <service id>, version => <version number>, name => <name> <opts>
324  
325 =head2 create_healthcheck service_id => <service id>, version => <version number>, name => <name> <opts>
326  
327 =head2 create_match service_id => <service id>, version => <version number>, name => <name> <opts>
328  
329 =head2 create_origin service_id => <service id>, version => <version number>, name => <name> <opts>
330  
331 =head2 create_syslog service_id => <service id>, version => <version number>, name => <name> <opts>
332  
333 =head2 create_vcl service_id => <service id>, version => <version number>, name => <name> <opts>
334  
335 =head2 create_condition service_id => <service id>, version => <version number>, name => <name> <opts>
336  
337 Create new objects.
338  
339 =cut
340  
341 =head2 get_user <id>
342  
343 =head2 get_customer <id>
344  
345 =head2 get_service <id>
346  
347 =head2 get_version <service id> <number>
348  
349 =head2 get_backend <service id> <version number> <name>
350  
351 =head2 get_director <service id> <version number> <name>
352  
353 =head2 get_domain <service id> <version number> <name>
354  
355 =head2 get_healthcheck <service id> <version number> <name>
356  
357 =head2 get_invoice [<year> <month>]
358  
359 Return a Net::Fastly::Invoice objects representing an invoice for all services.
360  
361 If a year and month are passed in returns the invoice for that whole month.
362  
363 Otherwise it returns the invoices for the current month to date.
364  
365 =head2 get_match <service id> <version number> <name>
366  
367 =head2 get_origin <service id> <version number> <name>
368  
369 =head2 get_syslog <service id> <version number> <name>
370  
371 =head2 get_vcl <service id> <version number> <name>
372  
373 =head2 get_version <service id> <version number> <name>
374  
375 =head2 get_settings <service id> <version number>
376  
377 =head2 get_condition <service id> <version number> <name>
378  
379 Get existing objects.
380  
381 =cut
382  
383  
384 =head2 update_user <obj>
385  
386 =head2 update_customer <obj>
387  
388 =head2 update_service <obj>
389  
390 =head2 update_version <obj>
391  
392 =head2 update_backend <obj>
393  
394 =head2 update_director <obj>
395  
396 =head2 update_domain <obj>
397  
398 =head2 update_healthcheck <obj>
399  
400 =head2 update_match <obj>
401  
402 =head2 update_origin <obj>
403  
404 =head2 update_syslog <obj>
405  
406 =head2 update_vcl <obj>
407  
408 =head2 update_version <obj>
409  
410 =head2 update_settings <obj>
411  
412 =head2 update_condition <obj>
413  
414 Update existing objects.
415  
416 Note - you can also do
417  
418 $obj->save;
419  
420 =cut
421  
422  
423 =head2 delete_user <obj>
424  
425 =head2 delete_customer <obj>
426  
427 =head2 delete_service <obj>
428  
429 =head2 delete_version <obj>
430  
431 =head2 delete_backend <obj>
432  
433 =head2 delete_director <obj>
434  
435 =head2 delete_domain <obj>
436  
437 =head2 delete_healthcheck <obj>
438  
439 =head2 delete_match <obj>
440  
441 =head2 delete_origin <obj>
442  
443 =head2 delete_syslog <obj>
444  
445 =head2 delete_vcl <obj>
446  
447 =head2 delete_version <obj>
448  
449 =head2 delete_condition <obj>
450  
451 Delete existing objects.
452  
453 Note - you can also do
454  
455 $obj->delete
456  
457 =cut
458  
459  
460  
461 =head2 list_users
462  
463 =head2 list_customers
464  
465 =head2 list_versions
466  
467 =head2 list_services
468  
469 =head2 list_backends
470  
471 =head2 list_directors
472  
473 =head2 list_domains
474  
475 =head2 list_healthchecks
476  
477 =head2 list_matchs
478  
479 =head2 list_origins
480  
481 =head2 list_syslogs
482  
483 =head2 list_vcls
484  
485 =head2 list_versions
486  
487 =head2 list_conditions
488  
489 Get a list of all objects
490  
491 =head2 search_services <param[s]>
492  
493 Search all the services that the current customer has.
494  
495 In general you'll want to do
496  
497 my @services = $fastly->search_services(name => $name);
498  
499 or
500  
501 my ($service) = $fastly->search_services(name => $name, version => $number);
502  
503 =cut
504  
505 use Carp;
506 sub _list {
507 my $self = shift;
508 my $class = shift;
509 my %opts = @_;
510 my $list = $self->client->_get($class->_list_path(%opts), %opts);
511 return () unless $list;
512 return map { $class->new($self, %$_) } @$list;
513 }
514  
515 sub _get {
516 my $self = shift;
517 my $class = shift;
518 my $hash;
519 if (@_) {
520 $hash = $self->client->_get($class->_get_path(@_));
521 } else {
522 $hash = $self->client->_get("/current_".$class->_path);
523 }
524 return undef unless $hash;
525 return $class->new($self, %$hash);
526 }
527  
528 sub _create {
529 my $self = shift;
530 my $class = shift;
531 my %args = @_;
532 my $hash = $self->client->_post($class->_post_path(%args), %args);
533 return $class->new($self, %$hash);
534 }
535  
536 sub _update {
537 my $self = shift;
538 my $class = shift;
539 my $obj = shift;
540 my %fds = $obj->_as_hash;
541 my $hash = $self->client->_put($class->_put_path($obj), map { $_ => $fds{$_} } grep { $_ !~ m/^(service_id|version)$/ } keys %fds);
542 return $class->new($self, %$hash);
543 }
544  
545 sub _delete {
546 my $self = shift;
547 my $class = shift;
548 my $obj = shift;
549 $obj = bless $obj, $class if 'HASH' eq ref($obj);
550 return defined $self->client->_delete($class->_delete_path($obj));
551 }
552  
553  
554 =head1 CLASS METHODS
555  
556 =head2 load_options <file>
557  
558 Attempts to load various config options in the form
559  
560 <key> = <value>
561
562 From a file.
563  
564 Skips whitespace and lines starting with C<#>.
565  
566 =cut
567  
568 sub load_options {
569 my $file = shift;
570 my %options = ();
571 return %options unless -f $file;
572  
573 open(my $fh, $file) || die "Couldn't open $file: $!\n";
574 while (<$fh>) {
575 chomp;
576 next if /^#/;
577 next if /^\s*$/;
578 next unless /=/;
579 s/(^\s*|\s*$)//g;
580 my ($key, $val) = split /\s*=\s*/, $_, 2;
581 $options{$key} = $val;
582 }
583 close($fh);
584 return %options;
585 }
586  
587 =head2 get_options <file[s]>
588  
589 Tries to load options from the file[s] passed in using,
590 C<load_options>, stopping when it finds the first one.
591  
592 Then it overrides those options with command line options
593 of the form
594  
595 --<key>=<value>
596  
597 =cut
598 sub get_options {
599 my @configs = @_;
600 my %options;
601 foreach my $config (@configs) {
602 next unless -f $config;
603 %options = load_options($config);
604 last;
605 }
606 while (@ARGV && $ARGV[0] =~ m!^-+(\w+)\=(.+)$!) {
607 if ($1 eq "config") {
608 die "No such file '$2'" unless -f $2;
609 my %tmp = load_options($2);
610 $options{$_} = $tmp{$_} for keys %tmp;
611 } else {
612 $options{$1} = $2;
613 }
614 shift @ARGV;
615 }
616 die "Couldn't find options from command line arguments or ".join(", ", @configs)."\n" unless keys %options;
617 return %options;
618 }
619  
620 =head1 COPYING
621  
622 Copyright 2011 - Fastly Inc
623  
624 Distributed under the same terms as Perl itself.
625  
626 =head1 SUPPORT
627  
628 Mail support at fastly dot com if you have problems.
629  
630 =head1 DEVELOPERS
631  
632 http://github.com/fastly/fastly-perl
633  
634 http://www.fastly.com/documentation
635  
636 =cut
637 1;
« blib/lib/Net/Fastly/Origin.pm ↑ Summary blib/lib/Net/Fastly/Client.pm »