Config API: added hostgroups

Change-Id: I0105dd4b6901fd039e4f991cf96abee82b8dc137
This commit is contained in:
aviau 2015-04-28 16:45:22 -04:00
parent 45ea20ea19
commit a7cdaaaa6e
5 changed files with 262 additions and 1 deletions

View File

@ -14,6 +14,7 @@
from surveil.api.controllers.v2.config import commands from surveil.api.controllers.v2.config import commands
from surveil.api.controllers.v2.config import contacts from surveil.api.controllers.v2.config import contacts
from surveil.api.controllers.v2.config import hostgroups
from surveil.api.controllers.v2.config import hosts from surveil.api.controllers.v2.config import hosts
from surveil.api.controllers.v2.config import realms from surveil.api.controllers.v2.config import realms
from surveil.api.controllers.v2.config import reload_config from surveil.api.controllers.v2.config import reload_config
@ -34,7 +35,7 @@ class ConfigController(rest.RestController):
timeperiods = timeperiods.TimePeriodsController() timeperiods = timeperiods.TimePeriodsController()
realms = realms.RealmsController() realms = realms.RealmsController()
servicegroups = servicegroup.ServiceGroupsController() servicegroups = servicegroup.ServiceGroupsController()
# hostgroups = HostGroupsController() hostgroups = hostgroups.HostGroupsController()
# contactgroups = ContactGroupsController() # contactgroups = ContactGroupsController()
# notificationways = NotificationWayController() # notificationways = NotificationWayController()
# engine = EngineController() # 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 hostgroup
from surveil.api.handlers.config import hostgroup_handler
class HostGroupsController(rest.RestController):
@wsme_pecan.wsexpose([hostgroup.HostGroup])
def get_all(self):
"""Returns all host groups."""
handler = hostgroup_handler.HostGroupHandler(pecan.request)
host_groups = handler.get_all()
return host_groups
@wsme_pecan.wsexpose(hostgroup.HostGroup, unicode)
def get_one(self, group_name):
"""Returns a host group."""
handler = hostgroup_handler.HostGroupHandler(pecan.request)
hostgroup = handler.get(group_name)
return hostgroup
@wsme_pecan.wsexpose(body=hostgroup.HostGroup, status_code=201)
def post(self, data):
"""Create a new host group.
:param data: a host group within the request body.
"""
handler = hostgroup_handler.HostGroupHandler(pecan.request)
handler.create(data)
@wsme_pecan.wsexpose(hostgroup.HostGroup, unicode, status_code=204)
def delete(self, group_name):
"""Returns a specific host group."""
handler = hostgroup_handler.HostGroupHandler(pecan.request)
handler.delete(group_name)
@wsme_pecan.wsexpose(hostgroup.HostGroup,
unicode,
body=hostgroup.HostGroup,
status_code=204)
def put(self, group_name, hostgroup):
"""Update a specific host group."""
handler = hostgroup_handler.HostGroupHandler(pecan.request)
handler.update(group_name, hostgroup)

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 HostGroup(types.Base):
hostgroup_name = wsme.wsattr(wtypes.text, mandatory=True)
members = wsme.wsattr(wtypes.text, mandatory=False)
alias = wsme.wsattr(wtypes.text, mandatory=False)
hostgroup_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(
hostgroup_name='dbservices',
alias='Novell Servers',
members='netware1,netware2,netware3,netware4'
)

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

@ -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 hostgroup
from surveil.tests.api import functionalTest
class TestHostGroupsController(functionalTest.FunctionalTest):
def setUp(self):
super(TestHostGroupsController, self).setUp()
self.groups = [
{
'hostgroup_name': 'novell-servers',
'members': 'netware1,netware2,netware3,netware4',
},
{
'hostgroup_name': 'otherservers',
'members': 'googul,sfl',
},
]
self.mongoconnection.shinken.hostgroups.insert(
copy.deepcopy(self.groups)
)
def test_get_all_hostgroups(self):
response = self.app.get('/v2/config/hostgroups')
self.assert_count_equal_backport(
json.loads(response.body.decode()),
self.groups
)
self.assertEqual(response.status_int, 200)
def test_get_one_hostgroups(self):
response = self.app.get('/v2/config/hostgroups/novell-servers')
self.assertEqual(
json.loads(response.body.decode()),
self.groups[0]
)
def test_create_hostgroup(self):
s = hostgroup.HostGroup(
hostgroup_name='John',
members="marie,bob,joe",
)
self.app.post_json('/v2/config/hostgroups', s.as_dict())
self.assertIsNotNone(
self.mongoconnection.shinken.hostgroups.find_one(s.as_dict())
)
def test_delete_hostgroup(self):
self.assertIsNotNone(
self.mongoconnection.shinken.hostgroups.find_one(self.groups[0])
)
self.app.delete('/v2/config/hostgroups/novell-servers')
self.assertIsNone(
self.mongoconnection.shinken.hostgroups.find_one(self.groups[0])
)
def test_put_hostgroup(self):
self.assertEqual(
self.mongoconnection.shinken.hostgroups.find_one(
{'hostgroup_name': 'novell-servers'}
)['members'],
'netware1,netware2,netware3,netware4'
)
self.app.put_json(
'/v2/config/hostgroups/novell-servers',
{"hostgroup_name": "novell-servers",
"members": "updated"}
)
self.assertEqual(
self.mongoconnection.shinken.hostgroups.find_one(
{'hostgroup_name': 'novell-servers'}
)['members'],
'updated'
)