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 st2client.client import Client |
17
|
|
|
from st2client.models import KeyValuePair |
18
|
|
|
from st2common.services.access import create_token |
19
|
|
|
from st2common.util.api import get_full_public_api_url |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class DatastoreService(object): |
23
|
|
|
""" |
24
|
|
|
Instance of this class is passed to the python action runner and exposes "public" |
25
|
|
|
methods which can be called by the action. |
26
|
|
|
""" |
27
|
|
|
|
28
|
|
|
DATASTORE_NAME_SEPARATOR = ':' |
29
|
|
|
|
30
|
|
|
def __init__(self, logger, pack_name, class_name, api_username): |
31
|
|
|
self._api_username = api_username |
32
|
|
|
self._pack_name = pack_name |
33
|
|
|
self._class_name = class_name |
34
|
|
|
self._logger = logger |
35
|
|
|
|
36
|
|
|
self._client = None |
37
|
|
|
|
38
|
|
|
################################## |
39
|
|
|
# Methods for datastore management |
40
|
|
|
################################## |
41
|
|
|
|
42
|
|
|
def list_values(self, local=True, prefix=None): |
43
|
|
|
""" |
44
|
|
|
Retrieve all the datastores items. |
45
|
|
|
|
46
|
|
|
:param local: List values from a namespace local to this sensor. Defaults to True. |
47
|
|
|
:type: local: ``bool`` |
48
|
|
|
|
49
|
|
|
:param prefix: Optional key name prefix / startswith filter. |
50
|
|
|
:type prefix: ``str`` |
51
|
|
|
|
52
|
|
|
:rtype: ``list`` of :class:`KeyValuePair` |
53
|
|
|
""" |
54
|
|
|
client = self._get_api_client() |
55
|
|
|
self._logger.audit('Retrieving all the value from the datastore') |
56
|
|
|
|
57
|
|
|
key_prefix = self._get_full_key_prefix(local=local, prefix=prefix) |
58
|
|
|
kvps = client.keys.get_all(prefix=key_prefix) |
59
|
|
|
return kvps |
60
|
|
|
|
61
|
|
|
def get_value(self, name, local=True): |
62
|
|
|
""" |
63
|
|
|
Retrieve a value from the datastore for the provided key. |
64
|
|
|
|
65
|
|
|
By default, value is retrieved from the namespace local to the sensor. If you want to |
66
|
|
|
retrieve a global value from a datastore, pass local=False to this method. |
67
|
|
|
|
68
|
|
|
:param name: Key name. |
69
|
|
|
:type name: ``str`` |
70
|
|
|
|
71
|
|
|
:param local: Retrieve value from a namespace local to the sensor. Defaults to True. |
72
|
|
|
:type: local: ``bool`` |
73
|
|
|
|
74
|
|
|
:rtype: ``str`` or ``None`` |
75
|
|
|
""" |
76
|
|
|
name = self._get_full_key_name(name=name, local=local) |
77
|
|
|
|
78
|
|
|
client = self._get_api_client() |
79
|
|
|
self._logger.audit('Retrieving value from the datastore (name=%s)', name) |
80
|
|
|
|
81
|
|
|
try: |
82
|
|
|
kvp = client.keys.get_by_id(id=name) |
83
|
|
|
except Exception: |
84
|
|
|
return None |
85
|
|
|
|
86
|
|
|
if kvp: |
87
|
|
|
return kvp.value |
88
|
|
|
|
89
|
|
|
return None |
90
|
|
|
|
91
|
|
|
def set_value(self, name, value, ttl=None, local=True): |
92
|
|
|
""" |
93
|
|
|
Set a value for the provided key. |
94
|
|
|
|
95
|
|
|
By default, value is set in a namespace local to the sensor. If you want to |
96
|
|
|
set a global value, pass local=False to this method. |
97
|
|
|
|
98
|
|
|
:param name: Key name. |
99
|
|
|
:type name: ``str`` |
100
|
|
|
|
101
|
|
|
:param value: Key value. |
102
|
|
|
:type value: ``str`` |
103
|
|
|
|
104
|
|
|
:param ttl: Optional TTL (in seconds). |
105
|
|
|
:type ttl: ``int`` |
106
|
|
|
|
107
|
|
|
:param local: Set value in a namespace local to the sensor. Defaults to True. |
108
|
|
|
:type: local: ``bool`` |
109
|
|
|
|
110
|
|
|
:return: ``True`` on success, ``False`` otherwise. |
111
|
|
|
:rtype: ``bool`` |
112
|
|
|
""" |
113
|
|
|
name = self._get_full_key_name(name=name, local=local) |
114
|
|
|
|
115
|
|
|
value = str(value) |
116
|
|
|
client = self._get_api_client() |
117
|
|
|
|
118
|
|
|
self._logger.audit('Setting value in the datastore (name=%s)', name) |
119
|
|
|
|
120
|
|
|
instance = KeyValuePair() |
121
|
|
|
instance.id = name |
122
|
|
|
instance.name = name |
123
|
|
|
instance.value = value |
124
|
|
|
|
125
|
|
|
if ttl: |
126
|
|
|
instance.ttl = ttl |
127
|
|
|
|
128
|
|
|
client.keys.update(instance=instance) |
129
|
|
|
return True |
130
|
|
|
|
131
|
|
|
def delete_value(self, name, local=True): |
132
|
|
|
""" |
133
|
|
|
Delete the provided key. |
134
|
|
|
|
135
|
|
|
By default, value is deleted from a namespace local to the sensor. If you want to |
136
|
|
|
delete a global value, pass local=False to this method. |
137
|
|
|
|
138
|
|
|
:param name: Name of the key to delete. |
139
|
|
|
:type name: ``str`` |
140
|
|
|
|
141
|
|
|
:param local: Delete a value in a namespace local to the sensor. Defaults to True. |
142
|
|
|
:type: local: ``bool`` |
143
|
|
|
|
144
|
|
|
:return: ``True`` on success, ``False`` otherwise. |
145
|
|
|
:rtype: ``bool`` |
146
|
|
|
""" |
147
|
|
|
name = self._get_full_key_name(name=name, local=local) |
148
|
|
|
|
149
|
|
|
client = self._get_api_client() |
150
|
|
|
|
151
|
|
|
instance = KeyValuePair() |
152
|
|
|
instance.id = name |
153
|
|
|
instance.name = name |
154
|
|
|
|
155
|
|
|
self._logger.audit('Deleting value from the datastore (name=%s)', name) |
156
|
|
|
|
157
|
|
|
try: |
158
|
|
|
client.keys.delete(instance=instance) |
159
|
|
|
except Exception: |
160
|
|
|
return False |
161
|
|
|
|
162
|
|
|
return True |
163
|
|
|
|
164
|
|
|
def _get_api_client(self): |
165
|
|
|
""" |
166
|
|
|
Retrieve API client instance. |
167
|
|
|
""" |
168
|
|
|
if not self._client: |
169
|
|
|
ttl = (24 * 60 * 60) |
170
|
|
|
temporary_token = create_token(username=self._api_username, ttl=ttl) |
171
|
|
|
api_url = get_full_public_api_url() |
172
|
|
|
self._client = Client(api_url=api_url, token=temporary_token.token) |
173
|
|
|
|
174
|
|
|
return self._client |
175
|
|
|
|
176
|
|
|
def _get_full_key_name(self, name, local): |
177
|
|
|
""" |
178
|
|
|
Retrieve a full key name. |
179
|
|
|
|
180
|
|
|
:rtype: ``str`` |
181
|
|
|
""" |
182
|
|
|
if local: |
183
|
|
|
name = self._get_key_name_with_sensor_prefix(name=name) |
184
|
|
|
|
185
|
|
|
return name |
186
|
|
|
|
187
|
|
|
def _get_full_key_prefix(self, local, prefix=None): |
188
|
|
|
if local: |
189
|
|
|
key_prefix = self._get_sensor_local_key_name_prefix() |
190
|
|
|
|
191
|
|
|
if prefix: |
192
|
|
|
key_prefix += prefix |
193
|
|
|
else: |
194
|
|
|
key_prefix = prefix |
195
|
|
|
|
196
|
|
|
return key_prefix |
197
|
|
|
|
198
|
|
|
def _get_sensor_local_key_name_prefix(self): |
199
|
|
|
""" |
200
|
|
|
Retrieve key prefix which is local to this sensor. |
201
|
|
|
""" |
202
|
|
|
key_prefix = self._get_datastore_key_prefix() + self.DATASTORE_NAME_SEPARATOR |
203
|
|
|
return key_prefix |
204
|
|
|
|
205
|
|
|
def _get_key_name_with_sensor_prefix(self, name): |
206
|
|
|
""" |
207
|
|
|
Retrieve a full key name which is local to the current sensor. |
208
|
|
|
|
209
|
|
|
:param name: Base datastore key name. |
210
|
|
|
:type name: ``str`` |
211
|
|
|
|
212
|
|
|
:rtype: ``str`` |
213
|
|
|
""" |
214
|
|
|
prefix = self._get_datastore_key_prefix() |
215
|
|
|
full_name = prefix + self.DATASTORE_NAME_SEPARATOR + name |
216
|
|
|
return full_name |
217
|
|
|
|
218
|
|
|
def _get_datastore_key_prefix(self): |
219
|
|
|
prefix = '%s.%s' % (self._pack_name, self._class_name) |
220
|
|
|
return prefix |
221
|
|
|
|