AWS-IP
view release on metacpan or search on metacpan
# get the raw data as a Perl reference
my $aws_ip_data = $aws->get_raw_data;
# check if an ip address is AWS
if ($aws->is_aws_ip('50.0.0.1')
{
..
}
# get a list of all AWS cidrs
my $cidrs = $aws->get_cidrs;
for (@$cidrs)
{
...
}
# create your own ip checks
use Net::CIDR::Set;
my $ec2_cidrs = $aws->get_cidrs_by_service('EC2');
my $aws_ec2_set = Net::CIDR::Set->new( @$ec2_cidrs );
if ($aws_ec2_set->contains($ip)
{
...
}
# time passes, cache has expired
$aws_ip_data = $aws->get_raw_data; # auto refreshes
=head2 DESCRIPTION
=cut
=head2 get_raw_data
Returns the entire raw IP dataset as a Perl data structure.
=cut
=head2 get_cidrs
Returns an arrayref of the L<CIDRs|http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing> in the AWS IP address data.
=cut
=head2 get_cidrs_by_region ($region)
Returns an arrayref of CIDRs matching the provided region.
=cut
=head2 get_cidrs_by_service ($service)
Returns an arrayref of CIDRs matching the provided service (AMAZON|EC2|CLOUDFRONT|ROUTE53|ROUTE53_HEALTHCHECKS).
=cut
=head2 get_regions
Returns an arrayref of the regions in the AWS IP address data.
lib/AWS/IP.pm view on Meta::CPAN
# get the raw data as a Perl reference
my $aws_ip_data = $aws->get_raw_data;
# check if an ip address is AWS
if ($aws->is_aws_ip('50.0.0.1')
{
..
}
# get a list of all AWS cidrs
my $cidrs = $aws->get_cidrs;
for (@$cidrs)
{
...
}
# create your own ip checks
use Net::CIDR::Set;
my $ec2_cidrs = $aws->get_cidrs_by_service('EC2');
my $aws_ec2_set = Net::CIDR::Set->new( @$ec2_cidrs );
if ($aws_ec2_set->contains($ip)
{
...
}
# time passes, cache has expired
$aws_ip_data = $aws->get_raw_data; # auto refreshes
=head2 DESCRIPTION
lib/AWS/IP.pm view on Meta::CPAN
if ($entry->exists)
{
decode_json($entry->get());
}
else
{
decode_json($self->_refresh_cache);
}
}
=head2 get_cidrs
Returns an arrayref of the L<CIDRs|http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing> in the AWS IP address data.
=cut
sub get_cidrs
{
my ($self) = @_;
[ map { $_->{ip_prefix} } @{$self->get_raw_data->{prefixes}} ];
}
=head2 get_cidrs_by_region ($region)
Returns an arrayref of CIDRs matching the provided region.
=cut
sub get_cidrs_by_region
{
my ($self, $region) = @_;
croak 'Error must provide region' unless $region;
[ map { $_->{ip_prefix} } grep { $_->{region} eq $region } @{$self->get_raw_data->{prefixes}} ];
}
=head2 get_cidrs_by_service ($service)
Returns an arrayref of CIDRs matching the provided service (AMAZON|EC2|CLOUDFRONT|ROUTE53|ROUTE53_HEALTHCHECKS).
=cut
sub get_cidrs_by_service
{
my ($self, $service) = @_;
croak 'Error must provide service' unless $service;
[ map { $_->{ip_prefix} } grep { $_->{service} eq $service } @{$self->get_raw_data->{prefixes}} ];
}
=head2 get_regions
Returns an arrayref of the regions in the AWS IP address data.
use_ok('AWS::IP', 'load module');
ok my $aws = AWS::IP->new(600), 'constructor';
ok $aws->_refresh_cache_from_string($test_data), 'refresh cache';
# ip range checks
ok $aws->ip_is_aws('50.19.0.1'), 'ip 50.19.0.1 is found in AWS range';
ok $aws->ip_is_aws('54.239.98.0', 'AMAZON'), 'ip 54.239.98.0 is found in AMAZON AWS range';
ok !$aws->ip_is_aws('54.239.98.0', 'EC2'), 'ip 54.239.98.0 is not found in EC2 AWS range';
# counts
is 383, @{$aws->get_cidrs}, '383 CIDRs are present';
is 5, @{$aws->get_services}, '5 services are present';
is 12, @{$aws->get_regions}, '12 regions are present';
# cache expiry
ok my $aws_2 = AWS::IP->new(1), 'constructor with 1 second cache';
ok $aws->_refresh_cache_from_string($test_data), 'refresh cache';
sleep(2); # let cache expire
ok !$aws_2->{cache}->entry('AWS_IPS')->exists, 'Entry no longer exists';
ok !$aws_2->{cache}->entry('AWS_IPS')->get, 'Data is no longer cached';
( run in 0.411 second using v1.01-cache-2.11-cpan-de7293f3b23 )