From 42fe1bfa92fe96e3cca14e14fd17255670c928cd Mon Sep 17 00:00:00 2001 From: aviau Date: Mon, 26 Jan 2015 16:22:56 -0500 Subject: [PATCH] Support custom fields in hosts Change-Id: I8771448a00a1ae6c70cfec6d2676b64dbd853d65 --- surveil/api/controllers/v1/datamodel/host.py | 27 ++++++++++++ surveil/api/controllers/v1/hosts.py | 5 ++- surveil/cmd/os_discovery.py | 26 +++++------ .../api/controllers/v1/datamodel/__init__.py | 0 .../api/controllers/v1/datamodel/test_host.py | 43 +++++++++++++++++++ .../tests/api/controllers/v1/test_hosts.py | 4 +- 6 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 surveil/tests/api/controllers/v1/datamodel/__init__.py create mode 100644 surveil/tests/api/controllers/v1/datamodel/test_host.py diff --git a/surveil/api/controllers/v1/datamodel/host.py b/surveil/api/controllers/v1/datamodel/host.py index 0968752..404db65 100644 --- a/surveil/api/controllers/v1/datamodel/host.py +++ b/surveil/api/controllers/v1/datamodel/host.py @@ -44,6 +44,32 @@ class Host(types.Base): use = wsme.wsattr(wtypes.text, mandatory=False) """The template to use for this host""" + # TODO(aviau): Custom fields starting without '_' should raise an error. + custom_fields = wsme.wsattr( + wtypes.DictType(wtypes.text, wtypes.text), + mandatory=False + ) + """Custom fields for the host""" + + def __init__(self, **kwargs): + super(Host, self).__init__(**kwargs) + + # Custom fields start with '_'. Detect them ans assign them. + custom_fields = [i for i in kwargs.items() + if isinstance(i[0], str) and i[0][0] == '_'] + if len(custom_fields) > 0: + self.custom_fields = {} + for item in custom_fields: + self.custom_fields[item[0]] = item[1] + + def as_dict(self): + host_dict = super(Host, self).as_dict() + custom_fields = host_dict.pop("custom_fields", None) + if custom_fields: + for item in custom_fields.items(): + host_dict[item[0]] = item[1] + return host_dict + @classmethod def sample(cls): return cls( @@ -56,4 +82,5 @@ class Host(types.Base): contact_groups="router-admins", notification_interval=30, notification_period="24x7", + custom_fields={"OS_AUTH_URL": "http://localhost:8080/v2"} ) diff --git a/surveil/api/controllers/v1/hosts.py b/surveil/api/controllers/v1/hosts.py index 780e2eb..a75720b 100644 --- a/surveil/api/controllers/v1/hosts.py +++ b/surveil/api/controllers/v1/hosts.py @@ -118,7 +118,7 @@ class HostController(rest.RestController): def get(self): """Returns a specific host.""" h = pecan.request.mongo_connection.shinken.hosts.find_one( - {"host_name": self._id} + {"host_name": self._id}, {'_id': 0} ) return host.Host(**h) @@ -160,7 +160,8 @@ class HostsController(rest.RestController): def get_all(self): """Returns all hosts.""" hosts = [h for h - in pecan.request.mongo_connection.shinken.hosts.find()] + in pecan.request.mongo_connection. + shinken.hosts.find(None, {'_id': 0})] return [host.Host(**h) for h in hosts] diff --git a/surveil/cmd/os_discovery.py b/surveil/cmd/os_discovery.py index da3a65d..09150af 100644 --- a/surveil/cmd/os_discovery.py +++ b/surveil/cmd/os_discovery.py @@ -99,13 +99,13 @@ def main(args=None): host_name='OS_keystone_host_%s' % ep['id'], use='linux-keystone', address=urlparse.urlparse(ep['publicURL']).hostname, - - _OS_AUTH_URL=ep['publicURL'], - _OS_USERNAME=cfg.os_username, - _OS_PASSWORD=cfg.os_password, - _OS_TENANT_NAME=cfg.os_tenant_name, - _KS_SERVICES='identity', - + custom_fields={ + '_OS_AUTH_URL': ep['publicURL'], + '_OS_USERNAME': cfg.os_username, + '_OS_PASSWORD': cfg.os_password, + '_OS_TENANT_NAME': cfg.os_tenant_name, + '_KS_SERVICES': 'identity', + } ) for ep in endpoints.get('image', []): @@ -113,11 +113,13 @@ def main(args=None): host_name='OS_glance_host_%s' % ep['id'], use='linux-glance', address=urlparse.urlparse(ep['publicURL']).hostname, - _OS_AUTH_URL=ep['publicURL'], - _OS_USERNAME=cfg.os_username, - _OS_PASSWORD=cfg.os_password, - _OS_TENANT_NAME=cfg.os_tenant_name, - _OS_GLANCE_URL=ep['publicURL'] + '/v1', + custom_fields={ + '_OS_AUTH_URL': ep['publicURL'], + '_OS_USERNAME': cfg.os_username, + '_OS_PASSWORD': cfg.os_password, + '_OS_TENANT_NAME': cfg.os_tenant_name, + '_OS_GLANCE_URL': ep['publicURL'] + '/v1', + } ) diff --git a/surveil/tests/api/controllers/v1/datamodel/__init__.py b/surveil/tests/api/controllers/v1/datamodel/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveil/tests/api/controllers/v1/datamodel/test_host.py b/surveil/tests/api/controllers/v1/datamodel/test_host.py new file mode 100644 index 0000000..84ea95a --- /dev/null +++ b/surveil/tests/api/controllers/v1/datamodel/test_host.py @@ -0,0 +1,43 @@ +# 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.controllers.v1.datamodel import host +from surveil.tests import base + + +class TestHostDataModel(base.BaseTestCase): + + def test_host_as_and_from_dict(self): + h = host.Host( + host_name="bogus-router", + address="192.168.1.254", + max_check_attempts=5, + check_period="24x7", + contacts="admin,carl", + contact_groups="router-admins", + notification_interval=30, + notification_period="24x7", + custom_fields={"_OS_AUTH_URL": "http://localhost:8080/v2"} + ) + + h_dict = {'check_period': u'24x7', 'notification_interval': 30, + 'contacts': u'admin,carl', 'notification_period': u'24x7', + 'contact_groups': u'router-admins', + 'host_name': u'bogus-router', 'address': u'192.168.1.254', + 'max_check_attempts': 5, + '_OS_AUTH_URL': 'http://localhost:8080/v2'} + + self.assertEqual(h.as_dict(), h_dict) + self.assertEqual(h_dict, host.Host(**h_dict).as_dict()) diff --git a/surveil/tests/api/controllers/v1/test_hosts.py b/surveil/tests/api/controllers/v1/test_hosts.py index c7eac44..5a57664 100644 --- a/surveil/tests/api/controllers/v1/test_hosts.py +++ b/surveil/tests/api/controllers/v1/test_hosts.py @@ -103,7 +103,7 @@ class TestHostController(functionalTest.FunctionalTest): mongo_host = host.Host( **self.mongoconnection.shinken.hosts.find_one( - {'host_name': 'bogus-router333'} + {'host_name': 'bogus-router333'}, {'_id': 0} ) ) @@ -133,7 +133,7 @@ class TestHostController(functionalTest.FunctionalTest): response = self.app.post_json("/v1/hosts", params=new_host) hosts = [host.Host(**h).as_dict() for h - in self.mongoconnection.shinken.hosts.find()] + in self.mongoconnection.shinken.hosts.find(None, {'_id': 0})] self.assertTrue(new_host in hosts) self.assertEqual(response.status_int, 201)