WMIQueryAction._get_client()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
import json
2
from distutils.spawn import find_executable
3
4
import wmi_client_wrapper as wmi
5
6
from st2actions.runners.pythonrunner import Action
7
8
__all__ = [
9
    'WMIQueryAction'
10
]
11
12
WMIC_EXISTS = find_executable('wmic') is not None
13
14
15
class WMIQueryAction(Action):
16
    def run(self, host, password, query, username='Administrator'):
17
        if not WMIC_EXISTS:
18
            msg = ('Cannot find "wmic" binary. Please make sure WMI client'
19
                   '(wmic) for'
20
                   ' Linux is installed and available in $PATH')
21
            raise Exception(msg)
22
23
        client = self._get_client(host=host, username=username,
24
                                  password=password)
25
26
        # Note: This method throws on connection issue, invalid query, etc
27
        result = client.query(query)
28
29
        try:
30
            result = json.dumps(result)
31
        except Exception:
32
            pass
33
34
        return result
35
36
    def _get_client(self, host, username, password):
37
        client = wmi.WmiClientWrapper(username=username,
38
                                      password=password,
39
                                      host=host)
40
        return client
41