|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import six |
|
17
|
|
|
|
|
18
|
|
|
from st2common import log as logging |
|
19
|
|
|
from st2common.constants.triggers import TRIGGER_INSTANCE_STATUS_PENDING |
|
20
|
|
|
from st2common.exceptions.db import StackStormDBObjectNotFoundError |
|
21
|
|
|
from st2common.models.db.trigger import TriggerInstanceDB |
|
22
|
|
|
from st2common.persistence.trigger import TriggerInstance |
|
23
|
|
|
from st2common.services import triggers as TriggerService |
|
24
|
|
|
|
|
25
|
|
|
LOG = logging.getLogger('st2reactor.sensor.container_utils') |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigger=False): |
|
29
|
|
|
""" |
|
30
|
|
|
This creates a trigger instance object given trigger and payload. |
|
31
|
|
|
Trigger can be just a string reference (pack.name) or a ``dict`` |
|
32
|
|
|
containing 'type' and 'parameters'. |
|
33
|
|
|
|
|
34
|
|
|
:param trigger: Dictionary with trigger query filters. |
|
35
|
|
|
:type trigger: ``dict`` |
|
36
|
|
|
|
|
37
|
|
|
:param payload: Trigger payload. |
|
38
|
|
|
:type payload: ``dict`` |
|
39
|
|
|
""" |
|
40
|
|
|
# TODO: This is nasty, this should take a unique reference and not a dict |
|
41
|
|
|
if isinstance(trigger, six.string_types): |
|
42
|
|
|
trigger_db = TriggerService.get_trigger_db_by_ref(trigger) |
|
43
|
|
|
else: |
|
44
|
|
|
type_ = trigger.get('type', None) |
|
45
|
|
|
parameters = trigger.get('parameters', {}) |
|
46
|
|
|
trigger_db = TriggerService.get_trigger_db_given_type_and_params(type=type_, |
|
47
|
|
|
parameters=parameters) |
|
48
|
|
|
|
|
49
|
|
|
if trigger_db is None: |
|
50
|
|
|
LOG.debug('No trigger in db for %s', trigger) |
|
51
|
|
|
if raise_on_no_trigger: |
|
52
|
|
|
raise StackStormDBObjectNotFoundError('Trigger not found for %s', trigger) |
|
53
|
|
|
return None |
|
54
|
|
|
|
|
55
|
|
|
trigger_ref = trigger_db.get_reference().ref |
|
56
|
|
|
|
|
57
|
|
|
trigger_instance = TriggerInstanceDB() |
|
58
|
|
|
trigger_instance.trigger = trigger_ref |
|
59
|
|
|
trigger_instance.payload = payload |
|
60
|
|
|
trigger_instance.occurrence_time = occurrence_time |
|
61
|
|
|
trigger_instance.status = TRIGGER_INSTANCE_STATUS_PENDING |
|
62
|
|
|
return TriggerInstance.add_or_update(trigger_instance) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def update_trigger_instance_status(trigger_instance, status): |
|
66
|
|
|
trigger_instance.status = status |
|
67
|
|
|
return TriggerInstance.add_or_update(trigger_instance) |
|
68
|
|
|
|