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