Added basic Services controller

Change-Id: I3d55447480458a8bc3f3c201f58763953827c296
This commit is contained in:
aviau 2014-08-21 17:01:25 -04:00
parent 09dd47b37e
commit df0e8d83ef
5 changed files with 213 additions and 1 deletions

View File

@ -22,6 +22,16 @@ Hosts
.. autotype:: surveil.api.controllers.v1.datamodel.host.Host
:members:
Services
========
.. rest-controller:: surveil.api.controllers.v1.services:ServicesController
:webprefix: /v1/services
.. autotype:: surveil.api.controllers.v1.datamodel.service.Service
:members:
Commands
========
@ -32,4 +42,4 @@ Commands
:webprefix: /v1/hosts
.. autotype:: surveil.api.controllers.v1.datamodel.command.Command
:members:
:members:

View File

@ -0,0 +1,58 @@
# Copyright 2014 - Savoir-Faire Linux inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import wsme
import wsme.types as wtypes
from surveil.api.controllers.v1.datamodel import types
class Service(types.Base):
host_name = wsme.wsattr(wtypes.text, mandatory=True)
service_description = wsme.wsattr(wtypes.text, mandatory=True)
check_command = wsme.wsattr(wtypes.text, mandatory=True)
max_check_attempts = wsme.wsattr(wtypes.text, mandatory=True)
check_interval = wsme.wsattr(wtypes.text, mandatory=True)
retry_interval = wsme.wsattr(wtypes.text, mandatory=True)
check_period = wsme.wsattr(wtypes.text, mandatory=True)
notification_interval = wsme.wsattr(wtypes.text, mandatory=True)
notification_period = wsme.wsattr(wtypes.text, mandatory=True)
contacts = wsme.wsattr(wtypes.text, mandatory=True)
contact_groups = wsme.wsattr(wtypes.text, mandatory=True)
@classmethod
def sample(cls):
return cls(
host_name="sample-server",
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",
contact_groups="linux-admins",
)

View File

@ -0,0 +1,42 @@
# Copyright 2014 - Savoir-Faire Linux inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import pecan
from pecan import rest
import wsmeext.pecan as wsme_pecan
from surveil.api.controllers.v1.datamodel import service
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()]
return [service.Service(**s) for s in services]
@wsme_pecan.wsexpose(service.Service,
body=service.Service,
status_code=201)
def post(self, data):
"""Create a new service.
:param data: a service within the request body.
"""
pecan.request.mongo_connection.shinken.services.insert(
data.as_dict()
)

View File

@ -15,6 +15,7 @@
from surveil.api.controllers.v1 import commands
from surveil.api.controllers.v1 import hello
from surveil.api.controllers.v1 import hosts
from surveil.api.controllers.v1 import services
class V1Controller(object):
@ -22,3 +23,4 @@ class V1Controller(object):
hello = hello.HelloController()
hosts = hosts.HostsController()
commands = commands.CommandsController()
services = services.ServicesController()

View File

@ -0,0 +1,100 @@
# Copyright 2014 - Savoir-Faire Linux inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import copy
import json
from surveil.tests.api import functionalTest
class TestServiceController(functionalTest.FunctionalTest):
def setUp(self):
super(TestServiceController, self).setUp()
self.services = [
{
"host_name": "sample-server1",
"service_description": "check-",
"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",
"contact_groups": "linux-admins"
},
{
"host_name": "sample-server2",
"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",
"contact_groups": "linux-admins"
},
{
"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",
"contact_groups": "linux-admins"
},
]
self.mongoconnection.shinken.services.insert(
copy.deepcopy(self.services)
)
def test_get_all_services(self):
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",
"service_description": "check-new-thing",
"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",
"contact_groups": "linux-admins"
}
response = self.app.post_json("/v1/services", params=new_service)
services = [s for s in self.mongoconnection.shinken.services.find()]
for s in services:
del s["_id"]
self.assertTrue(new_service in services)
self.assertEqual(response.status_int, 201)