Config API: added businessimpactmodulations

Change-Id: I04363232f3bfc778981cf9bc572b5b8450ae9f95
This commit is contained in:
aviau 2015-05-05 14:18:53 -04:00
parent 562ad7809d
commit 9cd8f2e48d
6 changed files with 301 additions and 0 deletions

View File

@ -48,6 +48,12 @@ Commands
.. rest-controller:: surveil.api.controllers.v2.config.commands:CommandController .. rest-controller:: surveil.api.controllers.v2.config.commands:CommandController
:webprefix: /v2/config/commands :webprefix: /v2/config/commands
Business impact modulations
===========================
.. rest-controller:: surveil.api.controllers.v2.config.businessimpactmodulations:BusinessImpactModulationsController
:webprefix: /v2/config/businessimpactmodulations
types documentation types documentation
=================== ===================
@ -61,3 +67,5 @@ types documentation
.. autotype:: surveil.api.datamodel.checkresult.CheckResult .. autotype:: surveil.api.datamodel.checkresult.CheckResult
:members: :members:
.. autotype:: surveil.api.datamodel.config.businessimpactmodulation.BuisnessImpactModulation
:members:

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from surveil.api.controllers.v2.config import businessimpactmodulations
from surveil.api.controllers.v2.config import commands from surveil.api.controllers.v2.config import commands
from surveil.api.controllers.v2.config import contactgroups from surveil.api.controllers.v2.config import contactgroups
from surveil.api.controllers.v2.config import contacts from surveil.api.controllers.v2.config import contacts
@ -38,5 +39,8 @@ class ConfigController(rest.RestController):
servicegroups = servicegroup.ServiceGroupsController() servicegroups = servicegroup.ServiceGroupsController()
hostgroups = hostgroups.HostGroupsController() hostgroups = hostgroups.HostGroupsController()
contactgroups = contactgroups.ContactGroupsController() contactgroups = contactgroups.ContactGroupsController()
businessimpactmodulations = (businessimpactmodulations.
BusinessImpactModulationsController())
# notificationways = NotificationWayController() # notificationways = NotificationWayController()
# engine = EngineController() # engine = EngineController()

View File

@ -0,0 +1,70 @@
# Copyright 2015 - 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.datamodel.config import businessimpactmodulation as mod
from surveil.api.handlers.config import businessimpactmodulation_handler as bh
from surveil.common import util
class BusinessImpactModulationsController(rest.RestController):
@util.policy_enforce(['authenticated'])
@wsme_pecan.wsexpose([mod.BusinessImpactModulation])
def get_all(self):
"""Returns all business impact modulations."""
handler = bh.BusinessImpactModulationHandler(pecan.request)
modulations = handler.get_all()
return modulations
@util.policy_enforce(['authenticated'])
@wsme_pecan.wsexpose(mod.BusinessImpactModulation, unicode)
def get_one(self, modulation_name):
"""Returns a specific business impact modulation."""
handler = bh.BusinessImpactModulationHandler(pecan.request)
modulation = handler.get(modulation_name)
return modulation
@util.policy_enforce(['authenticated'])
@wsme_pecan.wsexpose(body=mod.BusinessImpactModulation, status_code=201)
def post(self, data):
"""Create a new business impact modulation.
:param data: a business impact modulation within the request body.
"""
handler = bh.BusinessImpactModulationHandler(pecan.request)
handler.create(data)
@util.policy_enforce(['authenticated'])
@wsme_pecan.wsexpose(mod.BusinessImpactModulation,
unicode,
status_code=204)
def delete(self, modulation_name):
"""Returns a specific business impact modulation."""
handler = bh.BusinessImpactModulationHandler(pecan.request)
handler.delete(modulation_name)
@util.policy_enforce(['authenticated'])
@wsme_pecan.wsexpose(mod.BusinessImpactModulation,
unicode,
body=mod.BusinessImpactModulation,
status_code=204)
def put(self, modulaion_name, modulation):
"""Update a specific business impact modulation."""
handler = bh.BusinessImpactModulationHandler(pecan.request)
handler.update(modulaion_name, modulation)

View File

@ -0,0 +1,34 @@
# Copyright 2015 - 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.datamodel import types
class BusinessImpactModulation(types.Base):
business_impact_modulation_name = wsme.wsattr(wtypes.text, mandatory=True)
business_impact = wsme.wsattr(int, mandatory=True)
modulation_period = wsme.wsattr(wtypes.text, mandatory=True)
@classmethod
def sample(cls):
return cls(
business_impact_modulation_name='LowImpactOnNight',
business_impact=1,
modulation_period='night'
)

View File

@ -0,0 +1,65 @@
# Copyright 2015 - 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.config import businessimpactmodulation
from surveil.api.handlers import handler
class BusinessImpactModulationHandler(handler.Handler):
"""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}
)
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},
modulation_dict
)
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

@ -0,0 +1,120 @@
# 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 TestBusinessImpactModulationController(functionalTest.FunctionalTest):
def setUp(self):
super(TestBusinessImpactModulationController, self).setUp()
self.modulations = [
{
'business_impact_modulation_name': 'LowImpactOnNight',
'business_impact': 1,
'modulation_period': 'night'
},
{
'business_impact_modulation_name': 'LowImpactOnDay',
'business_impact': 1,
'modulation_period': 'day'
},
]
self.mongoconnection.shinken.businessimpactmodulations.insert(
copy.deepcopy(self.modulations)
)
def test_get_all_modulations(self):
response = self.get('/v2/config/businessimpactmodulations')
self.assertItemsEqual(
json.loads(response.body.decode()),
[
{'business_impact': 1,
'business_impact_modulation_name': 'LowImpactOnDay',
'modulation_period': 'day'},
{'business_impact': 1,
'business_impact_modulation_name': 'LowImpactOnNight',
'modulation_period': 'night'},
]
)
self.assertEqual(response.status_int, 200)
def test_get_one_modulation(self):
response = self.get(
'/v2/config/businessimpactmodulations/LowImpactOnDay'
)
self.assertEqual(
json.loads(response.body.decode()),
{'business_impact': 1,
'business_impact_modulation_name': 'LowImpactOnDay',
'modulation_period': 'day'}
)
def test_create_modulation(self):
m = {'business_impact': 1,
'business_impact_modulation_name': 'testtt',
'modulation_period': 'day'}
self.assertIsNone(
self.mongoconnection.shinken.businessimpactmodulations.find_one(m)
)
self.post_json('/v2/config/businessimpactmodulations', m)
self.assertIsNotNone(
self.mongoconnection.shinken.businessimpactmodulations.find_one(m)
)
def test_delete_modulation(self):
self.assertIsNotNone(
self.mongoconnection.shinken.businessimpactmodulations.find_one(
{"business_impact_modulation_name": 'LowImpactOnNight'}
)
)
self.delete('/v2/config/businessimpactmodulations/LowImpactOnNight')
self.assertIsNone(
self.mongoconnection.shinken.timeperiods.find_one(
{"business_impact_modulation_name": 'LowImpactOnNight'}
)
)
def test_put_modulation(self):
self.assertEqual(
self.mongoconnection.shinken.businessimpactmodulations.find_one(
{'business_impact_modulation_name': 'LowImpactOnNight'}
)['modulation_period'],
'night'
)
self.put_json(
'/v2/config/businessimpactmodulations/LowImpactOnNight',
{"business_impact_modulation_name": "LowImpactOnNight",
'business_impact': 1,
"modulation_period": 'updated'}
)
self.assertEqual(
self.mongoconnection.shinken.businessimpactmodulations.find_one(
{'business_impact_modulation_name': 'LowImpactOnNight'}
)['modulation_period'],
'updated'
)