File Coverage

blib/lib/Net/RackSpace/CloudServers/Limits.pm
Criterion Covered Total %
statement 25 49 51.0
branch 0 24 0.0
condition n/a
subroutine 9 11 81.8
pod 2 2 100.0
total 36 86 41.8


line stmt bran cond sub pod time code
1             package Net::RackSpace::CloudServers::Limits;
2              
3             BEGIN {
4 2     2   39 $Net::RackSpace::CloudServers::Limits::VERSION = '0.14';
5             }
6 2     2   11 use warnings;
  2         2  
  2         54  
7 2     2   10 use strict;
  2         2  
  2         77  
8             our $DEBUG = 0;
9 2     2   8 use Any::Moose;
  2         5  
  2         16  
10 2     2   946 use HTTP::Request;
  2         4  
  2         42  
11 2     2   9 use JSON;
  2         3  
  2         23  
12 2     2   264 use YAML;
  2         4  
  2         93  
13 2     2   9 use Carp;
  2         5  
  2         241  
14              
15             has 'cloudservers' =>
16             ( is => 'rw', isa => 'Net::RackSpace::CloudServers', required => 1 );
17             has 'totalramsize' => ( is => 'rw', isa => 'Int', );
18             has 'maxipgroups' => ( is => 'rw', isa => 'Int', );
19             has 'maxipgroupmembers' => ( is => 'rw', isa => 'Int', );
20             has 'rate' => ( is => 'rw', isa => 'Maybe[ArrayRef]', );
21              
22 2     2   11 no Any::Moose;
  2         2  
  2         8  
23             __PACKAGE__->meta->make_immutable();
24              
25             sub BUILD {
26 0     0 1   my $self = shift;
27 0           $self->refresh();
28             }
29              
30             sub refresh {
31 0     0 1   my $self = shift;
32 0           my $request = HTTP::Request->new(
33             'GET',
34             $self->cloudservers->server_management_url . '/limits',
35             [ 'X-Auth-Token' => $self->cloudservers->token ]
36             );
37 0           my $response = $self->cloudservers->_request($request);
38 0 0         return if $response->code == 204;
39 0           confess 'Unknown error ' . $response->code
40 0 0         unless scalar grep { $response->code eq $_ } ( 200, 203 );
41 0           my $hr = from_json( $response->content );
42 0 0         warn Dump($hr) if $DEBUG;
43              
44             #{"limits":{"absolute":{"maxTotalRAMSize":51200,"maxIPGroupMembers":25,"maxNumServers":25,"maxIPGroups":25},"rate":[{"value":50,"unit":"DAY","verb":"POST","remaining":50,"URI":"\/servers*","resetTime":1247769469,"regex":"^\/servers"},{"value":10,"unit":"MINUTE","verb":"POST","remaining":10,"URI":"*","resetTime":1247769469,"regex":".*"},{"value":600,"unit":"MINUTE","verb":"DELETE","remaining":600,"URI":"*","resetTime":1247769469,"regex":".*"},{"value":10,"unit":"MINUTE","verb":"PUT","remaining":10,"URI":"*","resetTime":1247769469,"regex":".*"},{"value":3,"unit":"MINUTE","verb":"GET","remaining":3,"URI":"*changes-since*","resetTime":1247769469,"regex":"changes-since"}]}}
45 0 0         confess 'response does not contain key "limits"'
46             unless defined $hr->{limits};
47 0 0         confess 'response does not contain hashref of "limits"'
48             unless ( ref $hr->{limits} eq 'HASH' );
49              
50 0 0         confess 'response "limits" does not contain key "rate"'
51             unless defined $hr->{limits}->{rate};
52 0 0         confess 'response "limits", key "rate" is not an arrayref'
53             unless ( ref $hr->{limits}->{rate} eq 'ARRAY' );
54 0           $self->rate( $hr->{limits}->{rate} );
55              
56 0 0         confess 'response "limits" does not contain key "absolute"'
57             unless defined $hr->{limits}->{absolute};
58 0 0         confess 'response "limits", key "absolute" is not an hashref'
59             unless ( ref $hr->{limits}->{absolute} eq 'HASH' );
60 0 0         confess
61             'response "limits", key "absolute" does not contain key "maxTotalRAMSize"'
62             unless ( defined $hr->{limits}->{absolute}->{"maxTotalRAMSize"} );
63 0           $self->totalramsize( $hr->{limits}->{absolute}->{"maxTotalRAMSize"} );
64 0 0         confess
65             'response "limits", key "absolute" does not contain key "maxIPGroups"'
66             unless ( defined $hr->{limits}->{absolute}->{"maxIPGroups"} );
67 0           $self->maxipgroups( $hr->{limits}->{absolute}->{"maxIPGroups"} );
68 0 0         confess
69             'response "limits", key "absolute" does not contain key "maxIPGroupMembers"'
70             unless ( defined $hr->{limits}->{absolute}->{"maxIPGroupMembers"} );
71 0           $self->maxipgroupmembers(
72             $hr->{limits}->{absolute}->{"maxIPGroupMembers"} );
73 0           return $self;
74             }
75              
76             =head1 NAME
77              
78             Net::RackSpace::CloudServers::Limits - a RackSpace CloudServers Limits instance
79              
80             =head1 VERSION
81              
82             version 0.14
83              
84             =head1 SYNOPSIS
85              
86             use Net::RackSpace::CloudServers;
87             use Net::RackSpace::CloudServers::Limits;
88             my $cs = Net::RackSpace::CloudServers->new(
89             user => $ENV{CLOUDSERVERS_USER},
90             key => $ENV{CLOUDSERVERS_KEY},
91             );
92             my $limits = Net::RackSpace::CloudServers::Limits->new(
93             cloudservers => $cs,
94             );
95             $limits->refresh();
96             print "Can still allocate ", $limits->totalramsize, " MB RAM\n";
97             print "Can still use ", $limits->maxipgroups, " IP Groups\n";
98             print "Can have ", $limits->maxipgroupmembers, " more IP groups members\n";
99             # next bit isn't OO-ed yet.
100             foreach my $k ( @{ $limits->rate } ) {
101             print $k->{verb}, ' to URI ', $k->{URI}, ' remaining: ',
102             $k->{remaining}, ' per ', $k->{unit},
103             ' (will be reset at: ', scalar localtime $k->{resetTime}, ')',
104             "\n";
105             }
106              
107             =head1 METHODS
108              
109             =head2 new / BUILD
110              
111             The constructor creates a Limits half-object. See L<refresh> to refresh the limits once gathered.
112              
113             This normally gets created for you by L<Net::RackSpace::Cloudserver>'s L<limits> method.
114             Needs a Net::RackSpace::CloudServers object as B<cloudserver> parameter.
115              
116             =head2 refresh
117              
118             This method refreshes the information contained in the object
119              
120             =head1 ATTRIBUTES
121              
122             =head2 totalramsize
123              
124             Indicates the maximum amount of RAM (in megabytes) linked to your account.
125              
126             =head2 maxipgroups
127              
128             Indicates the maximum number of shared IP groups your account can create
129              
130             =head2 maxipgroupmembers
131              
132             Indicates the maximum amount of servers that can be associated with any one shared IP group
133              
134             =head2 rate
135              
136             TODO: not yet OO-ified.
137              
138             Is an arrayref of the rate-limits that currently apply to your account via the API.
139             You'll receive 413 errors in case you exceed the limits described.
140              
141             =head1 AUTHOR
142              
143             Marco Fontani, C<< <mfontani at cpan.org> >>
144              
145             =head1 BUGS
146              
147             Please report any bugs or feature requests to C<bug-net-rackspace-cloudservers at rt.cpan.org>, or through
148             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-RackSpace-CloudServers>.
149             I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
150              
151             =head1 SUPPORT
152              
153             You can find documentation for this module with the perldoc command.
154              
155             perldoc Net::RackSpace::CloudServers::Limits
156              
157             You can also look for information at:
158              
159             =over 4
160              
161             =item * RT: CPAN's request tracker
162              
163             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-RackSpace-CloudServers>
164              
165             =item * AnnoCPAN: Annotated CPAN documentation
166              
167             L<http://annocpan.org/dist/Net-RackSpace-CloudServers>
168              
169             =item * CPAN Ratings
170              
171             L<http://cpanratings.perl.org/d/Net-RackSpace-CloudServers>
172              
173             =item * Search CPAN
174              
175             L<http://search.cpan.org/dist/Net-RackSpace-CloudServers/>
176              
177             =back
178              
179             =head1 COPYRIGHT & LICENSE
180              
181             Copyright 2009 Marco Fontani, all rights reserved.
182              
183             This program is free software; you can redistribute it and/or modify it
184             under the same terms as Perl itself.
185              
186             =cut
187              
188             1; # End of Net::RackSpace::CloudServers::Limits