From 0ff915cc0477cd9d268c36801feaee693576d75e Mon Sep 17 00:00:00 2001 From: Anton Studenov Date: Mon, 10 Oct 2016 17:08:43 +0300 Subject: [PATCH] Update configuration driver arguments were moved to separate 'args' field in cloud_config Change-Id: If8100b19b56a127b448052e9a8ece5677e9f8ce5 --- README.rst | 12 +++++--- doc/source/usage.rst | 12 +++++--- examples/due.py | 6 ++-- examples/power_off_on_ipmi_node.py | 18 +++++++----- examples/power_off_on_vm_node.py | 11 ++++++-- examples/uno.py | 12 +++++--- os_faults/__init__.py | 28 +++++++----------- os_faults/api/error.py | 4 +++ os_faults/registry.py | 12 +++++++- os_faults/tests/unit/test_os_faults.py | 39 +++++++++++++++++--------- 10 files changed, 97 insertions(+), 57 deletions(-) diff --git a/README.rst b/README.rst index e34fe07..28e2d2e 100644 --- a/README.rst +++ b/README.rst @@ -26,13 +26,17 @@ library: cloud_config = { 'cloud_management': { 'driver': 'devstack', - 'address': 'devstack.local', - 'username': 'root', + 'args': { + 'address': 'devstack.local', + 'username': 'root', + } }, 'power_management': { 'driver': 'libvirt', - 'address': 'host.local', - 'username': 'root', + 'args': { + 'address': 'host.local', + 'username': 'root', + } } } diff --git a/doc/source/usage.rst b/doc/source/usage.rst index 8897367..825e053 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -14,13 +14,17 @@ library: cloud_config = { 'cloud_management': { 'driver': 'devstack', - 'address': 'devstack.local', - 'username': 'root', + 'args': { + 'address': 'devstack.local', + 'username': 'root', + } }, 'power_management': { 'driver': 'libvirt', - 'address': 'host.local', - 'username': 'root', + 'args': { + 'address': 'host.local', + 'username': 'root', + } } } diff --git a/examples/due.py b/examples/due.py index 12601bc..0d53705 100644 --- a/examples/due.py +++ b/examples/due.py @@ -20,8 +20,10 @@ def main(): cloud_config = { 'cloud_management': { 'driver': 'devstack', - 'address': 'devstack.local', - 'username': 'developer', + 'args': { + 'address': 'devstack.local', + 'username': 'developer', + } } } diff --git a/examples/power_off_on_ipmi_node.py b/examples/power_off_on_ipmi_node.py index aa6b238..7b48395 100644 --- a/examples/power_off_on_ipmi_node.py +++ b/examples/power_off_on_ipmi_node.py @@ -20,16 +20,20 @@ def main(): cloud_config = { 'cloud_management': { 'driver': 'fuel', - 'address': 'fuel.local', - 'username': 'root', + 'args': { + 'address': 'fuel.local', + 'username': 'root', + } }, 'power_management': { 'driver': 'ipmi', - 'mac_to_bmc': { - '00:00:00:00:00:00': { - 'address': '55.55.55.55', - 'username': 'foo', - 'password': 'bar', + 'args': { + 'mac_to_bmc': { + '00:00:00:00:00:00': { + 'address': '55.55.55.55', + 'username': 'foo', + 'password': 'bar', + } } } } diff --git a/examples/power_off_on_vm_node.py b/examples/power_off_on_vm_node.py index 04e9820..3e96149 100644 --- a/examples/power_off_on_vm_node.py +++ b/examples/power_off_on_vm_node.py @@ -20,12 +20,16 @@ def main(): cloud_config = { 'cloud_management': { 'driver': 'fuel', - 'address': 'fuel.local', - 'username': 'root', + 'args': { + 'address': 'fuel.local', + 'username': 'root', + } }, 'power_management': { 'driver': 'libvirt', - 'connection_uri': "qemu+ssh://ubuntu@host.local/system" + 'args': { + 'connection_uri': "qemu+ssh://ubuntu@host.local/system" + } } } @@ -44,6 +48,7 @@ def main(): one.poweroff() one.poweron() + if __name__ == '__main__': logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG) diff --git a/examples/uno.py b/examples/uno.py index 1f20f1a..e3acc34 100644 --- a/examples/uno.py +++ b/examples/uno.py @@ -20,13 +20,17 @@ def main(): cloud_config = { 'cloud_management': { 'driver': 'fuel', - 'address': 'fuel.local', - 'username': 'root', - 'private_key_file': '~/.ssh/os_faults', + 'args': { + 'address': 'fuel.local', + 'username': 'root', + 'private_key_file': '~/.ssh/os_faults', + } }, 'power_management': { 'driver': 'libvirt', - 'connection_uri': 'qemu+ssh://ubuntu@host.local/system' + 'args': { + 'connection_uri': 'qemu+ssh://ubuntu@host.local/system' + } } } diff --git a/os_faults/__init__.py b/os_faults/__init__.py index 4c40feb..c091968 100644 --- a/os_faults/__init__.py +++ b/os_faults/__init__.py @@ -52,16 +52,8 @@ def _read_config(config_filename): def _init_driver(params): - all_drivers = registry.get_drivers() - - name = params.get('driver') - if not name: - return None - - if name not in all_drivers: - raise error.OSFError('Driver %s is not found' % name) - - return all_drivers[name](params) + driver_cls = registry.get_driver(params['driver']) + return driver_cls(params.get('args', {})) def connect(cloud_config=None, config_filename=None): @@ -71,19 +63,19 @@ def connect(cloud_config=None, config_filename=None): :param config_filename: name of the file where to read config from :return: CloudManagement object """ - if not cloud_config: + if cloud_config is None: cloud_config = _read_config(config_filename) - cloud_management_params = cloud_config.get('cloud_management') or {} - cloud_management = _init_driver(cloud_management_params) - - if not cloud_management: + if 'cloud_management' not in cloud_config: raise error.OSFError('Cloud management driver name is not specified') - power_management_params = cloud_config.get('power_management') or {} - power_management = _init_driver(power_management_params) + cloud_management_conf = cloud_config['cloud_management'] + cloud_management = _init_driver(cloud_management_conf) - cloud_management.set_power_management(power_management) + power_management_conf = cloud_config.get('power_management', {}) + if power_management_conf: + power_management = _init_driver(power_management_conf) + cloud_management.set_power_management(power_management) return cloud_management diff --git a/os_faults/api/error.py b/os_faults/api/error.py index e777233..d624a5d 100644 --- a/os_faults/api/error.py +++ b/os_faults/api/error.py @@ -30,3 +30,7 @@ class ServiceError(OSFError): class NodeCollectionError(OSFError): """Base Error class for NodeCollection API""" + + +class OSFDriverNotFound(OSFError): + """Driver Not Found by os-faults registry""" diff --git a/os_faults/registry.py b/os_faults/registry.py index e819552..0631cb1 100644 --- a/os_faults/registry.py +++ b/os_faults/registry.py @@ -18,6 +18,7 @@ import sys from oslo_utils import importutils from os_faults.api import base_driver +from os_faults.api import error from os_faults import drivers DRIVERS = {} @@ -57,7 +58,7 @@ def _list_drivers(): klazz = class_info[1] if issubclass(klazz, base_driver.BaseDriver): - yield class_info[1] + yield klazz def get_drivers(): @@ -67,3 +68,12 @@ def get_drivers(): DRIVERS = dict((k.get_driver_name(), k) for k in _list_drivers()) return DRIVERS + + +def get_driver(name): + all_drivers = get_drivers() + + if name not in all_drivers: + raise error.OSFDriverNotFound('Driver %s is not found' % name) + + return all_drivers[name] diff --git a/os_faults/tests/unit/test_os_faults.py b/os_faults/tests/unit/test_os_faults.py index 3a56b23..1adbf2f 100644 --- a/os_faults/tests/unit/test_os_faults.py +++ b/os_faults/tests/unit/test_os_faults.py @@ -31,12 +31,16 @@ class OSFaultsTestCase(test.TestCase): self.cloud_config = { 'cloud_management': { 'driver': 'fuel', - 'address': '10.30.00.5', - 'username': 'root', + 'args': { + 'address': '10.30.00.5', + 'username': 'root', + } }, 'power_management': { 'driver': 'libvirt', - 'connection_uri': "qemu+ssh://user@10.30.20.21/system" + 'args': { + 'connection_uri': "qemu+ssh://user@10.30.20.21/system" + } } } @@ -44,8 +48,10 @@ class OSFaultsTestCase(test.TestCase): cloud_config = { 'cloud_management': { 'driver': 'devstack', - 'address': 'devstack.local', - 'username': 'developer', + 'args': { + 'address': 'devstack.local', + 'username': 'developer', + } } } destructor = os_faults.connect(cloud_config) @@ -61,16 +67,20 @@ class OSFaultsTestCase(test.TestCase): cloud_config = { 'cloud_management': { 'driver': 'fuel', - 'address': '10.30.00.5', - 'username': 'root', + 'args': { + 'address': '10.30.00.5', + 'username': 'root', + } }, 'power_management': { 'driver': 'ipmi', - 'mac_to_bmc': { - '00:00:00:00:00:00': { - 'address': '55.55.55.55', - 'username': 'foo', - 'password': 'bar', + 'args': { + 'mac_to_bmc': { + '00:00:00:00:00:00': { + 'address': '55.55.55.55', + 'username': 'foo', + 'password': 'bar', + } } } } @@ -85,10 +95,11 @@ class OSFaultsTestCase(test.TestCase): 'driver': 'non-existing', } } - self.assertRaises(error.OSFError, os_faults.connect, cloud_config) + self.assertRaises( + error.OSFDriverNotFound, os_faults.connect, cloud_config) def test_connect_driver_not_specified(self): - cloud_config = {} + cloud_config = {'foo': 'bar'} self.assertRaises(error.OSFError, os_faults.connect, cloud_config) @mock.patch('os.path.exists', return_value=True)