Add backend-independent access to cookiejar

Having the cookiejar available as attribute on the client instead of
some child objects exposes an interface which depending code can rely
on. This will help with upcoming efforts to switch the SOAP library
backing oslo.vmware.

This is part of phase 1 of
https://specs.openstack.org/openstack/oslo-specs/specs/victoria/oslo-vmware-soap-library-switch.html

Change-Id: I72082f10a184a2451dfda3d002a9288fefcef961
This commit is contained in:
Johannes Kulik 2020-05-11 15:57:32 +02:00
parent 32c8d43a20
commit dc4eb020ad
6 changed files with 41 additions and 14 deletions

View File

@ -492,7 +492,7 @@ class VmdkWriteHandle(VmdkHandle):
url, thumbprint = self._find_vmdk_url(lease_info, host, port)
self._vm_ref = lease_info.entity
cookies = session.vim.client.options.transport.cookiejar
cookies = session.vim.client.cookiejar
# Create HTTP connection to write to VMDK URL
if http_method == 'PUT':
overwrite = 't'
@ -600,7 +600,7 @@ class VmdkReadHandle(VmdkHandle):
# find URL of the VMDK file to be read and open connection
url, thumbprint = self._find_vmdk_url(lease_info, host, port)
cookies = session.vim.client.options.transport.cookiejar
cookies = session.vim.client.cookiejar
self._conn = self._create_read_connection(url,
cookies=cookies,
ssl_thumbprint=thumbprint)

View File

@ -209,6 +209,25 @@ class MemoryCache(cache.ObjectCache):
_CACHE = MemoryCache()
class CompatibilitySudsClient(client.Client):
"""suds client with added cookiejar attribute
The cookiejar properties allow reading/setting the cookiejar used by the
underlying transport.
"""
def __init__(self, *args, **kwargs):
super(CompatibilitySudsClient, self).__init__(*args, **kwargs)
@property
def cookiejar(self):
return self.options.transport.cookiejar
@cookiejar.setter
def cookiejar(self, cookies):
self.options.transport.session.cookies = cookies
self.options.transport.cookiejar = cookies
class Service(object):
"""Base class containing common functionality for invoking vSphere
services
@ -226,11 +245,11 @@ class Service(object):
insecure=insecure,
pool_maxsize=pool_maxsize,
connection_timeout=connection_timeout)
self.client = client.Client(self.wsdl_url,
transport=transport,
location=self.soap_url,
plugins=[ServiceMessagePlugin()],
cache=_CACHE)
self.client = CompatibilitySudsClient(self.wsdl_url,
transport=transport,
location=self.soap_url,
plugins=[ServiceMessagePlugin()],
cache=_CACHE)
self._service_content = None
self._vc_session_cookie = None
@ -312,7 +331,7 @@ class Service(object):
def get_http_cookie(self):
"""Return the vCenter session cookie."""
cookies = self.client.options.transport.cookiejar
cookies = self.client.cookiejar
for cookie in cookies:
if cookie.name.lower() == 'vmware_soap_session':
return cookie.value

View File

@ -217,7 +217,7 @@ class VmdkWriteHandleTest(base.TestCase):
vim_cookie = mock.Mock()
vim_cookie.name = 'name'
vim_cookie.value = 'value'
session.vim.client.options.transport.cookiejar = [vim_cookie]
session.vim.client.cookiejar = [vim_cookie]
return session
def test_init_failure(self):
@ -344,7 +344,7 @@ class VmdkReadHandleTest(base.TestCase):
vim_cookie = mock.Mock()
vim_cookie.name = 'name'
vim_cookie.value = 'value'
session.vim.client.options.transport.cookiejar = [vim_cookie]
session.vim.client.cookiejar = [vim_cookie]
return session
def test_init_failure(self):

View File

@ -60,7 +60,7 @@ class ServiceTest(base.TestCase):
def setUp(self):
super(ServiceTest, self).setUp()
patcher = mock.patch('suds.client.Client')
patcher = mock.patch('oslo_vmware.service.CompatibilitySudsClient')
self.addCleanup(patcher.stop)
self.SudsClientMock = patcher.start()
@ -377,7 +377,7 @@ class ServiceTest(base.TestCase):
cookie = mock.Mock()
cookie.name = 'vmware_soap_session'
cookie.value = cookie_value
svc_obj.client.options.transport.cookiejar = [cookie]
svc_obj.client.cookiejar = [cookie]
self.assertEqual(cookie_value, svc_obj.get_http_cookie())
def test_get_session_cookie_with_no_cookie(self):
@ -385,7 +385,7 @@ class ServiceTest(base.TestCase):
cookie = mock.Mock()
cookie.name = 'cookie'
cookie.value = 'xyz'
svc_obj.client.options.transport.cookiejar = [cookie]
svc_obj.client.cookiejar = [cookie]
self.assertIsNone(svc_obj.get_http_cookie())
def test_set_soap_headers(self):

View File

@ -31,7 +31,7 @@ class VimTest(base.TestCase):
def setUp(self):
super(VimTest, self).setUp()
patcher = mock.patch('suds.client.Client')
patcher = mock.patch('oslo_vmware.service.CompatibilitySudsClient')
self.addCleanup(patcher.stop)
self.SudsClientMock = patcher.start()
self.useFixture(i18n_fixture.ToggleLazy(True))

View File

@ -0,0 +1,8 @@
---
upgrade:
- Code accessing the ``cookiejar`` must use ``session.client.cookiejar``
instead of the previous ``session.client.options.transport.cookiejar``,
because with `this spec
<https://specs.openstack.org/openstack/oslo-specs/specs/victoria/oslo-vmware-soap-library-switch.html>`_
we switch the backing SOAP library and different libraries have different
locations for their transport and cookiejar objects.