def create_or_update_trigger_db(trigger): """ Create a new TriggerDB model if one doesn't exist yet or update existing one. :param trigger: Trigger info. :type trigger: ``dict`` """ assert isinstance(trigger, dict) existing_trigger_db = _get_trigger_db(trigger) if existing_trigger_db: is_update = True else: is_update = False trigger_api = TriggerAPI(**trigger) trigger_db = TriggerAPI.to_model(trigger_api) if is_update: trigger_db.id = existing_trigger_db.id trigger_db = Trigger.add_or_update(trigger_db) if is_update: LOG.audit('Trigger updated. Trigger=%s', trigger_db) else: LOG.audit('Trigger created. Trigger=%s', trigger_db) return trigger_db
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance']) } # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update( TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model( TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. liveaction = LiveAction.get( context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(liveaction) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(liveaction.status, LIVEACTION_STATUS_FAILED) execution = self._get_action_execution(liveaction__id=str( liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(execution.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual( execution.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(execution.rule, vars(RuleAPI.from_model(rule))) action = action_utils.get_action_by_ref(liveaction.action) self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction['callback'], liveaction.callback) self.assertEqual(execution.liveaction['action'], liveaction.action)
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance']) } # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update( TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model( TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. execution = ActionExecution.get( context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED) history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True) self.assertDictEqual(history.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(history.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual( history.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(history.rule, vars(RuleAPI.from_model(rule))) action_ref = ResourceReference.from_string_reference( ref=execution.action) action, _ = action_utils.get_action_by_dict({ 'name': action_ref.name, 'pack': action_ref.pack }) self.assertDictEqual(history.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner))) execution = ActionExecution.get_by_id(str(execution.id)) self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
def create_trigger_db(trigger): trigger_api = trigger if isinstance(trigger, dict): trigger_api = TriggerAPI(**trigger) trigger_db = get_trigger_db(trigger_api) if not trigger_db: trigger_db = TriggerAPI.to_model(trigger_api) LOG.debug('verified trigger and formulated TriggerDB=%s', trigger_db) trigger_db = Trigger.add_or_update(trigger_db) return trigger_db
def _get_trigger_api_given_rule(rule): trigger = rule.trigger triggertype_ref = ResourceReference.from_string_reference(trigger.get('type')) trigger_dict = {} trigger_name = trigger.get('name', None) if trigger_name: trigger_dict['name'] = trigger_name trigger_dict['pack'] = triggertype_ref.pack trigger_dict['type'] = triggertype_ref.ref trigger_dict['parameters'] = rule.trigger.get('parameters', {}) trigger_api = TriggerAPI(**trigger_dict) return trigger_api
def _setup_sample_rules(self): rules = [] # Rules for st2.test.trigger1 RULE_1 = { 'enabled': True, 'name': 'st2.test.rule1', 'trigger': { 'type': 'dummy_pack_1.st2.test.trigger1' }, 'criteria': { 'k1': { # Missing prefix 'trigger'. This rule won't match. 'pattern': 't1_p_v', 'type': 'equals' } }, 'action': { 'ref': 'sixpack.st2.test.action', 'parameters': { 'ip2': '{{rule.k1}}', 'ip1': '{{trigger.t1_p}}' } }, 'id': '23', 'description': '' } rule_api = RuleAPI(**RULE_1) rule_db = RuleAPI.to_model(rule_api) trigger_api = TriggerAPI(**rule_api.trigger) trigger_db = TriggerService.create_trigger_db(trigger_api) trigger_ref = reference.get_str_resource_ref_from_model(trigger_db) rule_db.trigger = trigger_ref rule_db = Rule.add_or_update(rule_db) rules.append(rule_db) RULE_2 = { # Rule should match. 'enabled': True, 'name': 'st2.test.rule2', 'trigger': { 'type': 'dummy_pack_1.st2.test.trigger1' }, 'criteria': { 'trigger.k1': { 'pattern': 't1_p_v', 'type': 'equals' } }, 'action': { 'ref': 'sixpack.st2.test.action', 'parameters': { 'ip2': '{{rule.k1}}', 'ip1': '{{trigger.t1_p}}' } }, 'id': '23', 'description': '' } rule_api = RuleAPI(**RULE_2) rule_db = RuleAPI.to_model(rule_api) rule_db.trigger = trigger_ref rule_db = Rule.add_or_update(rule_db) rules.append(rule_db) RULE_3 = { 'enabled': False, # Disabled rule shouldn't match. 'name': 'st2.test.rule3', 'trigger': { 'type': 'dummy_pack_1.st2.test.trigger1' }, 'criteria': { 'trigger.k1': { 'pattern': 't1_p_v', 'type': 'equals' } }, 'action': { 'ref': 'sixpack.st2.test.action', 'parameters': { 'ip2': '{{rule.k1}}', 'ip1': '{{trigger.t1_p}}' } }, 'id': '23', 'description': '' } rule_api = RuleAPI(**RULE_3) rule_db = RuleAPI.to_model(rule_api) rule_db.trigger = trigger_ref rule_db = Rule.add_or_update(rule_db) rules.append(rule_db) # Rules for st2.test.trigger2 RULE_4 = { 'enabled': True, 'name': 'st2.test.rule4', 'trigger': { 'type': 'dummy_pack_1.st2.test.trigger2' }, 'criteria': { 'trigger.k1': { 'pattern': 't1_p_v', 'type': 'equals' } }, 'action': { 'ref': 'sixpack.st2.test.action', 'parameters': { 'ip2': '{{rule.k1}}', 'ip1': '{{trigger.t1_p}}' } }, 'id': '23', 'description': '' } rule_api = RuleAPI(**RULE_4) rule_db = RuleAPI.to_model(rule_api) trigger_api = TriggerAPI(**rule_api.trigger) trigger_db = TriggerService.create_trigger_db(trigger_api) trigger_ref = reference.get_str_resource_ref_from_model(trigger_db) rule_db.trigger = trigger_ref rule_db = Rule.add_or_update(rule_db) rules.append(rule_db) return rules