Added PUT and DELETE to hosts controller

Change-Id: I4899e97500b3a459c08303fac9cfb3cc175cb4b4
This commit is contained in:
aviau 2014-08-18 18:30:07 -04:00
parent 566f89cece
commit 57fd4d048f
3 changed files with 70 additions and 8 deletions
surveil
api
config.py
controllers/v1
tests/api/controllers/v1

@ -69,10 +69,3 @@ logging = {
}
}
}
# Custom Configurations must be in Python dictionary format::
#
# foo = {'bar':'baz'}
#
# All configurations are accessible at::
# pecan.conf

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import pecan
from pecan import rest
@ -32,6 +34,24 @@ class HostController(rest.RestController):
del host['_id']
return host
@pecan.expose()
def put(self):
"""Modify this host."""
body = json.loads(pecan.request.body.decode())
pecan.request.mongo_connection.shinken.hosts.update(
{"host_name": self._id},
body
)
pecan.response.status = 204
@pecan.expose()
def delete(self):
"""Delete this host."""
pecan.request.mongo_connection.shinken.hosts.remove(
{"host_name": self._id}
)
pecan.response.status = 204
class HostsController(rest.RestController):
@ -47,4 +67,4 @@ class HostsController(rest.RestController):
for host in hosts:
del host['_id']
return hosts
return hosts

@ -56,3 +56,52 @@ class TestRootController(functionalTest.FunctionalTest):
json.loads(response.body.decode()),
hosts[1]
)
self.assertEqual(response.status_int, 200)
def test_update_host(self):
hosts = [{u"use": u"generic-host", u"contact_groups": u"admins",
u"host_name": u"testhost1", u"address": u"www.google.ca"}]
self.mongoconnection.shinken.hosts.insert(copy.deepcopy(hosts))
put_body = {u"use": u"generic-host", u"contact_groups": u"admins",
u"host_name": u"testhost1", u"address": u"www.test.com"}
response = self.app.put_json("/v1/hosts/testhost1", params=put_body)
expected_hosts = [
{u"use": u"generic-host", u"contact_groups": u"admins",
u"host_name": u"testhost1", u"address": u"www.test.com"}
]
mongo_hosts = [host for host
in self.mongoconnection.shinken.hosts.find()]
for host in mongo_hosts:
del host['_id']
self.assertEqual(expected_hosts, mongo_hosts)
self.assertEqual(response.status_int, 204)
def test_delete_host(self):
hosts = [
{u"use": u"generic-host", u"contact_groups": u"admins",
u"host_name": u"testhost1", u"address": u"www.google.ca"},
{u"use": u"generic-host", u"contact_groups": u"admins",
u"host_name": u"testhost2", u"address": u"www.google.ca"}
]
self.mongoconnection.shinken.hosts.insert(copy.deepcopy(hosts))
response = self.app.delete('/v1/hosts/testhost2')
expected_hosts = [
{u"use": u"generic-host", u"contact_groups": u"admins",
u"host_name": u"testhost1", u"address": u"www.google.ca"}
]
mongo_hosts = [host for host
in self.mongoconnection.shinken.hosts.find()]
for host in mongo_hosts:
del host['_id']
self.assertEqual(expected_hosts, mongo_hosts)
self.assertEqual(response.status_int, 204)