本文整理汇总了Python中st2common.persistence.trigger.Trigger类的典型用法代码示例。如果您正苦于以下问题:Python Trigger类的具体用法?Python Trigger怎么用?Python Trigger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Trigger类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_register_rules
def test_register_rules(self):
# Verify DB is empty at the beginning
self.assertEqual(len(Rule.get_all()), 0)
self.assertEqual(len(Trigger.get_all()), 0)
registrar = RulesRegistrar()
registrar.register_from_packs(base_dirs=[PACKS_DIR])
# Verify modeles are created
rule_dbs = Rule.get_all()
trigger_dbs = Trigger.get_all()
self.assertEqual(len(rule_dbs), 2)
self.assertEqual(len(trigger_dbs), 1)
self.assertEqual(rule_dbs[0].name, 'sample.with_the_same_timer')
self.assertEqual(rule_dbs[1].name, 'sample.with_timer')
self.assertTrue(trigger_dbs[0].name is not None)
# Verify second register call updates existing models
registrar.register_from_packs(base_dirs=[PACKS_DIR])
rule_dbs = Rule.get_all()
trigger_dbs = Trigger.get_all()
self.assertEqual(len(rule_dbs), 2)
self.assertEqual(len(trigger_dbs), 1)
示例2: test_create_or_update_trigger_db_simple_triggers
def test_create_or_update_trigger_db_simple_triggers(self):
test_fixtures = {
'triggertypes': ['triggertype1.yaml']
}
loader = FixturesLoader()
fixtures = loader.save_fixtures_to_db(fixtures_pack='generic', fixtures_dict=test_fixtures)
triggertypes = fixtures['triggertypes']
trigger_type_ref = ResourceReference.to_string_reference(
name=triggertypes['triggertype1.yaml']['name'],
pack=triggertypes['triggertype1.yaml']['pack'])
trigger = {
'name': triggertypes['triggertype1.yaml']['name'],
'pack': triggertypes['triggertype1.yaml']['pack'],
'type': trigger_type_ref
}
trigger_service.create_or_update_trigger_db(trigger)
triggers = Trigger.get_all()
self.assertTrue(len(triggers) == 1, 'Only one trigger should be created.')
self.assertTrue(triggers[0]['name'] == triggertypes['triggertype1.yaml']['name'])
# Try adding duplicate
trigger_service.create_or_update_trigger_db(trigger)
triggers = Trigger.get_all()
self.assertTrue(len(triggers) == 1, 'Only one trigger should be present.')
self.assertTrue(triggers[0]['name'] == triggertypes['triggertype1.yaml']['name'])
示例3: test_existing_rules_are_loaded_on_start
def test_existing_rules_are_loaded_on_start(self):
# Assert that we dispatch message for every existing Trigger object
St2Timer._handle_create_trigger = mock.Mock()
timer = St2Timer()
timer._scheduler = mock.Mock()
timer._trigger_watcher.run = mock.Mock()
# Verify there are no Trigger and TriggerType in the db wh:w
self.assertItemsEqual(Trigger.get_all(), [])
self.assertItemsEqual(TriggerType.get_all(), [])
# Add a dummy timer Trigger object
type_ = TIMER_TRIGGER_TYPES.keys()[0]
parameters = {'unit': 'seconds', 'delta': 1000}
trigger_db = TriggerDB(id=bson.ObjectId(), name='test_trigger_1', pack='dummy',
type=type_, parameters=parameters)
trigger_db = Trigger.add_or_update(trigger_db)
# Verify object has been added
self.assertEqual(len(Trigger.get_all()), 1)
timer.start()
timer._trigger_watcher._load_thread.wait()
# Verify handlers are called
timer._handle_create_trigger.assert_called_with(trigger_db)
示例4: _setup_sample_trigger
def _setup_sample_trigger(self, name):
trigtype = TriggerTypeDB(name=name, pack='dummy_pack_1', payload_schema={},
parameters_schema={})
TriggerType.add_or_update(trigtype)
created = TriggerDB(name=name, pack='dummy_pack_1', type=trigtype.get_reference().ref,
parameters={})
Trigger.add_or_update(created)
示例5: cleanup_trigger_db_for_rule
def cleanup_trigger_db_for_rule(rule_db):
# rule.trigger is actually trigger_db ref.
existing_trigger_db = get_trigger_db_by_ref(rule_db.trigger)
if not existing_trigger_db or not existing_trigger_db.parameters:
# nothing to be done here so moving on.
LOG.debug('ref_count decrement for %s not required.', existing_trigger_db)
return
Trigger.update(existing_trigger_db, dec__ref_count=1)
Trigger.delete_if_unreferenced(existing_trigger_db)
示例6: get_trigger_db_given_type_and_params
def get_trigger_db_given_type_and_params(type=None, parameters=None):
try:
parameters = parameters or {}
trigger_dbs = Trigger.query(type=type, parameters=parameters)
trigger_db = trigger_dbs[0] if len(trigger_dbs) > 0 else None
# NOTE: This is a work-around which we might be able to remove once we upgrade
# pymongo and mongoengine
# Work around for cron-timer when in some scenarios finding an object fails when Python
# value types are unicode :/
is_cron_trigger = type == CRON_TIMER_TRIGGER_REF
has_parameters = bool(parameters)
if not trigger_db and six.PY2 and is_cron_trigger and has_parameters:
non_unicode_literal_parameters = {}
for key, value in six.iteritems(parameters):
key = key.encode("utf-8")
if isinstance(value, six.text_type):
# We only encode unicode to str
value = value.encode("utf-8")
non_unicode_literal_parameters[key] = value
parameters = non_unicode_literal_parameters
trigger_dbs = Trigger.query(type=type, parameters=non_unicode_literal_parameters).no_cache()
# Note: We need to directly access the object, using len or accessing the query set
# twice won't work - there seems to bug a bug with cursor where accessing it twice
# will throw an exception
try:
trigger_db = trigger_dbs[0]
except IndexError:
trigger_db = None
if not parameters and not trigger_db:
# We need to do double query because some TriggeDB objects without
# parameters have "parameters" attribute stored in the db and others
# don't
trigger_db = Trigger.query(type=type, parameters=None).first()
return trigger_db
except StackStormDBObjectNotFoundError as e:
LOG.debug(
'Database lookup for type="%s" parameters="%s" resulted ' + "in exception : %s.",
type,
parameters,
e,
exc_info=True,
)
return None
示例7: increment_trigger_ref_count
def increment_trigger_ref_count(rule_api):
"""
Given the rule figures out the TriggerType with parameter and increments
reference count on the appropriate Trigger.
:param rule_api: Rule object used to infer the Trigger.
:type rule_api: ``RuleApi``
"""
trigger_dict = _get_trigger_dict_given_rule(rule_api)
# Special reference counting for trigger with parameters.
if trigger_dict.get('parameters', None):
trigger_db = _get_trigger_db(trigger_dict)
Trigger.update(trigger_db, inc__ref_count=1)
示例8: test_get_trigger_db_given_type_and_params
def test_get_trigger_db_given_type_and_params(self):
# Add dummy triggers
trigger_1 = TriggerDB(pack='testpack', name='testtrigger1', type='testpack.testtrigger1')
trigger_2 = TriggerDB(pack='testpack', name='testtrigger2', type='testpack.testtrigger2')
trigger_3 = TriggerDB(pack='testpack', name='testtrigger3', type='testpack.testtrigger3')
trigger_4 = TriggerDB(pack='testpack', name='testtrigger4', type='testpack.testtrigger4',
parameters={'ponies': 'unicorn'})
Trigger.add_or_update(trigger_1)
Trigger.add_or_update(trigger_2)
Trigger.add_or_update(trigger_3)
Trigger.add_or_update(trigger_4)
# Trigger with no parameters, parameters={} in db
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_1.type,
parameters={})
self.assertEqual(trigger_db, trigger_1)
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_1.type,
parameters=None)
self.assertEqual(trigger_db, trigger_1)
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_1.type,
parameters={'fo': 'bar'})
self.assertEqual(trigger_db, None)
# Trigger with no parameters, no parameters attribute in the db
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_2.type,
parameters={})
self.assertEqual(trigger_db, trigger_2)
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_2.type,
parameters=None)
self.assertEqual(trigger_db, trigger_2)
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_2.type,
parameters={'fo': 'bar'})
self.assertEqual(trigger_db, None)
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_3.type,
parameters={})
self.assertEqual(trigger_db, trigger_3)
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_3.type,
parameters=None)
self.assertEqual(trigger_db, trigger_3)
# Trigger with parameters
trigger_db = trigger_service.get_trigger_db_given_type_and_params(
type=trigger_4.type,
parameters=trigger_4.parameters)
self.assertEqual(trigger_db, trigger_4)
trigger_db = trigger_service.get_trigger_db_given_type_and_params(type=trigger_4.type,
parameters=None)
self.assertEqual(trigger_db, None)
示例9: _delete_shadow_trigger
def _delete_shadow_trigger(triggertype_db):
# shadow Trigger's have the same name as the shadowed TriggerType.
triggertype_ref = ResourceReference(name=triggertype_db.name, pack=triggertype_db.pack)
trigger_db = TriggerService.get_trigger_db_by_ref(triggertype_ref.ref)
if not trigger_db:
LOG.warn('No shadow trigger found for %s. Will skip delete.', triggertype_db)
return
try:
Trigger.delete(trigger_db)
except Exception:
LOG.exception('Database delete encountered exception during delete of id="%s". ',
trigger_db.id)
extra = {'trigger_db': trigger_db}
LOG.audit('Trigger deleted. Trigger.id=%s' % (trigger_db.id), extra=extra)
示例10: setUp
def setUp(self):
super(ContainerUtilsTest, self).setUp()
# Insert mock TriggerDB
trigger_db = TriggerDB(name='name1', pack='pack1', type='type1',
parameters={'a': 1, 'b': '2', 'c': 'foo'})
self.trigger_db = Trigger.add_or_update(trigger_db)
示例11: _setup_sample_triggers
def _setup_sample_triggers(
self, names=["st2.test.trigger1", "st2.test.trigger2", "st2.test.trigger3", "st2.test.trigger4"]
):
trigger_dbs = []
for name in names:
trigtype = None
try:
trigtype = TriggerTypeDB()
trigtype.pack = "dummy_pack_1"
trigtype.name = name
trigtype.description = ""
trigtype.payload_schema = {}
trigtype.parameters_schema = {}
try:
trigtype = TriggerType.get_by_name(name)
except:
trigtype = TriggerType.add_or_update(trigtype)
except NotUniqueError:
pass
created = TriggerDB()
created.name = name
created.pack = "dummy_pack_1"
created.description = ""
created.type = trigtype.get_reference().ref
if name in ["st2.test.trigger4"]:
created.parameters = {"url": "sample"}
else:
created.parameters = {}
created = Trigger.add_or_update(created)
trigger_dbs.append(created)
return trigger_dbs
示例12: test_register_triggers_from_pack
def test_register_triggers_from_pack(self):
base_path = get_fixtures_base_path()
pack_dir = os.path.join(base_path, 'dummy_pack_1')
trigger_type_dbs = TriggerType.get_all()
self.assertEqual(len(trigger_type_dbs), 0)
count = triggers_registrar.register_triggers(pack_dir=pack_dir)
self.assertEqual(count, 2)
# Verify TriggerTypeDB and corresponding TriggerDB objects have been created
trigger_type_dbs = TriggerType.get_all()
trigger_dbs = Trigger.get_all()
self.assertEqual(len(trigger_type_dbs), 2)
self.assertEqual(len(trigger_dbs), 2)
self.assertEqual(trigger_type_dbs[0].name, 'event_handler')
self.assertEqual(trigger_type_dbs[0].pack, 'dummy_pack_1')
self.assertEqual(trigger_dbs[0].name, 'event_handler')
self.assertEqual(trigger_dbs[0].pack, 'dummy_pack_1')
self.assertEqual(trigger_dbs[0].type, 'dummy_pack_1.event_handler')
self.assertEqual(trigger_type_dbs[1].name, 'head_sha_monitor')
self.assertEqual(trigger_type_dbs[1].pack, 'dummy_pack_1')
self.assertEqual(trigger_type_dbs[1].payload_schema['type'], 'object')
示例13: get_all
def get_all(self, exclude_attributes=None, include_attributes=None, sort=None, offset=0,
limit=None, requester_user=None, **raw_filters):
"""
List all triggerinstances.
Handles requests:
GET /triggerinstances/
"""
# If trigger_type filter is provided, filter based on the TriggerType via Trigger object
trigger_type_ref = raw_filters.get('trigger_type', None)
if trigger_type_ref:
# 1. Retrieve TriggerType object id which match this trigger_type ref
trigger_dbs = Trigger.query(type=trigger_type_ref,
only_fields=['ref', 'name', 'pack', 'type'])
trigger_refs = [trigger_db.ref for trigger_db in trigger_dbs]
raw_filters['trigger'] = trigger_refs
if trigger_type_ref and len(raw_filters.get('trigger', [])) == 0:
# Empty list means trigger_type_ref filter was provided, but we matched no Triggers so
# we should return back empty result
return []
trigger_instances = self._get_trigger_instances(exclude_fields=exclude_attributes,
include_fields=include_attributes,
sort=sort,
offset=offset,
limit=limit,
raw_filters=raw_filters,
requester_user=requester_user)
return trigger_instances
示例14: create_or_update_trigger_db
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_api.validate()
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)
extra = {'trigger_db': trigger_db}
if is_update:
LOG.audit('Trigger updated. Trigger.id=%s' % (trigger_db.id), extra=extra)
else:
LOG.audit('Trigger created. Trigger.id=%s' % (trigger_db.id), extra=extra)
return trigger_db
示例15: _setup_sample_triggers
def _setup_sample_triggers(self, names=['st2.test.trigger1', 'st2.test.trigger2',
'st2.test.trigger3', 'st2.test.trigger4']):
trigger_dbs = []
for name in names:
trigtype = None
try:
trigtype = TriggerTypeDB(pack='dummy_pack_1', name=name, description='',
payload_schema={}, parameters_schema={})
try:
trigtype = TriggerType.get_by_name(name)
except:
trigtype = TriggerType.add_or_update(trigtype)
except NotUniqueError:
pass
created = TriggerDB(pack='dummy_pack_1', name=name, description='',
type=trigtype.get_reference().ref)
if name in ['st2.test.trigger4']:
created.parameters = {'url': 'sample'}
else:
created.parameters = {}
created = Trigger.add_or_update(created)
trigger_dbs.append(created)
return trigger_dbs