Skip to content

Commit fccd2ba

Browse files
authored
fix: resolve DuplicateCredentialArgs when using credentials_file (googleapis#1159)
* fix: resolve DuplicateCredentialArgs when using credentials_file * update golden files * exclude test for rest transport
1 parent 21e8136 commit fccd2ba

File tree

25 files changed

+368
-44
lines changed

25 files changed

+368
-44
lines changed

gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
159159
if not self._grpc_channel:
160160
self._grpc_channel = type(self).create_channel(
161161
self._host,
162+
# use the credentials which are saved
162163
credentials=self._credentials,
163-
credentials_file=credentials_file,
164+
# Set ``credentials_file`` to ``None`` here as
165+
# the credentials that we saved earlier should be used.
166+
credentials_file=None,
164167
scopes=self._scopes,
165168
ssl_credentials=self._ssl_channel_credentials,
166169
quota_project_id=quota_project_id,

gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class {{service.name}}RestTransport({{service.name}}Transport):
231231

232232
rest_transport = operations_v1.OperationsRestTransport(
233233
host=self._host,
234+
# use the credentials which are saved
234235
credentials=self._credentials,
235236
scopes=self._scopes,
236237
http_options=http_options)

gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
380380
options = client_options.ClientOptions(
381381
credentials_file="credentials.json"
382382
)
383+
383384
with mock.patch.object(transport_class, '__init__') as patched:
384385
patched.return_value = None
385386
client = client_class(client_options=options, transport=transport_name)
@@ -393,6 +394,42 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
393394
client_info=transports.base.DEFAULT_CLIENT_INFO,
394395
always_use_jwt_access=True,
395396
)
397+
398+
{% if 'grpc' in opts.transport %}
399+
if "grpc" in transport_name:
400+
# test that the credentials from file are saved and used as the credentials.
401+
with mock.patch.object(
402+
google.auth, "load_credentials_from_file", autospec=True
403+
) as load_creds, mock.patch.object(
404+
google.auth, "default", autospec=True
405+
) as adc, mock.patch.object(
406+
grpc_helpers, "create_channel"
407+
) as create_channel:
408+
creds = ga_credentials.AnonymousCredentials()
409+
file_creds = ga_credentials.AnonymousCredentials()
410+
load_creds.return_value = (file_creds, None)
411+
adc.return_value = (creds, None)
412+
client = client_class(client_options=options, transport=transport_name)
413+
{% with host = (service.host|default('localhost', true)) %}
414+
create_channel.assert_called_with(
415+
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
416+
credentials=file_creds,
417+
credentials_file=None,
418+
quota_project_id=None,
419+
default_scopes=(
420+
{% for scope in service.oauth_scopes %}
421+
'{{ scope }}',
422+
{% endfor %}),
423+
scopes=None,
424+
default_host="{{ host }}",
425+
ssl_credentials=None,
426+
options=[
427+
("grpc.max_send_message_length", -1),
428+
("grpc.max_receive_message_length", -1),
429+
],
430+
)
431+
{% endwith %}
432+
{% endif %}
396433
{% if 'grpc' in opts.transport %}
397434
{# TODO(dovs): genericize this function#}
398435

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
159159
if not self._grpc_channel:
160160
self._grpc_channel = type(self).create_channel(
161161
self._host,
162+
# use the credentials which are saved
162163
credentials=self._credentials,
163-
credentials_file=credentials_file,
164+
# Set ``credentials_file`` to ``None`` here as
165+
# the credentials that we saved earlier should be used.
166+
credentials_file=None,
164167
scopes=self._scopes,
165168
ssl_credentials=self._ssl_channel_credentials,
166169
quota_project_id=quota_project_id,

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
203203
if not self._grpc_channel:
204204
self._grpc_channel = type(self).create_channel(
205205
self._host,
206+
# use the credentials which are saved
206207
credentials=self._credentials,
207-
credentials_file=credentials_file,
208+
# Set ``credentials_file`` to ``None`` here as
209+
# the credentials that we saved earlier should be used.
210+
credentials_file=None,
208211
scopes=self._scopes,
209212
ssl_credentials=self._ssl_channel_credentials,
210213
quota_project_id=quota_project_id,

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class {{service.name}}RestTransport({{service.name}}Transport):
231231

232232
rest_transport = operations_v1.OperationsRestTransport(
233233
host=self._host,
234+
# use the credentials which are saved
234235
credentials=self._credentials,
235236
scopes=self._scopes,
236237
http_options=http_options)

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,20 +447,21 @@ def test_{{ service.client_name|snake_case }}_client_options_scopes(client_class
447447
always_use_jwt_access=True,
448448
)
449449

450-
@pytest.mark.parametrize("client_class,transport_class,transport_name", [
450+
@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
451451
{% if 'grpc' in opts.transport %}
452-
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc"),
453-
({{ service.async_client_name }}, transports.{{ service.grpc_asyncio_transport_name }}, "grpc_asyncio"),
452+
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc", grpc_helpers),
453+
({{ service.async_client_name }}, transports.{{ service.grpc_asyncio_transport_name }}, "grpc_asyncio", grpc_helpers_async),
454454
{% endif %}
455455
{% if 'rest' in opts.transport %}
456-
({{ service.client_name }}, transports.{{ service.rest_transport_name }}, "rest"),
456+
({{ service.client_name }}, transports.{{ service.rest_transport_name }}, "rest", None),
457457
{% endif %}
458458
])
459-
def test_{{ service.client_name|snake_case }}_client_options_credentials_file(client_class, transport_class, transport_name):
459+
def test_{{ service.client_name|snake_case }}_client_options_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
460460
# Check the case credentials file is provided.
461461
options = client_options.ClientOptions(
462462
credentials_file="credentials.json"
463463
)
464+
464465
with mock.patch.object(transport_class, '__init__') as patched:
465466
patched.return_value = None
466467
client = client_class(client_options=options, transport=transport_name)
@@ -474,6 +475,42 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
474475
client_info=transports.base.DEFAULT_CLIENT_INFO,
475476
always_use_jwt_access=True,
476477
)
478+
479+
{% if 'grpc' in opts.transport %}
480+
if "grpc" in transport_name:
481+
# test that the credentials from file are saved and used as the credentials.
482+
with mock.patch.object(
483+
google.auth, "load_credentials_from_file", autospec=True
484+
) as load_creds, mock.patch.object(
485+
google.auth, "default", autospec=True
486+
) as adc, mock.patch.object(
487+
grpc_helpers, "create_channel"
488+
) as create_channel:
489+
creds = ga_credentials.AnonymousCredentials()
490+
file_creds = ga_credentials.AnonymousCredentials()
491+
load_creds.return_value = (file_creds, None)
492+
adc.return_value = (creds, None)
493+
client = client_class(client_options=options, transport=transport_name)
494+
{% with host = (service.host|default('localhost', true)) %}
495+
create_channel.assert_called_with(
496+
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
497+
credentials=file_creds,
498+
credentials_file=None,
499+
quota_project_id=None,
500+
default_scopes=(
501+
{% for scope in service.oauth_scopes %}
502+
'{{ scope }}',
503+
{% endfor %}),
504+
scopes=None,
505+
default_host="{{ host }}",
506+
ssl_credentials=None,
507+
options=[
508+
("grpc.max_send_message_length", -1),
509+
("grpc.max_receive_message_length", -1),
510+
],
511+
)
512+
{% endwith %}
513+
{% endif %}
477514
{% if 'grpc' in opts.transport %}
478515
{# TODO(dovs): genericize this function#}
479516

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ def __init__(self, *,
159159
if not self._grpc_channel:
160160
self._grpc_channel = type(self).create_channel(
161161
self._host,
162+
# use the credentials which are saved
162163
credentials=self._credentials,
163-
credentials_file=credentials_file,
164+
# Set ``credentials_file`` to ``None`` here as
165+
# the credentials that we saved earlier should be used.
166+
credentials_file=None,
164167
scopes=self._scopes,
165168
ssl_credentials=self._ssl_channel_credentials,
166169
quota_project_id=quota_project_id,

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc_asyncio.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ def __init__(self, *,
203203
if not self._grpc_channel:
204204
self._grpc_channel = type(self).create_channel(
205205
self._host,
206+
# use the credentials which are saved
206207
credentials=self._credentials,
207-
credentials_file=credentials_file,
208+
# Set ``credentials_file`` to ``None`` here as
209+
# the credentials that we saved earlier should be used.
210+
credentials_file=None,
208211
scopes=self._scopes,
209212
ssl_credentials=self._ssl_channel_credentials,
210213
quota_project_id=quota_project_id,

tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,16 @@ def test_asset_service_client_client_options_scopes(client_class, transport_clas
394394
always_use_jwt_access=True,
395395
)
396396

397-
@pytest.mark.parametrize("client_class,transport_class,transport_name", [
398-
(AssetServiceClient, transports.AssetServiceGrpcTransport, "grpc"),
399-
(AssetServiceAsyncClient, transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio"),
397+
@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
398+
(AssetServiceClient, transports.AssetServiceGrpcTransport, "grpc", grpc_helpers),
399+
(AssetServiceAsyncClient, transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio", grpc_helpers_async),
400400
])
401-
def test_asset_service_client_client_options_credentials_file(client_class, transport_class, transport_name):
401+
def test_asset_service_client_client_options_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
402402
# Check the case credentials file is provided.
403403
options = client_options.ClientOptions(
404404
credentials_file="credentials.json"
405405
)
406+
406407
with mock.patch.object(transport_class, '__init__') as patched:
407408
patched.return_value = None
408409
client = client_class(client_options=options, transport=transport_name)
@@ -417,6 +418,37 @@ def test_asset_service_client_client_options_credentials_file(client_class, tran
417418
always_use_jwt_access=True,
418419
)
419420

421+
if "grpc" in transport_name:
422+
# test that the credentials from file are saved and used as the credentials.
423+
with mock.patch.object(
424+
google.auth, "load_credentials_from_file", autospec=True
425+
) as load_creds, mock.patch.object(
426+
google.auth, "default", autospec=True
427+
) as adc, mock.patch.object(
428+
grpc_helpers, "create_channel"
429+
) as create_channel:
430+
creds = ga_credentials.AnonymousCredentials()
431+
file_creds = ga_credentials.AnonymousCredentials()
432+
load_creds.return_value = (file_creds, None)
433+
adc.return_value = (creds, None)
434+
client = client_class(client_options=options, transport=transport_name)
435+
create_channel.assert_called_with(
436+
"cloudasset.googleapis.com:443",
437+
credentials=file_creds,
438+
credentials_file=None,
439+
quota_project_id=None,
440+
default_scopes=(
441+
'https://www.googleapis.com/auth/cloud-platform',
442+
),
443+
scopes=None,
444+
default_host="cloudasset.googleapis.com",
445+
ssl_credentials=None,
446+
options=[
447+
("grpc.max_send_message_length", -1),
448+
("grpc.max_receive_message_length", -1),
449+
],
450+
)
451+
420452
def test_asset_service_client_client_options_from_dict():
421453
with mock.patch('google.cloud.asset_v1.services.asset_service.transports.AssetServiceGrpcTransport.__init__') as grpc_transport:
422454
grpc_transport.return_value = None

0 commit comments

Comments
 (0)