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
32import google.protobuf.empty_pb2 as 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 # Save the scopes.
95 self._scopes = scopes
96 if not hasattr(self, "_ignore_credentials"):
97 self._ignore_credentials: bool = False
98
99 # If no credentials are provided, then determine the appropriate
100 # defaults.
101 if credentials and credentials_file:
102 raise core_exceptions.DuplicateCredentialArgs(
103 "'credentials_file' and 'credentials' are mutually exclusive"
104 )
105
106 if credentials_file is not None:
107 credentials, _ = google.auth.load_credentials_from_file(
108 credentials_file,
109 scopes=scopes,
110 quota_project_id=quota_project_id,
111 default_scopes=self.AUTH_SCOPES,
112 )
113 elif credentials is None and not self._ignore_credentials:
114 credentials, _ = google.auth.default(
115 scopes=scopes,
116 quota_project_id=quota_project_id,
117 default_scopes=self.AUTH_SCOPES,
118 )
119 # Don't apply audience if the credentials file passed from user.
120 if hasattr(credentials, "with_gdch_audience"):
121 credentials = credentials.with_gdch_audience(
122 api_audience if api_audience else host
123 )
124
125 # If the credentials are service account credentials, then always try to use self signed JWT.
126 if (
127 always_use_jwt_access
128 and isinstance(credentials, service_account.Credentials)
129 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
130 ):
131 credentials = credentials.with_always_use_jwt_access(True)
132
133 # Save the credentials.
134 self._credentials = credentials
135
136 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
137 if ":" not in host:
138 host += ":443"
139 self._host = host
140
141 @property
142 def host(self):
143 return self._host
144
145 def _prep_wrapped_messages(self, client_info):
146 # Precompute the wrapped methods.
147 self._wrapped_methods = {
148 self.delete_log: gapic_v1.method.wrap_method(
149 self.delete_log,
150 default_retry=retries.Retry(
151 initial=0.1,
152 maximum=60.0,
153 multiplier=1.3,
154 predicate=retries.if_exception_type(
155 core_exceptions.DeadlineExceeded,
156 core_exceptions.InternalServerError,
157 core_exceptions.ServiceUnavailable,
158 ),
159 deadline=60.0,
160 ),
161 default_timeout=60.0,
162 client_info=client_info,
163 ),
164 self.write_log_entries: gapic_v1.method.wrap_method(
165 self.write_log_entries,
166 default_retry=retries.Retry(
167 initial=0.1,
168 maximum=60.0,
169 multiplier=1.3,
170 predicate=retries.if_exception_type(
171 core_exceptions.DeadlineExceeded,
172 core_exceptions.InternalServerError,
173 core_exceptions.ServiceUnavailable,
174 ),
175 deadline=60.0,
176 ),
177 default_timeout=60.0,
178 client_info=client_info,
179 ),
180 self.list_log_entries: gapic_v1.method.wrap_method(
181 self.list_log_entries,
182 default_retry=retries.Retry(
183 initial=0.1,
184 maximum=60.0,
185 multiplier=1.3,
186 predicate=retries.if_exception_type(
187 core_exceptions.DeadlineExceeded,
188 core_exceptions.InternalServerError,
189 core_exceptions.ServiceUnavailable,
190 ),
191 deadline=60.0,
192 ),
193 default_timeout=60.0,
194 client_info=client_info,
195 ),
196 self.list_monitored_resource_descriptors: gapic_v1.method.wrap_method(
197 self.list_monitored_resource_descriptors,
198 default_retry=retries.Retry(
199 initial=0.1,
200 maximum=60.0,
201 multiplier=1.3,
202 predicate=retries.if_exception_type(
203 core_exceptions.DeadlineExceeded,
204 core_exceptions.InternalServerError,
205 core_exceptions.ServiceUnavailable,
206 ),
207 deadline=60.0,
208 ),
209 default_timeout=60.0,
210 client_info=client_info,
211 ),
212 self.list_logs: gapic_v1.method.wrap_method(
213 self.list_logs,
214 default_retry=retries.Retry(
215 initial=0.1,
216 maximum=60.0,
217 multiplier=1.3,
218 predicate=retries.if_exception_type(
219 core_exceptions.DeadlineExceeded,
220 core_exceptions.InternalServerError,
221 core_exceptions.ServiceUnavailable,
222 ),
223 deadline=60.0,
224 ),
225 default_timeout=60.0,
226 client_info=client_info,
227 ),
228 self.tail_log_entries: gapic_v1.method.wrap_method(
229 self.tail_log_entries,
230 default_retry=retries.Retry(
231 initial=0.1,
232 maximum=60.0,
233 multiplier=1.3,
234 predicate=retries.if_exception_type(
235 core_exceptions.DeadlineExceeded,
236 core_exceptions.InternalServerError,
237 core_exceptions.ServiceUnavailable,
238 ),
239 deadline=3600.0,
240 ),
241 default_timeout=3600.0,
242 client_info=client_info,
243 ),
244 self.cancel_operation: gapic_v1.method.wrap_method(
245 self.cancel_operation,
246 default_timeout=None,
247 client_info=client_info,
248 ),
249 self.get_operation: gapic_v1.method.wrap_method(
250 self.get_operation,
251 default_timeout=None,
252 client_info=client_info,
253 ),
254 self.list_operations: gapic_v1.method.wrap_method(
255 self.list_operations,
256 default_timeout=None,
257 client_info=client_info,
258 ),
259 }
260
261 def close(self):
262 """Closes resources associated with the transport.
263
264 .. warning::
265 Only call this method if the transport is NOT shared
266 with other clients - this may cause errors in other clients!
267 """
268 raise NotImplementedError()
269
270 @property
271 def delete_log(
272 self,
273 ) -> Callable[
274 [logging.DeleteLogRequest], Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]]
275 ]:
276 raise NotImplementedError()
277
278 @property
279 def write_log_entries(
280 self,
281 ) -> Callable[
282 [logging.WriteLogEntriesRequest],
283 Union[
284 logging.WriteLogEntriesResponse, Awaitable[logging.WriteLogEntriesResponse]
285 ],
286 ]:
287 raise NotImplementedError()
288
289 @property
290 def list_log_entries(
291 self,
292 ) -> Callable[
293 [logging.ListLogEntriesRequest],
294 Union[
295 logging.ListLogEntriesResponse, Awaitable[logging.ListLogEntriesResponse]
296 ],
297 ]:
298 raise NotImplementedError()
299
300 @property
301 def list_monitored_resource_descriptors(
302 self,
303 ) -> Callable[
304 [logging.ListMonitoredResourceDescriptorsRequest],
305 Union[
306 logging.ListMonitoredResourceDescriptorsResponse,
307 Awaitable[logging.ListMonitoredResourceDescriptorsResponse],
308 ],
309 ]:
310 raise NotImplementedError()
311
312 @property
313 def list_logs(
314 self,
315 ) -> Callable[
316 [logging.ListLogsRequest],
317 Union[logging.ListLogsResponse, Awaitable[logging.ListLogsResponse]],
318 ]:
319 raise NotImplementedError()
320
321 @property
322 def tail_log_entries(
323 self,
324 ) -> Callable[
325 [logging.TailLogEntriesRequest],
326 Union[
327 logging.TailLogEntriesResponse, Awaitable[logging.TailLogEntriesResponse]
328 ],
329 ]:
330 raise NotImplementedError()
331
332 @property
333 def list_operations(
334 self,
335 ) -> Callable[
336 [operations_pb2.ListOperationsRequest],
337 Union[
338 operations_pb2.ListOperationsResponse,
339 Awaitable[operations_pb2.ListOperationsResponse],
340 ],
341 ]:
342 raise NotImplementedError()
343
344 @property
345 def get_operation(
346 self,
347 ) -> Callable[
348 [operations_pb2.GetOperationRequest],
349 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
350 ]:
351 raise NotImplementedError()
352
353 @property
354 def cancel_operation(
355 self,
356 ) -> Callable[[operations_pb2.CancelOperationRequest], None,]:
357 raise NotImplementedError()
358
359 @property
360 def kind(self) -> str:
361 raise NotImplementedError()
362
363
364__all__ = ("LoggingServiceV2Transport",)