def resolve_pending_commissions(request): input_data = utils.get_json_body(request) check_is_dict(input_data) client_key = unicode(request.component_instance) accept = input_data.get('accept', []) reject = input_data.get('reject', []) if not isinstance(accept, list) or not isinstance(reject, list): m = '"accept" and "reject" should reference lists of serials.' raise BadRequest(m) if not are_integer(accept) or not are_integer(reject): raise BadRequest("Serials should be integer.") result = qh.resolve_pending_commissions(clientkey=client_key, accept_set=accept, reject_set=reject) accepted, rejected, notFound, conflicting = result notFound = [(serial, notFoundCF(serial)) for serial in notFound] conflicting = [(serial, conflictingCF(serial)) for serial in conflicting] cloudfaults = notFound + conflicting data = {'accepted': accepted, 'rejected': rejected, 'failed': cloudfaults} return json_response(data)
def serial_action(request, serial): input_data = utils.get_json_body(request) check_is_dict(input_data) try: serial = int(serial) except ValueError: raise BadRequest("Serial should be an integer.") client_key = unicode(request.component_instance) accept = 'accept' in input_data reject = 'reject' in input_data if accept == reject: raise BadRequest('Specify either accept or reject action.') result = qh.resolve_pending_commission(clientkey=client_key, serial=serial, accept=accept) response = HttpResponse() if not result: response.status_code = 404 return response
def serial_action(request, serial): data = request.body try: input_data = json.loads(data) except json.JSONDecodeError: raise BadRequest("POST data should be in json format.") try: serial = int(serial) except ValueError: raise BadRequest("Serial should be an integer.") client_key = str(request.component_instance) accept = 'accept' in input_data reject = 'reject' in input_data if accept == reject: raise BadRequest('Specify either accept or reject action.') result = qh.resolve_pending_commission(clientkey=client_key, serial=serial, accept=accept) response = HttpResponse() if not result: response.status_code = 404 return response
def resolve_pending_commissions(request): data = request.body try: input_data = json.loads(data) except json.JSONDecodeError: raise BadRequest("POST data should be in json format.") client_key = str(request.component_instance) accept = input_data.get('accept', []) reject = input_data.get('reject', []) if not isinstance(accept, list) or not isinstance(reject, list): m = '"accept" and "reject" should reference lists of serials.' raise BadRequest(m) if not are_integer(accept) or not are_integer(reject): raise BadRequest("Serials should be integer.") result = qh.resolve_pending_commissions(clientkey=client_key, accept_set=accept, reject_set=reject) accepted, rejected, notFound, conflicting = result notFound = [(serial, notFoundCF(serial)) for serial in notFound] conflicting = [(serial, conflictingCF(serial)) for serial in conflicting] cloudfaults = notFound + conflicting data = {'accepted': accepted, 'rejected': rejected, 'failed': cloudfaults} return json_response(data)
def issue_commission(request): input_data = utils.get_json_body(request) check_is_dict(input_data) client_key = unicode(request.component_instance) provisions = input_data.get('provisions') if provisions is None: raise BadRequest("Provisions are missing.") if not isinstance(provisions, list): raise BadRequest("Provisions should be a list.") provisions = _provisions_to_list(provisions) force = input_data.get('force', False) if not isinstance(force, bool): raise BadRequest('"force" option should be a boolean.') auto_accept = input_data.get('auto_accept', False) if not isinstance(auto_accept, bool): raise BadRequest('"auto_accept" option should be a boolean.') name = input_data.get('name', "") if not isinstance(name, basestring): raise BadRequest("Commission name should be a string.") try: result = _issue_commission(clientkey=client_key, provisions=provisions, name=name, force=force, accept=auto_accept) data = {"serial": result} status_code = 201 except (qh_exception.NoCapacityError, qh_exception.NoQuantityError) as e: status_code = 413 body = { "message": e.message, "code": status_code, "data": e.data, } data = {"overLimit": body} except qh_exception.NoHoldingError as e: status_code = 404 body = { "message": e.message, "code": status_code, "data": e.data, } data = {"itemNotFound": body} except qh_exception.InvalidDataError as e: status_code = 400 body = { "message": e.message, "code": status_code, } data = {"badRequest": body} return json_response(data, status_code=status_code)
def get_commission(request, serial): data = request.GET client_key = unicode(request.component_instance) try: serial = int(serial) except ValueError: raise BadRequest("Serial should be an integer.") try: data = qh.get_commission(clientkey=client_key, serial=serial) status_code = 200 return json_response(data, status_code) except qh_exception.NoCommissionError: return HttpResponse(status=404)
def _provisions_to_list(provisions): lst = [] for provision in provisions: try: holder = provision['holder'] source = provision['source'] resource = provision['resource'] quantity = provision['quantity'] key = (holder, source, resource) lst.append((key, quantity)) if not is_integer(quantity): raise ValueError() except (TypeError, KeyError, ValueError): raise BadRequest("Malformed provision %s" % unicode(provision)) return lst