From dc4eb020ad3f9ebd90d030bd20853019918c51b9 Mon Sep 17 00:00:00 2001 From: Johannes Kulik Date: Mon, 11 May 2020 15:57:32 +0200 Subject: [PATCH] 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 --- oslo_vmware/rw_handles.py | 4 +-- oslo_vmware/service.py | 31 +++++++++++++++---- oslo_vmware/tests/test_rw_handles.py | 4 +-- oslo_vmware/tests/test_service.py | 6 ++-- oslo_vmware/tests/test_vim.py | 2 +- ...tch-cookiejar-access-d7efcc23d0eaee98.yaml | 8 +++++ 6 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 releasenotes/notes/bp-oslo-vmware-soap-library-switch-cookiejar-access-d7efcc23d0eaee98.yaml diff --git a/oslo_vmware/rw_handles.py b/oslo_vmware/rw_handles.py index e0dfc7c9..e3769ecd 100644 --- a/oslo_vmware/rw_handles.py +++ b/oslo_vmware/rw_handles.py @@ -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) diff --git a/oslo_vmware/service.py b/oslo_vmware/service.py index 09702f43..89ed2219 100644 --- a/oslo_vmware/service.py +++ b/oslo_vmware/service.py @@ -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 diff --git a/oslo_vmware/tests/test_rw_handles.py b/oslo_vmware/tests/test_rw_handles.py index d6ef43db..6f7d4c10 100644 --- a/oslo_vmware/tests/test_rw_handles.py +++ b/oslo_vmware/tests/test_rw_handles.py @@ -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): diff --git a/oslo_vmware/tests/test_service.py b/oslo_vmware/tests/test_service.py index 8fc99458..b03c3ffd 100644 --- a/oslo_vmware/tests/test_service.py +++ b/oslo_vmware/tests/test_service.py @@ -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): diff --git a/oslo_vmware/tests/test_vim.py b/oslo_vmware/tests/test_vim.py index 0887a05a..ca0fb9e6 100644 --- a/oslo_vmware/tests/test_vim.py +++ b/oslo_vmware/tests/test_vim.py @@ -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)) diff --git a/releasenotes/notes/bp-oslo-vmware-soap-library-switch-cookiejar-access-d7efcc23d0eaee98.yaml b/releasenotes/notes/bp-oslo-vmware-soap-library-switch-cookiejar-access-d7efcc23d0eaee98.yaml new file mode 100644 index 00000000..3927e832 --- /dev/null +++ b/releasenotes/notes/bp-oslo-vmware-soap-library-switch-cookiejar-access-d7efcc23d0eaee98.yaml @@ -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 + `_ + we switch the backing SOAP library and different libraries have different + locations for their transport and cookiejar objects.