Support custom fields in hosts
Change-Id: I8771448a00a1ae6c70cfec6d2676b64dbd853d65
This commit is contained in:
parent
4f42097d02
commit
42fe1bfa92
@ -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"}
|
||||
)
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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',
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
43
surveil/tests/api/controllers/v1/datamodel/test_host.py
Normal file
43
surveil/tests/api/controllers/v1/datamodel/test_host.py
Normal file
@ -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())
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user