Пример #1
0
    def to_model(cls, rule):
        name = getattr(rule, 'name', None)
        description = getattr(rule, 'description', None)

        # Create a trigger for the provided rule
        trigger_db = TriggerService.create_trigger_db_from_rule(rule)

        trigger = reference.get_str_resource_ref_from_model(trigger_db)
        criteria = dict(getattr(rule, 'criteria', {}))
        pack = getattr(rule, 'pack', DEFAULT_PACK_NAME)
        ref = ResourceReference.to_string_reference(pack=pack, name=name)

        # Validate criteria
        validator.validate_criteria(criteria)

        # Validate trigger parameters
        validator.validate_trigger_parameters(trigger_db=trigger_db)

        action = ActionExecutionSpecDB(ref=rule.action['ref'],
                                       parameters=rule.action['parameters'])

        enabled = rule.enabled
        tags = TagsHelper.to_model(getattr(rule, 'tags', []))

        model = cls.model(name=name,
                          description=description,
                          pack=pack,
                          ref=ref,
                          trigger=trigger,
                          criteria=criteria,
                          action=action,
                          enabled=enabled,
                          tags=tags)
        return model
Пример #2
0
Файл: rule.py Проект: hejin/st2
    def to_model(cls, rule):
        kwargs = {}
        kwargs['name'] = getattr(rule, 'name', None)
        kwargs['description'] = getattr(rule, 'description', None)

        # Create a trigger for the provided rule
        trigger_db = TriggerService.create_trigger_db_from_rule(rule)
        kwargs['trigger'] = reference.get_str_resource_ref_from_model(trigger_db)

        # Validate trigger parameters
        validator.validate_trigger_parameters(trigger_db=trigger_db)

        kwargs['pack'] = getattr(rule, 'pack', DEFAULT_PACK_NAME)
        kwargs['ref'] = ResourceReference.to_string_reference(pack=kwargs['pack'],
                                                              name=kwargs['name'])

        # Validate criteria
        kwargs['criteria'] = dict(getattr(rule, 'criteria', {}))
        validator.validate_criteria(kwargs['criteria'])

        kwargs['action'] = ActionExecutionSpecDB(ref=rule.action['ref'],
                                                 parameters=rule.action.get('parameters', {}))

        rule_type = dict(getattr(rule, 'type', {}))
        if rule_type:
            kwargs['type'] = RuleTypeSpecDB(ref=rule_type['ref'],
                                            parameters=rule_type.get('parameters', {}))

        kwargs['enabled'] = getattr(rule, 'enabled', False)
        kwargs['tags'] = TagsHelper.to_model(getattr(rule, 'tags', []))

        model = cls.model(**kwargs)
        return model
Пример #3
0
    def to_model(cls, rule):
        name = getattr(rule, 'name', None)
        description = getattr(rule, 'description', None)

        # Create a trigger for the provided rule
        trigger_db = TriggerService.create_trigger_db_from_rule(rule)

        trigger = reference.get_str_resource_ref_from_model(trigger_db)
        criteria = dict(getattr(rule, 'criteria', {}))
        pack = getattr(rule, 'pack', DEFAULT_PACK_NAME)
        ref = ResourceReference.to_string_reference(pack=pack, name=name)

        # Validate criteria
        validator.validate_criteria(criteria)

        # Validate trigger parameters
        validator.validate_trigger_parameters(trigger_db=trigger_db)

        action = ActionExecutionSpecDB(ref=rule.action['ref'],
                                       parameters=rule.action['parameters'])

        enabled = rule.enabled
        tags = TagsHelper.to_model(getattr(rule, 'tags', []))

        model = cls.model(name=name, description=description, pack=pack, ref=ref, trigger=trigger,
                          criteria=criteria, action=action, enabled=enabled, tags=tags)
        return model
Пример #4
0
    def test_create_trigger_db_from_rule_duplicate(self):
        test_fixtures = {
            'rules': ['cron_timer_rule_1.yaml', 'cron_timer_rule_2.yaml']
        }
        loader = FixturesLoader()
        fixtures = loader.load_fixtures(fixtures_pack='generic', fixtures_dict=test_fixtures)
        rules = fixtures['rules']

        trigger_db_ret_1 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_1.yaml']))
        self.assertTrue(trigger_db_ret_1 is not None)
        trigger_db_ret_2 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_2.yaml']))
        self.assertTrue(trigger_db_ret_2 is not None)
        self.assertEqual(trigger_db_ret_1, trigger_db_ret_2, 'Should reuse same trigger.')
        trigger_db = Trigger.get_by_id(trigger_db_ret_1.id)
        self.assertDictEqual(trigger_db.parameters,
                             rules['cron_timer_rule_1.yaml']['trigger']['parameters'])
Пример #5
0
 def to_model(cls, rule):
     model = super(cls, cls).to_model(rule)
     trigger_db = TriggerService.create_trigger_db_from_rule(rule)
     model.trigger = reference.get_str_resource_ref_from_model(trigger_db)
     model.criteria = dict(getattr(rule, 'criteria', {}))
     validator.validate_criteria(model.criteria)
     model.action = ActionExecutionSpecDB()
     model.action.ref = rule.action['ref']
     model.action.parameters = rule.action['parameters']
     model.enabled = rule.enabled
     model.tags = TagsHelper.to_model(getattr(rule, 'tags', []))
     return model
Пример #6
0
    def test_create_trigger_db_from_rule_duplicate(self):
        test_fixtures = {
            "rules": ["cron_timer_rule_1.yaml", "cron_timer_rule_2.yaml"]
        }
        loader = FixturesLoader()
        fixtures = loader.load_fixtures(fixtures_pack="generic",
                                        fixtures_dict=test_fixtures)
        rules = fixtures["rules"]

        trigger_db_ret_1 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules["cron_timer_rule_1.yaml"]))
        self.assertIsNotNone(trigger_db_ret_1)
        trigger_db_ret_2 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules["cron_timer_rule_2.yaml"]))
        self.assertIsNotNone(trigger_db_ret_2)
        self.assertEqual(trigger_db_ret_1, trigger_db_ret_2,
                         "Should reuse same trigger.")
        trigger_db = Trigger.get_by_id(trigger_db_ret_1.id)
        self.assertDictEqual(
            trigger_db.parameters,
            rules["cron_timer_rule_1.yaml"]["trigger"]["parameters"],
        )
Пример #7
0
 def to_model(cls, rule):
     model = super(cls, cls).to_model(rule)
     trigger_db = TriggerService.create_trigger_db_from_rule(rule)
     model.trigger = reference.get_str_resource_ref_from_model(trigger_db)
     model.criteria = dict(getattr(rule, 'criteria', {}))
     for oldkey, value in six.iteritems(model.criteria):
         newkey = oldkey.replace('.', u'\u2024')
         if oldkey != newkey:
             model.criteria[newkey] = value
             del model.criteria[oldkey]
     validator.validate_criteria(model.criteria)
     model.action = ActionExecutionSpecDB()
     model.action.ref = rule.action['ref']
     model.action.parameters = rule.action['parameters']
     model.enabled = rule.enabled
     return model
Пример #8
0
    def to_model(cls, rule):
        kwargs = {}
        kwargs["name"] = getattr(rule, "name", None)
        kwargs["description"] = getattr(rule, "description", None)

        # Validate trigger parameters
        # Note: This must happen before we create a trigger, otherwise create trigger could fail
        # with a cryptic error
        trigger = getattr(rule, "trigger", {})
        trigger_type_ref = trigger.get("type", None)
        parameters = trigger.get("parameters", {})

        validator.validate_trigger_parameters(
            trigger_type_ref=trigger_type_ref, parameters=parameters
        )

        # Create a trigger for the provided rule
        trigger_db = TriggerService.create_trigger_db_from_rule(rule)
        kwargs["trigger"] = reference.get_str_resource_ref_from_model(trigger_db)

        kwargs["pack"] = getattr(rule, "pack", DEFAULT_PACK_NAME)
        kwargs["ref"] = ResourceReference.to_string_reference(
            pack=kwargs["pack"], name=kwargs["name"]
        )

        # Validate criteria
        kwargs["criteria"] = dict(getattr(rule, "criteria", {}))
        validator.validate_criteria(kwargs["criteria"])

        kwargs["action"] = ActionExecutionSpecDB(
            ref=rule.action["ref"], parameters=rule.action.get("parameters", {})
        )

        rule_type = dict(getattr(rule, "type", {}))
        if rule_type:
            kwargs["type"] = RuleTypeSpecDB(
                ref=rule_type["ref"], parameters=rule_type.get("parameters", {})
            )

        kwargs["enabled"] = getattr(rule, "enabled", False)
        kwargs["context"] = getattr(rule, "context", dict())
        kwargs["tags"] = TagsHelper.to_model(getattr(rule, "tags", []))
        kwargs["metadata_file"] = getattr(rule, "metadata_file", None)

        model = cls.model(**kwargs)
        return model
Пример #9
0
    def to_model(cls, rule):
        kwargs = {}
        kwargs['name'] = getattr(rule, 'name', None)
        kwargs['description'] = getattr(rule, 'description', None)

        # Validate trigger parameters
        # Note: This must happen before we create a trigger, otherwise create trigger could fail
        # with a cryptic error
        trigger = getattr(rule, 'trigger', {})
        trigger_type_ref = trigger.get('type', None)
        parameters = trigger.get('parameters', {})

        validator.validate_trigger_parameters(
            trigger_type_ref=trigger_type_ref, parameters=parameters)

        # Create a trigger for the provided rule
        trigger_db = TriggerService.create_trigger_db_from_rule(rule)
        kwargs['trigger'] = reference.get_str_resource_ref_from_model(
            trigger_db)

        kwargs['pack'] = getattr(rule, 'pack', DEFAULT_PACK_NAME)
        kwargs['ref'] = ResourceReference.to_string_reference(
            pack=kwargs['pack'], name=kwargs['name'])

        # Validate criteria
        kwargs['criteria'] = dict(getattr(rule, 'criteria', {}))
        validator.validate_criteria(kwargs['criteria'])

        kwargs['action'] = ActionExecutionSpecDB(ref=rule.action['ref'],
                                                 parameters=rule.action.get(
                                                     'parameters', {}))

        rule_type = dict(getattr(rule, 'type', {}))
        if rule_type:
            kwargs['type'] = RuleTypeSpecDB(ref=rule_type['ref'],
                                            parameters=rule_type.get(
                                                'parameters', {}))

        kwargs['enabled'] = getattr(rule, 'enabled', False)
        kwargs['context'] = getattr(rule, 'context', dict())
        kwargs['tags'] = TagsHelper.to_model(getattr(rule, 'tags', []))
        kwargs['metadata_file'] = getattr(rule, 'metadata_file', None)

        model = cls.model(**kwargs)
        return model
Пример #10
0
Файл: rule.py Проект: nzlosh/st2
    def to_model(cls, rule):
        kwargs = {}
        kwargs['name'] = getattr(rule, 'name', None)
        kwargs['description'] = getattr(rule, 'description', None)

        # Validate trigger parameters
        # Note: This must happen before we create a trigger, otherwise create trigger could fail
        # with a cryptic error
        trigger = getattr(rule, 'trigger', {})
        trigger_type_ref = trigger.get('type', None)
        parameters = trigger.get('parameters', {})

        validator.validate_trigger_parameters(trigger_type_ref=trigger_type_ref,
                                              parameters=parameters)

        # Create a trigger for the provided rule
        trigger_db = TriggerService.create_trigger_db_from_rule(rule)
        kwargs['trigger'] = reference.get_str_resource_ref_from_model(trigger_db)

        kwargs['pack'] = getattr(rule, 'pack', DEFAULT_PACK_NAME)
        kwargs['ref'] = ResourceReference.to_string_reference(pack=kwargs['pack'],
                                                              name=kwargs['name'])

        # Validate criteria
        kwargs['criteria'] = dict(getattr(rule, 'criteria', {}))
        validator.validate_criteria(kwargs['criteria'])

        kwargs['action'] = ActionExecutionSpecDB(ref=rule.action['ref'],
                                                 parameters=rule.action.get('parameters', {}))

        rule_type = dict(getattr(rule, 'type', {}))
        if rule_type:
            kwargs['type'] = RuleTypeSpecDB(ref=rule_type['ref'],
                                            parameters=rule_type.get('parameters', {}))

        kwargs['enabled'] = getattr(rule, 'enabled', False)
        kwargs['context'] = getattr(rule, 'context', dict())
        kwargs['tags'] = TagsHelper.to_model(getattr(rule, 'tags', []))
        kwargs['metadata_file'] = getattr(rule, 'metadata_file', None)

        model = cls.model(**kwargs)
        return model
Пример #11
0
    def to_model(cls, rule):
        kwargs = {}
        kwargs['name'] = getattr(rule, 'name', None)
        kwargs['description'] = getattr(rule, 'description', None)

        # Create a trigger for the provided rule
        trigger_db = TriggerService.create_trigger_db_from_rule(rule)
        kwargs['trigger'] = reference.get_str_resource_ref_from_model(
            trigger_db)

        # Validate trigger parameters
        validator.validate_trigger_parameters(trigger_db=trigger_db)

        kwargs['pack'] = getattr(rule, 'pack', DEFAULT_PACK_NAME)
        kwargs['ref'] = ResourceReference.to_string_reference(
            pack=kwargs['pack'], name=kwargs['name'])

        # Validate criteria
        kwargs['criteria'] = dict(getattr(rule, 'criteria', {}))
        validator.validate_criteria(kwargs['criteria'])

        kwargs['action'] = ActionExecutionSpecDB(ref=rule.action['ref'],
                                                 parameters=rule.action.get(
                                                     'parameters', {}))

        rule_type = dict(getattr(rule, 'type', {}))
        if rule_type:
            kwargs['type'] = RuleTypeSpecDB(ref=rule_type['ref'],
                                            parameters=rule_type.get(
                                                'parameters', {}))

        kwargs['enabled'] = getattr(rule, 'enabled', False)
        kwargs['tags'] = TagsHelper.to_model(getattr(rule, 'tags', []))

        model = cls.model(**kwargs)
        return model