def setUp(self): super(TestRuleBase, self).setUp() self.context = utils.dummy_context() environment.global_env().register_rule('bilean.rule.dummy', DummyRule) self.spec = { 'type': 'bilean.rule.dummy', 'version': '1.0', 'properties': { 'key1': 'value1', 'key2': 2, } }
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()
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()
def test_init_using_specified_cloud_backend(self): plugin2 = mock.Mock() plugin2.compute = 'Compute2' plugin2.network = 'Network2' env = environment.global_env() env.register_driver('cloud_backend_2', plugin2) # Using specified cloud backend bd = driver_base.BileanDriver('cloud_backend_2') self.assertEqual('Compute2', bd.compute) self.assertEqual('Network2', bd.network)
def __init__(self, backend_name=None): if backend_name is None: backend_name = cfg.CONF.cloud_backend backend = environment.global_env().get_driver(backend_name) self.compute = backend.compute self.network = backend.network self.identity = backend.identity self.block_store = backend.block_store
def test_init_using_default_cloud_backend(self): plugin1 = mock.Mock() plugin1.compute = 'Compute1' plugin1.network = 'Network1' env = environment.global_env() env.register_driver('cloud_backend_1', plugin1) # Using default cloud backend defined in configure file cfg.CONF.set_override('cloud_backend', 'cloud_backend_1', enforce_type=True) bd = driver_base.BileanDriver() self.assertEqual('Compute1', bd.compute) self.assertEqual('Network1', bd.network)
def __new__(cls, name, spec, **kwargs): """Create a new rule of the appropriate class. :param name: The name for the rule. :param spec: A dictionary containing the spec for the rule. :param kwargs: Keyword arguments for rule creation. :returns: An instance of a specific sub-class of Rule. """ type_name, version = schema.get_spec_version(spec) if cls != Rule: RuleClass = cls else: RuleClass = environment.global_env().get_rule(type_name) return super(Rule, cls).__new__(RuleClass)
def topics_and_exchanges(self): topics_exchanges = set() plugins = environment.global_env().get_plugins() for plugin in plugins: try: topic_exchanges = plugin.get_notification_topics_exchanges() for plugin_topic in topic_exchanges: if isinstance(plugin_topic, basestring): raise Exception( _LE("Plugin %s should return a list of topic " "exchange pairs") % plugin.__class__.__name__) topics_exchanges.add(plugin_topic) except Exception as e: LOG.error(_LE("Failed to retrieve notification topic(s) " "and exchanges from bilean plugin " "%(ext)s: %(e)s") % {'ext': plugin.__name__, 'e': e}) return topics_exchanges