Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
feat: add context manager support in client (#121)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

chore: fix docstring for first attribute of protos

committer: @busunkim96
PiperOrigin-RevId: 401271153

Source-Link: googleapis/googleapis@787f8c9

Source-Link: googleapis/googleapis-gen@81decff
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
  • Loading branch information
gcf-owl-bot[bot] committed Oct 7, 2021
1 parent 9af0970 commit 8118ca8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
Expand Up @@ -698,6 +698,12 @@ async def update_ssh_public_key(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/oslogin_v1/services/os_login_service/client.py
Expand Up @@ -357,10 +357,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def delete_posix_account(
Expand Down Expand Up @@ -829,6 +826,19 @@ def update_ssh_public_key(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Expand Up @@ -249,6 +249,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def delete_posix_account(
self,
Expand Down
Expand Up @@ -393,5 +393,8 @@ def update_ssh_public_key(
)
return self._stubs["update_ssh_public_key"]

def close(self):
self.grpc_channel.close()


__all__ = ("OsLoginServiceGrpcTransport",)
Expand Up @@ -397,5 +397,8 @@ def update_ssh_public_key(
)
return self._stubs["update_ssh_public_key"]

def close(self):
return self.grpc_channel.close()


__all__ = ("OsLoginServiceGrpcAsyncIOTransport",)
6 changes: 6 additions & 0 deletions google/cloud/oslogin_v1/types/oslogin.py
Expand Up @@ -60,6 +60,7 @@ class LoginProfile(proto.Message):

class DeletePosixAccountRequest(proto.Message):
r"""A request message for deleting a POSIX account entry.
Attributes:
name (str):
Required. A reference to the POSIX account to update. POSIX
Expand All @@ -73,6 +74,7 @@ class DeletePosixAccountRequest(proto.Message):

class DeleteSshPublicKeyRequest(proto.Message):
r"""A request message for deleting an SSH public key.
Attributes:
name (str):
Required. The fingerprint of the public key to update.
Expand Down Expand Up @@ -107,6 +109,7 @@ class GetLoginProfileRequest(proto.Message):

class GetSshPublicKeyRequest(proto.Message):
r"""A request message for retrieving an SSH public key.
Attributes:
name (str):
Required. The fingerprint of the public key to retrieve.
Expand All @@ -120,6 +123,7 @@ class GetSshPublicKeyRequest(proto.Message):

class ImportSshPublicKeyRequest(proto.Message):
r"""A request message for importing an SSH public key.
Attributes:
parent (str):
Required. The unique ID for the user in format
Expand All @@ -139,6 +143,7 @@ class ImportSshPublicKeyRequest(proto.Message):

class ImportSshPublicKeyResponse(proto.Message):
r"""A response message for importing an SSH public key.
Attributes:
login_profile (google.cloud.oslogin_v1.types.LoginProfile):
The login profile information for the user.
Expand All @@ -149,6 +154,7 @@ class ImportSshPublicKeyResponse(proto.Message):

class UpdateSshPublicKeyRequest(proto.Message):
r"""A request message for updating an SSH public key.
Attributes:
name (str):
Required. The fingerprint of the public key to update.
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/gapic/oslogin_v1/test_os_login_service.py
Expand Up @@ -29,6 +29,7 @@
from google.api_core import gapic_v1
from google.api_core import grpc_helpers
from google.api_core import grpc_helpers_async
from google.api_core import path_template
from google.auth import credentials as ga_credentials
from google.auth.exceptions import MutualTLSChannelError
from google.cloud.oslogin_v1 import common # type: ignore
Expand Down Expand Up @@ -1941,6 +1942,9 @@ def test_os_login_service_base_transport():
with pytest.raises(NotImplementedError):
getattr(transport, method)(request=object())

with pytest.raises(NotImplementedError):
transport.close()


@requires_google_auth_gte_1_25_0
def test_os_login_service_base_transport_with_credentials_file():
Expand Down Expand Up @@ -2462,3 +2466,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
)
prep.assert_called_once_with(client_info)


@pytest.mark.asyncio
async def test_transport_close_async():
client = OsLoginServiceAsyncClient(
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
)
with mock.patch.object(
type(getattr(client.transport, "grpc_channel")), "close"
) as close:
async with client:
close.assert_not_called()
close.assert_called_once()


def test_transport_close():
transports = {
"grpc": "_grpc_channel",
}

for transport, close_name in transports.items():
client = OsLoginServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
with mock.patch.object(
type(getattr(client.transport, close_name)), "close"
) as close:
with client:
close.assert_not_called()
close.assert_called_once()


def test_client_ctx():
transports = [
"grpc",
]
for transport in transports:
client = OsLoginServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
# Test client calls underlying transport.
with mock.patch.object(type(client.transport), "close") as close:
close.assert_not_called()
with client:
pass
close.assert_called()

0 comments on commit 8118ca8

Please sign in to comment.