AWS-IP
view release on metacpan or search on metacpan
lib/AWS/IP.pm view on Meta::CPAN
bless {
cache => Cache::File->new( cache_root => ($cache_path || tempdir()),
lock_level => Cache::File::LOCK_LOCAL(),
default_expires => "$cache_timeout_secs sec"),
}, $class;
}
=head2 ip_is_aws ($ip, [$service])
Boolean method to test if an ip address is from AWS. Optionally takes a service name (AMAZON|EC2|CLOUDFRONT|ROUTE53|ROUTE53_HEALTHCHECKS) and restricts the check to AWS ip addresses for that service.
If you are checking more than one ip address, it's more efficient to pull the CIDRs you want, then use L<Net::CIDR::Set> to test if the ips are present in the CIDRs (see example in SYNOPSIS).
=cut
sub ip_is_aws
{
my ($self, $ip, $service) = @_;
croak 'Error must supply an ip address' unless $ip;
my $ip_ranges;
if ($service)
{
$ip_ranges = Net::CIDR::Set->new( map { $_->{ip_prefix} } grep { $_->{service} eq $service } @{$self->get_raw_data->{prefixes}});
}
else
{
$ip_ranges = Net::CIDR::Set->new( map { $_->{ip_prefix} } @{$self->get_raw_data->{prefixes}} );
}
$ip_ranges->contains($ip);
}
=head2 get_raw_data
Returns the entire raw IP dataset as a Perl data structure.
=cut
sub get_raw_data
{
my ($self) = @_;
my $entry = $self->{cache}->entry(CACHE_KEY);
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.
=cut
sub get_regions
{
my ($self) = @_;
my %regions;
for (@{$self->get_raw_data->{prefixes}})
{
$regions{ $_->{region} } = 1;
}
[ keys %regions ];
}
=head2 get_services
Returns an arrayref of the services (Amazon, EC2 etc) in the AWS IP address data.
=cut
( run in 0.363 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )