diff --git a/e2e/test_services.py b/e2e/test_services.py index 78ad9e8..9a4594a 100644 --- a/e2e/test_services.py +++ b/e2e/test_services.py @@ -110,3 +110,8 @@ def test_api_v0_services_current_oncall(team, service, user, role, event): results = re.json() assert results[0]['start'] == start assert results[0]['end'] == end + + re = requests.get(api_v0('services/%s/oncall' % service_name)) + assert re.status_code == 200 + results = re.json() + assert len(results) == 2 diff --git a/e2e/test_teams.py b/e2e/test_teams.py index a116c88..c24fa95 100644 --- a/e2e/test_teams.py +++ b/e2e/test_teams.py @@ -352,3 +352,7 @@ def test_api_v0_team_current_oncall(team, user, role, event): assert results[0]['start'] == start assert results[0]['end'] == end + re = requests.get(api_v0('teams/%s/oncall' % team_name)) + assert re.status_code == 200 + results = re.json() + assert len(results) == 2 diff --git a/src/oncall/api/v0/__init__.py b/src/oncall/api/v0/__init__.py index 5db721c..bff19d8 100644 --- a/src/oncall/api/v0/__init__.py +++ b/src/oncall/api/v0/__init__.py @@ -7,6 +7,7 @@ def init(application, config): application.add_route('/api/v0/teams', teams) application.add_route('/api/v0/teams/{team}', team) application.add_route('/api/v0/teams/{team}/summary', team_summary) + application.add_route('/api/v0/teams/{team}/oncall', team_oncall) application.add_route('/api/v0/teams/{team}/oncall/{role}', team_oncall) application.add_route('/api/v0/teams/{team}/changes', team_changes) @@ -36,6 +37,7 @@ def init(application, config): from . import services, service, service_oncall application.add_route('/api/v0/services', services) application.add_route('/api/v0/services/{service}', service) + application.add_route('/api/v0/services/{service}/oncall', service_oncall) application.add_route('/api/v0/services/{service}/oncall/{role}', service_oncall) from . import team_services, team_service, service_teams diff --git a/src/oncall/api/v0/service_oncall.py b/src/oncall/api/v0/service_oncall.py index 10b0f1f..ddae086 100644 --- a/src/oncall/api/v0/service_oncall.py +++ b/src/oncall/api/v0/service_oncall.py @@ -5,7 +5,7 @@ from ujson import dumps as json_dumps from ... import db -def on_get(req, resp, service, role): +def on_get(req, resp, service, role=None): ''' Get the current user on-call for a given service/role. Returns event start/end, contact info, and user name. @@ -40,19 +40,25 @@ def on_get(req, resp, service, role): ] ''' - get_oncall_query = '''SELECT `user`.`full_name` AS `user`, `event`.`start`, `event`.`end`, - `contact_mode`.`name` AS `mode`, `user_contact`.`destination` + get_oncall_query = '''SELECT `user`.`full_name` AS `full_name`, `event`.`start`, `event`.`end`, + `contact_mode`.`name` AS `mode`, `user_contact`.`destination`, `role`.`name` AS `role`, + `team`.`name` AS `team`, `user`.`name` AS `user` FROM `service` JOIN `team_service` ON `service`.`id` = `team_service`.`service_id` JOIN `event` ON `event`.`team_id` = `team_service`.`team_id` JOIN `user` ON `user`.`id` = `event`.`user_id` - JOIN `role` ON `role`.`id` = `event`.`role_id` AND `role`.`name` = %s + JOIN `role` ON `role`.`id` = `event`.`role_id` + JOIN `team` ON `team`.`id` = `event`.`team_id` LEFT JOIN `user_contact` ON `user`.`id` = `user_contact`.`user_id` LEFT JOIN `contact_mode` ON `contact_mode`.`id` = `user_contact`.`mode_id` WHERE UNIX_TIMESTAMP() BETWEEN `event`.`start` AND `event`.`end` - AND `service`.`name` = %s''' + AND `service`.`name` = %s ''' + query_params = [service] + if role is not None: + get_oncall_query += ' AND `role`.`name` = %s' + query_params.append(role) connection = db.connect() cursor = connection.cursor(db.DictCursor) - cursor.execute(get_oncall_query, (role, service)) + cursor.execute(get_oncall_query, query_params) data = cursor.fetchall() ret = {} for row in data: diff --git a/src/oncall/api/v0/team_oncall.py b/src/oncall/api/v0/team_oncall.py index 201e9ef..9fc8dd4 100644 --- a/src/oncall/api/v0/team_oncall.py +++ b/src/oncall/api/v0/team_oncall.py @@ -5,23 +5,7 @@ from ujson import dumps as json_dumps from ... import db -get_oncall_query = ''' - SELECT `user`.`full_name` AS `full_name`, - `event`.`start`, `event`.`end`, - `contact_mode`.`name` AS `mode`, - `user_contact`.`destination`, - `user`.`name` as `user` - FROM `event` - JOIN `user` ON `event`.`user_id` = `user`.`id` - JOIN `team` ON `event`.`team_id` = `team`.`id` - JOIN `role` ON `role`.`id` = `event`.`role_id` AND `role`.`name` = %s - LEFT JOIN `user_contact` ON `user`.`id` = `user_contact`.`user_id` - LEFT JOIN `contact_mode` ON `contact_mode`.`id` = `user_contact`.`mode_id` - WHERE UNIX_TIMESTAMP() BETWEEN `event`.`start` AND `event`.`end` - AND `team`.`name` = %s''' - - -def on_get(req, resp, team, role): +def on_get(req, resp, team, role=None): """ Get current active event for team based on given role. @@ -68,9 +52,30 @@ def on_get(req, resp, team, role): :statuscode 200: no error """ + get_oncall_query = ''' + SELECT `user`.`full_name` AS `full_name`, + `event`.`start`, `event`.`end`, + `contact_mode`.`name` AS `mode`, + `user_contact`.`destination`, + `user`.`name` AS `user`, + `team`.`name` AS `team`, + `role`.`name` AS `role` + FROM `event` + JOIN `user` ON `event`.`user_id` = `user`.`id` + JOIN `team` ON `event`.`team_id` = `team`.`id` + JOIN `role` ON `role`.`id` = `event`.`role_id` + LEFT JOIN `user_contact` ON `user`.`id` = `user_contact`.`user_id` + LEFT JOIN `contact_mode` ON `contact_mode`.`id` = `user_contact`.`mode_id` + WHERE UNIX_TIMESTAMP() BETWEEN `event`.`start` AND `event`.`end` + AND `team`.`name` = %s''' + + query_params = [team] + if role is not None: + get_oncall_query += 'AND `role`.`name` = %s' + query_params.append(role) connection = db.connect() cursor = connection.cursor(db.DictCursor) - cursor.execute(get_oncall_query, (role, team)) + cursor.execute(get_oncall_query, query_params) data = cursor.fetchall() ret = {} for row in data: diff --git a/src/oncall/api/v0/users.py b/src/oncall/api/v0/users.py index 06cc1ea..93a17e8 100644 --- a/src/oncall/api/v0/users.py +++ b/src/oncall/api/v0/users.py @@ -137,7 +137,7 @@ def on_get(req, resp): .. sourcecode:: http - GET /api/v0/events?team=foo-sre&end__gt=1487466146&role=primary HTTP/1.1 + GET /api/v0/users?name=jdoe HTTP/1.1 Host: example.com **Example response**: