Beispiel #1
0
    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}
Beispiel #2
0
    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}
Beispiel #3
0
    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}