Merge "Refactored controllers to use handlers"

This commit is contained in:
Jenkins 2015-03-28 01:12:25 +00:00 committed by Gerrit Code Review
commit 49e537bc67
19 changed files with 278 additions and 88 deletions

View File

@ -16,7 +16,8 @@ import pecan
from pecan import rest
import wsmeext.pecan as wsme_pecan
from surveil.api.controllers.v2.datamodel import command
from surveil.api.datamodel import command
from surveil.api.handlers import command_handler
class CommandController(rest.RestController):
@ -28,10 +29,9 @@ class CommandController(rest.RestController):
@wsme_pecan.wsexpose(command.Command)
def get(self):
"""Returns a specific command."""
c = pecan.request.mongo_connection.shinken.commands.find_one(
{"command_name": self._id}
)
return command.Command(**c)
handler = command_handler.CommandHandler(pecan.request)
c = handler.get(self._id)
return c
@wsme_pecan.wsexpose(None, body=command.Command, status_code=204)
def put(self, data):
@ -39,22 +39,14 @@ class CommandController(rest.RestController):
:param data: a command within the request body.
"""
command_dict = data.as_dict()
if "command_name" not in command_dict.keys():
command_dict['command_name'] = self._id
pecan.request.mongo_connection.shinken.commands.update(
{"command_name": self._id},
command_dict
)
handler = command_handler.CommandHandler(pecan.request)
handler.update(self._id, data)
@wsme_pecan.wsexpose(None, status_code=204)
def delete(self):
"""Delete this command."""
pecan.request.mongo_connection.shinken.commands.remove(
{"command_name": self._id}
)
handler = command_handler.CommandHandler(pecan.request)
handler.delete(self._id)
class CommandsController(rest.RestController):
@ -66,10 +58,9 @@ class CommandsController(rest.RestController):
@wsme_pecan.wsexpose([command.Command])
def get_all(self):
"""Returns all commands."""
commands = [c for c
in pecan.request.mongo_connection.shinken.commands.find()]
return [command.Command(**c) for c in commands]
handler = command_handler.CommandHandler(pecan.request)
commands = handler.get_all()
return commands
@wsme_pecan.wsexpose(command.Command,
body=command.Command,
@ -79,6 +70,5 @@ class CommandsController(rest.RestController):
:param data: a command within the request body.
"""
pecan.request.mongo_connection.shinken.commands.insert(
data.as_dict()
)
handler = command_handler.CommandHandler(pecan.request)
handler.create(data)

View File

@ -17,9 +17,11 @@ from pecan import rest
import requests
import wsmeext.pecan as wsme_pecan
from surveil.api.controllers.v2.datamodel import checkresult
from surveil.api.controllers.v2.datamodel import host
from surveil.api.controllers.v2.datamodel import service
from surveil.api.datamodel import checkresult
from surveil.api.datamodel import host
from surveil.api.datamodel import service
from surveil.api.handlers import host_handler
from surveil.api.handlers import service_handler
class ServiceCheckResultsSubController(rest.RestController):
@ -53,16 +55,12 @@ class HostServiceSubController(rest.RestController):
@wsme_pecan.wsexpose(service.Service)
def get(self):
"""Returns a specific service."""
mongo_s = pecan.request.mongo_connection.shinken.services.find_one(
{
"host_name": pecan.request.context['host_name'],
"service_description": pecan.request.context[
'service_description'
]
}
handler = service_handler.ServiceHandler(pecan.request)
s = handler.get(
pecan.request.context['host_name'],
pecan.request.context['service_description']
)
return service.Service(**mongo_s)
return s
class HostServicesSubController(rest.RestController):
@ -70,15 +68,10 @@ class HostServicesSubController(rest.RestController):
@wsme_pecan.wsexpose([service.Service])
def get_all(self):
"""Returns all services assocaited with this host."""
mongo_s = [
s for s
in pecan.request.mongo_connection.shinken.services.find(
{"host_name": pecan.request.context['host_name']}
)
]
services = [service.Service(**s) for s in mongo_s]
handler = service_handler.ServiceHandler(pecan.request)
services = handler.get_all(
host_name=pecan.request.context['host_name']
)
return services
@pecan.expose()
@ -117,10 +110,9 @@ class HostController(rest.RestController):
@wsme_pecan.wsexpose(host.Host)
def get(self):
"""Returns a specific host."""
h = pecan.request.mongo_connection.shinken.hosts.find_one(
{"host_name": self._id}, {'_id': 0}
)
return host.Host(**h)
handler = host_handler.HostHandler(pecan.request)
h = handler.get(self._id)
return h
@wsme_pecan.wsexpose(None, body=host.Host, status_code=204)
def put(self, data):
@ -128,22 +120,14 @@ class HostController(rest.RestController):
:param data: a host within the request body.
"""
host_dict = data.as_dict()
if "host_name" not in host_dict.keys():
host_dict['host_name'] = self._id
pecan.request.mongo_connection.shinken.hosts.update(
{"host_name": self._id},
host_dict
)
handler = host_handler.HostHandler(pecan.request)
handler.update(self._id, data)
@wsme_pecan.wsexpose(None, status_code=204)
def delete(self):
"""Delete this host."""
pecan.request.mongo_connection.shinken.hosts.remove(
{"host_name": self._id}
)
handler = host_handler.HostHandler(pecan.request)
handler.delete(self._id)
@pecan.expose()
def _lookup(self, *remainder):
@ -159,14 +143,9 @@ class HostsController(rest.RestController):
@wsme_pecan.wsexpose([host.Host])
def get_all(self):
"""Returns all hosts."""
hosts = [h for h
in pecan.request.mongo_connection.
shinken.hosts.find(
{"register": {"$ne": "0"}}, # Don't return templates
{'_id': 0}
)]
return [host.Host(**h) for h in hosts]
handler = host_handler.HostHandler(pecan.request)
hosts = handler.get_all()
return hosts
@wsme_pecan.wsexpose(host.Host, body=host.Host, status_code=201)
def post(self, data):
@ -174,6 +153,5 @@ class HostsController(rest.RestController):
:param data: a host within the request body.
"""
pecan.request.mongo_connection.shinken.hosts.insert(
data.as_dict()
)
handler = host_handler.HostHandler(pecan.request)
handler.create(data)

View File

@ -17,7 +17,7 @@ from pecan import rest
import requests
import wsmeext.pecan as wsme_pecan
from surveil.api.controllers.v2.datamodel import info
from surveil.api.datamodel import info
class ReloadConfigController(rest.RestController):

View File

@ -16,7 +16,8 @@ import pecan
from pecan import rest
import wsmeext.pecan as wsme_pecan
from surveil.api.controllers.v2.datamodel import service
from surveil.api.datamodel import service
from surveil.api.handlers import service_handler
class ServicesController(rest.RestController):
@ -24,14 +25,9 @@ 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.
# Don't return templates
shinken.services.find({"register": {"$ne": "0"}})
]
return [service.Service(**s) for s in services]
handler = service_handler.ServiceHandler(pecan.request)
services = handler.get_all()
return services
@wsme_pecan.wsexpose(service.Service,
body=service.Service,
@ -41,6 +37,6 @@ class ServicesController(rest.RestController):
:param data: a service within the request body.
"""
pecan.request.mongo_connection.shinken.services.insert(
data.as_dict()
)
handler = service_handler.ServiceHandler(pecan.request)
services = handler.create(data)
return services

View File

View File

@ -0,0 +1,57 @@
# 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.
from surveil.api.datamodel import command
from surveil.api.handlers import handler
class CommandHandler(handler.Handler):
"""Fulfills a request on the service resource."""
def get(self, command_name):
"""Return a command."""
c = self.request.mongo_connection.shinken.commands.find_one(
{"command_name": command_name}
)
return command.Command(**c)
def update(self, command_name, command):
"""Modify existing command."""
command_dict = command.as_dict()
if "command_name" not in command_dict.keys():
command_dict['command_name'] = command_name
self.request.mongo_connection.shinken.commands.update(
{"command_name": command_name},
command_dict
)
def delete(self, command_name):
"""Delete an existing command."""
self.request.mongo_connection.shinken.commands.remove(
{"command_name": command_name}
)
def create(self, data):
"""Create a new command."""
self.request.mongo_connection.shinken.commands.insert(
data.as_dict()
)
def get_all(self):
"""Return all commands."""
commands = [c for c
in self.request.mongo_connection.shinken.commands.find()]
return [command.Command(**c) for c in commands]

View File

@ -0,0 +1,41 @@
# Copyright 2013 - Rackspace
#
# 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.
class Handler(object):
"""The handler is responsible for fulfilling *one* request."""
def __init__(self, request):
super(Handler, self).__init__()
self.request = request
def get(self, *args, **kwargs):
"""Return a resource."""
raise NotImplementedError()
def update(self, *args, **kwargs):
"""Modify a resource."""
raise NotImplementedError()
def delete(self, *args, **kwargs):
"""Delete a resource."""
raise NotImplementedError()
def create(self, *args, **kwargs):
"""Create a new resource."""
raise NotImplementedError()
def get_all(self, *args, **kwargs):
"""Return all resources, based on the query provided."""
raise NotImplementedError()

View File

@ -0,0 +1,62 @@
# 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.
from surveil.api.datamodel import host
from surveil.api.handlers import handler
class HostHandler(handler.Handler):
"""Fulfills a request on the host resource."""
def get(self, host_name):
"""Return a host."""
h = self.request.mongo_connection.shinken.hosts.find_one(
{"host_name": host_name}, {'_id': 0}
)
return host.Host(**h)
def update(self, host_name, host):
"""Modify existing host."""
host_dict = host.as_dict()
if "host_name" not in host_dict.keys():
host_dict['host_name'] = host_name
self.request.mongo_connection.shinken.hosts.update(
{"host_name": host_name},
host_dict
)
def delete(self, host_name):
"""Delete existing host."""
self.request.mongo_connection.shinken.hosts.remove(
{"host_name": host_name}
)
def create(self, host):
"""Create a new host."""
self.request.mongo_connection.shinken.hosts.insert(
host.as_dict()
)
def get_all(self):
"""Return all hosts."""
hosts = [h for h
in self.request.mongo_connection.
shinken.hosts.find(
{"register": {"$ne": "0"}}, # Don't return templates
{'_id': 0}
)]
hosts = [host.Host(**h) for h in hosts]
return hosts

View File

@ -0,0 +1,66 @@
# 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.
from surveil.api.datamodel import service
from surveil.api.handlers import handler
class ServiceHandler(handler.Handler):
"""Fulfills a request on the service resource."""
def get(self, host_name, service_description):
"""Return a service."""
mongo_s = self.request.mongo_connection.shinken.services.find_one(
{"host_name": host_name,
"service_description": service_description}
)
return service.Service(**mongo_s)
def update(self, id, data):
"""Modify existing host."""
host_dict = data.as_dict()
if "host_name" not in host_dict.keys():
host_dict['host_name'] = id
self.request.mongo_connection.shinken.hosts.update(
{"host_name": id},
host_dict
)
def delete(self, id):
"""Delete existing host."""
self.request.mongo_connection.shinken.hosts.remove(
{"host_name": id}
)
def create(self, data):
"""Create a new service."""
self.request.mongo_connection.shinken.services.insert(
data.as_dict()
)
def get_all(self, host_name=None):
"""Return all services."""
filters = {"register": {"$ne": "0"}}
if host_name is not None:
filters['host_name'] = host_name
services = [
s for s
in self.request.mongo_connection.
# Don't return templates
shinken.services.find(filters)
]
return [service.Service(**s) for s in services]

View File

@ -15,7 +15,7 @@
import copy
import json
from surveil.api.controllers.v2.datamodel import command
from surveil.api.datamodel import command
from surveil.tests.api import functionalTest

View File

@ -17,7 +17,7 @@ import json
import httpretty
from surveil.api.controllers.v2.datamodel import host
from surveil.api.datamodel import host
from surveil.tests.api import functionalTest

View File

@ -15,7 +15,7 @@
import copy
import json
from surveil.api.controllers.v2.datamodel import service
from surveil.api.datamodel import service
from surveil.tests.api import functionalTest