AWS-CloudFront

 view release on metacpan or  search on metacpan

lib/AWS/CloudFront.pm  view on Meta::CPAN

sub distribution
{
  my ($s, %args) = @_;
  
  my $type = 'GetDistribution';
  my $response = $s->request( $type, %args )->request();
  my $xpc = $response->xpc;
  
  if( my ($node) = $xpc->findnodes('.//cf:Distribution') )
  {
    my $origin;
    if( my ($origin_s3) = $xpc->findnodes('.//cf:S3Origin', $node) )
    {
      $origin = AWS::CloudFront::S3Origin->new(
        OriginAccessIdentity  => $xpc->findvalue('.//cf:OriginAccessIdentity', $origin_s3),
        DNSName               => $xpc->findvalue('.//cf:DNSName', $origin_s3),
      );
    }
    elsif( my $origin_custom = $xpc->findnode('.//cf:CustomOrigin', $node) )
    {
    
    }# end if()
    my $dist = AWS::CloudFront::Distribution->new(
      cf                          => $s,
      Id                          => $xpc->findvalue('.//cf:Id', $node),
      Status                      => $xpc->findvalue('.//cf:Status', $node),
      LastModifiedTime            => $xpc->findvalue('.//cf:LastModifiedTime', $node),
      DomainName                  => $xpc->findvalue('.//cf:DomainName', $node),
      Enabled                     => $xpc->findvalue('.//cf:Enabled') eq 'true' ? 1 : 0,
      DefaultRootObject           => $xpc->findvalue('.//cf:DefaultRootObject') || undef,
      CNAME                       => $xpc->findvalue('.//cf:CNAME') || undef,
      InProgressValidationBatches => $xpc->findvalue('.//cf:InProgressValidationBatches') || undef,
      Comment                     => $xpc->findvalue('.//cf:Comment') || undef,
      CallerReference             => $xpc->findvalue('.//cf:CallerReference') || undef,
      Origin                      => $origin,
      # TODO: Logging, ActiveTrustedSigners.
    );
    return $dist;
  }# end if()
}# end distribution()


sub add_distribution
{
  my ($s, %args) = @_;
  
  my $type = 'CreateDistribution';
  my $response = $s->request( $type, %args )->request();
  my $xpc = $response->xpc;

  if( my ($node) = $xpc->findnodes('.//cf:Distribution') )
  {
    my $origin;
    if( my ($origin_s3) = $xpc->findnodes('.//cf:S3Origin', $node) )
    {
      $origin = AWS::CloudFront::S3Origin->new(
        OriginAccessIdentity  => $xpc->findvalue('.//cf:OriginAccessIdentity', $origin_s3),
        DNSName               => $xpc->findvalue('.//cf:DNSName', $origin_s3),
      );
    }
    elsif( my $origin_custom = $xpc->findnode('.//cf:CustomOrigin', $node) )
    {
    
    }# end if()
    my $dist = AWS::CloudFront::Distribution->new(
      cf                          => $s,
      Id                          => $xpc->findvalue('.//cf:Id', $node),
      Status                      => $xpc->findvalue('.//cf:Status', $node),
      LastModifiedTime            => $xpc->findvalue('.//cf:LastModifiedTime', $node),
      DomainName                  => $xpc->findvalue('.//cf:DomainName', $node),
      Enabled                     => $xpc->findvalue('.//cf:Enabled') eq 'true' ? 1 : 0,
      DefaultRootObject           => $xpc->findvalue('.//cf:DefaultRootObject') || undef,
      CNAME                       => $xpc->findvalue('.//cf:CNAME') || undef,
      InProgressValidationBatches => $xpc->findvalue('.//cf:InProgressValidationBatches') || undef,
      Comment                     => $xpc->findvalue('.//cf:Comment') || undef,
      CallerReference             => $xpc->findvalue('.//cf:CallerReference') || undef,
      Origin                      => $origin,
      # TODO: Logging, ActiveTrustedSigners.
    );
    return $dist;
  }# end if()
}# end add_distribution()


sub distributions
{
  my ($s) = @_;
  
  my $type = 'GetDistributionList';
  my $response = $s->request( $type )->request();
  
  my $xpc = $response->xpc;
  my @dists = ( );
  foreach my $node ( $xpc->findnodes('.//cf:DistributionSummary') )
  {
    my $origin;
    if( my ($origin_s3) = $xpc->findnodes('.//cf:S3Origin', $node) )
    {
      $origin = AWS::CloudFront::S3Origin->new(
        OriginAccessIdentity  => $xpc->findvalue('.//cf:OriginAccessIdentity', $origin_s3),
        DNSName               => $xpc->findvalue('.//cf:DNSName', $origin_s3),
      );
    }
    elsif( my $origin_custom = $xpc->findnode('.//cf:CustomOrigin', $node) )
    {
      # TODO
    }# end if()
    my $dist = AWS::CloudFront::Distribution->new(
      cf                          => $s,
      Id                          => $xpc->findvalue('.//cf:Id', $node),
      Status                      => $xpc->findvalue('.//cf:Status', $node),
      LastModifiedTime            => $xpc->findvalue('.//cf:LastModifiedTime', $node),
      DomainName                  => $xpc->findvalue('.//cf:DomainName', $node),
      Enabled                     => $xpc->findvalue('.//cf:Enabled') eq 'true' ? 1 : 0,
      DefaultRootObject           => $xpc->findvalue('.//cf:DefaultRootObject') || undef,
      CNAME                       => $xpc->findvalue('.//cf:CNAME') || undef,
      InProgressValidationBatches => $xpc->findvalue('.//cf:InProgressValidationBatches') || undef,
      Comment                     => $xpc->findvalue('.//cf:Comment') || undef,
      CallerReference             => $xpc->findvalue('.//cf:CallerReference') || undef,
      Origin                      => $origin,
      # TODO: Logging, ActiveTrustedSigners.
    );
    push @dists, $dist;
  }# end foreach()
  
  return @dists;
}# end distributions()


sub origin_access_identities
{
  my ($s, %args) = @_;
  
  my @out = ( );
  FETCH: {
    my $response = $s->request( 'GetOriginAccessIdentityList', %args )->request();

    my $xpc = $response->xpc;
    foreach my $node ( $xpc->findnodes('.//cf:CloudFrontOriginAccessIdentitySummary') )
    {
      my ($config) = $xpc->findnodes('.//cf:CloudFrontOriginAccessIdentityConfig');
      my $ident = $s->origin_access_identity( $xpc->findvalue('.//cf:Id', $node) );
      push @out, $ident;
    }# end foreach()
    if( $xpc->findvalue('.//cf:IsTruncated') eq 'true' )
    {
      $args{Marker} = $xpc->findvalue('.//cf:NextMarker');
      next FETCH;
    }# end if()
  };
  
  return @out;
}# end origin_access_identities()


sub origin_access_identity
{
  my ($s, $id) = @_;
  
  my $response = $s->request( 'GetOriginAccessIdentity', Id => $id )->request();
  my $xpc = $response->xpc;
  return AWS::CloudFront::OriginAccessIdentity->new(
    Id                => $xpc->findvalue('.//cf:Id'),
    S3CanonicalUserId => $xpc->findvalue('.//cf:S3CanonicalUserId'),
    CallerReference   => $xpc->findvalue('.//cf:CallerReference'),
    Comment           => $xpc->findvalue('.//cf:Comment'),
  );
}# end origin_access_identity()


1;# return true:

=pod

=head1 NAME

AWS::CloudFront - Lightweight interface to Amazon CloudFront CDN

lib/AWS/CloudFront/Distribution.pm  view on Meta::CPAN

);

has 'OriginAccessIdentity' => (
  is        => 'ro',
  isa       => 'Maybe[AWS::CloudFront::OriginAccessIdentity]',
  required  => 0,
  lazy      => 1,
  default   => sub {
    my $s = shift;
    
    foreach my $ident ( $s->cf->origin_access_identities )
    {
    
    }# end foreach()
  }
);


sub update
{
  my $s = shift;

lib/AWS/CloudFront/Distribution.pm  view on Meta::CPAN

  my $type = 'DeleteDistribution';
  my $response = $s->cf->request( $type, Id => $s->Id )->request();
  
  if( $response->error_code )
  {
    die $response->msg;
  }# end if()
}# end delete()


sub create_origin_access_identity
{
  my ($s, %args) = @_;
  
  my $type = 'CreateOriginAccessIdentity';
  my $response = $s->cf->request( $type,
    CallerReference => $s->CallerReference,
    Comment         => $args{Comment}
  )->request();
  
  if( $response->error_code )

lib/AWS/CloudFront/Distribution.pm  view on Meta::CPAN

      Id                => $xpc->findvalue('.//cf:Id', $node),
      S3CanonicalUserId => $xpc->findvalue('.//cf:S3CanonicalUserId', $node),
      CallerReference   => $xpc->findvalue('.//cf:CallerReference', $node),
      Location          => $response->response->header('Location'),
    );
  }
  elsif( my ($error) = $xpc->findnodes('.//cf:Error') )
  {
    if( my ($code) = $response->response->content =~ m{<Code>(.+?)</Code>}s )
    {
      # The origin already exists or some other error.
      die $code;
    }
    else
    {
      die "Invalid response: ", $response->response->content;
    }# end if()
  }
  else
  {
    die "Invalid response: ", $response->response->content;
  }# end if()
}# end create_origin_access_identity()

1;# return true:

lib/AWS/CloudFront/Request/CreateDistribution.pm  view on Meta::CPAN


sub request
{
  my $s = shift;
  
  my $uri = 'https://cloudfront.amazonaws.com/2010-11-01/distribution';

  my $xml = <<"XML";
<?xml version="1.0" encoding="UTF-8"?>
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/">
  @{[ $s->_origin_xml ]}
  <CallerReference>@{[ $s->CallerReference ]}</CallerReference>
  @{[ $s->CNAME ? q(<CNAME>) . $s->CNAME . q(</CNAME>) : '' ]}
  <Comment>@{[ $s->Comment ]}</Comment>
  <Enabled>@{[ $s->Enabled ? 'true' : 'false' ]}</Enabled>
  @{[ $s->DefaultRootObject ? ('<DefaultRootObject>' . $s->DefaultRootObject. '</DefaultRootObject>') : '' ]}
  @{[ $s->_logging_xml ]}
</DistributionConfig>
XML

  my $signer = AWS::CloudFront::Signer->new(

lib/AWS/CloudFront/Request/CreateDistribution.pm  view on Meta::CPAN

  my ($s, $res) = @_;
  
  AWS::CloudFront::ResponseParser->new(
    response        => $res,
    expect_nothing  => 0,
    type            => $s->type,
  );
}# end http_request()


sub _origin_xml
{
  my $s = shift;
  
  my $type = ref($s->Origin);
  if( $type->isa('AWS::CloudFront::S3Origin') )
  {
    return <<"XML";
   <S3Origin>
      <DNSName>@{[ $s->Origin->DNSName ]}</DNSName>
   </S3Origin>

lib/AWS/CloudFront/Request/CreateDistribution.pm  view on Meta::CPAN

  elsif( $type->isa('AWS::CloudFront::CustomOrigin') )
  {
    return <<"XML";
   <CustomOrigin>
      <DNSName>@{[ $s->Origin->DNSName ]}</DNSName>
      <HTTPPort>@{[ $s->Origin->HTTPPort ]}</HTTPPort>
      <OriginProtocolPolicy>@{[ $s->Origin->OriginProtocolPolicy ]}</OriginProtocolPolicy>
   </CustomOrigin>
XML
  }# end if()
}# end _origin_xml()


sub _logging_xml
{
  my $s = shift;
  return '' unless $s->Logging;
  
  return <<"XML";
   <Logging>
      <Bucket>@{[ $s->Logging->Bucket ]}.s3.amazonaws.com</Bucket>

lib/AWS/CloudFront/Request/CreateOriginAccessIdentity.pm  view on Meta::CPAN

  isa       => 'Str',
  required  => 0,
  default   => sub { '' },
);


sub request
{
  my $s = shift;
  
  my $uri = 'https://cloudfront.amazonaws.com/2010-11-01/origin-access-identity/cloudfront';

  my $xml = <<"XML";
<?xml version="1.0" encoding="UTF-8"?>
<CloudFrontOriginAccessIdentityConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/">
   <CallerReference>@{[ $s->CallerReference ]}</CallerReference>
   <Comment>@{[ $s->Comment ]}</Comment>
</CloudFrontOriginAccessIdentityConfig>
XML

  my $signer = AWS::CloudFront::Signer->new(

lib/AWS/CloudFront/Request/GetOriginAccessIdentity.pm  view on Meta::CPAN

  is        => 'ro',
  isa       => 'Str',
  required  => 1,
);


sub request
{
  my $s = shift;
  
  my $uri = 'https://cloudfront.amazonaws.com/2010-11-01/origin-access-identity/cloudfront/' . $s->Id;
  my $signer = AWS::CloudFront::Signer->new(
    cf  => $s->cf,
  );
  $s->_send_request( 'GET' => $uri => {
    Authorization => $signer->auth_header,
    'x-amz-date'  => $signer->date,
  });
}# end request()

sub parse_response

lib/AWS/CloudFront/Request/GetOriginAccessIdentityList.pm  view on Meta::CPAN

  is        => 'ro',
  isa       => 'Int',
  required  => 0,
);


sub request
{
  my $s = shift;
  
  my $uri = 'https://cloudfront.amazonaws.com/2010-11-01/origin-access-identity/cloudfront';
  my @params = ( );
  push @params, 'Marker=' . $s->Marker if defined $s->Marker;
  push @params, 'MaxItems=' . $s->MaxItems if defined $s->MaxItems;
  $uri .= '?' . join('&', @params) if @params;
  
  my $signer = AWS::CloudFront::Signer->new(
    cf  => $s->cf,
  );
  $s->_send_request( 'GET' => $uri => {
    Authorization => $signer->auth_header,

lib/AWS/CloudFront/Request/UpdateDistribution.pm  view on Meta::CPAN

      'x-amz-date'  => $signer->date,
    });
    return $res->response->header('etag');
  })->();
  
  my $uri = 'https://cloudfront.amazonaws.com/2010-11-01/distribution/' . $s->d->Id . '/config';

  my $xml = <<"XML";
<?xml version="1.0" encoding="UTF-8"?>
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/">
  @{[ $s->_origin_xml ]}
  <CallerReference>@{[ $s->d->CallerReference ]}</CallerReference>
  @{[ $s->d->CNAME ? q(<CNAME>) . $s->d->CNAME . q(</CNAME>) : '' ]}
  <Comment>@{[ $s->d->Comment ]}</Comment>
  <Enabled>@{[ $s->d->Enabled ? 'true' : 'false' ]}</Enabled>
  @{[ $s->d->DefaultRootObject ? ('<DefaultRootObject>' . $s->d->efaultRootObject. '</DefaultRootObject>') : '' ]}
  @{[ $s->_logging_xml ]}
</DistributionConfig>
XML

  my $signer = AWS::CloudFront::Signer->new(

lib/AWS/CloudFront/Request/UpdateDistribution.pm  view on Meta::CPAN

  my ($s, $res) = @_;
  
  AWS::CloudFront::ResponseParser->new(
    response        => $res,
    expect_nothing  => 0,
    type            => $s->type,
  );
}# end http_request()


sub _origin_xml
{
  my $s = shift;
  
  my $type = ref($s->d->Origin);
  if( $type->isa('AWS::CloudFront::S3Origin') )
  {
    return <<"XML";
   <S3Origin>
      <DNSName>@{[ $s->d->Origin->DNSName ]}</DNSName>
   </S3Origin>

lib/AWS/CloudFront/Request/UpdateDistribution.pm  view on Meta::CPAN

  elsif( $type->isa('AWS::CloudFront::CustomOrigin') )
  {
    return <<"XML";
   <CustomOrigin>
      <DNSName>@{[ $s->d->Origin->DNSName ]}</DNSName>
      <HTTPPort>@{[ $s->d->Origin->HTTPPort ]}</HTTPPort>
      <OriginProtocolPolicy>@{[ $s->Origin->OriginProtocolPolicy ]}</OriginProtocolPolicy>
   </CustomOrigin>
XML
  }# end if()
}# end _origin_xml()


sub _logging_xml
{
  my $s = shift;
  return '' unless $s->d->Logging;
  
  return <<"XML";
   <Logging>
      <Bucket>@{[ $s->d->Logging->Bucket ]}.s3.amazonaws.com</Bucket>

t/010-basic/010-basic.t  view on Meta::CPAN



my $dist = $cf->add_distribution(
  Origin  => AWS::CloudFront::S3Origin->new(
    DNSName => 'aws-s3-test-12344.s3.amazonaws.com',
  ),
);

warn "New Dist ID: " . $dist->Id;

my $ident = $dist->create_origin_access_identity( Comment => 'testing only' );
use Data::Dumper;
warn Dumper( $ident );

while( 1 )
{
  warn "Checking status of ", $dist->Id, "\n";
  last if $cf->distribution(Id => $dist->Id)->Status eq 'Deployed';
  sleep(2);
}# end while()



( run in 1.763 second using v1.01-cache-2.11-cpan-f985c23238c )