1# -*- coding: utf-8 -*-
2# Copyright 2023 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import abc
17from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
18
19from google.cloud.logging_v2 import gapic_version as package_version
20
21import google.auth # type: ignore
22import google.api_core
23from google.api_core import exceptions as core_exceptions
24from google.api_core import gapic_v1
25from google.api_core import retry as retries
26from google.auth import credentials as ga_credentials # type: ignore
27from google.oauth2 import service_account # type: ignore
28
29from google.cloud.logging_v2.types import logging_metrics
30from google.longrunning import operations_pb2 # type: ignore
31from google.protobuf import empty_pb2 # type: ignore
32
33DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
34 gapic_version=package_version.__version__
35)
36
37
38class MetricsServiceV2Transport(abc.ABC):
39 """Abstract transport class for MetricsServiceV2."""
40
41 AUTH_SCOPES = (
42 "https://www.googleapis.com/auth/cloud-platform",
43 "https://www.googleapis.com/auth/cloud-platform.read-only",
44 "https://www.googleapis.com/auth/logging.admin",
45 "https://www.googleapis.com/auth/logging.read",
46 "https://www.googleapis.com/auth/logging.write",
47 )
48
49 DEFAULT_HOST: str = "logging.googleapis.com"
50
51 def __init__(
52 self,
53 *,
54 host: str = DEFAULT_HOST,
55 credentials: Optional[ga_credentials.Credentials] = None,
56 credentials_file: Optional[str] = None,
57 scopes: Optional[Sequence[str]] = None,
58 quota_project_id: Optional[str] = None,
59 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
60 always_use_jwt_access: Optional[bool] = False,
61 api_audience: Optional[str] = None,
62 **kwargs,
63 ) -> None:
64 """Instantiate the transport.
65
66 Args:
67 host (Optional[str]):
68 The hostname to connect to.
69 credentials (Optional[google.auth.credentials.Credentials]): The
70 authorization credentials to attach to requests. These
71 credentials identify the application to the service; if none
72 are specified, the client will attempt to ascertain the
73 credentials from the environment.
74 credentials_file (Optional[str]): A file with credentials that can
75 be loaded with :func:`google.auth.load_credentials_from_file`.
76 This argument is mutually exclusive with credentials.
77 scopes (Optional[Sequence[str]]): A list of scopes.
78 quota_project_id (Optional[str]): An optional project to use for billing
79 and quota.
80 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
81 The client info used to send a user-agent string along with
82 API requests. If ``None``, then default info will be used.
83 Generally, you only need to set this if you're developing
84 your own client library.
85 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
86 be used for service account credentials.
87 """
88
89 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}
90
91 # Save the scopes.
92 self._scopes = scopes
93
94 # If no credentials are provided, then determine the appropriate
95 # defaults.
96 if credentials and credentials_file:
97 raise core_exceptions.DuplicateCredentialArgs(
98 "'credentials_file' and 'credentials' are mutually exclusive"
99 )
100
101 if credentials_file is not None:
102 credentials, _ = google.auth.load_credentials_from_file(
103 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
104 )
105 elif credentials is None:
106 credentials, _ = google.auth.default(
107 **scopes_kwargs, quota_project_id=quota_project_id
108 )
109 # Don't apply audience if the credentials file passed from user.
110 if hasattr(credentials, "with_gdch_audience"):
111 credentials = credentials.with_gdch_audience(
112 api_audience if api_audience else host
113 )
114
115 # If the credentials are service account credentials, then always try to use self signed JWT.
116 if (
117 always_use_jwt_access
118 and isinstance(credentials, service_account.Credentials)
119 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
120 ):
121 credentials = credentials.with_always_use_jwt_access(True)
122
123 # Save the credentials.
124 self._credentials = credentials
125
126 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
127 if ":" not in host:
128 host += ":443"
129 self._host = host
130
131 def _prep_wrapped_messages(self, client_info):
132 # Precompute the wrapped methods.
133 self._wrapped_methods = {
134 self.list_log_metrics: gapic_v1.method.wrap_method(
135 self.list_log_metrics,
136 default_retry=retries.Retry(
137 initial=0.1,
138 maximum=60.0,
139 multiplier=1.3,
140 predicate=retries.if_exception_type(
141 core_exceptions.DeadlineExceeded,
142 core_exceptions.InternalServerError,
143 core_exceptions.ServiceUnavailable,
144 ),
145 deadline=60.0,
146 ),
147 default_timeout=60.0,
148 client_info=client_info,
149 ),
150 self.get_log_metric: gapic_v1.method.wrap_method(
151 self.get_log_metric,
152 default_retry=retries.Retry(
153 initial=0.1,
154 maximum=60.0,
155 multiplier=1.3,
156 predicate=retries.if_exception_type(
157 core_exceptions.DeadlineExceeded,
158 core_exceptions.InternalServerError,
159 core_exceptions.ServiceUnavailable,
160 ),
161 deadline=60.0,
162 ),
163 default_timeout=60.0,
164 client_info=client_info,
165 ),
166 self.create_log_metric: gapic_v1.method.wrap_method(
167 self.create_log_metric,
168 default_timeout=60.0,
169 client_info=client_info,
170 ),
171 self.update_log_metric: gapic_v1.method.wrap_method(
172 self.update_log_metric,
173 default_retry=retries.Retry(
174 initial=0.1,
175 maximum=60.0,
176 multiplier=1.3,
177 predicate=retries.if_exception_type(
178 core_exceptions.DeadlineExceeded,
179 core_exceptions.InternalServerError,
180 core_exceptions.ServiceUnavailable,
181 ),
182 deadline=60.0,
183 ),
184 default_timeout=60.0,
185 client_info=client_info,
186 ),
187 self.delete_log_metric: gapic_v1.method.wrap_method(
188 self.delete_log_metric,
189 default_retry=retries.Retry(
190 initial=0.1,
191 maximum=60.0,
192 multiplier=1.3,
193 predicate=retries.if_exception_type(
194 core_exceptions.DeadlineExceeded,
195 core_exceptions.InternalServerError,
196 core_exceptions.ServiceUnavailable,
197 ),
198 deadline=60.0,
199 ),
200 default_timeout=60.0,
201 client_info=client_info,
202 ),
203 }
204
205 def close(self):
206 """Closes resources associated with the transport.
207
208 .. warning::
209 Only call this method if the transport is NOT shared
210 with other clients - this may cause errors in other clients!
211 """
212 raise NotImplementedError()
213
214 @property
215 def list_log_metrics(
216 self,
217 ) -> Callable[
218 [logging_metrics.ListLogMetricsRequest],
219 Union[
220 logging_metrics.ListLogMetricsResponse,
221 Awaitable[logging_metrics.ListLogMetricsResponse],
222 ],
223 ]:
224 raise NotImplementedError()
225
226 @property
227 def get_log_metric(
228 self,
229 ) -> Callable[
230 [logging_metrics.GetLogMetricRequest],
231 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]],
232 ]:
233 raise NotImplementedError()
234
235 @property
236 def create_log_metric(
237 self,
238 ) -> Callable[
239 [logging_metrics.CreateLogMetricRequest],
240 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]],
241 ]:
242 raise NotImplementedError()
243
244 @property
245 def update_log_metric(
246 self,
247 ) -> Callable[
248 [logging_metrics.UpdateLogMetricRequest],
249 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]],
250 ]:
251 raise NotImplementedError()
252
253 @property
254 def delete_log_metric(
255 self,
256 ) -> Callable[
257 [logging_metrics.DeleteLogMetricRequest],
258 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
259 ]:
260 raise NotImplementedError()
261
262 @property
263 def list_operations(
264 self,
265 ) -> Callable[
266 [operations_pb2.ListOperationsRequest],
267 Union[
268 operations_pb2.ListOperationsResponse,
269 Awaitable[operations_pb2.ListOperationsResponse],
270 ],
271 ]:
272 raise NotImplementedError()
273
274 @property
275 def get_operation(
276 self,
277 ) -> Callable[
278 [operations_pb2.GetOperationRequest],
279 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
280 ]:
281 raise NotImplementedError()
282
283 @property
284 def cancel_operation(
285 self,
286 ) -> Callable[[operations_pb2.CancelOperationRequest], None,]:
287 raise NotImplementedError()
288
289 @property
290 def kind(self) -> str:
291 raise NotImplementedError()
292
293
294__all__ = ("MetricsServiceV2Transport",)