Merge "Refactored mongo handlers"

This commit is contained in:
Jenkins 2015-06-29 13:15:08 +00:00 committed by Gerrit Code Review
commit 3c0ac12ebb
13 changed files with 195 additions and 530 deletions

View File

@ -13,54 +13,17 @@
# under the License.
from surveil.api.datamodel.config import businessimpactmodulation
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class BusinessImpactModulationHandler(handler.Handler):
"""Fulfills a request on the business impact modulation resource."""
class BusinessImpactModulationHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Business Impact Modulation resource."""
def get(self, name):
"""Return a business impact modulation."""
t = (self.request.mongo_connection.
shinken.businessimpactmodulations).find_one(
{"business_impact_modulation_name": name},
{'_id': 0}
def __init__(self, *args, **kwargs):
super(BusinessImpactModulationHandler, self).__init__(
'businessimpactmodulations',
'business_impact_modulation_name',
businessimpactmodulation.BusinessImpactModulation,
*args,
**kwargs
)
return businessimpactmodulation.BusinessImpactModulation(**t)
def update(self, name, modulation):
"""Modify an existing business impact modulation."""
modulation_dict = modulation.as_dict()
if "business_impact_modulation_name" not in modulation_dict.keys():
modulation_dict['business_impact_modulation_name'] = modulation
self.request.mongo_connection.shinken.businessimpactmodulations.update(
{"business_impact_modulation_name": name},
{"$set": modulation_dict},
upsert=True
)
def delete(self, name):
"""Delete existing business impact modulation."""
self.request.mongo_connection.shinken.businessimpactmodulations.remove(
{"business_impact_modulation_name": name}
)
def create(self, modulation):
"""Create a new business impact modulation."""
self.request.mongo_connection.shinken.businessimpactmodulations.insert(
modulation.as_dict()
)
def get_all(self):
"""Return all business impact modulations."""
modulations = [m for m
in self.request.mongo_connection.
shinken.businessimpactmodulations.find(
{"register": {"$ne": "0"}},
{'_id': 0}
)]
modulations = [businessimpactmodulation.BusinessImpactModulation(**m)
for m in modulations]
return modulations

View File

@ -13,46 +13,17 @@
# under the License.
from surveil.api.datamodel.config import checkmodulation
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class CheckModulationHandler(handler.Handler):
"""Fulfills a request on the check modulation resource."""
class CheckModulationHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Check Modulation resource."""
def get(self, checkmodulation_name):
"""Return a check modulation."""
c = self.request.mongo_connection.shinken.checkmodulations.find_one(
{"checkmodulation_name": checkmodulation_name}
def __init__(self, *args, **kwargs):
super(CheckModulationHandler, self).__init__(
'checkmodulations',
'checkmodulation_name',
checkmodulation.CheckModulation,
*args,
**kwargs
)
return checkmodulation.CheckModulation(**c)
def update(self, checkmodulation_name, checkmodulation):
"""Modify existing check modulation."""
checkmodulation_dict = checkmodulation.as_dict()
if "checkmodulation_name" not in checkmodulation_dict.keys():
checkmodulation_dict['checkmodulation_name'] = checkmodulation_name
self.request.mongo_connection.shinken.checkmodulations.update(
{"checkmodulation_name": checkmodulation_name},
{"$set": checkmodulation_dict},
upsert=True
)
def delete(self, checkmodulation_name):
"""Delete an existing check modulation."""
self.request.mongo_connection.shinken.checkmodulations.remove(
{"checkmodulation_name": checkmodulation_name}
)
def create(self, data):
"""Create a new check modulation."""
self.request.mongo_connection.shinken.checkmodulations.insert(
data.as_dict()
)
def get_all(self):
"""Return all check modulation."""
find = self.request.mongo_connection.shinken.checkmodulations.find()
checkmodulations = [c for c in find]
return [checkmodulation.CheckModulation(**c) for c in checkmodulations]

View File

@ -13,46 +13,17 @@
# under the License.
from surveil.api.datamodel.config import command
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class CommandHandler(handler.Handler):
"""Fulfills a request on the service resource."""
class CommandHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Command resource."""
def get(self, command_name):
"""Return a command."""
c = self.request.mongo_connection.shinken.commands.find_one(
{"command_name": command_name}
def __init__(self, *args, **kwargs):
super(CommandHandler, self).__init__(
'commands',
'command_name',
command.Command,
*args,
**kwargs
)
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},
{"$set": command_dict},
upsert=True
)
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

@ -13,51 +13,17 @@
# under the License.
from surveil.api.datamodel.config import contact
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class ContactHandler(handler.Handler):
"""Fulfills a request on the contact resource."""
class ContactHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Contact resource."""
def get(self, contact_name):
"""Return a contact."""
c = self.request.mongo_connection.shinken.contacts.find_one(
{"contact_name": contact_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(ContactHandler, self).__init__(
'contacts',
'contact_name',
contact.Contact,
*args,
**kwargs
)
return contact.Contact(**c)
def update(self, contact_name, contact):
"""Modify an existing contact."""
contact_dict = contact.as_dict()
if "contact_name" not in contact_dict.keys():
contact_dict['contact_name'] = contact_name
self.request.mongo_connection.shinken.contacts.update(
{"contact_name": contact_name},
{"$set": contact_dict},
upsert=True
)
def delete(self, contact_name):
"""Delete existing contact."""
self.request.mongo_connection.shinken.contacts.remove(
{"contact_name": contact_name}
)
def create(self, contact):
"""Create a new contact."""
self.request.mongo_connection.shinken.contacts.insert(
contact.as_dict()
)
def get_all(self):
"""Return all contacts."""
contacts = [c for c
in self.request.mongo_connection.
shinken.contacts.find(
{"register": {"$ne": "0"}}, # Don't return templates
{'_id': 0}
)]
contacts = [contact.Contact(**c) for c in contacts]
return contacts

View File

@ -13,51 +13,17 @@
# under the License.
from surveil.api.datamodel.config import contactgroup
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class ContactGroupHandler(handler.Handler):
"""Fulfills a request on the contact group resource."""
class ContactGroupHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Contact Group resource."""
def get(self, group_name):
"""Return a contact group."""
g = self.request.mongo_connection.shinken.contactgroups.find_one(
{"contactgroup_name": group_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(ContactGroupHandler, self).__init__(
'contactgroups',
'contactgroup_name',
contactgroup.ContactGroup,
*args,
**kwargs
)
return contactgroup.ContactGroup(**g)
def update(self, group_name, group):
"""Modify an existing contact group."""
group_dict = group.as_dict()
if "contactgroup_name" not in group_dict.keys():
group_dict['contactgroup_name'] = group_name
self.request.mongo_connection.shinken.contactgroups.update(
{"contactgroup_name": group_name},
{"$set": group_dict},
upsert=True
)
def delete(self, group_name):
"""Delete existing contact group."""
self.request.mongo_connection.shinken.contactgroups.remove(
{"contactgroup_name": group_name}
)
def create(self, group):
"""Create a new contact group."""
self.request.mongo_connection.shinken.contactgroups.insert(
group.as_dict()
)
def get_all(self):
"""Return all contact groups."""
contactgroups = [g for g
in self.request.mongo_connection.
shinken.contactgroups.find(
{"register": {"$ne": "0"}},
{'_id': 0}
)]
contactgroups = [contactgroup.ContactGroup(**g) for g in contactgroups]
return contactgroups

View File

@ -13,51 +13,17 @@
# under the License.
from surveil.api.datamodel.config import host
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class HostHandler(handler.Handler):
"""Fulfills a request on the host resource."""
class HostHandler(mongo_object_handler.MongoObjectHandler):
"""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}
def __init__(self, *args, **kwargs):
super(HostHandler, self).__init__(
'hosts',
'host_name',
host.Host,
*args,
**kwargs
)
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},
{"$set": host_dict},
upsert=True
)
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

@ -13,51 +13,17 @@
# under the License.
from surveil.api.datamodel.config import hostgroup
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class HostGroupHandler(handler.Handler):
class HostGroupHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the host group resource."""
def get(self, group_name):
"""Return a host group."""
g = self.request.mongo_connection.shinken.hostgroups.find_one(
{"hostgroup_name": group_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(HostGroupHandler, self).__init__(
'hostgroups',
'hostgroup_name',
hostgroup.HostGroup,
*args,
**kwargs
)
return hostgroup.HostGroup(**g)
def update(self, group_name, group):
"""Modify an existing host group."""
group_dict = group.as_dict()
if "hostgroup_name" not in group_dict.keys():
group_dict['hostgroup_name'] = group_name
self.request.mongo_connection.shinken.hostgroups.update(
{"hostgroup_name": group_name},
{"$set": group_dict},
upsert=True
)
def delete(self, group_name):
"""Delete existing host group."""
self.request.mongo_connection.shinken.hostgroups.remove(
{"hostgroup_name": group_name}
)
def create(self, group):
"""Create a new host group."""
self.request.mongo_connection.shinken.hostgroups.insert(
group.as_dict()
)
def get_all(self):
"""Return all host groups."""
hostgroups = [g for g
in self.request.mongo_connection.
shinken.hostgroups.find(
{"register": {"$ne": "0"}},
{'_id': 0}
)]
hostgroups = [hostgroup.HostGroup(**g) for g in hostgroups]
return hostgroups

View File

@ -13,60 +13,17 @@
# under the License.
from surveil.api.datamodel.config import macromodulation
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class MacroModulationHandler(handler.Handler):
"""Fulfills a request on the macro modulation resource."""
class MacroModulationHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Macro Modulation resource."""
def get(self, modulation_name):
"""Return a macro modulation."""
m = self.request.mongo_connection.shinken.macromodulations.find_one(
{"macromodulation_name": modulation_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(MacroModulationHandler, self).__init__(
'macromodulations',
'macromodulation_name',
macromodulation.MacroModulation,
*args,
**kwargs
)
return macromodulation.MacroModulation(**m)
def update(self, modulation_name, modulation):
"""Modify an existing macro modulation."""
modulation_dict = modulation.as_dict()
if "macromodulation_name" not in modulation_dict.keys():
modulation_dict['contactgroup_name'] = modulation_name
self.request.mongo_connection.shinken.macromodulations.update(
{"macromodulation_name": modulation_name},
{"$set": modulation_dict},
upsert=True
)
def delete(self, modulation_name):
"""Delete existing macro modulation."""
self.request.mongo_connection.shinken.macromodulations.remove(
{"macromodulation_name": modulation_name}
)
def create(self, modulation):
"""Create a new macro modulation."""
self.request.mongo_connection.shinken.macromodulations.insert(
modulation.as_dict()
)
def get_all(self):
"""Return all macro modulation objects."""
modulations = [m for m
in self.request.mongo_connection.
shinken.macromodulations.find(
# Don't return templates
{
"register": {"$ne": "0"}
},
{
"_id": 0
}
)
]
modulations = [macromodulation.MacroModulation(**m)
for m in modulations]
return modulations

View File

@ -13,52 +13,17 @@
# under the License.
from surveil.api.datamodel.config import notificationway
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class NotificationWayHandler(handler.Handler):
def get(self, notificationway_name):
"""Return a notification way."""
class NotificationWayHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Notification Way resource."""
g = self.request.mongo_connection.shinken.notificationways.find_one(
{"notificationway_name": notificationway_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(NotificationWayHandler, self).__init__(
'notificationways',
'notificationway_name',
notificationway.NotificationWay,
*args,
**kwargs
)
return notificationway.NotificationWay(**g)
def update(self, notificationway_name, notificationway):
"""Modify an existing notification way."""
notificationway_dict = notificationway.as_dict()
if "notificationway_name" not in notificationway_dict.keys():
notificationway_dict['notificationway_name'] = notificationway_name
self.request.mongo_connection.shinken.notificationways.update(
{"notificationway_name": notificationway_name},
{"$set": notificationway_dict},
upsert=True
)
def delete(self, notificationway_name):
"""Delete existing notification way."""
self.request.mongo_connection.shinken.notificationways.remove(
{"notificationway_name": notificationway_name}
)
def create(self, notificationway):
"""Create a new notification way."""
self.request.mongo_connection.shinken.notificationways.insert(
notificationway.as_dict()
)
def get_all(self):
"""Return all notification way."""
notificationways = [
g for g in self.request.mongo_connection
.shinken.notificationways.find(
{"register": {"$ne": "0"}},
{'_id': 0}
)]
notificationways = [
notificationway.NotificationWay(**g) for g in notificationways
]
return notificationways

View File

@ -13,51 +13,17 @@
# under the License.
from surveil.api.datamodel.config import realm
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class RealmHandler(handler.Handler):
"""Fulfills a request on the realm resource."""
class RealmHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Realm resource."""
def get(self, realm_name):
"""Return a realm."""
r = self.request.mongo_connection.shinken.realms.find_one(
{"realm_name": realm_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(RealmHandler, self).__init__(
'realms',
'realm_name',
realm.Realm,
*args,
**kwargs
)
return realm.Realm(**r)
def update(self, realm_name, realm):
"""Modify an existing realm."""
realm_dict = realm.as_dict()
if "realm_name" not in realm_dict.keys():
realm_dict['realm_name'] = realm_name
self.request.mongo_connection.shinken.realms.update(
{"realm_name": realm_name},
{"$set": realm_dict},
upsert=True
)
def delete(self, realm_name):
"""Delete existing realm."""
self.request.mongo_connection.shinken.realms.remove(
{"realm_name": realm_name}
)
def create(self, realm):
"""Create a new realm."""
self.request.mongo_connection.shinken.realms.insert(
realm.as_dict()
)
def get_all(self):
"""Return all realms."""
realms = [c for c
in self.request.mongo_connection.
shinken.realms.find(
{"register": {"$ne": "0"}}, # Don't return templates
{'_id': 0}
)]
realms = [realm.Realm(**r) for r in realms]
return realms

View File

@ -13,51 +13,17 @@
# under the License.
from surveil.api.datamodel.config import servicegroup
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class ServiceGroupHandler(handler.Handler):
"""Fulfills a request on the service group resource."""
class ServiceGroupHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Service Group resource."""
def get(self, group_name):
"""Return a service group."""
s = self.request.mongo_connection.shinken.servicegroups.find_one(
{"servicegroup_name": group_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(ServiceGroupHandler, self).__init__(
'servicegroups',
'servicegroup_name',
servicegroup.ServiceGroup,
*args,
**kwargs
)
return servicegroup.ServiceGroup(**s)
def update(self, group_name, group):
"""Modify an existing service group."""
group_dict = group.as_dict()
if "servicegroup_name" not in group_dict.keys():
group_dict['servicegroup_name'] = group_name
self.request.mongo_connection.shinken.servicegroups.update(
{"servicegroup_name": group_name},
{"$set": group_dict},
upsert=True
)
def delete(self, group_name):
"""Delete existing service group."""
self.request.mongo_connection.shinken.servicegroups.remove(
{"servicegroup_name": group_name}
)
def create(self, group):
"""Create a new service group."""
self.request.mongo_connection.shinken.servicegroups.insert(
group.as_dict()
)
def get_all(self):
"""Return all service groups."""
servicegroups = [c for c
in self.request.mongo_connection.
shinken.servicegroups.find(
{"register": {"$ne": "0"}},
{'_id': 0}
)]
servicegroups = [servicegroup.ServiceGroup(**s) for s in servicegroups]
return servicegroups

View File

@ -13,51 +13,17 @@
# under the License.
from surveil.api.datamodel.config import timeperiod
from surveil.api.handlers import handler
from surveil.api.handlers import mongo_object_handler
class TimePeriodHandler(handler.Handler):
"""Fulfills a request on the contact resource."""
class TimePeriodHandler(mongo_object_handler.MongoObjectHandler):
"""Fulfills a request on the Time Period resource."""
def get(self, timeperiod_name):
"""Return a time period."""
t = self.request.mongo_connection.shinken.timeperiods.find_one(
{"timeperiod_name": timeperiod_name}, {'_id': 0}
def __init__(self, *args, **kwargs):
super(TimePeriodHandler, self).__init__(
'timeperiods',
'timeperiod_name',
timeperiod.TimePeriod,
*args,
**kwargs
)
return timeperiod.TimePeriod(**t)
def update(self, timeperiod_name, timeperiod):
"""Modify an existing time period."""
timeperiod_dict = timeperiod.as_dict()
if "timeperiod_name" not in timeperiod_dict.keys():
timeperiod_dict['timeperiod_name'] = timeperiod_name
self.request.mongo_connection.shinken.timeperiods.update(
{"timeperiod_name": timeperiod_name},
{"$set": timeperiod_dict},
upsert=True
)
def delete(self, timeperiod_name):
"""Delete existing time period."""
self.request.mongo_connection.shinken.timeperiods.remove(
{"timeperiod_name": timeperiod_name}
)
def create(self, timeperiod):
"""Create a new time period."""
self.request.mongo_connection.shinken.timeperiods.insert(
timeperiod.as_dict()
)
def get_all(self):
"""Return all time periods."""
timeperiods = [t for t
in self.request.mongo_connection.
shinken.timeperiods.find(
{"register": {"$ne": "0"}},
{'_id': 0}
)]
timeperiods = [timeperiod.TimePeriod(**t) for t in timeperiods]
return timeperiods

View File

@ -0,0 +1,76 @@
# 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.handlers import handler
class MongoObjectHandler(handler.Handler):
"""Fulfills a request on a MongoDB resource."""
def __init__(self,
resource_colleciton,
resource_key,
resource_datamodel,
*args,
**kwargs):
super(MongoObjectHandler, self).__init__(*args, **kwargs)
self.resource_collection = resource_colleciton
self.resource_key = resource_key
self.resource_datamodel = resource_datamodel
def _get_resource_collection(self):
shinken_db = self.request.mongo_connection.shinken
resource_colleciton = getattr(shinken_db, self.resource_collection)
return resource_colleciton
def get(self, resource_key_value):
"""Return the resource."""
r = self._get_resource_collection().find_one(
{self.resource_key: resource_key_value},
{'_id': 0}
)
return self.resource_datamodel(**r)
def update(self, resource_key_value, resource):
"""Modify an existing resource."""
resource_dict = resource.as_dict()
if self.resource_key not in resource_dict.keys():
resource_dict[self.resource_key] = resource_key_value
self._get_resource_collection().update(
{self.resource_key: resource_key_value},
{"$set": resource_dict},
upsert=True
)
def delete(self, resource_key_value):
"""Delete existing resource."""
self._get_resource_collection().remove(
{self.resource_key: resource_key_value}
)
def create(self, resource):
"""Create a new resource."""
self._get_resource_collection().insert(
resource.as_dict()
)
def get_all(self):
"""Return all resources."""
resources = [r for r
in self._get_resource_collection()
.find({"register": {"$ne": "0"}},
{'_id': 0})]
resources = [self.resource_datamodel(**r) for r in resources]
return resources