def dispatch_with_context(self, trigger, payload=None, trace_context=None): """ Method which dispatches the trigger. :param trigger: Full name / reference of the trigger. :type trigger: ``str`` :param payload: Trigger payload. :type payload: ``dict`` :param trace_context: Trace context to associate with Trigger. :type trace_context: ``st2common.api.models.api.trace.TraceContext`` """ # This means specified payload is complied with trigger_type schema, or not. is_valid = True try: validate_trigger_payload(trigger_type_ref=trigger, payload=payload) except (ValidationError, Exception) as e: is_valid = False self._logger.warn('Failed to validate payload (%s) for trigger "%s": %s' % (str(payload), trigger, str(e))) # If validation is disabled, still dispatch a trigger even if it failed validation # This condition prevents unexpected restriction. if not is_valid and cfg.CONF.system.validate_trigger_payload: self._logger.warn('Trigger payload validation failed and validation is enabled, not ' 'dispatching a trigger "%s" (%s)' % (trigger, str(payload))) return None self._logger.debug('Dispatching trigger %s with payload %s.', trigger, payload) self._dispatcher.dispatch(trigger, payload=payload, trace_context=trace_context)
def dispatch_with_context(self, trigger, payload=None, trace_context=None, throw_on_validation_error=False): """ Method which dispatches the trigger. :param trigger: Reference to the TriggerTypeDB (<pack>.<name>) or TriggerDB object. :type trigger: ``str`` :param payload: Trigger payload. :type payload: ``dict`` :param trace_context: Trace context to associate with Trigger. :type trace_context: ``st2common.api.models.api.trace.TraceContext`` :param throw_on_validation_error: True to throw on validation error (if validate_payload is True) instead of logging the error. :type throw_on_validation_error: ``boolean`` """ # Note: We perform validation even if it's disabled in the config so we can at least warn # the user if validation fals (but not throw if it's disabled) try: validate_trigger_payload( trigger_type_ref=trigger, payload=payload, throw_on_inexistent_trigger=True, ) except (ValidationError, ValueError, Exception) as e: self._logger.warn( 'Failed to validate payload (%s) for trigger "%s": %s' % (str(payload), trigger, six.text_type(e))) # If validation is disabled, still dispatch a trigger even if it failed validation # This condition prevents unexpected restriction. if cfg.CONF.system.validate_trigger_payload: msg = ( "Trigger payload validation failed and validation is enabled, not " 'dispatching a trigger "%s" (%s): %s' % (trigger, str(payload), six.text_type(e))) if throw_on_validation_error: raise ValueError(msg) self._logger.warn(msg) return None self._logger.debug("Dispatching trigger %s with payload %s.", trigger, payload) return self._dispatcher.dispatch(trigger, payload=payload, trace_context=trace_context)
def dispatch_with_context(self, trigger, payload=None, trace_context=None, throw_on_validation_error=False): """ Method which dispatches the trigger. :param trigger: Reference to the TriggerTypeDB (<pack>.<name>) or TriggerDB object. :type trigger: ``str`` :param payload: Trigger payload. :type payload: ``dict`` :param trace_context: Trace context to associate with Trigger. :type trace_context: ``st2common.api.models.api.trace.TraceContext`` :param throw_on_validation_error: True to throw on validation error (if validate_payload is True) instead of logging the error. :type throw_on_validation_error: ``boolean`` """ # Note: We perform validation even if it's disabled in the config so we can at least warn # the user if validation fals (but not throw if it's disabled) try: validate_trigger_payload(trigger_type_ref=trigger, payload=payload, throw_on_inexistent_trigger=True) except (ValidationError, ValueError, Exception) as e: self._logger.warn('Failed to validate payload (%s) for trigger "%s": %s' % (str(payload), trigger, six.text_type(e))) # If validation is disabled, still dispatch a trigger even if it failed validation # This condition prevents unexpected restriction. if cfg.CONF.system.validate_trigger_payload: msg = ('Trigger payload validation failed and validation is enabled, not ' 'dispatching a trigger "%s" (%s): %s' % (trigger, str(payload), six.text_type(e))) if throw_on_validation_error: raise ValueError(msg) self._logger.warn(msg) return None self._logger.debug('Dispatching trigger %s with payload %s.', trigger, payload) return self._dispatcher.dispatch(trigger, payload=payload, trace_context=trace_context)