def validate_creation(self, req, body): """Validate resources creation :param user_id: Id of user to validate :param body: dict body include resources and count :return True|False """ if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() resources = body.get('resources') if not resources: msg = _("Resources is empty") raise exc.HTTPBadRequest(explanation=msg) count = body.get('count') if count: try: validator.validate_integer(count, 'count', consts.MIN_RESOURCE_NUM, consts.MAX_RESOURCE_NUM) except exception.InvalidInput as e: raise exc.HTTPBadRequest(explanation=e.format_message()) try: for resource in resources: validator.validate_resource(resource) except exception.InvalidInput as e: raise exc.HTTPBadRequest(explanation=e.format_message()) except Exception as e: raise exc.HTTPBadRequest(explanation=e) return self.rpc_client.validate_creation(req.context, body)
def validate_creation(self, req, body): """Validate resources creation :param user_id: Id of user to validate :param body: dict body include resources and count :return True|False """ if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() resources = body.get('resources', None) if not resources: msg = _("Resources is empty") raise exc.HTTPBadRequest(explanation=msg) if body.get('count', None): try: validator.validate_integer(body.get('count'), 'count', consts.MIN_RESOURCE_NUM, consts.MAX_RESOURCE_NUM) except exception.InvalidInput as e: raise exc.HTTPBadRequest(explanation=e.format_message()) try: for resource in resources: validator.validate_resource(resource) except exception.InvalidInput as e: raise exc.HTTPBadRequest(explanation=e.format_message()) except Exception as e: raise exc.HTTPBadRequest(explanation=e) return self.rpc_client.validate_creation(req.context, body)
def create(self, req, body): """Create a new policy""" if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() policy_data = body.get('policy', None) if policy_data is None: raise exc.HTTPBadRequest( _("Malformed request data, missing " "'policy' key in request body.")) data = PolicyData(policy_data) policy = self.rpc_client.policy_create(req.context, data.name(), data.rules(), data.metadata()) return {'policy': policy}
def create(self, req, body): """Create a new rule""" if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() rule_data = body.get('rule', None) if rule_data is None: raise exc.HTTPBadRequest(_("Malformed request data, missing " "'rule' key in request body.")) data = RuleData(rule_data) rule = self.rpc_client.rule_create(req.context, data.name(), data.spec(), data.metadata()) return {'rule': rule}
def create(self, req, body): """Create a new policy""" if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() policy_data = body.get('policy', None) if policy_data is None: raise exc.HTTPBadRequest(_("Malformed request data, missing " "'policy' key in request body.")) data = PolicyData(policy_data) policy = self.rpc_client.policy_create(req.context, data.name(), data.rules(), data.metadata()) return {'policy': policy}
def update(self, req, policy_id, body): if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() policy_data = body.get('policy', None) if policy_data is None: raise exc.HTTPBadRequest(_("Malformed request data, missing " "'policy' key in request body.")) name = policy_data.get(consts.POLICY_NAME, None) metadata = policy_data.get(consts.POLICY_METADATA, None) is_default = policy_data.get(consts.POLICY_IS_DEFAULT, None) policy = self.rpc_client.policy_update(req.context, policy_id, name, metadata, is_default) return {'policy': policy}
def update(self, req, policy_id, body): if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() policy_data = body.get('policy', None) if policy_data is None: raise exc.HTTPBadRequest( _("Malformed request data, missing " "'policy' key in request body.")) name = policy_data.get(consts.POLICY_NAME, None) metadata = policy_data.get(consts.POLICY_METADATA, None) is_default = policy_data.get(consts.POLICY_IS_DEFAULT, None) policy = self.rpc_client.policy_update(req.context, policy_id, name, metadata, is_default) return {'policy': policy}
def recharge(self, req, user_id, body): """Recharge for a specify user :param user_id: Id of user to recharge """ if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() value = body.get('value', None) if value is None: raise exc.HTTPBadRequest(_("Malformed request data, missing " "'value' key in request body.")) try: validator.validate_float(value, 'recharge_value', consts.MIN_VALUE, consts.MAX_VALUE) except exception.InvalidInput as e: raise exc.HTTPBadRequest(explanation=e.format_message()) user = self.rpc_client.user_recharge(req.context, user_id, value) return {'user': user}
def recharge(self, req, user_id, body): """Recharge for a specify user :param user_id: Id of user to recharge """ if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() value = body.get('value', None) if value is None: raise exc.HTTPBadRequest( _("Malformed request data, missing " "'value' key in request body.")) try: validator.validate_float(value, 'recharge_value', consts.MIN_VALUE, consts.MAX_VALUE) except exception.InvalidInput as e: raise exc.HTTPBadRequest(explanation=e.format_message()) user = self.rpc_client.user_recharge(req.context, user_id, value) return {'user': user}
def action(self, req, user_id, body=None): """Perform specified action on a user.""" if not validator.is_valid_body(body): raise exc.HTTPUnprocessableEntity() if len(body) < 1: raise exc.HTTPBadRequest(_('No action specified')) if len(body) > 1: raise exc.HTTPBadRequest(_('Multiple actions specified')) action = list(body.keys())[0] if action not in self.SUPPORTED_ACTIONS: msg = _("Unrecognized action '%s' specified") % action raise exc.HTTPBadRequest(msg) if action == self.ATTACH_POLICY: policy = body.get(action).get('policy') if policy is None: raise exc.HTTPBadRequest(_("Malformed request data, no policy " "specified to attach.")) user = self.rpc_client.user_attach_policy( req.context, user_id, policy) elif action == self.RECHARGE: value = body.get(action).get('value') if value is None: raise exc.HTTPBadRequest(_("Malformed request data, missing " "'value' key in request body.")) try: validator.validate_float(value, 'recharge_value', consts.MIN_VALUE, consts.MAX_VALUE) except exception.InvalidInput as e: raise exc.HTTPBadRequest(explanation=e.format_message()) user = self.rpc_client.user_recharge(req.context, user_id, value) return {'user': user}