Permalink
Browse files

Initial support for CloudFront streaming and private distributions.

  • Loading branch information...
1 parent 6ee36ce commit 0a8ea52553be5ecdee9693a6bf0091a7c3bd9fdf Mitch.Garnaat committed Dec 16, 2009
View
@@ -1,4 +1,4 @@
-# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
+# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
@@ -26,14 +26,16 @@
import boto.utils
from boto.connection import AWSAuthConnection
from boto import handler
-from boto.cloudfront.distribution import Distribution, DistributionConfig, DistributionSummary
+from boto.cloudfront.distribution import *
+from boto.cloudfront.identity import OriginAccessIdentity
+from boto.cloudfront.identity import OriginAccessIdentityConfig
from boto.resultset import ResultSet
from boto.cloudfront.exception import CloudFrontServerError
class CloudFrontConnection(AWSAuthConnection):
DefaultHost = 'cloudfront.amazonaws.com'
- Version = '2008-06-30'
+ Version = '2009-12-01'
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
port=None, proxy=None, proxy_port=None,
@@ -42,22 +44,46 @@ def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
aws_access_key_id, aws_secret_access_key,
True, port, proxy, proxy_port, debug=debug)
- def get_all_distributions(self):
- response = self.make_request('GET', '/%s/distribution' % self.Version)
+ def get_etag(self, response):
+ response_headers = response.msg
+ for key in response_headers.keys():
+ if key.lower() == 'etag':
+ return response_headers[key]
+ return None
+
+ def add_aws_auth_header(self, headers, method, path):
+ if not headers.has_key('Date'):
+ headers['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT",
+ time.gmtime())
+
+ hmac = self.hmac.copy()
+ hmac.update(headers['Date'])
+ b64_hmac = base64.encodestring(hmac.digest()).strip()
+ headers['Authorization'] = "AWS %s:%s" % (self.aws_access_key_id, b64_hmac)
+
+ # Generics
+
+ def _get_all_objects(self, resource, tags):
+ if not tags:
+ tags=[('DistributionSummary', DistributionSummary)]
+ response = self.make_request('GET', '/%s/%s' % (self.Version, resource))
body = response.read()
+ print body
if response.status >= 300:
raise CloudFrontServerError(response.status, response.reason, body)
- rs = ResultSet([('DistributionSummary', DistributionSummary)])
+ rs = ResultSet(tags)
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
- def get_distribution_info(self, distribution_id):
- response = self.make_request('GET', '/%s/distribution/%s' % (self.Version, distribution_id))
+ def _get_info(self, id, resource, dist_class):
+ uri = '/%s/%s/%s' % (self.Version, resource, id)
+ response = self.make_request('GET', uri)
body = response.read()
+ print body
if response.status >= 300:
raise CloudFrontServerError(response.status, response.reason, body)
- d = Distribution(connection=self)
+ d = dist_class(connection=self)
response_headers = response.msg
for key in response_headers.keys():
if key.lower() == 'etag':
@@ -66,60 +92,137 @@ def get_distribution_info(self, distribution_id):
xml.sax.parseString(body, h)
return d
- def get_etag(self, response):
- response_headers = response.msg
- for key in response_headers.keys():
- if key.lower() == 'etag':
- return response_headers[key]
- return None
-
- def get_distribution_config(self, distribution_id):
- response = self.make_request('GET', '/%s/distribution/%s/config' % (self.Version, distribution_id))
+ def _get_config(self, id, resource, config_class):
+ uri = '/%s/%s/%s/config' % (self.Version, resource, id)
+ response = self.make_request('GET', uri)
body = response.read()
+ print body
if response.status >= 300:
raise CloudFrontServerError(response.status, response.reason, body)
- d = DistributionConfig(connection=self)
+ d = config_class(connection=self)
d.etag = self.get_etag(response)
h = handler.XmlHandler(d, self)
xml.sax.parseString(body, h)
return d
- def set_distribution_config(self, distribution_id, etag, config):
- response = self.make_request('PUT', '/%s/distribution/%s/config' % (self.Version, distribution_id),
- {'If-Match' : etag, 'Content-Type' : 'text/xml'}, config.to_xml())
+ def _set_config(self, distribution_id, etag, config):
+ if isinstance(config, StreamingDistributionConfig):
+ resource = 'streaming-distribution'
+ else:
+ resource = 'distribution'
+ uri = '/%s/%s/%s/config' % (self.Version, resource, distribution_id)
+ headers = {'If-Match' : etag, 'Content-Type' : 'text/xml'}
+ response = self.make_request('PUT', uri, headers, config.to_xml())
body = response.read()
+ print body
return self.get_etag(response)
if response.status != 200:
raise CloudFrontServerError(response.status, response.reason, body)
- def create_distribution(self, origin, enabled, caller_reference='', cnames=None, comment=''):
- config = DistributionConfig(origin=origin, enabled=enabled,
- caller_reference=caller_reference,
- cnames=cnames, comment=comment)
- response = self.make_request('POST', '/%s/distribution' % self.Version,
+ def _create_object(self, config, resource, dist_class):
+ response = self.make_request('POST', '/%s/%s' % (self.Version, resource),
{'Content-Type' : 'text/xml'}, data=config.to_xml())
body = response.read()
+ print body
if response.status == 201:
- d = Distribution(connection=self)
+ d = dist_class(connection=self)
h = handler.XmlHandler(d, self)
xml.sax.parseString(body, h)
return d
else:
raise CloudFrontServerError(response.status, response.reason, body)
- def delete_distribution(self, distribution_id, etag):
- response = self.make_request('DELETE', '/%s/distribution/%s' % (self.Version, distribution_id),
- {'If-Match' : etag})
+ def _delete_object(self, id, etag, resource):
+ uri = '/%s/%s/%s' % (self.Version, resource, id)
+ response = self.make_request('DELETE', uri, {'If-Match' : etag})
body = response.read()
+ print body
if response.status != 204:
raise CloudFrontServerError(response.status, response.reason, body)
- def add_aws_auth_header(self, headers, method, path):
- if not headers.has_key('Date'):
- headers['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT",
- time.gmtime())
+ # Distributions
+
+ def get_all_distributions(self):
+ tags=[('DistributionSummary', DistributionSummary)]
+ return self._get_all_objects('distribution', tags)
+
+ def get_distribution_info(self, distribution_id):
+ return self._get_info(distribution_id, 'distribution', Distribution)
+
+ def get_distribution_config(self, distribution_id):
+ return self._get_config(distribution_id, 'distribution',
+ DistributionConfig)
+
+ def set_distribution_config(self, distribution_id, etag, config):
+ return self._set_config(distribution_id, etag, config)
+
+ def create_distribution(self, origin, enabled, caller_reference='',
+ cnames=None, comment=''):
+ config = DistributionConfig(origin=origin, enabled=enabled,
+ caller_reference=caller_reference,
+ cnames=cnames, comment=comment)
+ return self._create_object(config, 'distribution', Distribution)
+
+ def delete_distribution(self, distribution_id, etag):
+ return self._delete_object(distribution_id, etag, 'distribution')
+
+ # Streaming Distributions
+
+ def get_all_streaming_distributions(self):
+ tags=[('StreamingDistributionSummary', StreamingDistributionSummary)]
+ return self._get_all_objects('streaming-distribution', tags)
+
+ def get_streaming_distribution_info(self, distribution_id):
+ return self._get_info(distribution_id, 'streaming-distribution',
+ StreamingDistribution)
+
+ def get_streaming_distribution_config(self, distribution_id):
+ return self._get_config(distribution_id, 'streaming-distribution',
+ StreamingDistributionConfig)
+
+ def set_streaming_distribution_config(self, distribution_id, etag, config):
+ return self._set_config(distribution_id, etag, config)
+
+ def create_streaming_distribution(self, origin, enabled,
+ caller_reference='',
+ cnames=None, comment=''):
+ config = StreamingDistributionConfig(origin=origin, enabled=enabled,
+ caller_reference=caller_reference,
+ cnames=cnames, comment=comment)
+ return self._create_object(config, 'streaming-distribution',
+ StreamingDistribution)
+
+ def delete_streaming_distribution(self, distribution_id, etag):
+ return self._delete_object(distribution_id, etag, 'streaming-distribution')
+
+ # Origin Access Identity
+
+ def get_all_origin_access_identity(self):
+ tags=[('CloudFrontOriginAccessIdentitySummary',
+ OriginAccessIdentitySummary)]
+ return self._get_all_objects('origin-access-identity/cloudfront', tags)
+
+ def get_origin_access_identity_info(self, access_id):
+ return self._get_info(access_id, 'origin-access-identity/cloudfront',
+ OriginAccessIdentity)
+
+ def get_origin_access_identity_config(self, access_id):
+ return self._get_config(access_id,
+ 'origin-access-identity/cloudfront',
+ OriginAccessIdentityConfig)
+
+ def set_origin_access_identity_config(self, access_id,
+ etag, config):
+ return self._set_config(access_id, etag, config)
+
+ def create_origin_access_identity(self, caller_reference='', comment=''):
+ config = OriginAccessIdentityConfig(caller_reference=caller_reference,
+ comment=comment)
+ return self._create_object(config, 'origin-access-identity/cloudfront',
+ OriginAccessIdentity)
+
+ def delete_origin_access_identity(self, access_id, etag):
+ return self._delete_object(access_id, etag,
+ 'origin-access-identity/cloudfront')
+
- hmac = self.hmac.copy()
- hmac.update(headers['Date'])
- b64_hmac = base64.encodestring(hmac.digest()).strip()
- headers['Authorization'] = "AWS %s:%s" % (self.aws_access_key_id, b64_hmac)
Oops, something went wrong.

0 comments on commit 0a8ea52

Please sign in to comment.