diff --git a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/async_client.py b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/async_client.py index a582e0e..df96c58 100644 --- a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/async_client.py +++ b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/async_client.py @@ -444,6 +444,12 @@ async def update_dashboard( # 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/monitoring_dashboard_v1/services/dashboards_service/client.py b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/client.py index 986dc71..b1994db 100644 --- a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/client.py +++ b/google/cloud/monitoring_dashboard_v1/services/dashboards_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 create_dashboard( @@ -648,6 +645,19 @@ def update_dashboard( # 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/monitoring_dashboard_v1/services/dashboards_service/transports/base.py b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/base.py index 775559b..143a306 100644 --- a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/base.py +++ b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/base.py @@ -178,6 +178,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 create_dashboard( self, diff --git a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc.py b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc.py index 64c8c1d..39fdb51 100644 --- a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc.py +++ b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc.py @@ -387,5 +387,8 @@ def update_dashboard( ) return self._stubs["update_dashboard"] + def close(self): + self.grpc_channel.close() + __all__ = ("DashboardsServiceGrpcTransport",) diff --git a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc_asyncio.py b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc_asyncio.py index fe1c3cf..93bf46c 100644 --- a/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc_asyncio.py +++ b/google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc_asyncio.py @@ -398,5 +398,8 @@ def update_dashboard( ) return self._stubs["update_dashboard"] + def close(self): + return self.grpc_channel.close() + __all__ = ("DashboardsServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/monitoring_dashboard_v1/types/alertchart.py b/google/cloud/monitoring_dashboard_v1/types/alertchart.py index 0bb3029..69dfc9f 100644 --- a/google/cloud/monitoring_dashboard_v1/types/alertchart.py +++ b/google/cloud/monitoring_dashboard_v1/types/alertchart.py @@ -23,6 +23,7 @@ class AlertChart(proto.Message): r"""A chart that displays alert policy data. + Attributes: name (str): Required. The resource name of the alert policy. The format diff --git a/google/cloud/monitoring_dashboard_v1/types/dashboards_service.py b/google/cloud/monitoring_dashboard_v1/types/dashboards_service.py index 825228e..84cda3c 100644 --- a/google/cloud/monitoring_dashboard_v1/types/dashboards_service.py +++ b/google/cloud/monitoring_dashboard_v1/types/dashboards_service.py @@ -33,6 +33,7 @@ class CreateDashboardRequest(proto.Message): r"""The ``CreateDashboard`` request. + Attributes: parent (str): Required. The project on which to execute the request. The @@ -59,6 +60,7 @@ class CreateDashboardRequest(proto.Message): class ListDashboardsRequest(proto.Message): r"""The ``ListDashboards`` request. + Attributes: parent (str): Required. The scope of the dashboards to list. The format @@ -85,6 +87,7 @@ class ListDashboardsRequest(proto.Message): class ListDashboardsResponse(proto.Message): r"""The ``ListDashboards`` request. + Attributes: dashboards (Sequence[google.cloud.monitoring_dashboard_v1.types.Dashboard]): The list of requested dashboards. @@ -107,6 +110,7 @@ def raw_page(self): class GetDashboardRequest(proto.Message): r"""The ``GetDashboard`` request. + Attributes: name (str): Required. The resource name of the Dashboard. The format is @@ -122,6 +126,7 @@ class GetDashboardRequest(proto.Message): class DeleteDashboardRequest(proto.Message): r"""The ``DeleteDashboard`` request. + Attributes: name (str): Required. The resource name of the Dashboard. The format is: @@ -136,6 +141,7 @@ class DeleteDashboardRequest(proto.Message): class UpdateDashboardRequest(proto.Message): r"""The ``UpdateDashboard`` request. + Attributes: dashboard (google.cloud.monitoring_dashboard_v1.types.Dashboard): Required. The dashboard that will replace the diff --git a/google/cloud/monitoring_dashboard_v1/types/layouts.py b/google/cloud/monitoring_dashboard_v1/types/layouts.py index fbfbb6b..d4ec2cb 100644 --- a/google/cloud/monitoring_dashboard_v1/types/layouts.py +++ b/google/cloud/monitoring_dashboard_v1/types/layouts.py @@ -104,6 +104,7 @@ class RowLayout(proto.Message): class Row(proto.Message): r"""Defines the layout properties and content for a row. + Attributes: weight (int): The relative weight of this row. The row @@ -137,6 +138,7 @@ class ColumnLayout(proto.Message): class Column(proto.Message): r"""Defines the layout properties and content for a column. + Attributes: weight (int): The relative weight of this column. The diff --git a/google/cloud/monitoring_dashboard_v1/types/metrics.py b/google/cloud/monitoring_dashboard_v1/types/metrics.py index 784d502..abcaa68 100644 --- a/google/cloud/monitoring_dashboard_v1/types/metrics.py +++ b/google/cloud/monitoring_dashboard_v1/types/metrics.py @@ -176,6 +176,7 @@ class RatioPart(proto.Message): class Threshold(proto.Message): r"""Defines a threshold for categorizing time series values. + Attributes: label (str): A label for the threshold. diff --git a/google/cloud/monitoring_dashboard_v1/types/text.py b/google/cloud/monitoring_dashboard_v1/types/text.py index 81da18b..8fb4831 100644 --- a/google/cloud/monitoring_dashboard_v1/types/text.py +++ b/google/cloud/monitoring_dashboard_v1/types/text.py @@ -23,6 +23,7 @@ class Text(proto.Message): r"""A widget that displays textual content. + Attributes: content (str): The text content to be displayed. diff --git a/google/cloud/monitoring_dashboard_v1/types/xychart.py b/google/cloud/monitoring_dashboard_v1/types/xychart.py index 0f78b40..d77adec 100644 --- a/google/cloud/monitoring_dashboard_v1/types/xychart.py +++ b/google/cloud/monitoring_dashboard_v1/types/xychart.py @@ -26,6 +26,7 @@ class XyChart(proto.Message): r"""A chart that displays data on a 2D (X and Y axes) plane. + Attributes: data_sets (Sequence[google.cloud.monitoring_dashboard_v1.types.XyChart.DataSet]): Required. The data displayed in this chart. @@ -50,6 +51,7 @@ class XyChart(proto.Message): class DataSet(proto.Message): r"""Groups a time series query definition with charting options. + Attributes: time_series_query (google.cloud.monitoring_dashboard_v1.types.TimeSeriesQuery): Required. Fields for querying time series @@ -90,6 +92,7 @@ class PlotType(proto.Enum): class Axis(proto.Message): r"""A chart axis. + Attributes: label (str): The label of the axis. @@ -121,6 +124,7 @@ class Scale(proto.Enum): class ChartOptions(proto.Message): r"""Options to control visual rendering of a chart. + Attributes: mode (google.cloud.monitoring_dashboard_v1.types.ChartOptions.Mode): The chart mode. diff --git a/tests/unit/gapic/dashboard_v1/test_dashboards_service.py b/tests/unit/gapic/dashboard_v1/test_dashboards_service.py index e30e6ea..373bcb5 100644 --- a/tests/unit/gapic/dashboard_v1/test_dashboards_service.py +++ b/tests/unit/gapic/dashboard_v1/test_dashboards_service.py @@ -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.monitoring_dashboard_v1.services.dashboards_service import ( @@ -1479,6 +1480,9 @@ def test_dashboards_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_dashboards_service_base_transport_with_credentials_file(): @@ -2016,3 +2020,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 = DashboardsServiceAsyncClient( + 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 = DashboardsServiceClient( + 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 = DashboardsServiceClient( + 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()