1# -*- coding: utf-8 -*-
2# Copyright 2025 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
28import google.protobuf
29
30from google.cloud.logging_v2.types import logging
31from google.longrunning import operations_pb2 # type: ignore
32from google.protobuf import empty_pb2 # type: ignore
33
34DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
35 gapic_version=package_version.__version__
36)
37
38if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER
39 DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__
40
41
42class LoggingServiceV2Transport(abc.ABC):
43 """Abstract transport class for LoggingServiceV2."""
44
45 AUTH_SCOPES = (
46 "https://www.googleapis.com/auth/cloud-platform",
47 "https://www.googleapis.com/auth/cloud-platform.read-only",
48 "https://www.googleapis.com/auth/logging.admin",
49 "https://www.googleapis.com/auth/logging.read",
50 "https://www.googleapis.com/auth/logging.write",
51 )
52
53 DEFAULT_HOST: str = "logging.googleapis.com"
54
55 def __init__(
56 self,
57 *,
58 host: str = DEFAULT_HOST,
59 credentials: Optional[ga_credentials.Credentials] = None,
60 credentials_file: Optional[str] = None,
61 scopes: Optional[Sequence[str]] = None,
62 quota_project_id: Optional[str] = None,
63 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
64 always_use_jwt_access: Optional[bool] = False,
65 api_audience: Optional[str] = None,
66 **kwargs,
67 ) -> None:
68 """Instantiate the transport.
69
70 Args:
71 host (Optional[str]):
72 The hostname to connect to (default: 'logging.googleapis.com').
73 credentials (Optional[google.auth.credentials.Credentials]): The
74 authorization credentials to attach to requests. These
75 credentials identify the application to the service; if none
76 are specified, the client will attempt to ascertain the
77 credentials from the environment.
78 credentials_file (Optional[str]): Deprecated. A file with credentials that can
79 be loaded with :func:`google.auth.load_credentials_from_file`.
80 This argument is mutually exclusive with credentials. This argument will be
81 removed in the next major version of this library.
82 scopes (Optional[Sequence[str]]): A list of scopes.
83 quota_project_id (Optional[str]): An optional project to use for billing
84 and quota.
85 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
86 The client info used to send a user-agent string along with
87 API requests. If ``None``, then default info will be used.
88 Generally, you only need to set this if you're developing
89 your own client library.
90 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
91 be used for service account credentials.
92 """
93
94 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}
95
96 # Save the scopes.
97 self._scopes = scopes
98 if not hasattr(self, "_ignore_credentials"):
99 self._ignore_credentials: bool = False
100
101 # If no credentials are provided, then determine the appropriate
102 # defaults.
103 if credentials and credentials_file:
104 raise core_exceptions.DuplicateCredentialArgs(
105 "'credentials_file' and 'credentials' are mutually exclusive"
106 )
107
108 if credentials_file is not None:
109 credentials, _ = google.auth.load_credentials_from_file(
110 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
111 )
112 elif credentials is None and not self._ignore_credentials:
113 credentials, _ = google.auth.default(
114 **scopes_kwargs, quota_project_id=quota_project_id
115 )
116 # Don't apply audience if the credentials file passed from user.
117 if hasattr(credentials, "with_gdch_audience"):
118 credentials = credentials.with_gdch_audience(
119 api_audience if api_audience else host
120 )
121
122 # If the credentials are service account credentials, then always try to use self signed JWT.
123 if (
124 always_use_jwt_access
125 and isinstance(credentials, service_account.Credentials)
126 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
127 ):
128 credentials = credentials.with_always_use_jwt_access(True)
129
130 # Save the credentials.
131 self._credentials = credentials
132
133 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
134 if ":" not in host:
135 host += ":443"
136 self._host = host
137
138 @property
139 def host(self):
140 return self._host
141
142 def _prep_wrapped_messages(self, client_info):
143 # Precompute the wrapped methods.
144 self._wrapped_methods = {
145 self.delete_log: gapic_v1.method.wrap_method(
146 self.delete_log,
147 default_retry=retries.Retry(
148 initial=0.1,
149 maximum=60.0,
150 multiplier=1.3,
151 predicate=retries.if_exception_type(
152 core_exceptions.DeadlineExceeded,
153 core_exceptions.InternalServerError,
154 core_exceptions.ServiceUnavailable,
155 ),
156 deadline=60.0,
157 ),
158 default_timeout=60.0,
159 client_info=client_info,
160 ),
161 self.write_log_entries: gapic_v1.method.wrap_method(
162 self.write_log_entries,
163 default_retry=retries.Retry(
164 initial=0.1,
165 maximum=60.0,
166 multiplier=1.3,
167 predicate=retries.if_exception_type(
168 core_exceptions.DeadlineExceeded,
169 core_exceptions.InternalServerError,
170 core_exceptions.ServiceUnavailable,
171 ),
172 deadline=60.0,
173 ),
174 default_timeout=60.0,
175 client_info=client_info,
176 ),
177 self.list_log_entries: gapic_v1.method.wrap_method(
178 self.list_log_entries,
179 default_retry=retries.Retry(
180 initial=0.1,
181 maximum=60.0,
182 multiplier=1.3,
183 predicate=retries.if_exception_type(
184 core_exceptions.DeadlineExceeded,
185 core_exceptions.InternalServerError,
186 core_exceptions.ServiceUnavailable,
187 ),
188 deadline=60.0,
189 ),
190 default_timeout=60.0,
191 client_info=client_info,
192 ),
193 self.list_monitored_resource_descriptors: gapic_v1.method.wrap_method(
194 self.list_monitored_resource_descriptors,
195 default_retry=retries.Retry(
196 initial=0.1,
197 maximum=60.0,
198 multiplier=1.3,
199 predicate=retries.if_exception_type(
200 core_exceptions.DeadlineExceeded,
201 core_exceptions.InternalServerError,
202 core_exceptions.ServiceUnavailable,
203 ),
204 deadline=60.0,
205 ),
206 default_timeout=60.0,
207 client_info=client_info,
208 ),
209 self.list_logs: gapic_v1.method.wrap_method(
210 self.list_logs,
211 default_retry=retries.Retry(
212 initial=0.1,
213 maximum=60.0,
214 multiplier=1.3,
215 predicate=retries.if_exception_type(
216 core_exceptions.DeadlineExceeded,
217 core_exceptions.InternalServerError,
218 core_exceptions.ServiceUnavailable,
219 ),
220 deadline=60.0,
221 ),
222 default_timeout=60.0,
223 client_info=client_info,
224 ),
225 self.tail_log_entries: gapic_v1.method.wrap_method(
226 self.tail_log_entries,
227 default_retry=retries.Retry(
228 initial=0.1,
229 maximum=60.0,
230 multiplier=1.3,
231 predicate=retries.if_exception_type(
232 core_exceptions.DeadlineExceeded,
233 core_exceptions.InternalServerError,
234 core_exceptions.ServiceUnavailable,
235 ),
236 deadline=3600.0,
237 ),
238 default_timeout=3600.0,
239 client_info=client_info,
240 ),
241 self.cancel_operation: gapic_v1.method.wrap_method(
242 self.cancel_operation,
243 default_timeout=None,
244 client_info=client_info,
245 ),
246 self.get_operation: gapic_v1.method.wrap_method(
247 self.get_operation,
248 default_timeout=None,
249 client_info=client_info,
250 ),
251 self.list_operations: gapic_v1.method.wrap_method(
252 self.list_operations,
253 default_timeout=None,
254 client_info=client_info,
255 ),
256 }
257
258 def close(self):
259 """Closes resources associated with the transport.
260
261 .. warning::
262 Only call this method if the transport is NOT shared
263 with other clients - this may cause errors in other clients!
264 """
265 raise NotImplementedError()
266
267 @property
268 def delete_log(
269 self,
270 ) -> Callable[
271 [logging.DeleteLogRequest], Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]]
272 ]:
273 raise NotImplementedError()
274
275 @property
276 def write_log_entries(
277 self,
278 ) -> Callable[
279 [logging.WriteLogEntriesRequest],
280 Union[
281 logging.WriteLogEntriesResponse, Awaitable[logging.WriteLogEntriesResponse]
282 ],
283 ]:
284 raise NotImplementedError()
285
286 @property
287 def list_log_entries(
288 self,
289 ) -> Callable[
290 [logging.ListLogEntriesRequest],
291 Union[
292 logging.ListLogEntriesResponse, Awaitable[logging.ListLogEntriesResponse]
293 ],
294 ]:
295 raise NotImplementedError()
296
297 @property
298 def list_monitored_resource_descriptors(
299 self,
300 ) -> Callable[
301 [logging.ListMonitoredResourceDescriptorsRequest],
302 Union[
303 logging.ListMonitoredResourceDescriptorsResponse,
304 Awaitable[logging.ListMonitoredResourceDescriptorsResponse],
305 ],
306 ]:
307 raise NotImplementedError()
308
309 @property
310 def list_logs(
311 self,
312 ) -> Callable[
313 [logging.ListLogsRequest],
314 Union[logging.ListLogsResponse, Awaitable[logging.ListLogsResponse]],
315 ]:
316 raise NotImplementedError()
317
318 @property
319 def tail_log_entries(
320 self,
321 ) -> Callable[
322 [logging.TailLogEntriesRequest],
323 Union[
324 logging.TailLogEntriesResponse, Awaitable[logging.TailLogEntriesResponse]
325 ],
326 ]:
327 raise NotImplementedError()
328
329 @property
330 def list_operations(
331 self,
332 ) -> Callable[
333 [operations_pb2.ListOperationsRequest],
334 Union[
335 operations_pb2.ListOperationsResponse,
336 Awaitable[operations_pb2.ListOperationsResponse],
337 ],
338 ]:
339 raise NotImplementedError()
340
341 @property
342 def get_operation(
343 self,
344 ) -> Callable[
345 [operations_pb2.GetOperationRequest],
346 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
347 ]:
348 raise NotImplementedError()
349
350 @property
351 def cancel_operation(
352 self,
353 ) -> Callable[[operations_pb2.CancelOperationRequest], None,]:
354 raise NotImplementedError()
355
356 @property
357 def kind(self) -> str:
358 raise NotImplementedError()
359
360
361__all__ = ("LoggingServiceV2Transport",)