add ca_file and token_file when initialize zvmconnectorrequesthandler

Change-Id: I1b03b8df879e3cf1612e2b6558005d99d7254c04
This commit is contained in:
dongyan yang 2018-01-16 18:33:20 +08:00
parent c7a0b18d56
commit 0085957cb5
5 changed files with 102 additions and 8 deletions

View File

@ -28,6 +28,20 @@ zvm_opts = [
help="""
URL to be used to communicate with z/VM Cloud Connector.
Example: https://10.10.10.1:8080.
"""),
cfg.StrOpt('zvm_cloud_connector_token_file',
default=None,
help="""
Token file that contains the admin token to be used when sending
request to z/VM Cloud Connector.
"""),
cfg.StrOpt('zvm_cloud_connector_ca_file',
default=None,
help="""
CA certificate file to be used to verify z/VM Cloud Connector
server certificate.
A string, it must be a path to a CA bundle to use.
"""),
]
@ -38,7 +52,7 @@ class ZVMInspector(virt_inspector.Inspector):
super(ZVMInspector, self).__init__(conf)
self.conf.register_opts(zvm_opts)
self._reqh = zvmutils.zVMConnectorRequestHandler(
self.conf.zvm_cloud_connector_url)
self.conf)
def inspect_vnics(self, instance, duration):
nics_data = self._inspect_inst_data(instance, 'vnics')

View File

@ -32,9 +32,26 @@ def get_inst_power_state(instance):
class zVMConnectorRequestHandler(object):
def __init__(self, connector_url):
_url = urlparse.urlparse(connector_url)
self._conn = connector.ZVMConnector(_url.hostname, _url.port)
def __init__(self, conf):
_url = urlparse.urlparse(conf.zvm_cloud_connector_url)
_ca_file = conf.zvm_cloud_connector_ca_file
_token_file = conf.zvm_cloud_connector_token_file
kwargs = {}
if _url.scheme == 'https':
kwargs['ssl_enabled'] = True
else:
kwargs['ssl_enabled'] = False
if _token_file is not None:
kwargs['token_path'] = _token_file
if (kwargs['ssl_enabled'] and
(_ca_file is not None)):
kwargs['verify'] = _ca_file
else:
kwargs['verify'] = False
self._conn = connector.ZVMConnector(_url.hostname, _url.port, **kwargs)
def call(self, func_name, *args, **kwargs):
results = self._conn.send_request(func_name, *args, **kwargs)

View File

@ -16,7 +16,9 @@
import mock
import unittest
from ceilometer_zvm.compute.virt.zvm import exception
from ceilometer_zvm.compute.virt.zvm import utils as zvmutils
from zvmconnector import connector
class TestZVMUtils(unittest.TestCase):
@ -36,3 +38,66 @@ class TestZVMUtils(unittest.TestCase):
def test_get_inst_power_state(self):
pst = zvmutils.get_inst_power_state(self._inst)
self.assertEqual(0x01, pst)
class TestZVMConnectorRequestHandler(unittest.TestCase):
@mock.patch.object(connector.ZVMConnector, '__init__')
def test_init_with_http(self, connector_init):
connector_init.return_value = None
conf = mock.Mock()
conf.zvm_cloud_connector_url = 'http://1.1.1.1:1111'
conf.zvm_cloud_connector_token_file = '/tmp/token.txt'
zvmutils.zVMConnectorRequestHandler(conf)
connector_init.assert_called_once_with('1.1.1.1', 1111,
ssl_enabled=False,
token_path='/tmp/token.txt',
verify=False)
@mock.patch.object(connector.ZVMConnector, '__init__')
def test_init_with_https_insecure(self, connector_init):
connector_init.return_value = None
conf = mock.Mock()
conf.zvm_cloud_connector_url = 'https://1.1.1.1:1111'
conf.zvm_cloud_connector_token_file = '/tmp/token.txt'
conf.zvm_cloud_connector_ca_file = None
zvmutils.zVMConnectorRequestHandler(conf)
connector_init.assert_called_once_with('1.1.1.1', 1111,
ssl_enabled=True,
token_path='/tmp/token.txt',
verify=False)
@mock.patch.object(connector.ZVMConnector, '__init__')
def test_init_with_https_secure(self, connector_init):
connector_init.return_value = None
conf = mock.Mock()
conf.zvm_cloud_connector_url = 'https://1.1.1.1:1111'
conf.zvm_cloud_connector_token_file = '/tmp/token.txt'
conf.zvm_cloud_connector_ca_file = '/tmp/ca.pem'
zvmutils.zVMConnectorRequestHandler(conf)
connector_init.assert_called_once_with('1.1.1.1', 1111,
ssl_enabled=True,
token_path='/tmp/token.txt',
verify='/tmp/ca.pem')
@mock.patch.object(connector.ZVMConnector, 'send_request')
def test_call(self, send_request):
send_request.return_value = {"overallRC": 0, 'output': "OK"}
conf = mock.Mock()
conf.zvm_cloud_connector_url = 'http://1.1.1.1:1111'
conf.zvm_cloud_connector_token_file = '/tmp/token.txt'
req_handler = zvmutils.zVMConnectorRequestHandler(conf)
info = req_handler.call('API', "parm1", "parm2")
send_request.assert_called_with('API', "parm1", "parm2")
self.assertEqual("OK", info)
@mock.patch.object(connector.ZVMConnector, 'send_request')
def test_call_exception(self, send_request):
conf = mock.Mock()
conf.zvm_cloud_connector_url = 'http://1.1.1.1:1111'
conf.zvm_cloud_connector_token_file = '/tmp/token.txt'
req_handler = zvmutils.zVMConnectorRequestHandler(conf)
send_request.return_value = {"overallRC": 1, 'output': ""}
self.assertRaises(exception.ZVMConnectorRequestFailed,
req_handler.call,
"API")

View File

@ -6,4 +6,4 @@ oslo.log>=1.8.0 # Apache-2.0
oslo.service>=0.6.0 # Apache-2.0
pbr<2.0,>=1.4
six>=1.9.0
zVMCloudConnector>=0.3.2 # Apache 2.0 License
zVMCloudConnector>=0.3.5 # Apache 2.0 License

View File

@ -11,7 +11,6 @@ setenv =
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
-egit+https://github.com/openstack/ceilometer@master#egg=ceilometer
-egit+https://github.com/mfcloud/python-zvm-sdk@master#egg=zvmsdk
commands = python setup.py testr --slowest --testr-args='{posargs}'
[testenv:pep8]
@ -21,7 +20,6 @@ commands = flake8
whitelist_externals = git
commands = /bin/cp -r {toxinidir}/ceilometer_zvm/compute/virt/zvm \
{toxinidir}/.tox/py27/src/ceilometer/ceilometer/compute/virt/
git --git-dir={toxinidir}/.tox/py27/src/zvmsdk/.git --work-tree={toxinidir}/.tox/py27/src/zvmsdk pull
python setup.py testr --slowest --testr-args='{posargs}'
[testenv:venv]
@ -31,7 +29,7 @@ commands = {posargs}
commands = python setup.py testr --coverage --testr-args='{posargs}'
[flake8]
ignore = E126,E127,E128
ignore = E126,E127,E128,E129
exclude = .venv,.git,.tox,dist,doc,*egg,build
[hacking]