OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/ |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 |
| 23 import xml.sax |
| 24 import time |
| 25 import boto |
| 26 from boto.connection import AWSAuthConnection |
| 27 from boto import handler |
| 28 from boto.cloudfront.distribution import Distribution, DistributionSummary, Dist
ributionConfig |
| 29 from boto.cloudfront.distribution import StreamingDistribution, StreamingDistrib
utionSummary, StreamingDistributionConfig |
| 30 from boto.cloudfront.identity import OriginAccessIdentity |
| 31 from boto.cloudfront.identity import OriginAccessIdentitySummary |
| 32 from boto.cloudfront.identity import OriginAccessIdentityConfig |
| 33 from boto.cloudfront.invalidation import InvalidationBatch, InvalidationSummary,
InvalidationListResultSet |
| 34 from boto.resultset import ResultSet |
| 35 from boto.cloudfront.exception import CloudFrontServerError |
| 36 |
| 37 |
| 38 class CloudFrontConnection(AWSAuthConnection): |
| 39 |
| 40 DefaultHost = 'cloudfront.amazonaws.com' |
| 41 Version = '2010-11-01' |
| 42 |
| 43 def __init__(self, aws_access_key_id=None, aws_secret_access_key=None, |
| 44 port=None, proxy=None, proxy_port=None, |
| 45 host=DefaultHost, debug=0, security_token=None, |
| 46 validate_certs=True): |
| 47 AWSAuthConnection.__init__(self, host, |
| 48 aws_access_key_id, aws_secret_access_key, |
| 49 True, port, proxy, proxy_port, debug=debug, |
| 50 security_token=security_token, |
| 51 validate_certs=validate_certs) |
| 52 |
| 53 def get_etag(self, response): |
| 54 response_headers = response.msg |
| 55 for key in response_headers.keys(): |
| 56 if key.lower() == 'etag': |
| 57 return response_headers[key] |
| 58 return None |
| 59 |
| 60 def _required_auth_capability(self): |
| 61 return ['cloudfront'] |
| 62 |
| 63 # Generics |
| 64 |
| 65 def _get_all_objects(self, resource, tags, result_set_class=None, |
| 66 result_set_kwargs=None): |
| 67 if not tags: |
| 68 tags = [('DistributionSummary', DistributionSummary)] |
| 69 response = self.make_request('GET', '/%s/%s' % (self.Version, |
| 70 resource)) |
| 71 body = response.read() |
| 72 boto.log.debug(body) |
| 73 if response.status >= 300: |
| 74 raise CloudFrontServerError(response.status, response.reason, body) |
| 75 rs_class = result_set_class or ResultSet |
| 76 rs_kwargs = result_set_kwargs or dict() |
| 77 rs = rs_class(tags, **rs_kwargs) |
| 78 h = handler.XmlHandler(rs, self) |
| 79 xml.sax.parseString(body, h) |
| 80 return rs |
| 81 |
| 82 def _get_info(self, id, resource, dist_class): |
| 83 uri = '/%s/%s/%s' % (self.Version, resource, id) |
| 84 response = self.make_request('GET', uri) |
| 85 body = response.read() |
| 86 boto.log.debug(body) |
| 87 if response.status >= 300: |
| 88 raise CloudFrontServerError(response.status, response.reason, body) |
| 89 d = dist_class(connection=self) |
| 90 response_headers = response.msg |
| 91 for key in response_headers.keys(): |
| 92 if key.lower() == 'etag': |
| 93 d.etag = response_headers[key] |
| 94 h = handler.XmlHandler(d, self) |
| 95 xml.sax.parseString(body, h) |
| 96 return d |
| 97 |
| 98 def _get_config(self, id, resource, config_class): |
| 99 uri = '/%s/%s/%s/config' % (self.Version, resource, id) |
| 100 response = self.make_request('GET', uri) |
| 101 body = response.read() |
| 102 boto.log.debug(body) |
| 103 if response.status >= 300: |
| 104 raise CloudFrontServerError(response.status, response.reason, body) |
| 105 d = config_class(connection=self) |
| 106 d.etag = self.get_etag(response) |
| 107 h = handler.XmlHandler(d, self) |
| 108 xml.sax.parseString(body, h) |
| 109 return d |
| 110 |
| 111 def _set_config(self, distribution_id, etag, config): |
| 112 if isinstance(config, StreamingDistributionConfig): |
| 113 resource = 'streaming-distribution' |
| 114 else: |
| 115 resource = 'distribution' |
| 116 uri = '/%s/%s/%s/config' % (self.Version, resource, distribution_id) |
| 117 headers = {'If-Match': etag, 'Content-Type': 'text/xml'} |
| 118 response = self.make_request('PUT', uri, headers, config.to_xml()) |
| 119 body = response.read() |
| 120 boto.log.debug(body) |
| 121 if response.status != 200: |
| 122 raise CloudFrontServerError(response.status, response.reason, body) |
| 123 return self.get_etag(response) |
| 124 |
| 125 def _create_object(self, config, resource, dist_class): |
| 126 response = self.make_request('POST', '/%s/%s' % (self.Version, |
| 127 resource), |
| 128 {'Content-Type': 'text/xml'}, |
| 129 data=config.to_xml()) |
| 130 body = response.read() |
| 131 boto.log.debug(body) |
| 132 if response.status == 201: |
| 133 d = dist_class(connection=self) |
| 134 h = handler.XmlHandler(d, self) |
| 135 xml.sax.parseString(body, h) |
| 136 d.etag = self.get_etag(response) |
| 137 return d |
| 138 else: |
| 139 raise CloudFrontServerError(response.status, response.reason, body) |
| 140 |
| 141 def _delete_object(self, id, etag, resource): |
| 142 uri = '/%s/%s/%s' % (self.Version, resource, id) |
| 143 response = self.make_request('DELETE', uri, {'If-Match': etag}) |
| 144 body = response.read() |
| 145 boto.log.debug(body) |
| 146 if response.status != 204: |
| 147 raise CloudFrontServerError(response.status, response.reason, body) |
| 148 |
| 149 # Distributions |
| 150 |
| 151 def get_all_distributions(self): |
| 152 tags = [('DistributionSummary', DistributionSummary)] |
| 153 return self._get_all_objects('distribution', tags) |
| 154 |
| 155 def get_distribution_info(self, distribution_id): |
| 156 return self._get_info(distribution_id, 'distribution', Distribution) |
| 157 |
| 158 def get_distribution_config(self, distribution_id): |
| 159 return self._get_config(distribution_id, 'distribution', |
| 160 DistributionConfig) |
| 161 |
| 162 def set_distribution_config(self, distribution_id, etag, config): |
| 163 return self._set_config(distribution_id, etag, config) |
| 164 |
| 165 def create_distribution(self, origin, enabled, caller_reference='', |
| 166 cnames=None, comment='', trusted_signers=None): |
| 167 config = DistributionConfig(origin=origin, enabled=enabled, |
| 168 caller_reference=caller_reference, |
| 169 cnames=cnames, comment=comment, |
| 170 trusted_signers=trusted_signers) |
| 171 return self._create_object(config, 'distribution', Distribution) |
| 172 |
| 173 def delete_distribution(self, distribution_id, etag): |
| 174 return self._delete_object(distribution_id, etag, 'distribution') |
| 175 |
| 176 # Streaming Distributions |
| 177 |
| 178 def get_all_streaming_distributions(self): |
| 179 tags = [('StreamingDistributionSummary', StreamingDistributionSummary)] |
| 180 return self._get_all_objects('streaming-distribution', tags) |
| 181 |
| 182 def get_streaming_distribution_info(self, distribution_id): |
| 183 return self._get_info(distribution_id, 'streaming-distribution', |
| 184 StreamingDistribution) |
| 185 |
| 186 def get_streaming_distribution_config(self, distribution_id): |
| 187 return self._get_config(distribution_id, 'streaming-distribution', |
| 188 StreamingDistributionConfig) |
| 189 |
| 190 def set_streaming_distribution_config(self, distribution_id, etag, config): |
| 191 return self._set_config(distribution_id, etag, config) |
| 192 |
| 193 def create_streaming_distribution(self, origin, enabled, |
| 194 caller_reference='', |
| 195 cnames=None, comment='', |
| 196 trusted_signers=None): |
| 197 config = StreamingDistributionConfig(origin=origin, enabled=enabled, |
| 198 caller_reference=caller_reference, |
| 199 cnames=cnames, comment=comment, |
| 200 trusted_signers=trusted_signers) |
| 201 return self._create_object(config, 'streaming-distribution', |
| 202 StreamingDistribution) |
| 203 |
| 204 def delete_streaming_distribution(self, distribution_id, etag): |
| 205 return self._delete_object(distribution_id, etag, |
| 206 'streaming-distribution') |
| 207 |
| 208 # Origin Access Identity |
| 209 |
| 210 def get_all_origin_access_identity(self): |
| 211 tags = [('CloudFrontOriginAccessIdentitySummary', |
| 212 OriginAccessIdentitySummary)] |
| 213 return self._get_all_objects('origin-access-identity/cloudfront', tags) |
| 214 |
| 215 def get_origin_access_identity_info(self, access_id): |
| 216 return self._get_info(access_id, 'origin-access-identity/cloudfront', |
| 217 OriginAccessIdentity) |
| 218 |
| 219 def get_origin_access_identity_config(self, access_id): |
| 220 return self._get_config(access_id, |
| 221 'origin-access-identity/cloudfront', |
| 222 OriginAccessIdentityConfig) |
| 223 |
| 224 def set_origin_access_identity_config(self, access_id, |
| 225 etag, config): |
| 226 return self._set_config(access_id, etag, config) |
| 227 |
| 228 def create_origin_access_identity(self, caller_reference='', comment=''): |
| 229 config = OriginAccessIdentityConfig(caller_reference=caller_reference, |
| 230 comment=comment) |
| 231 return self._create_object(config, 'origin-access-identity/cloudfront', |
| 232 OriginAccessIdentity) |
| 233 |
| 234 def delete_origin_access_identity(self, access_id, etag): |
| 235 return self._delete_object(access_id, etag, |
| 236 'origin-access-identity/cloudfront') |
| 237 |
| 238 # Object Invalidation |
| 239 |
| 240 def create_invalidation_request(self, distribution_id, paths, |
| 241 caller_reference=None): |
| 242 """Creates a new invalidation request |
| 243 :see: http://goo.gl/8vECq |
| 244 """ |
| 245 # We allow you to pass in either an array or |
| 246 # an InvalidationBatch object |
| 247 if not isinstance(paths, InvalidationBatch): |
| 248 paths = InvalidationBatch(paths) |
| 249 paths.connection = self |
| 250 uri = '/%s/distribution/%s/invalidation' % (self.Version, |
| 251 distribution_id) |
| 252 response = self.make_request('POST', uri, |
| 253 {'Content-Type': 'text/xml'}, |
| 254 data=paths.to_xml()) |
| 255 body = response.read() |
| 256 if response.status == 201: |
| 257 h = handler.XmlHandler(paths, self) |
| 258 xml.sax.parseString(body, h) |
| 259 return paths |
| 260 else: |
| 261 raise CloudFrontServerError(response.status, response.reason, body) |
| 262 |
| 263 def invalidation_request_status(self, distribution_id, |
| 264 request_id, caller_reference=None): |
| 265 uri = '/%s/distribution/%s/invalidation/%s' % (self.Version, |
| 266 distribution_id, |
| 267 request_id) |
| 268 response = self.make_request('GET', uri, {'Content-Type': 'text/xml'}) |
| 269 body = response.read() |
| 270 if response.status == 200: |
| 271 paths = InvalidationBatch([]) |
| 272 h = handler.XmlHandler(paths, self) |
| 273 xml.sax.parseString(body, h) |
| 274 return paths |
| 275 else: |
| 276 raise CloudFrontServerError(response.status, response.reason, body) |
| 277 |
| 278 def get_invalidation_requests(self, distribution_id, marker=None, |
| 279 max_items=None): |
| 280 """ |
| 281 Get all invalidation requests for a given CloudFront distribution. |
| 282 This returns an instance of an InvalidationListResultSet that |
| 283 automatically handles all of the result paging, etc. from CF - you just |
| 284 need to keep iterating until there are no more results. |
| 285 |
| 286 :type distribution_id: string |
| 287 :param distribution_id: The id of the CloudFront distribution |
| 288 |
| 289 :type marker: string |
| 290 :param marker: Use this only when paginating results and only in |
| 291 follow-up request after you've received a response where |
| 292 the results are truncated. Set this to the value of the |
| 293 Marker element in the response you just received. |
| 294 |
| 295 :type max_items: int |
| 296 :param max_items: Use this only when paginating results and only in a |
| 297 follow-up request to indicate the maximum number of |
| 298 invalidation requests you want in the response. You |
| 299 will need to pass the next_marker property from the |
| 300 previous InvalidationListResultSet response in the |
| 301 follow-up request in order to get the next 'page' of |
| 302 results. |
| 303 |
| 304 :rtype: :class:`boto.cloudfront.invalidation.InvalidationListResultSet` |
| 305 :returns: An InvalidationListResultSet iterator that lists invalidation |
| 306 requests for a given CloudFront distribution. Automatically |
| 307 handles paging the results. |
| 308 """ |
| 309 uri = 'distribution/%s/invalidation' % distribution_id |
| 310 params = dict() |
| 311 if marker: |
| 312 params['Marker'] = marker |
| 313 if max_items: |
| 314 params['MaxItems'] = max_items |
| 315 if params: |
| 316 uri += '?%s=%s' % params.popitem() |
| 317 for k, v in params.items(): |
| 318 uri += '&%s=%s' % (k, v) |
| 319 tags=[('InvalidationSummary', InvalidationSummary)] |
| 320 rs_class = InvalidationListResultSet |
| 321 rs_kwargs = dict(connection=self, distribution_id=distribution_id, |
| 322 max_items=max_items, marker=marker) |
| 323 return self._get_all_objects(uri, tags, result_set_class=rs_class, |
| 324 result_set_kwargs=rs_kwargs) |
OLD | NEW |