def __init__(self, service_obj, request): super(Model, self).__init__() self["name"] = service_obj.name self["domains"] = [domain.Model(d) for d in service_obj.domains] self["origins"] = [origin.Model(o) for o in service_obj.origins] self["status"] = service_obj.status self["flavorRef"] = uri.encode(u'{0}/v1.0/flavors/{1}'.format( request.host_url, service_obj.flavorRef)) # TODO(tonytan4ever) : add access_url links. # This has things to do with provider_detail change. (CDN-172) self["links"] = [link.Model( str( uri.encode(u'{0}/v1.0/services/{1}'.format( request.host_url, self['name']))), 'self')] for provider_name in service_obj.provider_details: for access_url in ( service_obj.provider_details[provider_name].access_urls): self["links"].append(link.Model( access_url, 'access_url'))
def test_uri_encode(self): url = 'http://example.com/v1/fizbit/messages?limit=3&echo=true' self.assertEqual(uri.encode(url), url) url = 'http://example.com/v1/fiz bit/messages' expected = 'http://example.com/v1/fiz%20bit/messages' self.assertEqual(uri.encode(url), expected) url = u'http://example.com/v1/fizbit/messages?limit=3&e\u00e7ho=true' expected = ('http://example.com/v1/fizbit/messages' '?limit=3&e%C3%A7ho=true') self.assertEqual(uri.encode(url), expected)
def __init__(self, service_obj, controller): super(Model, self).__init__() self["name"] = util.help_escape(service_obj.name) self["id"] = str(service_obj.service_id) self["domains"] = [domain.Model(d) for d in service_obj.domains] self["origins"] = [origin.Model(o) for o in service_obj.origins] self["restrictions"] = [restriction.Model(r) for r in service_obj.restrictions] self["caching"] = [cachingrules.Model(c) for c in service_obj.caching] self["flavor_id"] = service_obj.flavor_id self["log_delivery"] = log_delivery.Model(service_obj.log_delivery) self["status"] = service_obj.status if service_obj.operator_status == "disabled": self["status"] = service_obj.operator_status self["errors"] = [] self["links"] = [ link.Model( str( uri.encode(u'{0}/services/{1}'.format( controller.base_url, service_obj.service_id))), 'self'), link.Model( str( uri.encode(u'{0}/flavors/{1}'.format( controller.base_url, service_obj.flavor_id))), 'flavor')] for provider_name in service_obj.provider_details: provider_detail = service_obj.provider_details[provider_name] # add the access urls access_urls = provider_detail.access_urls for access_url in access_urls: if 'operator_url' in access_url: self['links'].append(link.Model( access_url['operator_url'], 'access_url')) elif 'log_delivery' in access_url: self['links'].append(link.Model( access_url['log_delivery'][0]['publicURL'], 'log_delivery')) # add any errors error_message = provider_detail.error_message if error_message: self["errors"].append({"message": error_message})
def patch_one(self, service_id): service_updates = json.loads(pecan.request.body.decode('utf-8')) services_controller = self._driver.manager.services_controller try: services_controller.update(self.project_id, service_id, self.auth_token, service_updates) except exceptions.ValidationFailed as e: pecan.abort(400, detail=u'{0}'.format(e)) except LookupError as e: # error handler for no flavor pecan.abort(400, detail=str(e)) except ValueError as e: # error handler for existing service name pecan.abort(400, detail=str(e)) except errors.ServiceNotFound as e: pecan.abort(404, detail=str(e)) except errors.ServiceStatusNeitherDeployedNorFailed as e: pecan.abort(400, detail=str(e)) except errors.SharedShardsExhausted as e: # domain - shared domains exhausted pecan.abort(400, detail=str(e)) except Exception as e: pecan.abort(400, detail=util.help_escape(str(e))) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format(pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
def patch_one(self, service_id): service_updates = json.loads(pecan.request.body.decode('utf-8')) services_controller = self._driver.manager.services_controller try: services_controller.update( self.project_id, service_id, self.auth_token, service_updates) except exceptions.ValidationFailed as e: pecan.abort(400, detail=u'{0}'.format(e)) except LookupError as e: # error handler for no flavor pecan.abort(400, detail=str(e)) except ValueError as e: # error handler for existing service name pecan.abort(400, detail=str(e)) except errors.ServiceNotFound as e: pecan.abort(404, detail=str(e)) except errors.ServiceStatusNeitherDeployedNorFailed as e: pecan.abort(400, detail=str(e)) except Exception as e: pecan.abort(400, detail=util.help_escape(str(e))) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format( pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
def post(self): services_controller = self._driver.manager.services_controller service_json_dict = json.loads(pecan.request.body.decode('utf-8')) service_id = None try: service_obj = services_controller.create(self.project_id, self.auth_token, service_json_dict) service_id = service_obj.service_id except errors.SharedShardsExhausted as e: # domain - shared domains exhausted pecan.abort(400, detail=str(e)) except LookupError as e: # error handler for no flavor pecan.abort(400, detail=str(e)) except ValueError as e: # error handler for existing service name pecan.abort(400, detail=str(e)) except errors.ServicesOverLimit as e: # error handler for services count exceeding limit pecan.abort(403, detail=str(e)) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format( pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
def test_get_one(self, value, mock_manager): return_flavor = flavor.load_from_json(value) # mock the storage response mock_response = return_flavor mock_manager.get.return_value = mock_response url = u'/v1.0/flavors/{0}'.format(uri.encode(value['id'])) response = self.app.get(url, headers={'X-Project-ID': self.project_id}) self.assertEqual(200, response.status_code)
def post(self): services_controller = self._driver.manager.services_controller service_json_dict = json.loads(pecan.request.body.decode('utf-8')) service_obj = req_service_model.load_from_json(service_json_dict) service_name = service_json_dict.get("name", None) try: services_controller.create(self.project_id, service_obj, service_json_dict) except LookupError as e: # error handler for no flavor pecan.abort(400, detail=str(e)) except ValueError as e: # error handler for existing service name pecan.abort(400, detail=str(e)) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format( pecan.request.host_url, service_name))) pecan.response.status = 202 pecan.response.headers["Location"] = service_url
def post(self): services_controller = self._driver.manager.services_controller service_json_dict = json.loads(pecan.request.body.decode('utf-8')) service_obj = req_service_model.load_from_json(service_json_dict) service_id = service_obj.service_id try: services_controller.create(self.project_id, service_obj) except LookupError as e: # error handler for no flavor pecan.abort(400, detail=str(e)) except ValueError as e: # error handler for existing service name pecan.abort(400, detail=str(e)) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format( pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
def delete(self, service_id): purge_url = pecan.request.GET.get('url', '/*') purge_all = pecan.request.GET.get('all', False) hard = pecan.request.GET.get('hard', 'True') if purge_url: try: purge_url.encode('ascii') except (UnicodeDecodeError, UnicodeEncodeError): pecan.abort(400, detail='non ascii character present in url') if hard and hard.lower() == 'false': hard = 'False' if hard and hard.lower() == 'true': hard = 'True' try: hard = ast.literal_eval(hard) except ValueError: pecan.abort(400, detail='hard can only be set to True or False') if hard not in [True, False]: pecan.abort(400, detail='hard can only be set to True or False') purge_all = (True if purge_all and purge_all.lower() == 'true' else False) if purge_all and purge_url != '/*': pecan.abort(400, detail='Cannot provide all=true ' 'and a url at the same time') services_controller = self._driver.manager.services_controller try: services_controller.purge(self.project_id, service_id, hard, purge_url) except errors.ServiceStatusNotDeployed as e: pecan.abort(400, detail=str(e)) except LookupError as e: pecan.abort(404, detail=str(e)) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format(pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
def delete(self, service_id): purge_url = pecan.request.GET.get('url', '/*') purge_all = pecan.request.GET.get('all', False) hard = pecan.request.GET.get('hard', 'True') if purge_url: try: purge_url.encode('ascii') except (UnicodeDecodeError, UnicodeEncodeError): pecan.abort(400, detail='non ascii character present in url') if hard and hard.lower() == 'false': hard = 'False' if hard and hard.lower() == 'true': hard = 'True' try: hard = ast.literal_eval(hard) except ValueError: pecan.abort(400, detail='hard can only be set to True or False') if hard not in [True, False]: pecan.abort(400, detail='hard can only be set to True or False') purge_all = ( True if purge_all and purge_all.lower() == 'true' else False) if purge_all and purge_url != '/*': pecan.abort(400, detail='Cannot provide all=true ' 'and a url at the same time') services_controller = self._driver.manager.services_controller try: services_controller.purge(self.project_id, service_id, hard, purge_url) except errors.ServiceStatusNotDeployed as e: pecan.abort(400, detail=str(e)) except LookupError as e: pecan.abort(404, detail=str(e)) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format( pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
def post(self): """POST :returns JSON flavor(HTTP 200) or HTTP 400 """ flavors_controller = self.driver.manager.flavors_controller flavor_json = json.loads(pecan.request.body.decode('utf-8')) try: new_flavor = flavor_request.load_from_json(flavor_json) flavors_controller.add(new_flavor) # form the success response flavor_url = str( uri.encode(u'{0}/flavors/{1}'.format( self.base_url, new_flavor.flavor_id))) pecan.response.status = 201 pecan.response.headers["Location"] = flavor_url result = flavors_controller.get(new_flavor.flavor_id) return flavor_response.Model(result, self) except Exception as e: pecan.abort(400, detail=str(e))
def post(self): services_controller = self._driver.manager.services_controller service_json_dict = json.loads(pecan.request.body.decode('utf-8')) service_id = None try: service_obj = services_controller.create(self.project_id, self.auth_token, service_json_dict) service_id = service_obj.service_id except errors.SharedShardsExhausted as e: # domain - shared domains exhausted pecan.abort(400, detail=str(e)) except LookupError as e: # error handler for no flavor pecan.abort(400, detail=str(e)) except ValueError as e: # error handler for existing service name pecan.abort(400, detail=str(e)) except errors.ServicesOverLimit as e: # error handler for services count exceeding limit pecan.abort(403, detail=str(e)) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format(pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
self["domains"] = [domain.Model(d) for d in service_obj.domains] self["origins"] = [origin.Model(o) for o in service_obj.origins] self["restrictions"] = [restriction.Model(r) for r in service_obj.restrictions] self["caching"] = [cachingrules.Model(c) for c in service_obj.caching] self["status"] = service_obj.status self["flavor_id"] = service_obj.flavor_id self["errors"] = [] self["links"] = [ link.Model( str( uri.encode(u'{0}/services/{1}'.format( controller.base_url, service_obj.service_id))), 'self'), link.Model( str( uri.encode(u'{0}/flavors/{1}'.format( controller.base_url, service_obj.flavor_id))), 'flavor')] for provider_name in service_obj.provider_details: provider_detail = service_obj.provider_details[provider_name] # add the access urls access_urls = provider_detail.access_urls for access_url in access_urls:
def __init__(self, service_obj, controller): super(Model, self).__init__() self["name"] = util.help_escape(service_obj.name) self["id"] = str(service_obj.service_id) self["project_id"] = str(service_obj.project_id) self["domains"] = [domain.Model(d) for d in service_obj.domains] # default other type of certificate domains to create_in_progress # Note(tonytan4ever): with out this piece of code # there will be a short period of time domain without certificate # status for domain_d in self["domains"]: if domain_d.get("protocol", "http") == "https": if domain_d.get("certificate") == "shared": domain_d["certificate_status"] = 'deployed' else: domain_d["certificate_status"] = 'create_in_progress' self["origins"] = [origin.Model(o) for o in service_obj.origins] self["restrictions"] = [ restriction.Model(r) for r in service_obj.restrictions ] self["caching"] = [cachingrules.Model(c) for c in service_obj.caching] self["flavor_id"] = service_obj.flavor_id self["log_delivery"] = log_delivery.Model(service_obj.log_delivery) self["status"] = service_obj.status if service_obj.operator_status == "disabled": self["status"] = service_obj.operator_status self["errors"] = [] self["links"] = [ link.Model( str( uri.encode(u'{0}/services/{1}'.format( controller.base_url, service_obj.service_id))), 'self'), link.Model( str( uri.encode(u'{0}/flavors/{1}'.format( controller.base_url, service_obj.flavor_id))), 'flavor') ] for provider_name in service_obj.provider_details: provider_detail = service_obj.provider_details[provider_name] # add any certificate_status for non shared ssl domains # Note(tonytan4ever): for right now we only consider one provider, # in case of multiple providers we really should consider all # provider's domain certificate status for domain_d in self["domains"]: if domain_d.get("protocol", "http") == "https": if domain_d.get("certificate") != "shared": domain_d["certificate_status"] = ( provider_detail.domains_certificate_status. get_domain_certificate_status(domain_d['domain'])) # add the access urls access_urls = provider_detail.access_urls for access_url in access_urls: if ('operator_url' in access_url and access_url['operator_url'] is not None): self['links'].append( link.Model(access_url['operator_url'], 'access_url')) elif 'log_delivery' in access_url: self['links'].append( link.Model(access_url['log_delivery'][0]['publicURL'], 'log_delivery')) # add any errors error_message = provider_detail.error_message if error_message: self["errors"].append({"message": error_message})
service_json_dict = json.loads(pecan.request.body.decode('utf-8')) >>>>>>> master # if service_updates is empty, abort if not service_updates: pecan.abort(400, detail='No details provided to update') services_controller = self._driver.manager.services_controller try: services_controller.update( self.project_id, service_id, service_updates) <<<<<<< HEAD except exceptions.ValidationFailed as e: pecan.abort(400, detail=str(e)) ======= >>>>>>> master except ValueError as e: pecan.abort(404, detail='service could not be found') except errors.ServiceStatusNotDeployed as e: pecan.abort(400, detail=str(e)) except Exception as e: pecan.abort(400, detail=str(e)) service_url = str( uri.encode(u'{0}/v1.0/services/{1}'.format( pecan.request.host_url, service_id))) return pecan.Response(None, 202, headers={"Location": service_url})
def __init__(self, service_obj, controller): super(Model, self).__init__() self["name"] = util.help_escape(service_obj.name) self["id"] = str(service_obj.service_id) self["project_id"] = str(service_obj.project_id) self["domains"] = [domain.Model(d) for d in service_obj.domains] # default other type of certificate domains to create_in_progress # Note(tonytan4ever): with out this piece of code # there will be a short period of time domain without certificate # status for domain_d in self["domains"]: if domain_d.get("protocol", "http") == "https": if domain_d.get("certificate") == "shared": domain_d["certificate_status"] = 'deployed' else: domain_d["certificate_status"] = 'create_in_progress' self["origins"] = [origin.Model(o) for o in service_obj.origins] self["restrictions"] = [restriction.Model(r) for r in service_obj.restrictions] self["caching"] = [cachingrules.Model(c) for c in service_obj.caching] self["flavor_id"] = service_obj.flavor_id self["log_delivery"] = log_delivery.Model(service_obj.log_delivery) self["status"] = service_obj.status if service_obj.operator_status == "disabled": self["status"] = service_obj.operator_status self["errors"] = [] self["links"] = [ link.Model( str( uri.encode(u'{0}/services/{1}'.format( controller.base_url, service_obj.service_id))), 'self'), link.Model( str( uri.encode(u'{0}/flavors/{1}'.format( controller.base_url, service_obj.flavor_id))), 'flavor')] for provider_name in service_obj.provider_details: provider_detail = service_obj.provider_details[provider_name] # add any certificate_status for non shared ssl domains # Note(tonytan4ever): for right now we only consider one provider, # in case of multiple providers we really should consider all # provider's domain certificate status for domain_d in self["domains"]: if domain_d.get("protocol", "http") == "https": if domain_d.get("certificate") != "shared": domain_d["certificate_status"] = ( provider_detail. domains_certificate_status. get_domain_certificate_status(domain_d['domain'])) # add the access urls access_urls = provider_detail.access_urls for access_url in access_urls: if ( 'operator_url' in access_url and access_url['operator_url'] is not None ): self['links'].append(link.Model( access_url['operator_url'], 'access_url')) elif 'log_delivery' in access_url: self['links'].append(link.Model( access_url['log_delivery'][0]['publicURL'], 'log_delivery')) # add any errors error_message = provider_detail.error_message if error_message: self["errors"].append({"message": error_message})