Skip to content

Commit

Permalink
Add backend-independent access to cookiejar
Browse files Browse the repository at this point in the history
Having the cookiejar available as attribute on the client instead of
some child objects exposes an interface depending code can rely on. This
will help with upcoming efforts to switch the SOAP library backing
oslo.vmware.

Change-Id: I72082f10a184a2451dfda3d002a9288fefcef961
  • Loading branch information
joker-at-work committed May 11, 2020
1 parent 6a531ba commit 993fe5f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions oslo_vmware/rw_handles.py
Expand Up @@ -500,7 +500,7 @@ def __init__(self, session, host, port, rp_ref, vm_folder_ref, import_spec,
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'
Expand Down Expand Up @@ -604,7 +604,7 @@ def __init__(self, session, host, port, vm_ref, vmdk_path,

# 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)
Expand Down
37 changes: 31 additions & 6 deletions oslo_vmware/service.py
Expand Up @@ -216,6 +216,31 @@ def put(self, key, value, time=CACHE_TIMEOUT):
_CACHE = MemoryCache()


class CompatibilitySudsClient(client.Client):
"""suds client with added cookiejar attribute
The cookiejar properties allow reading/setting the cookiejar used by the
transport.
"""

"""
Having the cookiejar attribute available as an interface on the client
instead of some child object exposed the interface in a way that makes
switching clients easier.
"""
def __init__(self, *args, **kwargs):
super(CompatibilitySudsClient, self).__init__(*args, **kwargs)

@property
def cookiejar(self):
return self.client.options.transport.cookiejar

@cookiejar.setter
def cookiejar(self, cookies):
self.client.options.transport.session.cookies = cookies
self.client.options.transport.cookiejar = cookies


class Service(object):
"""Base class containing common functionality for invoking vSphere
services
Expand All @@ -235,11 +260,11 @@ def __init__(self, wsdl_url=None, soap_url=None,
pool_maxsize=pool_maxsize,
connection_timeout=connection_timeout,
pool_block=pool_block)
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

Expand Down Expand Up @@ -321,7 +346,7 @@ def service_content(self):

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
Expand Down
4 changes: 2 additions & 2 deletions oslo_vmware/tests/test_rw_handles.py
Expand Up @@ -198,7 +198,7 @@ def session_invoke_api_side_effect(module, method, *args, **kwargs):
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):
Expand Down Expand Up @@ -299,7 +299,7 @@ def session_invoke_api_side_effect(module, method, *args, **kwargs):
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):
Expand Down
6 changes: 3 additions & 3 deletions oslo_vmware/tests/test_service.py
Expand Up @@ -58,7 +58,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()

Expand Down Expand Up @@ -373,15 +373,15 @@ def test_get_session_cookie(self):
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):
svc_obj = service.Service()
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):
Expand Down
2 changes: 1 addition & 1 deletion oslo_vmware/tests/test_vim.py
Expand Up @@ -33,7 +33,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))
Expand Down

0 comments on commit 993fe5f

Please sign in to comment.