Add str_to_bool util
A line like ``'True' == vim_obj.get('cert_verify', 'True') or False`` can be found many places in slightly different ways. It's not easy to maintenance and might incur bugs. This patch `adds str_to_bool` method as a common function for this process. Change-Id: Ieb708f8ddeb2748aff3ea009a9df613ad1f321ff
This commit is contained in:
parent
46da7f8f59
commit
fefe731ee7
|
@ -684,3 +684,13 @@ def str_to_bytes(value):
|
|||
value = bytes(value)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def str_to_bool(value):
|
||||
"""Convert string to Bool"""
|
||||
if value == 'True':
|
||||
return True
|
||||
if value == 'False':
|
||||
return False
|
||||
msg = _('Can\'t convert "{}" to Bool'.format(value))
|
||||
raise ValueError(msg)
|
||||
|
|
|
@ -28,6 +28,7 @@ from oslo_log import log as logging
|
|||
|
||||
from tacker._i18n import _
|
||||
from tacker.common import log
|
||||
from tacker.common import utils
|
||||
from tacker import context as t_context
|
||||
from tacker.extensions import nfvo
|
||||
from tacker.keymgr import API as KEYMGR_API
|
||||
|
@ -118,7 +119,8 @@ class OpenStack_Driver(abstract_vim_driver.VimAbstractDriver,
|
|||
|
||||
Initialize keystoneclient with provided authentication attributes.
|
||||
"""
|
||||
verify = 'True' == vim_obj['auth_cred'].get('cert_verify', 'True')
|
||||
verify = utils.str_to_bool(vim_obj['auth_cred']
|
||||
.get('cert_verify', 'True'))
|
||||
auth_url = vim_obj['auth_url']
|
||||
NfvoPlugin.validate_keystone_auth_url(
|
||||
auth_url=auth_url,
|
||||
|
@ -301,7 +303,7 @@ class OpenStack_Driver(abstract_vim_driver.VimAbstractDriver,
|
|||
:param client_type: openstack client to initialize
|
||||
:return: initialized client
|
||||
"""
|
||||
verify = 'True' == vim_obj.get('cert_verify', 'True')
|
||||
verify = utils.str_to_bool(vim_obj.get('cert_verify', 'True'))
|
||||
auth_url = vim_obj['auth_url']
|
||||
NfvoPlugin.validate_keystone_auth_url(
|
||||
auth_url=auth_url,
|
||||
|
@ -802,7 +804,7 @@ class NeutronClient(object):
|
|||
|
||||
def __init__(self, auth_attr):
|
||||
auth_cred = auth_attr.copy()
|
||||
verify = 'True' == auth_cred.pop('cert_verify', 'True') or False
|
||||
verify = utils.str_to_bool(auth_cred.pop('cert_verify', 'True'))
|
||||
auth = identity.Password(**auth_cred)
|
||||
sess = session.Session(auth=auth, verify=verify)
|
||||
self.client = neutron_client.Client(session=sess)
|
||||
|
|
|
@ -30,6 +30,7 @@ from oslo_serialization import jsonutils
|
|||
from tempest.lib import base
|
||||
|
||||
from tacker.common import clients
|
||||
from tacker.common import utils
|
||||
from tacker.plugins.common import constants as evt_constants
|
||||
from tacker.tests import constants
|
||||
from tacker.tests.utils import read_file
|
||||
|
@ -121,7 +122,7 @@ class BaseTackerTest(base.BaseTestCase):
|
|||
project_name=vim_params['project_name'],
|
||||
user_domain_name=vim_params['user_domain_name'],
|
||||
project_domain_name=vim_params['project_domain_name'])
|
||||
verify = 'True' == vim_params.pop('cert_verify', 'False')
|
||||
verify = utils.str_to_bool(vim_params.pop('cert_verify', 'False'))
|
||||
auth_ses = session.Session(auth=auth, verify=verify)
|
||||
return auth_ses
|
||||
|
||||
|
@ -146,7 +147,7 @@ class BaseTackerTest(base.BaseTestCase):
|
|||
project_name=vim_params['project_name'],
|
||||
user_domain_name=vim_params['user_domain_name'],
|
||||
project_domain_name=vim_params['project_domain_name'])
|
||||
verify = 'True' == vim_params.pop('cert_verify', 'False')
|
||||
verify = utils.str_to_bool(vim_params.pop('cert_verify', 'False'))
|
||||
auth_ses = session.Session(auth=auth, verify=verify)
|
||||
return nova_client.Client(constants.NOVA_CLIENT_VERSION,
|
||||
session=auth_ses)
|
||||
|
@ -160,7 +161,7 @@ class BaseTackerTest(base.BaseTestCase):
|
|||
project_name=vim_params['project_name'],
|
||||
user_domain_name=vim_params['user_domain_name'],
|
||||
project_domain_name=vim_params['project_domain_name'])
|
||||
verify = 'True' == vim_params.pop('cert_verify', 'False')
|
||||
verify = utils.str_to_bool(vim_params.pop('cert_verify', 'False'))
|
||||
auth_ses = session.Session(auth=auth, verify=verify)
|
||||
return neutron_client.Client(session=auth_ses)
|
||||
|
||||
|
@ -197,7 +198,7 @@ class BaseTackerTest(base.BaseTestCase):
|
|||
project_name=vim_params['project_name'],
|
||||
user_domain_name=vim_params['user_domain_name'],
|
||||
project_domain_name=vim_params['project_domain_name'])
|
||||
verify = 'True' == vim_params.pop('cert_verify', 'False')
|
||||
verify = utils.str_to_bool(vim_params.pop('cert_verify', 'False'))
|
||||
auth_ses = session.Session(auth=auth, verify=verify)
|
||||
return glance_client.Client(session=auth_ses)
|
||||
|
||||
|
@ -217,7 +218,7 @@ class BaseTackerTest(base.BaseTestCase):
|
|||
project_name=vim_params['project_name'],
|
||||
user_domain_name=vim_params['user_domain_name'],
|
||||
project_domain_name=vim_params['project_domain_name'])
|
||||
verify = 'True' == vim_params.pop('cert_verify', 'False')
|
||||
verify = utils.str_to_bool(vim_params.pop('cert_verify', 'False'))
|
||||
auth_ses = session.Session(auth=auth, verify=verify)
|
||||
return cinder_client.Client(constants.CINDER_CLIENT_VERSION,
|
||||
session=auth_ses)
|
||||
|
|
|
@ -34,6 +34,7 @@ from oslo_utils import uuidutils
|
|||
from tackerclient.v1_0 import client as tacker_client
|
||||
import yaml
|
||||
|
||||
from tacker.common.utils import str_to_bool
|
||||
from tacker.tests import constants
|
||||
from tacker.tests.functional.sol_separated_nfvo.vnflcm.fake_grant import Grant
|
||||
from tacker.tests.functional.sol_separated_nfvo_v2 import fake_grant_v2
|
||||
|
@ -98,7 +99,7 @@ class GrantServer:
|
|||
project_name=vim_params['project_name'],
|
||||
user_domain_name=vim_params['user_domain_name'],
|
||||
project_domain_name=vim_params['project_domain_name'])
|
||||
verify = 'True' == vim_params.pop('cert_verify', 'False')
|
||||
verify = str_to_bool(vim_params.pop('cert_verify', 'False'))
|
||||
auth_ses = session.Session(auth=auth, verify=verify)
|
||||
return auth_ses
|
||||
|
||||
|
@ -119,7 +120,7 @@ class GrantServer:
|
|||
project_name=vim_params['project_name'],
|
||||
user_domain_name=vim_params['user_domain_name'],
|
||||
project_domain_name=vim_params['project_domain_name'])
|
||||
verify = 'True' == vim_params.pop('cert_verify', 'False')
|
||||
verify = str_to_bool(vim_params.pop('cert_verify', 'False'))
|
||||
auth_ses = session.Session(auth=auth, verify=verify)
|
||||
return nova_client.Client(constants.NOVA_CLIENT_VERSION,
|
||||
session=auth_ses)
|
||||
|
|
|
@ -23,6 +23,8 @@ from keystoneclient import client
|
|||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
from tacker.common import utils
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
|
@ -52,7 +54,7 @@ class Keystone(object):
|
|||
return ses.get_endpoint(service_type, region_name)
|
||||
|
||||
def initialize_client(self, **kwargs):
|
||||
verify = 'True' == kwargs.pop('cert_verify', 'False')
|
||||
verify = utils.str_to_bool(kwargs.pop('cert_verify', 'False'))
|
||||
auth_plugin = v3.Password(**kwargs)
|
||||
ses = self.get_session(auth_plugin=auth_plugin, verify=verify)
|
||||
|
||||
|
|
|
@ -23,6 +23,8 @@ from keystoneauth1 import session
|
|||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
from tacker.common import utils
|
||||
|
||||
|
||||
DEFAULT_IDENTITY_VERSION = "v3"
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
@ -52,7 +54,7 @@ class Keystone(object):
|
|||
return ses.get_endpoint(service_type, region_name)
|
||||
|
||||
def initialize_client(self, **kwargs):
|
||||
verify = 'True' == kwargs.pop('cert_verify', 'True')
|
||||
verify = utils.str_to_bool(kwargs.pop('cert_verify', 'True'))
|
||||
if 'token' in kwargs:
|
||||
auth_plugin = identity.v3.Token(**kwargs)
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue