Example #1
0
 def _check_plugin_name(self, plugin_type, name):
     if name is None or name == "":
         msg = _('%s type name not specified') % plugin_type
         raise exception.InvalidPlugin(message=msg)
     elif not isinstance(name, six.string_types):
         msg = _('%s type name is not a string') % plugin_type
         raise exception.InvalidPlugin(message=msg)
Example #2
0
 def _check_plugin_name(self, plugin_type, name):
     if name is None or name == "":
         msg = _('%s type name not specified') % plugin_type
         raise exception.InvalidPlugin(message=msg)
     elif not isinstance(name, six.string_types):
         msg = _('%s type name is not a string') % plugin_type
         raise exception.InvalidPlugin(message=msg)
Example #3
0
    def __init__(self, name, trait_cfg):
        self.cfg = trait_cfg
        self.name = name

        type_name = trait_cfg.get('type', 'string')

        if 'fields' not in trait_cfg:
            raise EventDefinitionException(
                _("Required field in trait definition not specified: "
                  "'%s'") % 'fields', self.cfg)

        fields = trait_cfg['fields']
        if not isinstance(fields, six.string_types):
            # NOTE(mdragon): if not a string, we assume a list.
            if len(fields) == 1:
                fields = fields[0]
            else:
                fields = '|'.join('(%s)' % path for path in fields)
        try:
            self.fields = jsonpath_rw.parse(fields)
        except Exception as e:
            raise EventDefinitionException(
                _("Parse error in JSONPath specification "
                  "'%(jsonpath)s' for %(trait)s: %(err)s") %
                dict(jsonpath=fields, trait=name, err=e), self.cfg)
        self.trait_type = type_name
        if self.trait_type is None:
            raise EventDefinitionException(
                _("Invalid trait type '%(type)s' for trait %(trait)s") %
                dict(type=type_name, trait=name), self.cfg)
Example #4
0
    def policy_create(self, cnxt, name, rule_ids=None, metadata=None):
        """Create a new policy."""
        if len(policy_mod.Policy.load_all(cnxt, filters={'name': name})) > 0:
            msg = _("The policy (%(name)s) already exists."
                    ) % {"name": name}
            raise exception.BileanBadRequest(msg=msg)

        rules = []
        if rule_ids is not None:
            type_cache = []
            for rule_id in rule_ids:
                try:
                    rule = rule_base.Rule.load(cnxt, rule_id=rule_id)
                    if rule.type not in type_cache:
                        rules.append({'id': rule_id, 'type': rule.type})
                        type_cache.append(rule.type)
                    else:
                        msg = _("More than one rule in type: '%s', it's "
                                "not allowed.") % rule.type
                        raise exception.BileanBadRequest(msg=msg)
                except exception.RuleNotFound as ex:
                    raise exception.BileanBadRequest(msg=six.text_type(ex))

        kwargs = {
            'rules': rules,
            'metadata': metadata,
        }
        policy = policy_mod.Policy(name, **kwargs)
        policy.store(cnxt)
        LOG.info(_LI("Policy is created: %(id)s."), policy.id)
        return policy.to_dict()
Example #5
0
    def rule_create(self, cnxt, name, spec, metadata=None):
        if len(rule_base.Rule.load_all(cnxt, filters={'name': name})) > 0:
            msg = _("The rule (%(name)s) already exists."
                    ) % {"name": name}
            raise exception.BileanBadRequest(msg=msg)

        type_name, version = schema.get_spec_version(spec)
        try:
            plugin = environment.global_env().get_rule(type_name)
        except exception.RuleTypeNotFound:
            msg = _("The specified rule type (%(type)s) is not supported."
                    ) % {"type": type_name}
            raise exception.BileanBadRequest(msg=msg)

        LOG.info(_LI("Creating rule type: %(type)s, name: %(name)s."),
                 {'type': type_name, 'name': name})
        rule = plugin(name, spec, metadata=metadata)
        try:
            rule.validate()
        except exception.InvalidSpec as ex:
            msg = six.text_type(ex)
            LOG.error(_LE("Failed in creating rule: %s"), msg)
            raise exception.BileanBadRequest(msg=msg)

        rule.store(cnxt)
        LOG.info(_LI("Rule %(name)s is created: %(id)s."),
                 {'name': name, 'id': rule.id})
        return rule.to_dict()
Example #6
0
    def rule_create(self, cnxt, name, spec, metadata=None):
        if len(plugin_base.Rule.load_all(cnxt, filters={'name': name})) > 0:
            msg = _("The rule (%(name)s) already exists."
                    ) % {"name": name}
            raise exception.BileanBadRequest(msg=msg)

        type_name, version = schema.get_spec_version(spec)
        try:
            plugin = environment.global_env().get_plugin(type_name)
        except exception.RuleTypeNotFound:
            msg = _("The specified rule type (%(type)s) is not supported."
                    ) % {"type": type_name}
            raise exception.BileanBadRequest(msg=msg)

        LOG.info(_LI("Creating rule type: %(type)s, name: %(name)s."),
                 {'type': type_name, 'name': name})
        rule = plugin.RuleClass(name, spec, metadata=metadata)
        try:
            rule.validate()
        except exception.InvalidSpec as ex:
            msg = six.text_type(ex)
            LOG.error(_LE("Failed in creating rule: %s"), msg)
            raise exception.BileanBadRequest(msg=msg)

        rule.store(cnxt)
        LOG.info(_LI("Rule %(name)s is created: %(id)s."),
                 {'name': name, 'id': rule.id})
        return rule.to_dict()
Example #7
0
    def __init__(self, name, trait_cfg):
        self.cfg = trait_cfg
        self.name = name

        type_name = trait_cfg.get('type', 'string')

        if 'fields' not in trait_cfg:
            raise EventDefinitionException(
                _("Required field in trait definition not specified: "
                  "'%s'") % 'fields',
                self.cfg)

        fields = trait_cfg['fields']
        if not isinstance(fields, six.string_types):
            # NOTE(mdragon): if not a string, we assume a list.
            if len(fields) == 1:
                fields = fields[0]
            else:
                fields = '|'.join('(%s)' % path for path in fields)
        try:
            self.fields = jsonpath_rw.parse(fields)
        except Exception as e:
            raise EventDefinitionException(
                _("Parse error in JSONPath specification "
                  "'%(jsonpath)s' for %(trait)s: %(err)s")
                % dict(jsonpath=fields, trait=name, err=e), self.cfg)
        self.trait_type = type_name
        if self.trait_type is None:
            raise EventDefinitionException(
                _("Invalid trait type '%(type)s' for trait %(trait)s")
                % dict(type=type_name, trait=name), self.cfg)
Example #8
0
def validate_string(value, name=None, min_length=0, max_length=None,
                    available_fields=None):
    """Check the length of specified string

    :param value: the value of the string
    :param name: the name of the string
    :param min_length: the min_length of the string
    :param max_length: the max_length of the string
    """
    if not isinstance(value, six.string_types):
        if name is None:
            msg = _("The input is not a string or unicode")
        else:
            msg = _("%s is not a string or unicode") % name
        raise exception.InvalidInput(message=msg)

    if name is None:
        name = value

    if available_fields:
        if value not in available_fields:
            msg = _("%(name)s must be in %(fields)s") % {
                'name': name, 'fields': available_fields}
            raise exception.InvalidInput(message=msg)

    if len(value) < min_length:
        msg = _("%(name)s has a minimum character requirement of "
                "%(min_length)s.") % {'name': name, 'min_length': min_length}
        raise exception.InvalidInput(message=msg)

    if max_length and len(value) > max_length:
        msg = _("%(name)s has more than %(max_length)s "
                "characters.") % {'name': name, 'max_length': max_length}
        raise exception.InvalidInput(message=msg)
Example #9
0
def load_paste_app(app_name=None):
    """Builds and returns a WSGI app from a paste config file.

    We assume the last config file specified in the supplied ConfigOpts
    object is the paste config file.

    :param app_name: name of the application to load

    :raises RuntimeError when config file cannot be located or application
            cannot be loaded from config file
    """
    if app_name is None:
        app_name = cfg.CONF.prog

    conf_file = _get_deployment_config_file()
    if conf_file is None:
        raise RuntimeError(_("Unable to locate config file [%s]") %
                           cfg.CONF.paste_deploy['api_paste_config'])

    try:
        app = wsgi.paste_deploy_app(conf_file, app_name, cfg.CONF)

        # Log the options used when starting if we're in debug mode...
        if cfg.CONF.debug:
            cfg.CONF.log_opt_values(logging.getLogger(app_name),
                                    sys_logging.DEBUG)

        return app
    except (LookupError, ImportError) as e:
        raise RuntimeError(_("Unable to load %(app_name)s from "
                             "configuration file %(conf_file)s."
                             "\nGot: %(e)r") % {'app_name': app_name,
                                                'conf_file': conf_file,
                                                'e': e})
Example #10
0
    def policy_create(self, cnxt, name, rule_ids=None, metadata=None):
        """Create a new policy."""
        if len(policy_mod.Policy.load_all(cnxt, filters={'name': name})) > 0:
            msg = _("The policy (%(name)s) already exists."
                    ) % {"name": name}
            raise exception.BileanBadRequest(msg=msg)

        rules = []
        if rule_ids is not None:
            type_cache = []
            for rule_id in rule_ids:
                try:
                    rule = plugin_base.Rule.load(cnxt, rule_id=rule_id)
                    if rule.type not in type_cache:
                        rules.append({'id': rule_id, 'type': rule.type})
                        type_cache.append(rule.type)
                    else:
                        msg = _("More than one rule in type: '%s', it's "
                                "not allowed.") % rule.type
                        raise exception.BileanBadRequest(msg=msg)
                except exception.RuleNotFound as ex:
                    raise exception.BileanBadRequest(msg=six.text_type(ex))

        kwargs = {
            'rules': rules,
            'metadata': metadata,
        }
        policy = policy_mod.Policy(name, **kwargs)
        if not policy.is_default:
            default_policy = policy_mod.Policy.load_default(cnxt)
            if default_policy is None:
                policy.is_default = True
        policy.store(cnxt)
        LOG.info(_LI("Successfully create policy (%s)."), policy.id)
        return policy.to_dict()
Example #11
0
def validate_integer(value, name, min_value=None, max_value=None):
    """Make sure that value is a valid integer, potentially within range."""

    try:
        value = int(str(value))
    except (ValueError, UnicodeEncodeError):
        msg = _('%(value_name)s must be an integer')
        raise exception.InvalidInput(reason=(
            msg % {'value_name': name}))

    if min_value is not None:
        if value < min_value:
            msg = _('%(value_name)s must be >= %(min_value)d')
            raise exception.InvalidInput(
                reason=(msg % {'value_name': name,
                               'min_value': min_value}))
    if max_value is not None:
        if value > max_value:
            msg = _('%(value_name)s must be <= %(max_value)d')
            raise exception.InvalidInput(
                reason=(
                    msg % {'value_name': name,
                           'max_value': max_value})
            )
    return value
Example #12
0
    def do_recharge(self, context, value, recharge_type=None, timestamp=None,
                    metadata=None):
        """Recharge for user and update status.

        param context: The request context.
        param value: Recharge value.
        param recharge_type: Rechage type, 'Recharge'|'System bonus'.
        param timestamp: Record when recharge action occurs.
        param metadata: Some other keyword.
        """
        self.balance += utils.make_decimal(value)
        if self.status == self.INIT and self.balance > 0:
            self.status = self.FREE
            self.status_reason = "Recharged"
        elif self.status == self.FREEZE and self.balance > 0:
            reason = _("Status change from 'FREEZE' to 'FREE' because "
                       "of recharge.")
            self.status = self.FREE
            self.status_reason = reason
        elif self.status == self.WARNING:
            if not self._notify_or_not():
                reason = _("Status change from 'WARNING' to 'ACTIVE' because "
                           "of recharge.")
                self.status = self.ACTIVE
                self.status_reason = reason
        self.store(context)

        # Create recharge record
        values = {'user_id': self.id,
                  'value': value,
                  'type': recharge_type,
                  'timestamp': timestamp,
                  'metadata': metadata}
        db_api.recharge_create(context, values)
Example #13
0
    def update_rate(self, context, delta_rate, timestamp=None):
        """Update user's rate and update user status.

        :param context: The request context.
        :param delta_rate: Delta rate to change.
        :param timestamp: The time that resource action occurs.
        :param delayed_cost: User's action may be delayed by some reason,
                             adjust balance by delayed_cost.
        """

        # Settle account before update rate
        self._settle_account(context, timestamp=timestamp)

        old_rate = self.rate
        new_rate = old_rate + delta_rate
        if old_rate == 0 and new_rate > 0:
            # Set last_bill when status change to 'ACTIVE' from 'FREE'
            self.last_bill = timestamp or wallclock()
            reason = _("Status change to 'ACTIVE' cause resource creation.")
            self.status = self.ACTIVE
            self.status_reason = reason
        elif delta_rate < 0:
            if new_rate == 0 and self.balance >= 0:
                reason = _("Status change to 'FREE' because of resource "
                           "deletion.")
                self.status = self.FREE
                self.status_reason = reason
            elif self.status == self.WARNING and not self._notify_or_not():
                reason = _("Status change from 'WARNING' to 'ACTIVE' "
                           "because of resource deletion.")
                self.status = self.ACTIVE
                self.status_reason = reason
        self.rate = new_rate
        self.store(context)
Example #14
0
 def _freeze(self, context, reason=None):
     '''Freeze user when balance overdraft.'''
     LOG.info(_("Freeze user because of: %s") % reason)
     self._release_resource(context)
     LOG.info(_("Balance of user %s overdraft, change user's "
                "status to 'freeze'") % self.id)
     self.status = self.FREEZE
     self.status_reason = reason
Example #15
0
    def resolve_value(self, key):
        if key not in self:
            raise KeyError(_('Invalid spec item: "%s"') % key)

        schema_item = self._schema[key]
        if key in self._data:
            raw_value = self._data[key]
            return schema_item.resolve(raw_value)
        elif schema_item.has_default():
            return schema_item.get_default()
        elif schema_item.required:
            raise ValueError(_('Required spec item "%s" not assigned') % key)
Example #16
0
    def resolve_value(self, key):
        if key not in self:
            raise KeyError(_('Invalid spec item: "%s"') % key)

        schema_item = self._schema[key]
        if key in self._data:
            raw_value = self._data[key]
            return schema_item.resolve(raw_value)
        elif schema_item.has_default():
            return schema_item.get_default()
        elif schema_item.required:
            raise ValueError(_('Required spec item "%s" not assigned') % key)
Example #17
0
    def do_settle_account(self):
        try:
            flow_engine = bilean_flow.get_settle_account_flow(
                self.context, self.target, task=self.inputs.get('task'))
            with bilean_flow.DynamicLogListener(flow_engine, logger=LOG):
                flow_engine.run()
        except Exception as ex:
            LOG.error(_LE("Faied to execute action(%(action_id)s), error: "
                          "%(error_msg)s"), {"action_id": self.id,
                                             "error_msg": six.text_type(ex)})
            return self.RES_ERROR, _('Settle account failed.')

        return self.RES_OK, _('Settle account successfully.')
Example #18
0
def user_lock_acquire(context, user_id, action_id, engine=None,
                      forced=False):
    """Try to lock the specified user.

    :param context: the context used for DB operations;
    :param user_id: ID of the user to be locked.
    :param action_id: ID of the action that attempts to lock the user.
    :param engine: ID of the engine that attempts to lock the user.
    :param forced: set to True to cancel current action that owns the lock,
                   if any.
    :returns: True if lock is acquired, or False otherwise.
    """

    owner = db_api.user_lock_acquire(user_id, action_id)
    if action_id == owner:
        return True

    retries = cfg.CONF.lock_retry_times
    retry_interval = cfg.CONF.lock_retry_interval

    while retries > 0:
        sleep(retry_interval)
        LOG.debug(_('Acquire lock for user %s again'), user_id)
        owner = db_api.user_lock_acquire(user_id, action_id)
        if action_id == owner:
            return True
        retries = retries - 1

    if forced:
        owner = db_api.user_lock_steal(user_id, action_id)
        return action_id == owner

    action = db_api.action_get(context, owner)
    if (action and action.owner and action.owner != engine and
            is_engine_dead(context, action.owner)):
        LOG.info(_LI('The user %(u)s is locked by dead action %(a)s, '
                     'try to steal the lock.'), {
            'u': user_id,
            'a': owner
        })
        reason = _('Engine died when executing this action.')
        db_api.action_mark_failed(context, action.id, time.time(),
                                  reason=reason)
        db_api.user_lock_steal(user_id, action_id)
        return True

    LOG.error(_LE('User is already locked by action %(old)s, '
                  'action %(new)s failed grabbing the lock'),
              {'old': owner, 'new': action_id})

    return False
Example #19
0
    def do_create_resource(self):
        resource = plugin_base.Resource.from_dict(self.inputs)
        try:
            flow_engine = bilean_flow.get_create_resource_flow(
                self.context, self.target, resource)
            with bilean_flow.DynamicLogListener(flow_engine, logger=LOG):
                flow_engine.run()
        except Exception as ex:
            LOG.error(_LE("Faied to execute action(%(action_id)s), error: "
                          "%(error_msg)s"), {"action_id": self.id,
                                             "error_msg": six.text_type(ex)})
            return self.RES_ERROR, _('Resource creation failed.')

        return self.RES_OK, _('Resource creation successfully.')
Example #20
0
def get_spec_version(spec):
    if not isinstance(spec, dict):
        msg = _('The provided spec is not a map.')
        raise exception.SpecValidationFailed(message=msg)

    if 'type' not in spec:
        msg = _("The 'type' key is missing from the provided spec map.")
        raise exception.SpecValidationFailed(message=msg)

    if 'version' not in spec:
        msg = _("The 'version' key is missing from the provided spec map.")
        raise exception.SpecValidationFailed(message=msg)

    return (spec['type'], spec['version'])
Example #21
0
def get_spec_version(spec):
    if not isinstance(spec, dict):
        msg = _('The provided spec is not a map.')
        raise exception.SpecValidationFailed(message=msg)

    if 'type' not in spec:
        msg = _("The 'type' key is missing from the provided spec map.")
        raise exception.SpecValidationFailed(message=msg)

    if 'version' not in spec:
        msg = _("The 'version' key is missing from the provided spec map.")
        raise exception.SpecValidationFailed(message=msg)

    return (spec['type'], spec['version'])
Example #22
0
    def validate(self):
        '''Validate the schema.'''
        for (k, s) in self._schema.items():
            try:
                # validate through resolve
                self.resolve_value(k)
            except (TypeError, ValueError) as err:
                msg = _('Spec validation error (%(key)s): %(err)s') % dict(
                    key=k, err=six.text_type(err))
                raise exception.SpecValidationFailed(message=msg)

        for key in self._data:
            if key not in self._schema:
                msg = _('Unrecognizable spec item "%s"') % key
                raise exception.SpecValidationFailed(message=msg)
Example #23
0
    def validate(self):
        '''Validate the schema.'''
        for (k, s) in self._schema.items():
            try:
                # validate through resolve
                self.resolve_value(k)
            except (TypeError, ValueError) as err:
                msg = _('Spec validation error (%(key)s): %(err)s') % dict(
                    key=k, err=six.text_type(err))
                raise exception.SpecValidationFailed(message=msg)

        for key in self._data:
            if key not in self._schema:
                msg = _('Unrecognizable spec item "%s"') % key
                raise exception.SpecValidationFailed(message=msg)
Example #24
0
class InternalError(BileanException):
    '''A base class for internal exceptions in bilean.

    The internal exception classes which inherit from :class:`InternalError`
    class should be translated to a user facing exception type if need to be
    made user visible.
    '''
    msg_fmt = _('ERROR %(code)s happens for %(message)s.')
    message = _('Internal error happens')

    def __init__(self, **kwargs):
        super(InternalError, self).__init__(**kwargs)
        if 'code' in kwargs.keys():
            self.code = kwargs.get('code', 500)
            self.message = kwargs.get('message')
Example #25
0
 def get_driver(self, name):
     self._check_plugin_name('Driver', name)
     plugin = self.driver_registry.get_plugin(name)
     if plugin is None:
         msg = _('Driver plugin %(name)s is not found.') % {'name': name}
         raise exception.InvalidPlugin(message=msg)
     return plugin
Example #26
0
 def _get_action(self, event_type):
     available_actions = ['create', 'delete', 'update']
     for action in available_actions:
         if action in event_type:
             return action
     LOG.info(_("Can not get action info in event_type: %s") % event_type)
     return None
Example #27
0
    def validate(self, value, context=None):
        if not isinstance(value, collections.Mapping):
            raise TypeError(_('"%s" is not a Map') % value)

        for key, child in self.schema.items():
            item_value = value.get(key)
            child.validate(item_value, context)
Example #28
0
    def delete_server(self, server):
        '''Deletes a server and waits for it to disappear from Nova.'''
        if not server:
            return
        try:
            server.delete()
        except Exception as exc:
            self.ignore_not_found(exc)
            return

        while True:
            yield

            try:
                self.refresh_server(server)
            except Exception as exc:
                self.ignore_not_found(exc)
                break
            else:
                # Some clouds append extra (STATUS) strings to the status
                short_server_status = server.status.split('(')[0]
                if short_server_status in ("DELETED", "SOFT_DELETED"):
                    break
                if short_server_status == "ERROR":
                    fault = getattr(server, 'fault', {})
                    message = fault.get('message', 'Unknown')
                    code = fault.get('code')
                    errmsg = (_("Server %(name)s delete failed: (%(code)s) "
                                "%(message)s"))
                    raise exception.Error(errmsg % {"name": server.name,
                                                    "code": code,
                                                    "message": message})
Example #29
0
    def settle_account(self, context, task=None):
        '''Settle account for user.'''

        notifier = bilean_notifier.Notifier()
        timestamp = utils.make_decimal(wallclock())
        self._settle_account(context, timestamp=timestamp)

        if task == 'notify' and self._notify_or_not():
            self.status_reason = "The balance is almost used up"
            self.status = self.WARNING
            # Notify user
            msg = {'user': self.id, 'notification': self.status_reason}
            notifier.info('billing.notify', msg)
        elif task == 'freeze' and self.balance <= 0:
            reason = _("Balance overdraft")
            LOG.info(_LI("Freeze user %(user_id)s, reason: %(reason)s"),
                     {'user_id': self.id, 'reason': reason})
            resources = plugin_base.Resource.load_all(
                context, user_id=self.id, project_safe=False)
            for resource in resources:
                resource.do_delete(context, timestamp=timestamp)
            self.rate = 0
            self.status = self.FREEZE
            self.status_reason = reason
            # Notify user
            msg = {'user': self.id, 'notification': self.status_reason}
            notifier.info('billing.notify', msg)

        self.store(context)
Example #30
0
 def resolve(self, value):
     try:
         return int(value)
     except (TypeError, ValueError):
         msg = _('The value "%s" cannot be converted into an '
                 'integer.') % value
         raise exception.SpecValidationFailed(message=msg)
Example #31
0
    def __init__(self, definition_cfg):
        self._included_types = []
        self._excluded_types = []
        self.cfg = definition_cfg

        try:
            event_type = definition_cfg['event_type']
            self.resources = [
                ResourceDefinition(resource_def)
                for resource_def in definition_cfg['resources']
            ]
        except KeyError as err:
            raise EventDefinitionException(
                _("Required field %s not specified") % err.args[0], self.cfg)

        if isinstance(event_type, six.string_types):
            event_type = [event_type]

        for t in event_type:
            if t.startswith('!'):
                self._excluded_types.append(t[1:])
            else:
                self._included_types.append(t)

        if self._excluded_types and not self._included_types:
            self._included_types.append('*')
Example #32
0
 def _get_action(self, event_type):
     available_actions = ['create', 'delete', 'update']
     for action in available_actions:
         if action in event_type:
             return action
     LOG.info(_("Can not get action info in event_type: %s") % event_type)
     return None
Example #33
0
 def _parse_and_validate(self):
     for key in self.data.keys():
         if key not in ['id', 'user_id', 'resource_type']:
             self.properties[key] = self.data[key]
     if not self.id:
         msg = _("Id of resource can not be None")
         raise exception.InvalidResource(msg=msg)
     if not self.user_id:
         msg = _("User_id of resource can not be None")
         raise exception.InvalidResource(msg=msg)
     if not self.resource_type:
         msg = _("Resource_type of resource can not be None")
         raise exception.InvalidResource(msg=msg)
     if not self.properties:
         msg = _("Properties of resource can not be empty")
         raise exception.InvalidResource(msg=msg)
Example #34
0
def record(context, user_id, action=None, seconds=0, value=0):
    """Generate events for specify user

    :param context: oslo.messaging.context
    :param user_id: ID of user to mark event
    :param action: action of event, include 'charge' and 'recharge'
    :param seconds: use time length, needed when action is 'charge'
    :param value: value of recharge, needed when action is 'recharge'
    """
    try:
        if action == 'charge':
            resources = bilean_resources.resource_get_all(
                context, user_id=user_id)
            for resource in resources:
                usage = resource['rate'] * seconds
                event = Event(timeutils.utcnow(),
                              user_id=user_id,
                              action=action,
                              resource_type=resource['resource_type'],
                              value=usage)
                event.store(context)
        else:
            event = Event(timeutils.utcnow(),
                          user_id=user_id,
                          action=action,
                          value=value)
            event.store(context)
    except Exception as exc:
        LOG.error(_("Error generate events: %s") % six.text_type(exc))
Example #35
0
 def resolve(self, value):
     try:
         return int(value)
     except (TypeError, ValueError):
         msg = _('The value "%s" cannot be converted into an '
                 'integer.') % value
         raise exception.SpecValidationFailed(message=msg)
Example #36
0
    def policy_update(self, cnxt, policy_id, name=None, metadata=None,
                      is_default=None):
        LOG.info(_LI("Updating policy: '%(id)s'"), {'id': policy_id})

        policy = policy_mod.Policy.load(cnxt, policy_id=policy_id)
        changed = False
        if name is not None and name != policy.name:
            policies = policy_mod.Policy.load_all(cnxt, filters={'name': name})
            if len(policies) > 0:
                msg = _("The policy (%(name)s) already exists."
                        ) % {"name": name}
                raise exception.BileanBadRequest(msg=msg)
            policy.name = name
            changed = True
        if metadata is not None and metadata != policy.metadata:
            policy.metadata = metadata
            changed = True
        if is_default is not None and is_default != policy.is_default:
            is_default = utils.parse_bool_param('is_default', is_default)
            if is_default:
                # Set policy to default should unset old default policy.
                policies = policy_mod.load_all(cnxt,
                                               filters={'is_default': True})
                if len(policies) == 1:
                    default_policy = policies[0]
                    default_policy.is_default = False
                    default_policy.store(cnxt)
            policy.is_default = is_default
            changed = True

        if changed:
            policy.store(cnxt)

        LOG.info(_LI("Policy '%(id)s' is updated."), {'id': policy_id})
        return policy.to_dict()
Example #37
0
    def get_image_id_by_name(self, image_identifier):
        '''Return an id for the specified image name.

        :param image_identifier: image name
        :returns: the id of the requested :image_identifier:
        :raises: exception.ImageNotFound,
                 exception.PhysicalResourceNameAmbiguity
        '''
        try:
            filters = {'name': image_identifier}
            image_list = list(self.client().images.list(filters=filters))
        except exc.ClientException as ex:
            raise exception.Error(
                _("Error retrieving image list from glance: %s") % ex)
        num_matches = len(image_list)
        if num_matches == 0:
            LOG.info(_LI("Image %s was not found in glance"), image_identifier)
            raise exception.ImageNotFound(image_name=image_identifier)
        elif num_matches > 1:
            LOG.info(_LI("Multiple images %s were found in glance with name"),
                     image_identifier)
            raise exception.PhysicalResourceNameAmbiguity(
                name=image_identifier)
        else:
            return image_list[0].id
Example #38
0
def record(context, user_id, action=None, seconds=0, value=0):
    """Generate events for specify user

    :param context: oslo.messaging.context
    :param user_id: ID of user to mark event
    :param action: action of event, include 'charge' and 'recharge'
    :param seconds: use time length, needed when action is 'charge'
    :param value: value of recharge, needed when action is 'recharge'
    """
    try:
        if action == 'charge':
            resources = bilean_resources.resource_get_all(context,
                                                          user_id=user_id)
            for resource in resources:
                usage = resource['rate'] * seconds
                event = Event(timeutils.utcnow(),
                              user_id=user_id,
                              action=action,
                              resource_type=resource['resource_type'],
                              value=usage)
                event.store(context)
        else:
            event = Event(timeutils.utcnow(),
                          user_id=user_id,
                          action=action,
                          value=value)
            event.store(context)
    except Exception as exc:
        LOG.error(_("Error generate events: %s") % six.text_type(exc))
Example #39
0
    def delete_server(self, server):
        '''Deletes a server and waits for it to disappear from Nova.'''
        if not server:
            return
        try:
            server.delete()
        except Exception as exc:
            self.ignore_not_found(exc)
            return

        while True:
            yield

            try:
                self.refresh_server(server)
            except Exception as exc:
                self.ignore_not_found(exc)
                break
            else:
                # Some clouds append extra (STATUS) strings to the status
                short_server_status = server.status.split('(')[0]
                if short_server_status in ("DELETED", "SOFT_DELETED"):
                    break
                if short_server_status == "ERROR":
                    fault = getattr(server, 'fault', {})
                    message = fault.get('message', 'Unknown')
                    code = fault.get('code')
                    errmsg = (_("Server %(name)s delete failed: (%(code)s) "
                                "%(message)s"))
                    raise exception.Error(errmsg % {
                        "name": server.name,
                        "code": code,
                        "message": message
                    })
Example #40
0
    def resolve(self, value, context=None):
        if not isinstance(value, collections.Sequence):
            raise TypeError(_('"%s" is not a List') % value)

        return [v[1] for v in self._get_children(enumerate(value),
                                                 list(range(len(value))),
                                                 context)]
Example #41
0
    def validate(self, value, context=None):
        if not isinstance(value, collections.Mapping):
            raise TypeError(_('"%s" is not a Map') % value)

        for key, child in self.schema.items():
            item_value = value.get(key)
            child.validate(item_value, context)
Example #42
0
    def get_image_id_by_name(self, image_identifier):
        '''Return an id for the specified image name.

        :param image_identifier: image name
        :returns: the id of the requested :image_identifier:
        :raises: exception.ImageNotFound,
                 exception.PhysicalResourceNameAmbiguity
        '''
        try:
            filters = {'name': image_identifier}
            image_list = list(self.client().images.list(filters=filters))
        except exc.ClientException as ex:
            raise exception.Error(
                _("Error retrieving image list from glance: %s") % ex)
        num_matches = len(image_list)
        if num_matches == 0:
            LOG.info(_LI("Image %s was not found in glance"),
                     image_identifier)
            raise exception.ImageNotFound(image_name=image_identifier)
        elif num_matches > 1:
            LOG.info(_LI("Multiple images %s were found in glance with name"),
                     image_identifier)
            raise exception.PhysicalResourceNameAmbiguity(
                name=image_identifier)
        else:
            return image_list[0].id
Example #43
0
    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)
Example #44
0
    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)
Example #45
0
 def get_driver(self, name):
     self._check_plugin_name('Driver', name)
     plugin = self.driver_registry.get_plugin(name)
     if plugin is None:
         msg = _('Driver plugin %(name)s is not found.') % {'name': name}
         raise exception.InvalidPlugin(message=msg)
     return plugin
Example #46
0
class BileanException(Exception):
    '''Base Bilean Exception.

    To correctly use this class, inherit from it and define
    a 'msg_fmt' property. That msg_fmt will get printf'd
    with the keyword arguments provided to the constructor.
    '''

    message = _("An unknown exception occurred.")

    def __init__(self, **kwargs):
        self.kwargs = kwargs

        try:
            self.message = self.msg_fmt % kwargs
        except KeyError:
            # exc_info = sys.exc_info()
            # if kwargs doesn't match a variable in the message
            # log the issue and the kwargs
            LOG.exception(_LE('Exception in string format operation'))
            for name, value in six.iteritems(kwargs):
                LOG.error("%s: %s" % (name, value))  # noqa

            if _FATAL_EXCEPTION_FORMAT_ERRORS:
                raise
                # raise exc_info[0], exc_info[1], exc_info[2]

    def __str__(self):
        return six.text_type(self.message)

    def __unicode__(self):
        return six.text_type(self.message)

    def __deepcopy__(self, memo):
        return self.__class__(**self.kwargs)
Example #47
0
    def policy_update(self, cnxt, policy_id, name=None, metadata=None,
                      is_default=None):
        LOG.info(_LI("Updating policy: '%(id)s'"), {'id': policy_id})

        policy = policy_mod.Policy.load(cnxt, policy_id=policy_id)
        changed = False
        if name is not None and name != policy.name:
            policies = policy_mod.Policy.load_all(cnxt, filters={'name': name})
            if len(policies) > 0:
                msg = _("The policy (%(name)s) already exists."
                        ) % {"name": name}
                raise exception.BileanBadRequest(msg=msg)
            policy.name = name
            changed = True
        if metadata is not None and metadata != policy.metadata:
            policy.metadata = metadata
            changed = True
        if is_default is not None and is_default != policy.is_default:
            is_default = utils.parse_bool_param('is_default', is_default)
            if is_default:
                # Set policy to default should unset old default policy.
                policies = policy_mod.load_all(cnxt,
                                               filters={'is_default': True})
                if len(policies) == 1:
                    default_policy = policies[0]
                    default_policy.is_default = False
                    default_policy.store(cnxt)
            policy.is_default = is_default
            changed = True

        if changed:
            policy.store(cnxt)

        LOG.info(_LI("Policy '%(id)s' is updated."), {'id': policy_id})
        return policy.to_dict()
Example #48
0
    def resolve(self, value, context=None):
        if not isinstance(value, collections.Sequence):
            raise TypeError(_('"%s" is not a List') % value)

        return [v[1] for v in self._get_children(enumerate(value),
                                                 list(range(len(value))),
                                                 context)]
Example #49
0
def ActionProc(context, action_id):
    '''Action process.'''

    action = Action.load(context, action_id=action_id)
    if action is None:
        LOG.error(_LE('Action "%s" could not be found.'), action_id)
        return False

    reason = 'Action completed'
    success = True
    try:
        result, reason = action.execute()
    except Exception as ex:
        result = action.RES_ERROR
        reason = six.text_type(ex)
        LOG.exception(_('Unexpected exception occurred during action '
                        '%(action)s (%(id)s) execution: %(reason)s'),
                      {'action': action.action, 'id': action.id,
                       'reason': reason})
        success = False
    finally:
        # NOTE: locks on action is eventually released here by status update
        action.set_status(result, reason)

    return success
Example #50
0
 def _parse_and_validate(self):
     for key in self.data.keys():
         if key not in ['resource_ref', 'user_id', 'resource_type']:
             self.properties[key] = self.data[key]
     if not self.id:
         msg = _("Id of resource can not be None")
         raise exception.InvalidResource(msg=msg)
     if not self.user_id:
         msg = _("User_id of resource can not be None")
         raise exception.InvalidResource(msg=msg)
     if not self.resource_type:
         msg = _("Resource_type of resource can not be None")
         raise exception.InvalidResource(msg=msg)
     if not self.properties:
         msg = _("Properties of resource can not be empty")
         raise exception.InvalidResource(msg=msg)
Example #51
0
def get_socket(conf, default_port):
    '''Bind socket to bind ip:port in conf

    :param conf: a cfg.ConfigOpts object
    :param default_port: port to bind to if none is specified in conf

    :returns : a socket object as returned from socket.listen or
               ssl.wrap_socket if conf specifies cert_file
    '''

    bind_addr = get_bind_addr(conf, default_port)

    # TODO(jaypipes): eventlet's greened socket module does not actually
    # support IPv6 in getaddrinfo(). We need to get around this in the
    # future or monitor upstream for a fix
    address_family = [
        addr[0] for addr in socket.getaddrinfo(
            bind_addr[0], bind_addr[1], socket.AF_UNSPEC, socket.SOCK_STREAM)
        if addr[0] in (socket.AF_INET, socket.AF_INET6)
    ][0]

    cert_file = conf.cert_file
    key_file = conf.key_file
    use_ssl = cert_file or key_file
    if use_ssl and (not cert_file or not key_file):
        raise RuntimeError(
            _("When running server in SSL mode, you must "
              "specify both a cert_file and key_file "
              "option value in your configuration file"))

    sock = None
    retry_until = time.time() + 30
    while not sock and time.time() < retry_until:
        try:
            sock = eventlet.listen(bind_addr,
                                   backlog=conf.backlog,
                                   family=address_family)
        except socket.error as err:
            if err.args[0] != errno.EADDRINUSE:
                raise
            eventlet.sleep(0.1)

    if not sock:
        raise RuntimeError(
            _("Could not bind to %(bind_addr)s after trying "
              " 30 seconds") % {'bind_addr': bind_addr})
    return sock
Example #52
0
    def validate(self, value, context=None):
        if not isinstance(value, six.string_types):
            msg = _('The value "%s" cannot be converted into a '
                    'string.') % value
            raise exception.SpecValidationFailed(message=msg)

        self.resolve(value)
        self.validate_constraints(value, self, context)
Example #53
0
def validate_resource(resource):
    """Make sure that resource is valid"""

    if not is_valid_body(resource):
        msg = _("%s is not a dict") % resource
        raise exception.InvalidInput(message=msg)
    if resource['resource_type']:
        validate_string(resource['resource_type'],
                        available_fields=consts.RESOURCE_TYPES)
    else:
        msg = _('Expected resource_type field for resource')
        raise exception.InvalidInput(reason=msg)
    if resource['value']:
        validate_integer(resource['value'], 'resource_value', min_value=1)
    else:
        msg = _('Expected resource value field for resource')
        raise exception.InvalidInput(reason=msg)
Example #54
0
    def validate(self, value, context=None):
        if not isinstance(value, six.string_types):
            msg = _('The value "%s" cannot be converted into a '
                    'string.') % value
            raise exception.SpecValidationFailed(message=msg)

        self.resolve(value)
        self.validate_constraints(value, self, context)
Example #55
0
    def meta_serialize(self, metadata):
        """Serialize non-string metadata values before sending them to Nova."""
        if not isinstance(metadata, collections.Mapping):
            raise exception.StackValidationFailed(
                message=_("nova server metadata needs to be a Map."))

        return dict((key, (value if isinstance(value, six.string_types
                                               ) else json.dumps(value)))
                    for (key, value) in metadata.items())
Example #56
0
    def to_schema_type(self, value):
        if isinstance(value, six.integer_types):
            return value
        try:
            num = int(value)
        except ValueError:
            raise ValueError(_('%s is not an intger.') % num)

        return num
Example #57
0
    def _validate_default(self, context):
        if self.default is None:
            return

        try:
            self.validate(self.default, context)
        except (ValueError, TypeError) as exc:
            raise exception.InvalidSchemaError(
                message=_('Invalid default %(default)s (%(exc)s)') %
                dict(default=self.default, exc=exc))