This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from librato/feature/service_attributes
Add all attributes to Service model
- Loading branch information
Showing
4 changed files
with
99 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,12 @@ def clean(self): | |
self.metrics = {'gauges': OrderedDict(), 'counters': OrderedDict()} | ||
self.instruments = OrderedDict() | ||
self.alerts = OrderedDict() | ||
self.services = OrderedDict() | ||
self.dashboards = OrderedDict() | ||
self.spaces = OrderedDict() | ||
self.last_i_id = 0 | ||
self.last_a_id = 0 | ||
self.last_service_id = 0 | ||
self.last_db_id = 0 | ||
self.last_spc_id = 0 | ||
self.last_chrt_id = 0 | ||
|
@@ -106,6 +108,12 @@ def create_alert(self, payload): | |
self.alerts[self.last_a_id] = payload | ||
return json.dumps(payload).encode('utf-8') | ||
|
||
def create_service(self, payload): | ||
self.last_service_id += 1 | ||
payload["id"] = self.last_service_id | ||
self.services[self.last_service_id] = payload | ||
return json.dumps(payload).encode('utf-8') | ||
|
||
def get_alert(self, name, payload): | ||
return self.list_of_alerts(name) | ||
|
||
|
@@ -126,6 +134,21 @@ def list_of_alerts(self, name=None): | |
alert.append(c_alert) | ||
return json.dumps(answer).encode('utf-8') | ||
|
||
def list_of_services(self): | ||
#{ | ||
# 'services': [ | ||
# {'id': 1, 'title': 'A Service', 'type': 'mail', | ||
# 'settings': {'addresses': '[email protected]'}} | ||
# ] | ||
#} | ||
answer = {} | ||
answer['query'] = {} | ||
answer['services'] = [] | ||
for _id, c_service in self.services.items(): | ||
c_service['id'] = _id | ||
answer['services'].append(c_service) | ||
return json.dumps(answer).encode('utf-8') | ||
|
||
def list_of_dashboards(self): | ||
answer = {} | ||
answer["query"] = {} | ||
|
@@ -458,6 +481,8 @@ def _json_body_based_on_request(self): | |
return server.get_instrument(r.uri) | ||
elif self._req_is_list_of_alerts(): | ||
return server.list_of_alerts() | ||
elif self._req_is_list_of_services(): | ||
return server.list_of_services() | ||
elif self._req_is_get_alert(): | ||
return server.get_alert(self._extract_name_from_url_parameters(), r.body) | ||
elif self._req_is_create_alert(): | ||
|
@@ -552,6 +577,10 @@ def _req_is_delete_alert(self): | |
return (self._method_is('DELETE') and | ||
re.match('/v1/alerts/\d+', self.request.uri)) | ||
|
||
# Services | ||
def _req_is_list_of_services(self): | ||
return self._method_is('GET') and self._path_is('/v1/services') | ||
|
||
# dashboards | ||
def _req_is_create_dashboard(self): | ||
return self._method_is('POST') and self._path_is('/v1/dashboards') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,49 @@ def test_immediate_condition(self): | |
cond._duration = 60 | ||
assert cond.immediate() == False | ||
|
||
class TestService(unittest.TestCase): | ||
def setUp(self): | ||
self.conn = librato.connect('user_test', 'key_test') | ||
self.sample_payload = { | ||
'title': 'Email Ops', | ||
'type': 'mail', | ||
'settings': {'addresses': '[email protected]'} | ||
} | ||
server.clean() | ||
|
||
def test_list_services(self): | ||
services = self.conn.list_services() | ||
self.assertEqual(len(services), 0) | ||
# Hack this into the server until we have a :create_service | ||
# method on the actual connection | ||
server.create_service(self.sample_payload) | ||
# id is 1 | ||
self.assertEqual(server.services[1], self.sample_payload) | ||
services = self.conn.list_services() | ||
self.assertEqual(len(services), 1) | ||
s = services[0] | ||
self.assertIsInstance(s, librato.alerts.Service) | ||
self.assertEqual(s.title, self.sample_payload['title']) | ||
self.assertEqual(s.type, self.sample_payload['type']) | ||
self.assertEqual(s.settings, self.sample_payload['settings']) | ||
|
||
def test_init_service(self): | ||
s = librato.alerts.Service(123, title='the title', type='mail', | ||
settings={'addresses': '[email protected]'}) | ||
self.assertEqual(s._id, 123) | ||
self.assertEqual(s.title, 'the title') | ||
self.assertEqual(s.type, 'mail') | ||
self.assertEqual(s.settings['addresses'], '[email protected]') | ||
|
||
def test_service_from_dict(self): | ||
payload = {'id': 123, 'title': 'the title', 'type': 'slack', | ||
'settings': {'room': 'a room'}} | ||
s = librato.alerts.Service.from_dict(self.conn, payload) | ||
self.assertEqual(s._id, 123) | ||
self.assertEqual(s.title, payload['title']) | ||
self.assertEqual(s.type, payload['type']) | ||
self.assertEqual(s.settings['room'], payload['settings']['room']) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |