|
1
|
|
|
import os |
|
2
|
|
|
|
|
3
|
|
|
from st2actions.runners.pythonrunner import Action |
|
4
|
|
|
from st2client.client import Client |
|
5
|
|
|
from st2client.models.keyvalue import KeyValuePair # pylint: disable=no-name-in-module |
|
6
|
|
|
|
|
7
|
|
|
from lib.utils import filter_none_values |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
__all__ = [ |
|
11
|
|
|
'St2BaseAction' |
|
12
|
|
|
] |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class St2BaseAction(Action): |
|
16
|
|
|
def __init__(self, config): |
|
17
|
|
|
super(St2BaseAction, self).__init__(config) |
|
18
|
|
|
self._client = Client |
|
19
|
|
|
self._kvp = KeyValuePair |
|
20
|
|
|
self.client = self._get_client() |
|
21
|
|
|
|
|
22
|
|
|
def _get_client(self): |
|
23
|
|
|
base_url, api_url, auth_url = self._get_st2_urls() |
|
24
|
|
|
cacert = self._get_cacert() |
|
25
|
|
|
|
|
26
|
|
|
client_kwargs = { |
|
27
|
|
|
'base_url': base_url, |
|
28
|
|
|
'api_url': api_url, |
|
29
|
|
|
'auth_url': auth_url, |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if cacert: |
|
33
|
|
|
client_kwargs['cacert'] = cacert |
|
34
|
|
|
|
|
35
|
|
|
api_key = self._get_api_key() |
|
36
|
|
|
token = self._get_auth_token() |
|
37
|
|
|
|
|
38
|
|
|
# API key has precendece over auth token generated for each action |
|
39
|
|
|
# invocation |
|
40
|
|
|
if api_key: |
|
41
|
|
|
client_kwargs['api_key'] = api_key |
|
42
|
|
|
else: |
|
43
|
|
|
client_kwargs['token'] = token |
|
44
|
|
|
|
|
45
|
|
|
return self._client(**client_kwargs) |
|
46
|
|
|
|
|
47
|
|
|
def _get_st2_urls(self): |
|
48
|
|
|
# First try to use base_url from config. |
|
49
|
|
|
base_url = self.config.get('base_url', None) |
|
50
|
|
|
api_url = self.config.get('api_url', None) |
|
51
|
|
|
auth_url = self.config.get('auth_url', None) |
|
52
|
|
|
|
|
53
|
|
|
# not found look up from env vars. Assuming the pack is |
|
54
|
|
|
# configuered to work with current StackStorm instance. |
|
55
|
|
|
if not base_url: |
|
56
|
|
|
api_url = os.environ.get('ST2_ACTION_API_URL', None) |
|
57
|
|
|
auth_url = os.environ.get('ST2_ACTION_AUTH_URL', None) |
|
58
|
|
|
|
|
59
|
|
|
return base_url, api_url, auth_url |
|
60
|
|
|
|
|
61
|
|
|
def _get_api_key(self): |
|
62
|
|
|
api_key = self.config.get('api_key', None) |
|
63
|
|
|
return api_key |
|
64
|
|
|
|
|
65
|
|
|
def _get_auth_token(self): |
|
66
|
|
|
# First try to use auth_token from config. |
|
67
|
|
|
token = self.config.get('auth_token', None) |
|
68
|
|
|
|
|
69
|
|
|
# not found look up from env vars. Assuming the pack is |
|
70
|
|
|
# configuered to work with current StackStorm instance. |
|
71
|
|
|
if not token: |
|
72
|
|
|
token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None) |
|
73
|
|
|
|
|
74
|
|
|
return token |
|
75
|
|
|
|
|
76
|
|
|
def _get_cacert(self): |
|
77
|
|
|
cacert = self.config.get('cacert', None) |
|
78
|
|
|
return cacert |
|
79
|
|
|
|
|
80
|
|
|
def _run_client_method(self, method, method_kwargs, format_func, format_kwargs=None): |
|
81
|
|
|
""" |
|
82
|
|
|
Run the provided client method and format the result. |
|
83
|
|
|
|
|
84
|
|
|
:param method: Client method to run. |
|
85
|
|
|
:type method: ``func`` |
|
86
|
|
|
|
|
87
|
|
|
:param method_kwargs: Keyword arguments passed to the client method. |
|
88
|
|
|
:type method_kwargs: ``dict`` |
|
89
|
|
|
|
|
90
|
|
|
:param format_func: Function for formatting the result. |
|
91
|
|
|
:type format_func: ``func`` |
|
92
|
|
|
|
|
93
|
|
|
:rtype: ``list`` of ``dict`` |
|
94
|
|
|
""" |
|
95
|
|
|
# Filter out parameters with string value of "None" |
|
96
|
|
|
# This is a work around since the default values can only be strings |
|
97
|
|
|
method_kwargs = filter_none_values(method_kwargs) |
|
98
|
|
|
method_name = method.__name__ |
|
99
|
|
|
self.logger.debug('Calling client method "%s" with kwargs "%s"' % (method_name, |
|
100
|
|
|
method_kwargs)) |
|
101
|
|
|
|
|
102
|
|
|
result = method(**method_kwargs) |
|
103
|
|
|
result = format_func(result, **format_kwargs or {}) |
|
104
|
|
|
return result |
|
105
|
|
|
|