Completed
Pull Request — master (#425)
by Anthony
02:21
created

SaltAction.generate_package()   B

Complexity

Conditions 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 8.5454
cc 5
1
# pylint: disable=no-member
2
3
from st2actions.runners.pythonrunner import Action
4
from requests import Request
5
from utils import sanitize_payload
0 ignored issues
show
Bug introduced by
The name sanitize_payload does not seem to exist in module utils.
Loading history...
6
7
8
class SaltPackage(object):
9
    _expression_forms = [
10
        'glob',
11
        'grain',
12
        'pillar',
13
        'nodegroup',
14
        'list',
15
        'compound'
16
    ]
17
    _data = {"eauth": "",
18
             "username": "",
19
             "password": "",
20
             "client": "",
21
             "fun": ""}
22
23
    def __init__(self, client='local'):
24
        self._data['client'] = client
25
26
    @property
27
    def data(self):
28
        return self._data
29
30
    @data.setter
31
    def data(self, key_value=[]):
0 ignored issues
show
Bug Best Practice introduced by
The default value [] might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
32
        key, value = key_value
0 ignored issues
show
Bug introduced by
The tuple unpacking with sequence defined at line 31 seems to be unbalanced; 2 value(s) for 0 label(s)

This happens when the amount of values does not equal the amount of labels:

a, b = ("a", "b", "c")  # only 2 labels for 3 values
Loading history...
33
        self._data[key] = value
34
35
36
class SaltAction(Action):
37
38
    def __init__(self, config):
39
        super(SaltAction, self).__init__(config=config)
40
        self.url = self.config.get('api_url', None)
41
        self.eauth = self.config.get('eauth', None)
42
        self.username = self.config.get('username', None)
43
        self.password = self.config.get('password', None)
44
45
    def generate_package(self, client='local', cmd=None, args=None,
46
                         **kwargs):
47
        self.data = SaltPackage(client).data
0 ignored issues
show
Coding Style introduced by
The attribute data was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
48
        self.data['eauth'] = self.eauth
49
        self.data['username'] = self.username
50
        self.data['password'] = self.password
51
        if cmd:
52
            self.data['fun'] = cmd
53
        if client is 'local':
54
            self.data['tgt'] = kwargs.get('target', '*')
55
            self.data['expr_form'] = kwargs.get('expr_form', 'glob')
56
        if kwargs.get('kwargs', None) is not None:
57
            self.data['kwarg'] = kwargs['kwargs']['kwargs']
58
        if args is not None:
59
            self.data['arg'] = args
60
        clean_payload = sanitize_payload(('username', 'password'), self.data)
61
        self.logger.info("[salt] Payload to be sent: {0}".format(clean_payload))
62
63
    def generate_request(self):
64
        req = Request('POST',
65
                      "{0}/run".format(self.url),
66
                      headers={'content-type': 'application/json',
67
                               'charset': 'utf-8'})
68
        return req.prepare()
69