Config API: added servicegroups

Change-Id: I5c0864b9d5318719f7392c29951948ddfba5f1fe
This commit is contained in:
aviau 2015-04-28 16:20:38 -04:00
parent 548ca1deee
commit 45ea20ea19
5 changed files with 262 additions and 1 deletions

View File

@ -17,6 +17,7 @@ from surveil.api.controllers.v2.config import contacts
from surveil.api.controllers.v2.config import hosts
from surveil.api.controllers.v2.config import realms
from surveil.api.controllers.v2.config import reload_config
from surveil.api.controllers.v2.config import servicegroup
from surveil.api.controllers.v2.config import services
from surveil.api.controllers.v2.config import timeperiods
@ -32,8 +33,8 @@ class ConfigController(rest.RestController):
contacts = contacts.ContactsController()
timeperiods = timeperiods.TimePeriodsController()
realms = realms.RealmsController()
servicegroups = servicegroup.ServiceGroupsController()
# hostgroups = HostGroupsController()
# contactgroups = ContactGroupsController()
# servicegroups = ServiceGroupsController()
# notificationways = NotificationWayController()
# engine = EngineController()

View File

@ -0,0 +1,62 @@
# 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 servicegroup
from surveil.api.handlers.config import servicegroup_handler
class ServiceGroupsController(rest.RestController):
@wsme_pecan.wsexpose([servicegroup.ServiceGroup])
def get_all(self):
"""Returns all service groups."""
handler = servicegroup_handler.ServiceGroupHandler(pecan.request)
service_groups = handler.get_all()
return service_groups
@wsme_pecan.wsexpose(servicegroup.ServiceGroup, unicode)
def get_one(self, group_name):
"""Returns a service group."""
handler = servicegroup_handler.ServiceGroupHandler(pecan.request)
servicegroup = handler.get(group_name)
return servicegroup
@wsme_pecan.wsexpose(body=servicegroup.ServiceGroup, status_code=201)
def post(self, data):
"""Create a new service group.
:param data: a service group within the request body.
"""
handler = servicegroup_handler.ServiceGroupHandler(pecan.request)
handler.create(data)
@wsme_pecan.wsexpose(servicegroup.ServiceGroup, unicode, status_code=204)
def delete(self, group_name):
"""Returns a specific service group."""
handler = servicegroup_handler.ServiceGroupHandler(pecan.request)
handler.delete(group_name)
@wsme_pecan.wsexpose(servicegroup.ServiceGroup,
unicode,
body=servicegroup.ServiceGroup,
status_code=204)
def put(self, group_name, servicegroup):
"""Update a specific service group."""
handler = servicegroup_handler.ServiceGroupHandler(pecan.request)
handler.update(group_name, servicegroup)

View File

@ -0,0 +1,36 @@
# 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 ServiceGroup(types.Base):
servicegroup_name = wsme.wsattr(wtypes.text, mandatory=True)
members = wsme.wsattr(wtypes.text, mandatory=True)
alias = wsme.wsattr(wtypes.text, mandatory=False)
servicegroup_members = wsme.wsattr(wtypes.text, mandatory=False)
notes = wsme.wsattr(wtypes.text, mandatory=False)
notes_url = wsme.wsattr(wtypes.text, mandatory=False)
action_url = wsme.wsattr(wtypes.text, mandatory=False)
@classmethod
def sample(cls):
return cls(
servicegroup_name='dbservices',
alias='Database Services',
members='ms1,SQL Server,ms1,SQL Serverc Agent,ms1,SQL DTC'
)

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.config import servicegroup
from surveil.api.handlers import handler
class ServiceGroupHandler(handler.Handler):
"""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}
)
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},
group_dict
)
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

@ -0,0 +1,100 @@
# 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 copy
import json
from surveil.api.datamodel.config import servicegroup
from surveil.tests.api import functionalTest
class TestServiceGroupsController(functionalTest.FunctionalTest):
def setUp(self):
super(TestServiceGroupsController, self).setUp()
self.groups = [
{
'servicegroup_name': 'dbservices',
'members': 'ms1,SQL Server,ms1,SQL Serverc Agent,ms1,SQL DTC',
},
{
'servicegroup_name': 'otherservices',
'members': 'some,other,member',
},
]
self.mongoconnection.shinken.servicegroups.insert(
copy.deepcopy(self.groups)
)
def test_get_all_servicegroups(self):
response = self.app.get('/v2/config/servicegroups')
self.assert_count_equal_backport(
json.loads(response.body.decode()),
self.groups
)
self.assertEqual(response.status_int, 200)
def test_get_one_servicegroup(self):
response = self.app.get('/v2/config/servicegroups/dbservices')
self.assertEqual(
json.loads(response.body.decode()),
self.groups[0]
)
def test_create_servicegroup(self):
s = servicegroup.ServiceGroup(
servicegroup_name='John',
members="marie,bob,joe",
)
self.app.post_json('/v2/config/servicegroups', s.as_dict())
self.assertIsNotNone(
self.mongoconnection.shinken.servicegroups.find_one(s.as_dict())
)
def test_delete_servicegroup(self):
self.assertIsNotNone(
self.mongoconnection.shinken.servicegroups.find_one(self.groups[0])
)
self.app.delete('/v2/config/servicegroups/dbservices')
self.assertIsNone(
self.mongoconnection.shinken.servicegroups.find_one(self.groups[0])
)
def test_put_servicegroup(self):
self.assertEqual(
self.mongoconnection.shinken.servicegroups.find_one(
{'servicegroup_name': 'dbservices'}
)['members'],
'ms1,SQL Server,ms1,SQL Serverc Agent,ms1,SQL DTC'
)
self.app.put_json(
'/v2/config/servicegroups/dbservices',
{"servicegroup_name": "dbservices",
"members": "updated"}
)
self.assertEqual(
self.mongoconnection.shinken.servicegroups.find_one(
{'servicegroup_name': 'dbservices'}
)['members'],
'updated'
)