forked from zenoss/zenoss-RM-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zenApiDeviceRouterHelper.py
86 lines (72 loc) · 2.63 KB
/
zenApiDeviceRouterHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from zenApiLib import ZenAPIConnector
import zenApiLib
if not ('logging' in dir()):
import logging
logging.basicConfig(
format = '%(asctime)s %(levelname)s %(name)s: %(message)s'
)
logging.getLogger().setLevel(logging.ERROR)
class ZenDeviceUuidFinder():
'''
This class returns the name and UUID of a device when
provided a query
'''
def __init__(self, query):
self.uuid = None
self.results = []
self.log = logging.getLogger('zenApiDeviceRouterHelper.ZenDeviceUuidFinder')
deviceAPI = zenApiLib.zenConnector(routerName = 'DeviceRouter')
apiResults = deviceAPI.callMethod('getDeviceUuidsByName', query = query)
if apiResults.get('result', {}).get('success', False) is False:
raise Exception('Device query failed')
if apiResults['result']['data']:
self.results = apiResults['result']['data']
else:
self.log.error('Non-sucessful result returned: %s' % resp)
self.count = len(self.results)
def getFirstUuid(self):
try:
return self.results[0]['uuid']
except (KeyError, TypeError):
return None
def getAllUuids(self):
try:
return [x['uuid'] for x in self.results]
except (KeyError, TypeError):
return None
def getCount(self):
return self.count
def first(self):
return self.getFirstUuid()
class ZenDeviceUidFinder():
'''
This class returns the name and UID (path) of a device when
provided a query
'''
def __init__(self, name=None, ip=None):
self.router = 'DeviceRouter'
self.method = 'getDevices'
self.params = {}
if name is not None:
self.params['name'] = name
if ip is not None:
self.params['ipAddress'] = ip
self.data = {'params': self.params}
self.api_call = zenApiLib.zenConnector()
self.api_call.setRouter(self.router)
self.response_json = self.api_call.callMethod(self.method, **self.data)
self.count = len(self.response_json['result']['devices'])
def getFirstUid(self):
try:
return self.response_json['result']['devices'][0]['uid']
except (KeyError, TypeError):
return None
def getAllUids(self):
try:
return [x['uid'] for x in self.response_json['result']['devices']]
except (KeyError, TypeError):
return None
def getCount(self):
return self.count
def first(self):
return self.getFirstUid()