From e04d2d542109b023505a84650c2aeeec6b1df6b9 Mon Sep 17 00:00:00 2001 From: Lin Yang Date: Wed, 20 Sep 2017 17:34:05 -0700 Subject: [PATCH] Allow set RSD related environment variables Make this plugin can recognize RSD arguments from environment variables, include RSD_URL, RSD_USERNAME, RSD_PASSWORD and RSD_VERIFY. So user can set them in environment variables to avoid input them in every command. Change-Id: I1b4fe8fc529b504b0f80d40427132e8b9364627c --- rsdclient/common/utils.py | 11 +++++++ rsdclient/osc/plugin.py | 47 +++++++++++++++++++--------- rsdclient/tests/common/test_utils.py | 10 +++++- rsdclient/v1/client.py | 13 ++++++++ 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/rsdclient/common/utils.py b/rsdclient/common/utils.py index 873fdfc..9191823 100644 --- a/rsdclient/common/utils.py +++ b/rsdclient/common/utils.py @@ -51,3 +51,14 @@ def print_dict(obj_list, field_names): pt.add_row([element.get(i.lower(), None) for i in field_names]) return pt + + +def str2boolean(string_obj): + """Convert string 'True' and 'False' to corresponding boolean obj""" + + if string_obj.lower() == "true": + return True + elif string_obj.lower() == "false": + return False + else: + return string_obj diff --git a/rsdclient/osc/plugin.py b/rsdclient/osc/plugin.py index dcc8856..117e3c5 100644 --- a/rsdclient/osc/plugin.py +++ b/rsdclient/osc/plugin.py @@ -19,6 +19,9 @@ import logging from osc_lib import utils +from rsdclient.common import utils as rsdclient_utils + + LOG = logging.getLogger(__name__) DEFAULT_API_VERSION = '1.2' @@ -37,10 +40,11 @@ def make_client(instance): API_VERSIONS) LOG.debug('Instantiating RSD client: %s', rsd_client) - client = rsd_client(base_url=instance._cli_options.rsd_url, - username=instance._cli_options.rsd_username, - password=instance._cli_options.rsd_password, - verify=instance._cli_options.rsd_disable_verify) + client = rsd_client( + base_url=instance._cli_options.rsd_url, + username=instance._cli_options.rsd_username, + password=instance._cli_options.rsd_password, + verify=rsdclient_utils.str2boolean(instance._cli_options.rsd_verify)) return client @@ -52,28 +56,41 @@ def build_option_parser(parser): metavar='', default=utils.env( 'RSD_API_VERSION', - default=DEFAULT_API_VERSION), + default='1.3'), help='RSD API version, default=' + DEFAULT_API_VERSION + ' (Env: RSD_API_VERSION)') parser.add_argument( '--rsd-url', metavar='', - default='https://localhost:8443/redfish/v1/', - help='The base URL to RSD pod manager') + default=utils.env( + 'RSD_URL', + default='https://localhost:8443/redfish/v1/'), + help='The base URL to RSD pod manager (Env: RSD_URL)') parser.add_argument( '--rsd-username', metavar='', - default='admin', - help='User account with admin access') + default=utils.env( + 'RSD_USERNAME', + default='admin'), + help='User account with admin access (Env: RSD_USERNAME)') parser.add_argument( '--rsd-password', metavar='', - default='admin', - help='User account password') + default=utils.env( + 'RSD_PASSWORD', + default='admin'), + help='User account password (Env: RSD_PASSWORD)') parser.add_argument( - '--rsd-disable-verify', - action='store_false', - help='If this is set, it will ignore verifying the SSL ' + - 'certificate') + '--rsd-verify', + metavar='', + default=utils.env( + 'RSD_VERIFY', + default='True'), + help='Either a boolean value, a path to a CA_BUNDLE file or directory' + ' with certificates of trusted CAs. If set to True it will ' + 'verify the host certificates; if False it will ignore verifying' + ' the SSL certificate; if it\'s a path the driver will use the ' + 'specified certificate or one of the certificates in the ' + 'directory. Defaults to True. (Env: RSD_VERIFY)') return parser diff --git a/rsdclient/tests/common/test_utils.py b/rsdclient/tests/common/test_utils.py index 983e93a..cd6f63a 100644 --- a/rsdclient/tests/common/test_utils.py +++ b/rsdclient/tests/common/test_utils.py @@ -21,8 +21,16 @@ from rsdclient.tests.common import fakes class UtilsTest(testtools.TestCase): - def test_compose_node(self): + def test_extract_attr(self): fake_node = fakes.FakeNode() result = utils.extract_attr(fake_node) expected = fakes.FAKE_NODE_PYTHON_DICT self.assertEqual(result, expected) + + def test_str2boolean(self): + self.assertEqual(utils.str2boolean("True"), True) + self.assertEqual(utils.str2boolean("true"), True) + self.assertEqual(utils.str2boolean("False"), False) + self.assertEqual(utils.str2boolean("false"), False) + self.assertEqual(utils.str2boolean("fake string"), "fake string") + self.assertEqual(utils.str2boolean(""), "") diff --git a/rsdclient/v1/client.py b/rsdclient/v1/client.py index aa459a7..a3b304c 100644 --- a/rsdclient/v1/client.py +++ b/rsdclient/v1/client.py @@ -23,6 +23,19 @@ from rsdclient.v1 import storage_service class Client(object): def __init__(self, base_url, username, password, verify=True): + """A client class to control RSD pod manager + + :param base_url: The base URL to RSD pod manager. + :param username: User account with admin access privilege + :param password: User account password + :param verify: Either a boolean value, a path to a CA_BUNDLE + file or directory with certificates of trusted CAs. If set to + True it will verify the host certificates; if False it will + ignore verifying the SSL certificate; if it's a path the driver + will use the specified certificate or one of the certificates + in the directory. Defaults to True. + """ + self.client = rsd_lib.RSDLib(base_url, username, password, verify=verify) self.node = node.NodeManager(self.client)