Skip to content

Commit

Permalink
Merge pull request #4 from georgijd/f-get-logs
Browse files Browse the repository at this point in the history
Add get_logs method to the Device object
  • Loading branch information
tonybaloney committed Jul 19, 2016
2 parents a6c6d3f + 4ed1beb commit 0bbd465
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 11 deletions.
51 changes: 51 additions & 0 deletions sciencelogic/device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sciencelogic.performance_data import PerformanceData



class Device(object):
"""
Represents a monitored device
Expand Down Expand Up @@ -43,6 +44,56 @@ def _fill_details(self):
device = self._client.get(self.uri)
self.details = device.json()

def get_logs(self,
extended_fetch=0,
hide_filter_info=1,
link_disp_field=None,
limit=1000,
offset=None):
"""
Get logs for this device
:param extended_fetch: Fetch entire resource if 1 (true), or resource
link only if 0 (false).
:type extended_fetch: ``bool``
:param hide_filter_info: Suppress filterspec and current filter info
:type hide_filter_info: ``bool``
:param link_disp_field: When not using extended_fetch, this determines
which field is used for the "description" of the resource link
:type link_disp_field: ``list``
:param limit: Number of records to retrieve
:type limit: ``int``
:param offset: Specifies the index of the first returned resource
within the entire result set
:type offset: ``int``
:rtype: ``list`` of ``dict``
"""
params = { # defaults
'extended_fetch': extended_fetch,
'hide_filter_info': hide_filter_info,
}
if link_disp_field is not None:
params['link_disp_field'] = ','.join(link_disp_field)

if limit:
params['limit'] = limit

if offset:
params['offset'] = offset

uri = self.details['logs']['URI']
uri = uri[:uri.find('?')]
data = self._client.get(uri, params=params).json()['result_set']
if extended_fetch:
return data.values()

return [self._client.get(item['URI']).json() for item in data]

def performance_counters(self):
"""
Get a list of performance counters for this device
Expand Down
44 changes: 44 additions & 0 deletions tests/fixtures/api_device_12345_logs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"result_set": [
{
"URI": "/api/device/10857/log/88888",
"description": "Topology change: Parent DID changed from 444444 to 444445"
}
],
"searchspec": {
"fields": [
"_id",
"date",
"event",
"event_policy",
"message",
"repeats",
"severity",
"source"
],
"options": {"extended_fetch": {"default": "0",
"description": "Fetch entire resource if 1 (true), or resource link only if 0 (false)",
"type": "boolean"},
"hide_filterinfo": {"default": "0",
"description": "Suppress filterspec and current filter info if 1 (true)",
"type": "boolean"},
"limit": {"default": "100",
"description": "Number of records to retrieve",
"type": "int"},
"link_disp_field": {"default": "message",
"description": "When not using extended_fetch, this determines which field is used for the \"description\" of the resource link",
"type": "enum",
"values": ["severity",
"date",
"message",
"event_policy",
"source",
"event",
"repeats"]},
"offset": {"default": "0",
"description": "Specifies the index of the first returned resource within the entire result set",
"type": "int"}}
},
"total_matched": 821,
"total_returned": 1
}
53 changes: 42 additions & 11 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,62 @@ class DeviceTestCase(unittest.TestCase):

def test_device_bad_param(self):
with self.assertRaises(TypeError):
c = Device('potato!')
Device('potato!')

def test_device_bad_values(self):
with self.assertRaises(TypeError):
c = Device('me','pass','potato!')
Device('me', 'pass', 'potato!')

def test_get_device(self):
with requests_mock.mock() as m:
m.get('https://test.com/api/sysinfo', text=self._load_fixture('fixtures/sysinfo.json'))
m.get('https://test.com/api/device', text=self._load_fixture('fixtures/api_device_extended.json'))
m.get('https://test.com/api/device/12345', text=self._load_fixture('fixtures/api_device_12345.json'))
m.get('https://test.com/api/sysinfo',
text=self._load_fixture('fixtures/sysinfo.json'))
m.get('https://test.com/api/device',
text=self._load_fixture('fixtures/api_device_extended.json'))
m.get('https://test.com/api/device/12345',
text=self._load_fixture('fixtures/api_device_12345.json'))
c = Client('my', 'test', 'https://test.com')
device = c.get_device(12345)
self.assertEquals(device.details['name'], 'AU9/kubernetes-master01')
self.assertEquals(device.details['name'],
'AU9/kubernetes-master01')

def test_get_device_counters(self):
with requests_mock.mock() as m:
m.get('https://test.com/api/sysinfo', text=self._load_fixture('fixtures/sysinfo.json'))
m.get('https://test.com/api/device', text=self._load_fixture('fixtures/api_device_extended.json'))
m.get('https://test.com/api/device/12345', text=self._load_fixture('fixtures/api_device_12345.json'))
m.get('https://test.com/api/device/10857/performance_data', text=self._load_fixture('fixtures/api_device_12345_performance_data.json'))
m.get('https://test.com/api/sysinfo',
text=self._load_fixture('fixtures/sysinfo.json'))
m.get('https://test.com/api/device',
text=self._load_fixture('fixtures/api_device_extended.json'))
m.get('https://test.com/api/device/12345',
text=self._load_fixture('fixtures/api_device_12345.json'))
m.get('https://test.com/api/device/10857/performance_data',
text=self._load_fixture(
'fixtures/api_device_12345_performance_data.json'
))
c = Client('my', 'test', 'https://test.com')
device = c.get_device(12345)
device.performance_counters()
self.assertEquals(device.details['name'], 'AU9/kubernetes-master01')
self.assertEquals(device.details['name'],
'AU9/kubernetes-master01')

def test_get_logs(self):
with requests_mock.mock() as m:
m.get('https://test.com/api/sysinfo',
text=self._load_fixture('fixtures/sysinfo.json'))
m.get('https://test.com/api/device',
text=self._load_fixture('fixtures/api_device_extended.json'))
m.get('https://test.com/api/device/12345',
text=self._load_fixture('fixtures/api_device_12345.json'))
m.get('https://test.com/api/device/10857/log/',
text=self._load_fixture(
'fixtures/api_device_12345_logs.json'
))
m.get('https://test.com/api/device/10857/log/88888', text='{}')
c = Client('my', 'test', 'https://test.com')
device = c.get_device(12345)
self.assertEquals(device.details['name'],
'AU9/kubernetes-master01')
logs = device.get_logs()
self.assertEqual(1, len(logs))

def _load_fixture(self, path):
with open(os.path.join(os.getcwd(), 'tests', path), 'r') as fixture:
Expand Down

0 comments on commit 0bbd465

Please sign in to comment.