diff --git a/surveil/api/controllers/v1/hosts.py b/surveil/api/controllers/v1/hosts.py index a75720b..23494e2 100644 --- a/surveil/api/controllers/v1/hosts.py +++ b/surveil/api/controllers/v1/hosts.py @@ -161,7 +161,10 @@ class HostsController(rest.RestController): """Returns all hosts.""" hosts = [h for h in pecan.request.mongo_connection. - shinken.hosts.find(None, {'_id': 0})] + shinken.hosts.find( + {"register": {"$ne": "0"}}, # Don't return templates + {'_id': 0} + )] return [host.Host(**h) for h in hosts] diff --git a/surveil/api/controllers/v1/services.py b/surveil/api/controllers/v1/services.py index 35a5793..b065de1 100644 --- a/surveil/api/controllers/v1/services.py +++ b/surveil/api/controllers/v1/services.py @@ -24,8 +24,12 @@ class ServicesController(rest.RestController): @wsme_pecan.wsexpose([service.Service]) def get_all(self): """Returns all services.""" - services = [s for s - in pecan.request.mongo_connection.shinken.services.find()] + services = [ + s for s + in pecan.request.mongo_connection. + # Don't return templates + shinken.services.find({"register": {"$ne": "0"}}) + ] return [service.Service(**s) for s in services] diff --git a/surveil/tests/api/controllers/v1/test_hosts.py b/surveil/tests/api/controllers/v1/test_hosts.py index 5a57664..4e03426 100644 --- a/surveil/tests/api/controllers/v1/test_hosts.py +++ b/surveil/tests/api/controllers/v1/test_hosts.py @@ -77,6 +77,24 @@ class TestHostController(functionalTest.FunctionalTest): ) self.assertEqual(response.status_int, 200) + def test_get_all_hosts_no_templates(self): + self.mongoconnection.shinken.hosts.insert( + copy.deepcopy( + {"host_name": "bogus-router", "address": "192.168.1.254", + "max_check_attempts": 5, "check_period": "24x7", + "contacts": "admin,carl", "contact_groups": "router-admins", + "notification_interval": 30, "notification_period": "24x7", + "register": "0"} + ) + ) + response = self.app.get('/v1/hosts') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + self.hosts + ) + self.assertEqual(response.status_int, 200) + def test_get_specific_host(self): response = self.app.get('/v1/hosts/bogus-router333') diff --git a/surveil/tests/api/controllers/v1/test_services.py b/surveil/tests/api/controllers/v1/test_services.py index fca1dd7..a16ce56 100644 --- a/surveil/tests/api/controllers/v1/test_services.py +++ b/surveil/tests/api/controllers/v1/test_services.py @@ -77,6 +77,31 @@ class TestServiceController(functionalTest.FunctionalTest): ) self.assertEqual(response.status_int, 200) + def test_get_all_services_no_templates(self): + self.mongoconnection.shinken.services.insert( + copy.deepcopy( + {"host_name": "sample-server3", + "service_description": "check-disk-sdb", + "check_command": "check-disk!/dev/sdb1", + "max_check_attempts": 5, + "check_interval": 5, + "retry_interval": 3, + "check_period": "24x7", + "notification_interval": 30, + "notification_period": "24x7", + "contacts": "surveil-ptl,surveil-bob", + "register": "0", + "contact_groups": "linux-admins"} + ) + ) + response = self.app.get('/v1/services') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + self.services + ) + self.assertEqual(response.status_int, 200) + def test_add_host(self): new_service = { "host_name": "SOMEHOSTNAME",