def test_toomanyinvalidation_errors_cause_retry(self, MockClient): error = CloudFrontServerError(400, 'Bad Request') error.error_code = 'TooManyInvalidationsInProgress' cloudfront = Mock() cloudfront.create_invalidation_request.side_effect = error config = Client().config() client = Mock() client.config.return_value = config client.get_cloudfront.return_value = cloudfront MockClient.return_value = client session = Client().session() transaction_id = TaskTransaction.new_id() image = Image(filename='abad1dea') image.created_at = '2001-01-01 00:00:00' session.add(image) session.flush() invalidate = NeverCalledDirectlyInvalidate() invalidate.retry = Mock() invalidate(transaction_id, image.image_id) invalidate.retry.assert_called_with(exc=error)
def delete_distribution(self, distribution_id, etag): response = self.make_request( 'DELETE', '/%s/distribution/%s' % (self.Version, distribution_id), {'If-Match': etag}) body = response.read() if response.status != 204: raise CloudFrontServerError(response.status, response.reason, body)
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() boto.log.debug(body) if response.status != 204: raise CloudFrontServerError(response.status, response.reason, body)
def get_all_distributions(self): response = self.make_request('GET', '/%s/distribution' % self.Version) body = response.read() if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) rs = ResultSet([('DistributionSummary', DistributionSummary)]) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
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() if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) rs = ResultSet(tags) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
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() if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) 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()) body = response.read() return self.get_etag(response) if response.status != 200: raise CloudFrontServerError(response.status, response.reason, body)
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() if response.status == 201: 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 get_distribution_config(self, distribution_id): response = self.make_request( 'GET', '/%s/distribution/%s/config' % (self.Version, distribution_id)) body = response.read() if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) d = DistributionConfig(connection=self) d.etag = self.get_etag(response) h = handler.XmlHandler(d, self) xml.sax.parseString(body, h) return d
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() return self.get_etag(response) if response.status != 200: raise CloudFrontServerError(response.status, response.reason, body)
def test_unknown_cloudfront_errors_reraise(self, MockClient): error = CloudFrontServerError(400, 'Bad Request') error.error_code = 'CloudFrontHatesYouToday' cloudfront = Mock() cloudfront.create_invalidation_request.side_effect = error config = Client().config() client = Mock() client.config.return_value = config client.get_cloudfront.return_value = cloudfront MockClient.return_value = client transaction_id = TaskTransaction.new_id() session = Client().session() image = Image(filename='abad1dea') image.created_at = '2001-01-01 00:00:00' session.add(image) session.flush() Invalidate()(transaction_id, image.image_id)
def invalidation_request_status(self, distribution_id, request_id, caller_reference=None): uri = '/%s/distribution/%s/invalidation/%s' % (self.Version, distribution_id, request_id) response = self.make_request('GET', uri, {'Content-Type': 'text/xml'}) body = response.read() if response.status == 200: paths = InvalidationBatch([]) h = handler.XmlHandler(paths, self) xml.sax.parseString(body, h) return paths else: raise CloudFrontServerError(response.status, response.reason, body)
def get_distribution_info(self, distribution_id): response = self.make_request( 'GET', '/%s/distribution/%s' % (self.Version, distribution_id)) body = response.read() if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) d = Distribution(connection=self) response_headers = response.msg for key in response_headers.keys(): if key.lower() == 'etag': d.etag = response_headers[key] h = handler.XmlHandler(d, self) xml.sax.parseString(body, h) return d
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() if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) d = dist_class(connection=self) response_headers = response.msg for key in response_headers.keys(): if key.lower() == 'etag': d.etag = response_headers[key] h = handler.XmlHandler(d, self) xml.sax.parseString(body, h) return d
def _get_all_objects(self, resource, tags, result_set_class=None, result_set_kwargs=None): if not tags: tags = [('DistributionSummary', DistributionSummary)] response = self.make_request('GET', '/%s/%s' % (self.Version, resource)) body = response.read() boto.log.debug(body) if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) rs_class = result_set_class or ResultSet rs_kwargs = result_set_kwargs or dict() rs = rs_class(tags, **rs_kwargs) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
def create_invalidation_request(self, distribution_id, paths, caller_reference=None): """Creates a new invalidation request :see: http://goo.gl/8vECq """ # We allow you to pass in either an array or # an InvalidationBatch object if not isinstance(paths, InvalidationBatch): paths = InvalidationBatch(paths) paths.connection = self uri = '/%s/distribution/%s/invalidation' % (self.Version, distribution_id) response = self.make_request('POST', uri, {'Content-Type': 'text/xml'}, data=paths.to_xml()) body = response.read() if response.status == 201: h = handler.XmlHandler(paths, self) xml.sax.parseString(body, h) return paths else: 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, {'Content-Type': 'text/xml'}, data=config.to_xml()) body = response.read() if response.status == 201: d = Distribution(connection=self) h = handler.XmlHandler(d, self) xml.sax.parseString(body, h) return d else: raise CloudFrontServerError(response.status, response.reason, body)