|
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 json |
|
17
|
|
|
import hashlib |
|
18
|
|
|
|
|
19
|
|
|
import mongoengine as me |
|
20
|
|
|
|
|
21
|
|
|
from st2common.models.db import MongoDBAccess |
|
22
|
|
|
from st2common.models.db import stormbase |
|
23
|
|
|
from st2common.constants.types import ResourceType |
|
24
|
|
|
|
|
25
|
|
|
__all__ = [ |
|
26
|
|
|
'TriggerTypeDB', |
|
27
|
|
|
'TriggerDB', |
|
28
|
|
|
'TriggerInstanceDB', |
|
29
|
|
|
] |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
class TriggerTypeDB(stormbase.StormBaseDB, |
|
33
|
|
|
stormbase.ContentPackResourceMixin, |
|
34
|
|
|
stormbase.UIDFieldMixin, |
|
35
|
|
|
stormbase.TagsMixin): |
|
36
|
|
|
"""Description of a specific kind/type of a trigger. The |
|
37
|
|
|
(pack, name) tuple is expected uniquely identify a trigger in |
|
38
|
|
|
the namespace of all triggers provided by a specific trigger_source. |
|
39
|
|
|
Attribute: |
|
40
|
|
|
name - Trigger type name. |
|
41
|
|
|
pack - Name of the content pack this trigger belongs to. |
|
42
|
|
|
trigger_source: Source that owns this trigger type. |
|
43
|
|
|
payload_info: Meta information of the expected payload. |
|
44
|
|
|
""" |
|
45
|
|
|
|
|
46
|
|
|
RESOURCE_TYPE = ResourceType.TRIGGER_TYPE |
|
47
|
|
|
UID_FIELDS = ['pack', 'name'] |
|
48
|
|
|
|
|
49
|
|
|
ref = me.StringField(required=False) |
|
50
|
|
|
name = me.StringField(required=True) |
|
51
|
|
|
pack = me.StringField(required=True, unique_with='name') |
|
52
|
|
|
payload_schema = me.DictField() |
|
53
|
|
|
parameters_schema = me.DictField(default={}) |
|
54
|
|
|
|
|
55
|
|
|
meta = { |
|
56
|
|
|
'indexes': stormbase.TagsMixin.get_indices() + stormbase.UIDFieldMixin.get_indexes() |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
def __init__(self, *args, **values): |
|
60
|
|
|
super(TriggerTypeDB, self).__init__(*args, **values) |
|
61
|
|
|
self.ref = self.get_reference().ref |
|
62
|
|
|
# pylint: disable=no-member |
|
63
|
|
|
self.uid = self.get_uid() |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
class TriggerDB(stormbase.StormBaseDB, stormbase.ContentPackResourceMixin, |
|
67
|
|
|
stormbase.UIDFieldMixin): |
|
68
|
|
|
""" |
|
69
|
|
|
Attribute: |
|
70
|
|
|
name - Trigger name. |
|
71
|
|
|
pack - Name of the content pack this trigger belongs to. |
|
72
|
|
|
type - Reference to the TriggerType object. |
|
73
|
|
|
parameters - Trigger parameters. |
|
74
|
|
|
""" |
|
75
|
|
|
|
|
76
|
|
|
RESOURCE_TYPE = ResourceType.TRIGGER |
|
77
|
|
|
UID_FIELDS = ['pack', 'name'] |
|
78
|
|
|
|
|
79
|
|
|
ref = me.StringField(required=False) |
|
80
|
|
|
name = me.StringField(required=True) |
|
81
|
|
|
pack = me.StringField(required=True, unique_with='name') |
|
82
|
|
|
type = me.StringField() |
|
83
|
|
|
parameters = me.DictField() |
|
84
|
|
|
ref_count = me.IntField(default=0) |
|
85
|
|
|
|
|
86
|
|
|
meta = { |
|
87
|
|
|
'indexes': [ |
|
88
|
|
|
{'fields': ['name']}, |
|
89
|
|
|
{'fields': ['type']}, |
|
90
|
|
|
{'fields': ['parameters']}, |
|
91
|
|
|
] + stormbase.UIDFieldMixin.get_indexes() |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
def __init__(self, *args, **values): |
|
95
|
|
|
super(TriggerDB, self).__init__(*args, **values) |
|
96
|
|
|
self.ref = self.get_reference().ref |
|
97
|
|
|
self.uid = self.get_uid() |
|
98
|
|
|
|
|
99
|
|
|
def get_uid(self): |
|
100
|
|
|
# Note: Trigger is uniquely identified using name + pack + parameters attributes |
|
101
|
|
|
# pylint: disable=no-member |
|
102
|
|
|
uid = super(TriggerDB, self).get_uid() |
|
103
|
|
|
|
|
104
|
|
|
# Note: We sort the resulting JSON object so that the same dictionary always results |
|
105
|
|
|
# in the same hash |
|
106
|
|
|
parameters = getattr(self, 'parameters', {}) |
|
107
|
|
|
parameters = json.dumps(parameters, sort_keys=True) |
|
108
|
|
|
parameters = hashlib.md5(parameters).hexdigest() |
|
109
|
|
|
|
|
110
|
|
|
uid = uid + self.UID_SEPARATOR + parameters |
|
111
|
|
|
return uid |
|
112
|
|
|
|
|
113
|
|
|
def has_valid_uid(self): |
|
114
|
|
|
parts = self.get_uid_parts() |
|
115
|
|
|
# Note: We add 1 for parameters field which is not part of self.UID_FIELDS |
|
116
|
|
|
return len(parts) == len(self.UID_FIELDS) + 1 + 1 |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
class TriggerInstanceDB(stormbase.StormFoundationDB): |
|
120
|
|
|
"""An instance or occurrence of a type of Trigger. |
|
121
|
|
|
Attribute: |
|
122
|
|
|
trigger: Reference to the Trigger object. |
|
123
|
|
|
payload (dict): payload specific to the occurrence. |
|
124
|
|
|
occurrence_time (datetime): time of occurrence of the trigger. |
|
125
|
|
|
""" |
|
126
|
|
|
trigger = me.StringField() |
|
127
|
|
|
payload = stormbase.EscapedDictField() |
|
128
|
|
|
occurrence_time = me.DateTimeField() |
|
129
|
|
|
status = me.StringField( |
|
130
|
|
|
required=True, |
|
131
|
|
|
help_text='Processing status of TriggerInstance.') |
|
132
|
|
|
|
|
133
|
|
|
meta = { |
|
134
|
|
|
'indexes': [ |
|
135
|
|
|
{'fields': ['occurrence_time']}, |
|
136
|
|
|
{'fields': ['trigger']}, |
|
137
|
|
|
{'fields': ['-occurrence_time', 'trigger']}, |
|
138
|
|
|
{'fields': ['status']} |
|
139
|
|
|
] |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
# specialized access objects |
|
144
|
|
|
triggertype_access = MongoDBAccess(TriggerTypeDB) |
|
145
|
|
|
trigger_access = MongoDBAccess(TriggerDB) |
|
146
|
|
|
triggerinstance_access = MongoDBAccess(TriggerInstanceDB) |
|
147
|
|
|
|
|
148
|
|
|
MODELS = [TriggerTypeDB, TriggerDB, TriggerInstanceDB] |
|
149
|
|
|
|