Merge "gcs: Remove logic for google-api-python-client < 1.6.0"
This commit is contained in:
commit
e8e9e35d5a
@ -27,7 +27,6 @@ Server-centric flow is used for authentication.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import importlib.metadata as importlib_metadata
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -50,7 +49,6 @@ from oslo_config import cfg
|
|||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from oslo_utils import secretutils
|
from oslo_utils import secretutils
|
||||||
from oslo_utils import timeutils
|
from oslo_utils import timeutils
|
||||||
from packaging import version
|
|
||||||
|
|
||||||
from cinder.backup import chunkeddriver
|
from cinder.backup import chunkeddriver
|
||||||
from cinder import exception
|
from cinder import exception
|
||||||
@ -142,17 +140,6 @@ def gcs_logger(func):
|
|||||||
return func_wrapper
|
return func_wrapper
|
||||||
|
|
||||||
|
|
||||||
def _get_dist_version(name):
|
|
||||||
"""Mock-able wrapper for importlib_metadata.version()
|
|
||||||
|
|
||||||
The module name where version() is found varies by python
|
|
||||||
version. This function makes it easier for tests to mock the
|
|
||||||
function and change the return value.
|
|
||||||
|
|
||||||
"""
|
|
||||||
return importlib_metadata.version(name)
|
|
||||||
|
|
||||||
|
|
||||||
@interface.backupdriver
|
@interface.backupdriver
|
||||||
class GoogleBackupDriver(chunkeddriver.ChunkedBackupDriver):
|
class GoogleBackupDriver(chunkeddriver.ChunkedBackupDriver):
|
||||||
"""Provides backup, restore and delete of backup objects within GCS."""
|
"""Provides backup, restore and delete of backup objects within GCS."""
|
||||||
@ -184,22 +171,22 @@ class GoogleBackupDriver(chunkeddriver.ChunkedBackupDriver):
|
|||||||
os.environ['http_proxy'] = CONF.backup_gcs_proxy_url
|
os.environ['http_proxy'] = CONF.backup_gcs_proxy_url
|
||||||
|
|
||||||
backup_credential = CONF.backup_gcs_credential_file
|
backup_credential = CONF.backup_gcs_credential_file
|
||||||
# If we have google client that support google-auth library
|
# service_account is imported if all required libraries are available
|
||||||
# (v1.6.0 or higher) and all required libraries are installed use
|
if service_account:
|
||||||
# google-auth for the credentials
|
|
||||||
dist_version = _get_dist_version('google-api-python-client')
|
|
||||||
if (version.parse(dist_version) >= version.parse('1.6.0')
|
|
||||||
and service_account):
|
|
||||||
creds = service_account.Credentials.from_service_account_file(
|
creds = service_account.Credentials.from_service_account_file(
|
||||||
backup_credential)
|
backup_credential)
|
||||||
OAUTH_EXCEPTIONS = (gexceptions.RefreshError,
|
OAUTH_EXCEPTIONS = (gexceptions.RefreshError,
|
||||||
gexceptions.DefaultCredentialsError,
|
gexceptions.DefaultCredentialsError,
|
||||||
client.Error)
|
client.Error)
|
||||||
|
# The (deprecated) client is imported if the oauth2client library is
|
||||||
# Can't use google-auth, use deprecated oauth2client
|
# available
|
||||||
else:
|
elif client:
|
||||||
creds = client.GoogleCredentials.from_stream(backup_credential)
|
creds = client.GoogleCredentials.from_stream(backup_credential)
|
||||||
OAUTH_EXCEPTIONS = client.Error
|
OAUTH_EXCEPTIONS = client.Error
|
||||||
|
else:
|
||||||
|
msg = _('google-auth-httplib2 or oauth2client should be '
|
||||||
|
'installed.')
|
||||||
|
raise exception.BackupDriverException(reason=msg)
|
||||||
|
|
||||||
self.conn = discovery.build('storage',
|
self.conn = discovery.build('storage',
|
||||||
'v1',
|
'v1',
|
||||||
|
@ -635,59 +635,33 @@ class GoogleBackupDriverTestCase(test.TestCase):
|
|||||||
self.assertEqual('none', result[0])
|
self.assertEqual('none', result[0])
|
||||||
self.assertEqual(already_compressed_data, result[1])
|
self.assertEqual(already_compressed_data, result[1])
|
||||||
|
|
||||||
@mock.patch.object(google_dr, '_get_dist_version')
|
|
||||||
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
|
|
||||||
@mock.patch.object(google_dr.discovery, 'build')
|
|
||||||
@mock.patch.object(google_dr, 'service_account')
|
|
||||||
def test_non_google_auth_version(self, account, build, from_stream,
|
|
||||||
get_dist_version):
|
|
||||||
# Prior to v1.6.0 Google api client doesn't support google-auth library
|
|
||||||
get_dist_version.return_value = '1.5.5'
|
|
||||||
google_dr.CONF.set_override('backup_gcs_credential_file',
|
|
||||||
'credentials_file')
|
|
||||||
|
|
||||||
google_dr.GoogleBackupDriver(self.ctxt)
|
|
||||||
|
|
||||||
get_dist_version.assert_called_once_with('google-api-python-client')
|
|
||||||
from_stream.assert_called_once_with('credentials_file')
|
|
||||||
account.Credentials.from_service_account_file.assert_not_called()
|
|
||||||
build.assert_called_once_with('storage', 'v1', cache_discovery=False,
|
|
||||||
credentials=from_stream.return_value)
|
|
||||||
|
|
||||||
@mock.patch.object(google_dr, '_get_dist_version')
|
|
||||||
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
|
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
|
||||||
@mock.patch.object(google_dr.discovery, 'build')
|
@mock.patch.object(google_dr.discovery, 'build')
|
||||||
@mock.patch.object(google_dr, 'service_account', None)
|
@mock.patch.object(google_dr, 'service_account', None)
|
||||||
def test_no_httplib2_auth(self, build, from_stream, get_dist_version):
|
def test_no_httplib2_auth(self, build, from_stream):
|
||||||
# Google api client requires google-auth-httplib2 if not present we
|
# Google api client requires google-auth-httplib2 if not present we
|
||||||
# use legacy credentials
|
# use legacy credentials
|
||||||
get_dist_version.return_value = '1.6.6'
|
|
||||||
google_dr.CONF.set_override('backup_gcs_credential_file',
|
google_dr.CONF.set_override('backup_gcs_credential_file',
|
||||||
'credentials_file')
|
'credentials_file')
|
||||||
|
|
||||||
google_dr.GoogleBackupDriver(self.ctxt)
|
google_dr.GoogleBackupDriver(self.ctxt)
|
||||||
|
|
||||||
get_dist_version.assert_called_once_with('google-api-python-client')
|
|
||||||
from_stream.assert_called_once_with('credentials_file')
|
from_stream.assert_called_once_with('credentials_file')
|
||||||
build.assert_called_once_with('storage', 'v1', cache_discovery=False,
|
build.assert_called_once_with('storage', 'v1', cache_discovery=False,
|
||||||
credentials=from_stream.return_value)
|
credentials=from_stream.return_value)
|
||||||
|
|
||||||
@mock.patch.object(google_dr, '_get_dist_version')
|
|
||||||
@mock.patch.object(google_dr, 'gexceptions', mock.Mock())
|
@mock.patch.object(google_dr, 'gexceptions', mock.Mock())
|
||||||
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
|
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
|
||||||
@mock.patch.object(google_dr.discovery, 'build')
|
@mock.patch.object(google_dr.discovery, 'build')
|
||||||
@mock.patch.object(google_dr, 'service_account')
|
@mock.patch.object(google_dr, 'service_account')
|
||||||
def test_google_auth_used(self, account, build, from_stream,
|
def test_google_auth_used(self, account, build, from_stream):
|
||||||
get_dist_version):
|
|
||||||
# Google api client requires google-auth-httplib2 if not present we
|
# Google api client requires google-auth-httplib2 if not present we
|
||||||
# use legacy credentials
|
# use legacy credentials
|
||||||
get_dist_version.return_value = '1.6.6'
|
|
||||||
google_dr.CONF.set_override('backup_gcs_credential_file',
|
google_dr.CONF.set_override('backup_gcs_credential_file',
|
||||||
'credentials_file')
|
'credentials_file')
|
||||||
|
|
||||||
google_dr.GoogleBackupDriver(self.ctxt)
|
google_dr.GoogleBackupDriver(self.ctxt)
|
||||||
|
|
||||||
get_dist_version.assert_called_once_with('google-api-python-client')
|
|
||||||
from_stream.assert_not_called()
|
from_stream.assert_not_called()
|
||||||
create_creds = account.Credentials.from_service_account_file
|
create_creds = account.Credentials.from_service_account_file
|
||||||
create_creds.assert_called_once_with('credentials_file')
|
create_creds.assert_called_once_with('credentials_file')
|
||||||
|
Loading…
Reference in New Issue
Block a user