Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve DuplicateCredentialArgs when using credentials_file #1159

Merged
merged 5 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class {{service.name}}RestTransport({{service.name}}Transport):

rest_transport = operations_v1.OperationsRestTransport(
host=self._host,
# use the credentials which are saved
credentials=self._credentials,
scopes=self._scopes,
http_options=http_options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
options = client_options.ClientOptions(
credentials_file="credentials.json"
)

with mock.patch.object(transport_class, '__init__') as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
Expand All @@ -393,6 +394,42 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)

{% if 'grpc' in opts.transport %}
if "grpc" in transport_name:
# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
{% with host = (service.host|default('localhost', true)) %}
create_channel.assert_called_with(
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
{% for scope in service.oauth_scopes %}
'{{ scope }}',
{% endfor %}),
scopes=None,
default_host="{{ host }}",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
{% endwith %}
{% endif %}
{% if 'grpc' in opts.transport %}
{# TODO(dovs): genericize this function#}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class {{service.name}}RestTransport({{service.name}}Transport):

rest_transport = operations_v1.OperationsRestTransport(
host=self._host,
# use the credentials which are saved
credentials=self._credentials,
scopes=self._scopes,
http_options=http_options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,21 @@ def test_{{ service.client_name|snake_case }}_client_options_scopes(client_class
always_use_jwt_access=True,
)

@pytest.mark.parametrize("client_class,transport_class,transport_name", [
@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
{% if 'grpc' in opts.transport %}
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc"),
({{ service.async_client_name }}, transports.{{ service.grpc_asyncio_transport_name }}, "grpc_asyncio"),
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc", grpc_helpers),
({{ service.async_client_name }}, transports.{{ service.grpc_asyncio_transport_name }}, "grpc_asyncio", grpc_helpers_async),
{% endif %}
{% if 'rest' in opts.transport %}
({{ service.client_name }}, transports.{{ service.rest_transport_name }}, "rest"),
({{ service.client_name }}, transports.{{ service.rest_transport_name }}, "rest", None),
{% endif %}
])
def test_{{ service.client_name|snake_case }}_client_options_credentials_file(client_class, transport_class, transport_name):
def test_{{ service.client_name|snake_case }}_client_options_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
# Check the case credentials file is provided.
options = client_options.ClientOptions(
credentials_file="credentials.json"
)

with mock.patch.object(transport_class, '__init__') as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
Expand All @@ -474,6 +475,42 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)

{% if 'grpc' in opts.transport %}
if "grpc" in transport_name:
# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
{% with host = (service.host|default('localhost', true)) %}
create_channel.assert_called_with(
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
{% for scope in service.oauth_scopes %}
'{{ scope }}',
{% endfor %}),
scopes=None,
default_host="{{ host }}",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
{% endwith %}
{% endif %}
{% if 'grpc' in opts.transport %}
{# TODO(dovs): genericize this function#}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,16 @@ def test_asset_service_client_client_options_scopes(client_class, transport_clas
always_use_jwt_access=True,
)

@pytest.mark.parametrize("client_class,transport_class,transport_name", [
(AssetServiceClient, transports.AssetServiceGrpcTransport, "grpc"),
(AssetServiceAsyncClient, transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio"),
@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
(AssetServiceClient, transports.AssetServiceGrpcTransport, "grpc", grpc_helpers),
(AssetServiceAsyncClient, transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio", grpc_helpers_async),
])
def test_asset_service_client_client_options_credentials_file(client_class, transport_class, transport_name):
def test_asset_service_client_client_options_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
# Check the case credentials file is provided.
options = client_options.ClientOptions(
credentials_file="credentials.json"
)

with mock.patch.object(transport_class, '__init__') as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
Expand All @@ -417,6 +418,37 @@ def test_asset_service_client_client_options_credentials_file(client_class, tran
always_use_jwt_access=True,
)

if "grpc" in transport_name:
# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
create_channel.assert_called_with(
"cloudasset.googleapis.com:443",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
'https://www.googleapis.com/auth/cloud-platform',
),
scopes=None,
default_host="cloudasset.googleapis.com",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)

def test_asset_service_client_client_options_from_dict():
with mock.patch('google.cloud.asset_v1.services.asset_service.transports.AssetServiceGrpcTransport.__init__') as grpc_transport:
grpc_transport.return_value = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,16 @@ def test_iam_credentials_client_client_options_scopes(client_class, transport_cl
always_use_jwt_access=True,
)

@pytest.mark.parametrize("client_class,transport_class,transport_name", [
(IAMCredentialsClient, transports.IAMCredentialsGrpcTransport, "grpc"),
(IAMCredentialsAsyncClient, transports.IAMCredentialsGrpcAsyncIOTransport, "grpc_asyncio"),
@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
(IAMCredentialsClient, transports.IAMCredentialsGrpcTransport, "grpc", grpc_helpers),
(IAMCredentialsAsyncClient, transports.IAMCredentialsGrpcAsyncIOTransport, "grpc_asyncio", grpc_helpers_async),
])
def test_iam_credentials_client_client_options_credentials_file(client_class, transport_class, transport_name):
def test_iam_credentials_client_client_options_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
# Check the case credentials file is provided.
options = client_options.ClientOptions(
credentials_file="credentials.json"
)

with mock.patch.object(transport_class, '__init__') as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
Expand All @@ -408,6 +409,37 @@ def test_iam_credentials_client_client_options_credentials_file(client_class, tr
always_use_jwt_access=True,
)

if "grpc" in transport_name:
# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
create_channel.assert_called_with(
"iamcredentials.googleapis.com:443",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
'https://www.googleapis.com/auth/cloud-platform',
),
scopes=None,
default_host="iamcredentials.googleapis.com",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)

def test_iam_credentials_client_client_options_from_dict():
with mock.patch('google.iam.credentials_v1.services.iam_credentials.transports.IAMCredentialsGrpcTransport.__init__') as grpc_transport:
grpc_transport.return_value = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ def __init__(self, *,
if not self._grpc_channel:
self._grpc_channel = type(self).create_channel(
self._host,
# use the credentials which are saved
credentials=self._credentials,
credentials_file=credentials_file,
# Set ``credentials_file`` to ``None`` here as
# the credentials that we saved earlier should be used.
credentials_file=None,
scopes=self._scopes,
ssl_credentials=self._ssl_channel_credentials,
quota_project_id=quota_project_id,
Expand Down
Loading