Skip to content

Commit f108dab

Browse files
authored
Patch the AutoML v1 clients to get Kaggle managed credentials (Kaggle#715)
When the integration initially launched, there was only the v1beta1 version of the AutoML clients. As of Nov. 2019, there is a v1 version but we never updated the code to patch the v1 clients. Addresses b/149264883
1 parent 238d9a9 commit f108dab

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

patches/kaggle_gcp.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,8 @@ def patched_init(self, *args, **kwargs):
186186
if specified_credentials is None:
187187
Log.info("No credentials specified, using KaggleKernelCredentials.")
188188
kwargs['credentials'] = kaggle_kernel_credentials
189-
190-
# TODO(vimota): Remove the exclusion of TablesClient once
191-
# the client has fixed the error:
192-
# `multiple values for keyword argument 'client_info'``
193-
from google.cloud import automl_v1beta1
194-
if (client_klass != automl_v1beta1.TablesClient):
195-
kwargs['client_info'] = set_kaggle_user_agent(kwargs.get('client_info'))
196189

190+
kwargs['client_info'] = set_kaggle_user_agent(kwargs.get('client_info'))
197191

198192
return client_init(self, *args, **kwargs)
199193

@@ -227,30 +221,35 @@ def init_gcs():
227221

228222
def init_automl():
229223
is_user_secrets_token_set = "KAGGLE_USER_SECRETS_TOKEN" in os.environ
230-
from google.cloud import automl_v1beta1 as automl
224+
from google.cloud import automl, automl_v1beta1
231225
if not is_user_secrets_token_set:
232-
return automl
226+
return
233227

234228
from kaggle_gcp import get_integrations
235229
if not get_integrations().has_automl():
236-
return automl
230+
return
237231

238232
from kaggle_secrets import GcpTarget
239233
from kaggle_gcp import KaggleKernelCredentials
240234
kaggle_kernel_credentials = KaggleKernelCredentials(target=GcpTarget.AUTOML)
241235

242-
# The AutoML client library exposes 4 different client classes (AutoMlClient,
243-
# TablesClient, PredictionServiceClient and GcsClient), so patch each of them.
244-
# The same KaggleKernelCredentials are passed to all of them.
236+
# Patch the 2 GA clients: AutoMlClient and PreditionServiceClient
245237
monkeypatch_client(automl.AutoMlClient, kaggle_kernel_credentials)
246-
monkeypatch_client(automl.TablesClient, kaggle_kernel_credentials)
247238
monkeypatch_client(automl.PredictionServiceClient, kaggle_kernel_credentials)
248-
# TODO(markcollins): The GcsClient in the AutoML client library version
249-
# 0.5.0 doesn't handle credentials properly. I wrote PR:
250-
# https://github.com/googleapis/google-cloud-python/pull/9299
251-
# to address this issue. Add patching for GcsClient when we get a version of
252-
# the library that includes the fixes.
253-
return automl
239+
240+
# The AutoML client library exposes 3 different client classes (AutoMlClient,
241+
# TablesClient, PredictionServiceClient), so patch each of them.
242+
# The same KaggleKernelCredentials are passed to all of them.
243+
# The GcsClient class is only used internally by TablesClient.
244+
245+
# The beta version of the clients that are now GA are included here for now.
246+
# They are deprecated and will be removed by 1 May 2020.
247+
monkeypatch_client(automl_v1beta1.AutoMlClient, kaggle_kernel_credentials)
248+
monkeypatch_client(automl_v1beta1.PredictionServiceClient, kaggle_kernel_credentials)
249+
250+
# The TablesClient is still in beta, so this will not be deprecated until
251+
# the TablesClient is GA.
252+
monkeypatch_client(automl_v1beta1.TablesClient, kaggle_kernel_credentials)
254253

255254
def init():
256255
init_bigquery()

tests/test_automl.py

+33-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44

55
from kaggle_gcp import KaggleKernelCredentials, init_automl
66
from test.support import EnvironmentVarGuard
7-
from google.cloud import storage, automl_v1beta1 as automl
8-
from packaging.version import parse
7+
from google.cloud import storage, automl_v1beta1, automl
98

109
def _make_credentials():
1110
import google.auth.credentials
1211
return Mock(spec=google.auth.credentials.Credentials)
1312

1413
class TestAutoMl(unittest.TestCase):
1514

16-
def test_version(self):
17-
self.assertIsNotNone(automl.auto_ml_client._GAPIC_LIBRARY_VERSION)
18-
self.assertGreaterEqual(parse(automl.auto_ml_client._GAPIC_LIBRARY_VERSION), parse("0.5.0"))
19-
2015
class FakeClient:
2116
def __init__(self, credentials=None, client_info=None, **kwargs):
2217
self.credentials = credentials
@@ -27,7 +22,7 @@ def __init__(self, user_agent):
2722
if (client_info is not None):
2823
self._connection = FakeConnection(client_info.user_agent)
2924

30-
@patch("google.cloud.automl_v1beta1.AutoMlClient", new=FakeClient)
25+
@patch("google.cloud.automl.AutoMlClient", new=FakeClient)
3126
def test_user_provided_credentials(self):
3227
credentials = _make_credentials()
3328
env = EnvironmentVarGuard()
@@ -42,10 +37,10 @@ def test_user_provided_credentials(self):
4237
def test_tables_gcs_client(self):
4338
# The GcsClient can't currently be monkeypatched for default
4439
# credentials because it requires a project which can't be set.
45-
# Verify that creating an automl.GcsClient given an actual
40+
# Verify that creating an automl_v1beta1.GcsClient given an actual
4641
# storage.Client sets the client properly.
4742
gcs_client = storage.Client(project="xyz", credentials=_make_credentials())
48-
tables_gcs_client = automl.GcsClient(client=gcs_client)
43+
tables_gcs_client = automl_v1beta1.GcsClient(client=gcs_client)
4944
self.assertIs(tables_gcs_client.client, gcs_client)
5045

5146
@patch("google.cloud.automl_v1beta1.gapic.auto_ml_client.AutoMlClient", new=FakeClient)
@@ -56,10 +51,10 @@ def test_tables_client_credentials(self):
5651
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML')
5752
with env:
5853
init_automl()
59-
tables_client = automl.TablesClient(credentials=credentials)
54+
tables_client = automl_v1beta1.TablesClient(credentials=credentials)
6055
self.assertEqual(tables_client.auto_ml_client.credentials, credentials)
6156

62-
@patch("google.cloud.automl_v1beta1.AutoMlClient", new=FakeClient)
57+
@patch("google.cloud.automl.AutoMlClient", new=FakeClient)
6358
def test_default_credentials_automl_client(self):
6459
env = EnvironmentVarGuard()
6560
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
@@ -71,18 +66,31 @@ def test_default_credentials_automl_client(self):
7166
self.assertIsInstance(automl_client.credentials, KaggleKernelCredentials)
7267
self.assertTrue(automl_client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
7368

69+
@patch("google.cloud.automl_v1beta1.AutoMlClient", new=FakeClient)
70+
def test_default_credentials_automl_v1beta1_client(self):
71+
env = EnvironmentVarGuard()
72+
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
73+
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML')
74+
with env:
75+
init_automl()
76+
automl_client = automl_v1beta1.AutoMlClient()
77+
self.assertIsNotNone(automl_client.credentials)
78+
self.assertIsInstance(automl_client.credentials, KaggleKernelCredentials)
79+
self.assertTrue(automl_client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
80+
7481
@patch("google.cloud.automl_v1beta1.TablesClient", new=FakeClient)
7582
def test_default_credentials_tables_client(self):
7683
env = EnvironmentVarGuard()
7784
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
7885
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML')
7986
with env:
8087
init_automl()
81-
tables_client = automl.TablesClient()
88+
tables_client = automl_v1beta1.TablesClient()
8289
self.assertIsNotNone(tables_client.credentials)
8390
self.assertIsInstance(tables_client.credentials, KaggleKernelCredentials)
91+
self.assertTrue(tables_client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
8492

85-
@patch("google.cloud.automl_v1beta1.PredictionServiceClient", new=FakeClient)
93+
@patch("google.cloud.automl.PredictionServiceClient", new=FakeClient)
8694
def test_default_credentials_prediction_client(self):
8795
env = EnvironmentVarGuard()
8896
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
@@ -91,6 +99,18 @@ def test_default_credentials_prediction_client(self):
9199
prediction_client = automl.PredictionServiceClient()
92100
self.assertIsNotNone(prediction_client.credentials)
93101
self.assertIsInstance(prediction_client.credentials, KaggleKernelCredentials)
102+
self.assertTrue(prediction_client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
103+
104+
@patch("google.cloud.automl_v1beta1.PredictionServiceClient", new=FakeClient)
105+
def test_default_credentials_prediction_v1beta1_client(self):
106+
env = EnvironmentVarGuard()
107+
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
108+
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML')
109+
with env:
110+
prediction_client = automl_v1beta1.PredictionServiceClient()
111+
self.assertIsNotNone(prediction_client.credentials)
112+
self.assertIsInstance(prediction_client.credentials, KaggleKernelCredentials)
113+
self.assertTrue(prediction_client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
94114

95115
def test_monkeypatching_idempotent(self):
96116
env = EnvironmentVarGuard()

0 commit comments

Comments
 (0)