def clusters_list(): tenant_id = request.headers['X-Tenant-Id'] try: return api_u.render( clusters=[c.dict for c in api.get_clusters(tenant_id=tenant_id)]) except Exception, e: return api_u.internal_error(500, 'Exception while listing Clusters', e)
def templates_list(): try: return api_u.render( node_templates=[nt.dict for nt in api.get_node_templates()]) except Exception, e: return api_u.internal_error(500, "Exception while listing NodeTemplates", e)
def teardown_request(_ex=None): # TODO(slukjanov): how it'll work in case of exception? if context.has_ctx(): if flask.request.path != '/': try: session = context.session() if session.transaction: session.transaction.commit() except Exception as e: return api_utils.internal_error( 500, 'Internal Server Error', e) context.set_ctx(None)
def decorator(func): @functools.wraps(func) def handler(*args, **kwargs): try: if tenant_specific: tenant = request.headers['X-Tenant-Id'] service_func(*args, id=kwargs[id_prop], tenant_id=tenant) else: service_func(*args, id=kwargs[id_prop]) return func(*args, **kwargs) except ex.NotFoundException, e: e.__init__(kwargs[id_prop]) return api_u.not_found(e) except Exception, e: return api_u.internal_error( 500, "Unexpected error occurred", e)
def handler(*args, **kwargs): request_data = u.request_data() try: if schema: validator = api_validator.ApiValidator(schema) validator.validate(request_data) if validators: for validator in validators: validator(**kwargs) except jsonschema.ValidationError as e: e.code = "VALIDATION_ERROR" return u.bad_request(e) except ex.SavannaException as e: return u.bad_request(e) except os_ex.MalformedRequestBody as e: e.code = "MALFORMED_REQUEST_BODY" return u.bad_request(e) except Exception as e: return u.internal_error(500, "Error occurred during validation", e) return func(*args, **kwargs)
def handler(*args, **kwargs): request_data = u.request_data() try: if schema: validator = api_validator.ApiValidator(schema) validator.validate(request_data) if validators: for validator in validators: validator(**kwargs) except jsonschema.ValidationError as e: e.code = "VALIDATION_ERROR" return u.bad_request(e) except ex.SavannaException as e: return u.bad_request(e) except os_ex.MalformedRequestBody as e: e.code = "MALFORMED_REQUEST_BODY" return u.bad_request(e) except Exception as e: return u.internal_error( 500, "Error occurred during validation", e) return func(*args, **kwargs)
def not_implemented(): return u.internal_error( 501, NotImplementedError("This API operation isn't implemented"))
def validate(validate_func): def decorator(func): @functools.wraps(func) def handler(*args, **kwargs): try: validate_func(api_u.request_data(), **kwargs) except jsonschema.ValidationError, e: e.code = "VALIDATION_ERROR" return api_u.bad_request(e) except ex.SavannaException, e: return api_u.bad_request(e) except os_ex.MalformedRequestBody, e: e.code = "MALFORMED_REQUEST_BODY" return api_u.bad_request(e) except Exception, e: return api_u.internal_error( 500, "Error occurred during validation", e) return func(*args, **kwargs) return handler return decorator def exists_by_id(service_func, id_prop, tenant_specific=False): def decorator(func): @functools.wraps(func) def handler(*args, **kwargs): try: if tenant_specific: tenant = request.headers['X-Tenant-Id']
def clusters_update(cluster_id): return api_u.internal_error(501, NotImplementedError( "Cluster update op isn't implemented (id '%s')" % cluster_id))
def templates_update(template_id): return api_u.internal_error(501, NotImplementedError( "Template update op isn't implemented (id '%s')" % template_id))