|
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
|
|
|
from st2common import log as logging |
|
17
|
|
|
from st2common.constants.triggers import KEY_VALUE_PAIR_CREATE_TRIGGER |
|
18
|
|
|
from st2common.constants.triggers import KEY_VALUE_PAIR_UPDATE_TRIGGER |
|
19
|
|
|
from st2common.constants.triggers import KEY_VALUE_PAIR_VALUE_CHANGE_TRIGGER |
|
20
|
|
|
from st2common.constants.triggers import KEY_VALUE_PAIR_DELETE_TRIGGER |
|
21
|
|
|
from st2common.models.api.keyvalue import KeyValuePairAPI |
|
22
|
|
|
from st2common.models.db.keyvalue import keyvaluepair_access |
|
23
|
|
|
from st2common.models.system.common import ResourceReference |
|
24
|
|
|
from st2common.persistence.base import Access |
|
25
|
|
|
|
|
26
|
|
|
LOG = logging.getLogger(__name__) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class KeyValuePair(Access): |
|
30
|
|
|
impl = keyvaluepair_access |
|
31
|
|
|
publisher = None |
|
32
|
|
|
|
|
33
|
|
|
api_model_cls = KeyValuePairAPI |
|
34
|
|
|
dispatch_trigger_for_operations = ['create', 'update', 'value_change', 'delete'] |
|
35
|
|
|
operation_to_trigger_ref_map = { |
|
36
|
|
|
'create': ResourceReference.to_string_reference( |
|
37
|
|
|
name=KEY_VALUE_PAIR_CREATE_TRIGGER['name'], |
|
38
|
|
|
pack=KEY_VALUE_PAIR_CREATE_TRIGGER['pack']), |
|
39
|
|
|
'update': ResourceReference.to_string_reference( |
|
40
|
|
|
name=KEY_VALUE_PAIR_UPDATE_TRIGGER['name'], |
|
41
|
|
|
pack=KEY_VALUE_PAIR_UPDATE_TRIGGER['pack']), |
|
42
|
|
|
'value_change': ResourceReference.to_string_reference( |
|
43
|
|
|
name=KEY_VALUE_PAIR_VALUE_CHANGE_TRIGGER['name'], |
|
44
|
|
|
pack=KEY_VALUE_PAIR_VALUE_CHANGE_TRIGGER['pack']), |
|
45
|
|
|
'delete': ResourceReference.to_string_reference( |
|
46
|
|
|
name=KEY_VALUE_PAIR_DELETE_TRIGGER['name'], |
|
47
|
|
|
pack=KEY_VALUE_PAIR_DELETE_TRIGGER['pack']), |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
@classmethod |
|
51
|
|
|
def add_or_update(cls, model_object, publish=True, dispatch_trigger=True): |
|
|
|
|
|
|
52
|
|
|
""" |
|
53
|
|
|
Note: We override add_or_update because we also want to publish high level "value_change" |
|
54
|
|
|
event for this resource. |
|
55
|
|
|
""" |
|
56
|
|
|
if model_object.id: |
|
57
|
|
|
existing_model_object = cls.get_by_id(value=model_object.id) |
|
58
|
|
|
else: |
|
59
|
|
|
# Not an update |
|
60
|
|
|
existing_model_object = None |
|
61
|
|
|
|
|
62
|
|
|
model_object = super(KeyValuePair, cls).add_or_update(model_object=model_object, |
|
63
|
|
|
publish=publish, |
|
64
|
|
|
dispatch_trigger=dispatch_trigger) |
|
65
|
|
|
|
|
66
|
|
|
# Dispatch a value_change event which is specific to this resource |
|
67
|
|
|
if existing_model_object and existing_model_object.value != model_object.value: |
|
68
|
|
|
cls.dispatch_value_change_trigger(old_model_object=existing_model_object, |
|
69
|
|
|
new_model_object=model_object) |
|
70
|
|
|
|
|
71
|
|
|
return model_object |
|
72
|
|
|
|
|
73
|
|
|
@classmethod |
|
74
|
|
|
def dispatch_value_change_trigger(cls, old_model_object, new_model_object): |
|
75
|
|
|
operation = 'value_change' |
|
76
|
|
|
trigger = cls._get_trigger_ref_for_operation(operation=operation) |
|
77
|
|
|
|
|
78
|
|
|
old_object_payload = cls.api_model_cls.from_model(old_model_object, |
|
79
|
|
|
mask_secrets=True).__json__() |
|
80
|
|
|
new_object_payload = cls.api_model_cls.from_model(new_model_object, |
|
81
|
|
|
mask_secrets=True).__json__() |
|
82
|
|
|
payload = { |
|
83
|
|
|
'old_object': old_object_payload, |
|
84
|
|
|
'new_object': new_object_payload |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return cls._dispatch_trigger(operation=operation, trigger=trigger, payload=payload) |
|
88
|
|
|
|
|
89
|
|
|
@classmethod |
|
90
|
|
|
def get_by_scope_and_name(cls, scope, name): |
|
91
|
|
|
""" |
|
92
|
|
|
Get a key value store given a scope and name. |
|
93
|
|
|
|
|
94
|
|
|
:param scope: Scope which the key belongs to. |
|
95
|
|
|
:type scope: ``str`` |
|
96
|
|
|
|
|
97
|
|
|
:param name: Name of the key. |
|
98
|
|
|
:type key: ``str`` |
|
99
|
|
|
|
|
100
|
|
|
:rtype: :class:`KeyValuePairDB` or ``None`` |
|
101
|
|
|
""" |
|
102
|
|
|
query_result = cls.impl.query(scope=scope, name=name) |
|
103
|
|
|
return query_result.first() if query_result else None |
|
104
|
|
|
|
|
105
|
|
|
@classmethod |
|
106
|
|
|
def _get_impl(cls): |
|
107
|
|
|
return cls.impl |
|
108
|
|
|
|
|
109
|
|
|
@classmethod |
|
110
|
|
|
def _get_by_object(cls, object): |
|
|
|
|
|
|
111
|
|
|
# For KeyValuePair name is unique. |
|
112
|
|
|
name = getattr(object, 'name', '') |
|
113
|
|
|
return cls.get_by_name(name) |
|
114
|
|
|
|