File Coverage

blib/lib/AWS/Networks.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package AWS::Networks;
2 1     1   16246 use Moose;
  0            
  0            
3             use JSON;
4             use HTTP::Tiny;
5             use DateTime;
6              
7             our $VERSION = '0.01';
8              
9             has url => (
10             is => 'ro',
11             isa => 'Str|Undef',
12             default => 'https://ip-ranges.amazonaws.com/ip-ranges.json'
13             );
14              
15             has netinfo => (
16             is => 'ro',
17             isa => 'HashRef',
18             default => sub {
19             my $self = shift;
20             die "Can't get some properties from derived results" if (not $self->url);
21             my $response = HTTP::Tiny->new->get($self->url);
22             die "Error downloading URL" unless ($response->{ success });
23             return decode_json($response->{ content });
24             },
25             lazy => 1,
26             );
27              
28             has sync_token => (
29             is => 'ro',
30             isa => 'DateTime',
31             default => sub {
32             return DateTime->from_epoch( epoch => shift->netinfo->{ syncToken } );
33             },
34             lazy => 1,
35             );
36              
37             has networks => (
38             is => 'ro',
39             isa => 'ArrayRef',
40             default => sub {
41             return shift->netinfo->{ prefixes };
42             },
43             lazy => 1,
44             );
45              
46             has regions => (
47             is => 'ro',
48             isa => 'ArrayRef',
49             default => sub {
50             my ($self) = @_;
51             my $regions = {};
52             map { $regions->{ $_->{ region } } = 1 } @{ $self->networks };
53             return [ keys %$regions ];
54             },
55             lazy => 1,
56             );
57              
58             sub by_region {
59             my ($self, $region) = @_;
60             return AWS::Networks->new(
61             url => undef,
62             sync_token => $self->sync_token,
63             networks => [ grep { $_->{ region } eq $region } @{ $self->networks } ]
64             );
65             }
66              
67             has services => (
68             is => 'ro',
69             isa => 'ArrayRef',
70             default => sub {
71             my ($self) = @_;
72             my $services = {};
73             map { $services->{ $_->{ service } } = 1 } @{ $self->networks };
74             return [ keys %$services ];
75             },
76             lazy => 1,
77             );
78              
79             sub by_service {
80             my ($self, $service) = @_;
81             return AWS::Networks->new(
82             url => undef,
83             sync_token => $self->sync_token,
84             networks => [ grep { $_->{ service } eq $service } @{ $self->networks } ]
85             );
86             }
87              
88             has cidrs => (
89             is => 'ro',
90             isa => 'ArrayRef',
91             default => sub {
92             my ($self) = @_;
93             return [ map { $_->{ ip_prefix } } @{ $self->networks } ];
94             },
95             lazy => 1,
96             );
97              
98             1;
99              
100             #################### main pod documentation begin ###################
101              
102             =head1 NAME
103              
104             AWS::Networks - Parse and query official AWS network ranges
105              
106             =head1 SYNOPSIS
107              
108             use AWS::Networks;
109              
110             my $nets = AWS::Networks->new();
111              
112             say $nets->sync_token->iso8601;
113              
114             foreach my $cidr (@{ $nets->cidrs }){
115             say $cidr
116             }
117              
118             =head1 DESCRIPTION
119              
120             This module parses the official public IP network information published by Amazon Web Services at https://ip-ranges.amazonaws.com/ip-ranges.json
121              
122             Please read and understand the information can be found at http://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html to make sense of the data retured by this module.
123              
124             =head1 USAGE
125              
126             Instance an object, and use it to filter information of interest to you with the attributes and methods provided.
127              
128             =head1 METHODS
129              
130             =head2 new([ url => 'http....' ])
131              
132             Standard Moose constructor. Can specify a custom URL to download a document that follows the same schema
133              
134             =head2 url
135              
136             Returns the URL from which the information was retrieved. Returns undef on filtered datasets
137              
138             =head2 sync_token
139              
140             Returns a DateTime object created from the current timestamp of the syncToken reported from the service
141              
142             =head2 networks
143              
144             Returns an ArrayRef with HashRefs following the following structure:
145              
146             { ip_prefix => '0.0.0.0/0', region => '...', service => '...' }
147              
148             The keys and values in the HashRefs are the ones returned by the Network service
149              
150             service can be one of: AMAZON | EC2 | CLOUDFRONT | ROUTE53 | ROUTE53_HEALTHCHECKS, but expect
151             new values to appear
152              
153             region can be one of: ap-northeast-1 | ap-southeast-1 | ap-southeast-2 | cn-north-1 | eu-central-1 | eu-west-1 | sa-east-1 | us-east-1 | us-gov-west-1 | us-west-1 | us-west-2 | GLOBAL, but expect new values to appear
154              
155             =head2 services
156              
157             Returns an ArrayRef of the different services present in the current dataset
158              
159             =head2 regions
160              
161             Returns an ArrayRef of the different regions present in the current dataset
162              
163             =head2 cidrs
164              
165             Returns an ArrayRef with the CIDR blocks in the dataset
166              
167             =head2 by_region($region)
168              
169             Returns a new AWS::Networks object with the data filtered to only the objects in the
170             specified region
171              
172             =head2 by_service($service)
173              
174             Returns a new AWS::Networks object with the data filtered to only the services specified
175              
176             =cut
177              
178             =head1 CONTRIBUTE
179              
180             The source code is located here: https://github.com/pplu/aws-networks
181              
182             =head2 SEE ALSO
183              
184             The dist is bundled with a couple of sample scripts in bin that play around with
185             the information returned by this module, these scripts try to determine the number
186             of IP addresses that AWS has, and given an IP address, if it pertains to AWS, and
187             what service.
188              
189             =head1 AUTHOR
190              
191             Jose Luis Martinez
192             CPAN ID: JLMARTIN
193             CAPSiDE
194             jlmartinez@capside.com
195             http://www.pplusdomain.net
196              
197             =head1 COPYRIGHT
198              
199             Copyright (c) 2014 by Jose Luis Martinez Torres
200              
201             This program is free software; you can redistribute
202             it and/or modify it under the same terms as Perl itself.
203              
204             The full text of the license can be found in the
205             LICENSE file included with this module.
206              
207             =cut