gcs: Remove logic for google-api-python-client < 1.6.0

Because the minimum version in requirements is now 1.11.0 .

This also adds more explicit error message in case the library is not
installed, to make the error more actionable for operators.

Change-Id: I0a1834cfab824ad9af24ebfcab20e534c5427e87
This commit is contained in:
Takashi Kajinami 2023-11-29 03:23:14 +09:00
parent 28672aeb73
commit 8fb2b3550c
2 changed files with 11 additions and 50 deletions

View File

@ -27,7 +27,6 @@ Server-centric flow is used for authentication.
"""
import base64
import importlib.metadata as importlib_metadata
import io
import os
@ -50,7 +49,6 @@ from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import secretutils
from oslo_utils import timeutils
from packaging import version
from cinder.backup import chunkeddriver
from cinder import exception
@ -142,17 +140,6 @@ def gcs_logger(func):
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
class GoogleBackupDriver(chunkeddriver.ChunkedBackupDriver):
"""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
backup_credential = CONF.backup_gcs_credential_file
# If we have google client that support google-auth library
# (v1.6.0 or higher) and all required libraries are installed use
# 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):
# service_account is imported if all required libraries are available
if service_account:
creds = service_account.Credentials.from_service_account_file(
backup_credential)
OAUTH_EXCEPTIONS = (gexceptions.RefreshError,
gexceptions.DefaultCredentialsError,
client.Error)
# Can't use google-auth, use deprecated oauth2client
else:
# The (deprecated) client is imported if the oauth2client library is
# available
elif client:
creds = client.GoogleCredentials.from_stream(backup_credential)
OAUTH_EXCEPTIONS = client.Error
else:
msg = _('google-auth-httplib2 or oauth2client should be '
'installed.')
raise exception.BackupDriverException(reason=msg)
self.conn = discovery.build('storage',
'v1',

View File

@ -635,59 +635,33 @@ class GoogleBackupDriverTestCase(test.TestCase):
self.assertEqual('none', result[0])
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.discovery, 'build')
@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
# use legacy credentials
get_dist_version.return_value = '1.6.6'
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')
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, 'gexceptions', mock.Mock())
@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_google_auth_used(self, account, build, from_stream,
get_dist_version):
def test_google_auth_used(self, account, build, from_stream):
# Google api client requires google-auth-httplib2 if not present we
# use legacy credentials
get_dist_version.return_value = '1.6.6'
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_not_called()
create_creds = account.Credentials.from_service_account_file
create_creds.assert_called_once_with('credentials_file')