diff --git a/.github/.OwlBot.lock.yaml b/.github/.OwlBot.lock.yaml index 2567653..ee94722 100644 --- a/.github/.OwlBot.lock.yaml +++ b/.github/.OwlBot.lock.yaml @@ -1,3 +1,3 @@ docker: image: gcr.io/cloud-devrel-public-resources/owlbot-python:latest - digest: sha256:87eee22d276554e4e52863ec9b1cb6a7245815dfae20439712bf644348215a5a + digest: sha256:6e7328583be8edd3ba8f35311c76a1ecbc823010279ccb6ab46b7a76e25eafcc diff --git a/.kokoro/samples/lint/common.cfg b/.kokoro/samples/lint/common.cfg index c9ab7bc..5020b10 100644 --- a/.kokoro/samples/lint/common.cfg +++ b/.kokoro/samples/lint/common.cfg @@ -31,4 +31,4 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # Use the trampoline script to run in docker. -build_file: "python-network-management/.kokoro/trampoline.sh" \ No newline at end of file +build_file: "python-network-management/.kokoro/trampoline_v2.sh" \ No newline at end of file diff --git a/.kokoro/samples/python3.6/common.cfg b/.kokoro/samples/python3.6/common.cfg index 5260886..bc9028d 100644 --- a/.kokoro/samples/python3.6/common.cfg +++ b/.kokoro/samples/python3.6/common.cfg @@ -37,4 +37,4 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # Use the trampoline script to run in docker. -build_file: "python-network-management/.kokoro/trampoline.sh" \ No newline at end of file +build_file: "python-network-management/.kokoro/trampoline_v2.sh" \ No newline at end of file diff --git a/.kokoro/samples/python3.6/periodic.cfg b/.kokoro/samples/python3.6/periodic.cfg index 50fec96..71cd1e5 100644 --- a/.kokoro/samples/python3.6/periodic.cfg +++ b/.kokoro/samples/python3.6/periodic.cfg @@ -3,4 +3,4 @@ env_vars: { key: "INSTALL_LIBRARY_FROM_SOURCE" value: "False" -} \ No newline at end of file +} diff --git a/.kokoro/samples/python3.7/common.cfg b/.kokoro/samples/python3.7/common.cfg index 8fc97f0..a483566 100644 --- a/.kokoro/samples/python3.7/common.cfg +++ b/.kokoro/samples/python3.7/common.cfg @@ -37,4 +37,4 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # Use the trampoline script to run in docker. -build_file: "python-network-management/.kokoro/trampoline.sh" \ No newline at end of file +build_file: "python-network-management/.kokoro/trampoline_v2.sh" \ No newline at end of file diff --git a/.kokoro/samples/python3.7/periodic.cfg b/.kokoro/samples/python3.7/periodic.cfg index 50fec96..71cd1e5 100644 --- a/.kokoro/samples/python3.7/periodic.cfg +++ b/.kokoro/samples/python3.7/periodic.cfg @@ -3,4 +3,4 @@ env_vars: { key: "INSTALL_LIBRARY_FROM_SOURCE" value: "False" -} \ No newline at end of file +} diff --git a/.kokoro/samples/python3.8/common.cfg b/.kokoro/samples/python3.8/common.cfg index 2d2cb19..5dd3eec 100644 --- a/.kokoro/samples/python3.8/common.cfg +++ b/.kokoro/samples/python3.8/common.cfg @@ -37,4 +37,4 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # Use the trampoline script to run in docker. -build_file: "python-network-management/.kokoro/trampoline.sh" \ No newline at end of file +build_file: "python-network-management/.kokoro/trampoline_v2.sh" \ No newline at end of file diff --git a/.kokoro/samples/python3.8/periodic.cfg b/.kokoro/samples/python3.8/periodic.cfg index 50fec96..71cd1e5 100644 --- a/.kokoro/samples/python3.8/periodic.cfg +++ b/.kokoro/samples/python3.8/periodic.cfg @@ -3,4 +3,4 @@ env_vars: { key: "INSTALL_LIBRARY_FROM_SOURCE" value: "False" -} \ No newline at end of file +} diff --git a/.kokoro/samples/python3.9/common.cfg b/.kokoro/samples/python3.9/common.cfg index a790231..8709c14 100644 --- a/.kokoro/samples/python3.9/common.cfg +++ b/.kokoro/samples/python3.9/common.cfg @@ -37,4 +37,4 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # Use the trampoline script to run in docker. -build_file: "python-network-management/.kokoro/trampoline.sh" \ No newline at end of file +build_file: "python-network-management/.kokoro/trampoline_v2.sh" \ No newline at end of file diff --git a/.kokoro/samples/python3.9/periodic.cfg b/.kokoro/samples/python3.9/periodic.cfg index 50fec96..71cd1e5 100644 --- a/.kokoro/samples/python3.9/periodic.cfg +++ b/.kokoro/samples/python3.9/periodic.cfg @@ -3,4 +3,4 @@ env_vars: { key: "INSTALL_LIBRARY_FROM_SOURCE" value: "False" -} \ No newline at end of file +} diff --git a/.kokoro/test-samples-against-head.sh b/.kokoro/test-samples-against-head.sh index d80a5a7..ba3a707 100755 --- a/.kokoro/test-samples-against-head.sh +++ b/.kokoro/test-samples-against-head.sh @@ -23,6 +23,4 @@ set -eo pipefail # Enables `**` to include files nested inside sub-folders shopt -s globstar -cd github/python-network-management - exec .kokoro/test-samples-impl.sh diff --git a/.kokoro/test-samples.sh b/.kokoro/test-samples.sh index 372708c..11c042d 100755 --- a/.kokoro/test-samples.sh +++ b/.kokoro/test-samples.sh @@ -24,8 +24,6 @@ set -eo pipefail # Enables `**` to include files nested inside sub-folders shopt -s globstar -cd github/python-network-management - # Run periodic samples tests at latest release if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then # preserving the test runner implementation. diff --git a/.repo-metadata.json b/.repo-metadata.json index ba66861..f69e5be 100644 --- a/.repo-metadata.json +++ b/.repo-metadata.json @@ -9,5 +9,7 @@ "library_type": "GAPIC_AUTO", "repo": "googleapis/python-network-management", "distribution_name": "google-cloud-network-management", - "api_id": "networkmanagement.googleapis.com" -} \ No newline at end of file + "api_id": "networkmanagement.googleapis.com", + "default_version": "v1", + "codeowner_team": "" +} diff --git a/.trampolinerc b/.trampolinerc index 383b6ec..0eee72a 100644 --- a/.trampolinerc +++ b/.trampolinerc @@ -16,15 +16,26 @@ # Add required env vars here. required_envvars+=( - "STAGING_BUCKET" - "V2_STAGING_BUCKET" ) # Add env vars which are passed down into the container here. pass_down_envvars+=( + "NOX_SESSION" + ############### + # Docs builds + ############### "STAGING_BUCKET" "V2_STAGING_BUCKET" - "NOX_SESSION" + ################## + # Samples builds + ################## + "INSTALL_LIBRARY_FROM_SOURCE" + "RUN_TESTS_SESSION" + "BUILD_SPECIFIC_GCLOUD_PROJECT" + # Target directories. + "RUN_TESTS_DIRS" + # The nox session to run. + "RUN_TESTS_SESSION" ) # Prevent unintentional override on the default image. diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b5398b..df17d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.1.0](https://www.github.com/googleapis/python-network-management/compare/v1.0.2...v1.1.0) (2021-10-07) + + +### Features + +* add context manager support in client ([#38](https://www.github.com/googleapis/python-network-management/issues/38)) ([376873d](https://www.github.com/googleapis/python-network-management/commit/376873d0550999ebf9831509ce77b7b86a689059)) + ### [1.0.2](https://www.github.com/googleapis/python-network-management/compare/v1.0.1...v1.0.2) (2021-09-30) diff --git a/google/cloud/network_management_v1/services/reachability_service/async_client.py b/google/cloud/network_management_v1/services/reachability_service/async_client.py index adf2e00..f4816cb 100644 --- a/google/cloud/network_management_v1/services/reachability_service/async_client.py +++ b/google/cloud/network_management_v1/services/reachability_service/async_client.py @@ -729,6 +729,12 @@ async def delete_connectivity_test( # 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( diff --git a/google/cloud/network_management_v1/services/reachability_service/client.py b/google/cloud/network_management_v1/services/reachability_service/client.py index f0e4fe8..466057b 100644 --- a/google/cloud/network_management_v1/services/reachability_service/client.py +++ b/google/cloud/network_management_v1/services/reachability_service/client.py @@ -362,10 +362,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 list_connectivity_tests( @@ -916,6 +913,19 @@ def delete_connectivity_test( # 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( diff --git a/google/cloud/network_management_v1/services/reachability_service/transports/base.py b/google/cloud/network_management_v1/services/reachability_service/transports/base.py index 54216b2..42a0942 100644 --- a/google/cloud/network_management_v1/services/reachability_service/transports/base.py +++ b/google/cloud/network_management_v1/services/reachability_service/transports/base.py @@ -189,6 +189,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 operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/network_management_v1/services/reachability_service/transports/grpc.py b/google/cloud/network_management_v1/services/reachability_service/transports/grpc.py index d80be1d..68ad948 100644 --- a/google/cloud/network_management_v1/services/reachability_service/transports/grpc.py +++ b/google/cloud/network_management_v1/services/reachability_service/transports/grpc.py @@ -462,5 +462,8 @@ def delete_connectivity_test( ) return self._stubs["delete_connectivity_test"] + def close(self): + self.grpc_channel.close() + __all__ = ("ReachabilityServiceGrpcTransport",) diff --git a/google/cloud/network_management_v1/services/reachability_service/transports/grpc_asyncio.py b/google/cloud/network_management_v1/services/reachability_service/transports/grpc_asyncio.py index 914a8f5..4ceb3cf 100644 --- a/google/cloud/network_management_v1/services/reachability_service/transports/grpc_asyncio.py +++ b/google/cloud/network_management_v1/services/reachability_service/transports/grpc_asyncio.py @@ -471,5 +471,8 @@ def delete_connectivity_test( ) return self._stubs["delete_connectivity_test"] + def close(self): + return self.grpc_channel.close() + __all__ = ("ReachabilityServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/network_management_v1/types/connectivity_test.py b/google/cloud/network_management_v1/types/connectivity_test.py index b96cf43..bc57755 100644 --- a/google/cloud/network_management_v1/types/connectivity_test.py +++ b/google/cloud/network_management_v1/types/connectivity_test.py @@ -28,6 +28,7 @@ class ConnectivityTest(proto.Message): r"""A Connectivity Test for a network reachability analysis. + Attributes: name (str): Required. Unique name of the resource using the form: @@ -134,6 +135,7 @@ class ConnectivityTest(proto.Message): class Endpoint(proto.Message): r"""Source or destination of the Connectivity Test. + Attributes: ip_address (str): The IP address of the endpoint, which can be an external or diff --git a/google/cloud/network_management_v1/types/reachability.py b/google/cloud/network_management_v1/types/reachability.py index 203c5bb..2455270 100644 --- a/google/cloud/network_management_v1/types/reachability.py +++ b/google/cloud/network_management_v1/types/reachability.py @@ -37,6 +37,7 @@ class ListConnectivityTestsRequest(proto.Message): r"""Request for the ``ListConnectivityTests`` method. + Attributes: parent (str): Required. The parent resource of the Connectivity Tests: @@ -80,6 +81,7 @@ class ListConnectivityTestsRequest(proto.Message): class ListConnectivityTestsResponse(proto.Message): r"""Response for the ``ListConnectivityTests`` method. + Attributes: resources (Sequence[google.cloud.network_management_v1.types.ConnectivityTest]): List of Connectivity Tests. @@ -104,6 +106,7 @@ def raw_page(self): class GetConnectivityTestRequest(proto.Message): r"""Request for the ``GetConnectivityTest`` method. + Attributes: name (str): Required. ``ConnectivityTest`` resource name using the form: @@ -115,6 +118,7 @@ class GetConnectivityTestRequest(proto.Message): class CreateConnectivityTestRequest(proto.Message): r"""Request for the ``CreateConnectivityTest`` method. + Attributes: parent (str): Required. The parent resource of the Connectivity Test to @@ -142,6 +146,7 @@ class CreateConnectivityTestRequest(proto.Message): class UpdateConnectivityTestRequest(proto.Message): r"""Request for the ``UpdateConnectivityTest`` method. + Attributes: update_mask (google.protobuf.field_mask_pb2.FieldMask): Required. Mask of fields to update. At least @@ -160,6 +165,7 @@ class UpdateConnectivityTestRequest(proto.Message): class DeleteConnectivityTestRequest(proto.Message): r"""Request for the ``DeleteConnectivityTest`` method. + Attributes: name (str): Required. Connectivity Test resource name using the form: @@ -171,6 +177,7 @@ class DeleteConnectivityTestRequest(proto.Message): class RerunConnectivityTestRequest(proto.Message): r"""Request for the ``RerunConnectivityTest`` method. + Attributes: name (str): Required. Connectivity Test resource name using the form: @@ -182,6 +189,7 @@ class RerunConnectivityTestRequest(proto.Message): class OperationMetadata(proto.Message): r"""Metadata describing an [Operation][google.longrunning.Operation] + Attributes: create_time (google.protobuf.timestamp_pb2.Timestamp): The time the operation was created. diff --git a/google/cloud/network_management_v1/types/trace.py b/google/cloud/network_management_v1/types/trace.py index bfa1bbb..64fce56 100644 --- a/google/cloud/network_management_v1/types/trace.py +++ b/google/cloud/network_management_v1/types/trace.py @@ -428,6 +428,7 @@ class ForwardingRuleInfo(proto.Message): class LoadBalancerInfo(proto.Message): r"""For display only. Metadata associated with a load balancer. + Attributes: load_balancer_type (google.cloud.network_management_v1.types.LoadBalancerInfo.LoadBalancerType): Type of the load balancer. @@ -626,6 +627,7 @@ class EndpointInfo(proto.Message): class DeliverInfo(proto.Message): r"""Details of the final state "deliver" and associated resource. + Attributes: target (google.cloud.network_management_v1.types.DeliverInfo.Target): Target type where the packet is delivered to. @@ -649,6 +651,7 @@ class Target(proto.Enum): class ForwardInfo(proto.Message): r"""Details of the final state "forward" and associated resource. + Attributes: target (google.cloud.network_management_v1.types.ForwardInfo.Target): Target type where this packet is forwarded @@ -674,6 +677,7 @@ class Target(proto.Enum): class AbortInfo(proto.Message): r"""Details of the final state "abort" and associated resource. + Attributes: cause (google.cloud.network_management_v1.types.AbortInfo.Cause): Causes that the analysis is aborted. @@ -705,6 +709,7 @@ class Cause(proto.Enum): class DropInfo(proto.Message): r"""Details of the final state "drop" and associated resource. + Attributes: cause (google.cloud.network_management_v1.types.DropInfo.Cause): Cause that the packet is dropped. diff --git a/setup.py b/setup.py index 5f09ba2..20d4699 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ name = "google-cloud-network-management" description = "Network Management API client library" -version = "1.0.2" +version = "1.1.0" release_status = "Development Status :: 5 - Production/Stable" url = "https://github.com/googleapis/python-network-management" dependencies = [ diff --git a/tests/unit/gapic/network_management_v1/test_reachability_service.py b/tests/unit/gapic/network_management_v1/test_reachability_service.py index 54b9adb..42349f1 100644 --- a/tests/unit/gapic/network_management_v1/test_reachability_service.py +++ b/tests/unit/gapic/network_management_v1/test_reachability_service.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.network_management_v1.services.reachability_service import ( @@ -2130,6 +2131,9 @@ def test_reachability_service_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -2643,3 +2647,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 = ReachabilityServiceAsyncClient( + 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 = ReachabilityServiceClient( + 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 = ReachabilityServiceClient( + 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()