Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/logging_v2/services/logging_service_v2/transports/base.py: 59%

69 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:45 +0000

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 

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 LoggingServiceV2Transport(abc.ABC): 

39 """Abstract transport class for LoggingServiceV2.""" 

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.delete_log: gapic_v1.method.wrap_method( 

135 self.delete_log, 

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.write_log_entries: gapic_v1.method.wrap_method( 

151 self.write_log_entries, 

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.list_log_entries: gapic_v1.method.wrap_method( 

167 self.list_log_entries, 

168 default_retry=retries.Retry( 

169 initial=0.1, 

170 maximum=60.0, 

171 multiplier=1.3, 

172 predicate=retries.if_exception_type( 

173 core_exceptions.DeadlineExceeded, 

174 core_exceptions.InternalServerError, 

175 core_exceptions.ServiceUnavailable, 

176 ), 

177 deadline=60.0, 

178 ), 

179 default_timeout=60.0, 

180 client_info=client_info, 

181 ), 

182 self.list_monitored_resource_descriptors: gapic_v1.method.wrap_method( 

183 self.list_monitored_resource_descriptors, 

184 default_retry=retries.Retry( 

185 initial=0.1, 

186 maximum=60.0, 

187 multiplier=1.3, 

188 predicate=retries.if_exception_type( 

189 core_exceptions.DeadlineExceeded, 

190 core_exceptions.InternalServerError, 

191 core_exceptions.ServiceUnavailable, 

192 ), 

193 deadline=60.0, 

194 ), 

195 default_timeout=60.0, 

196 client_info=client_info, 

197 ), 

198 self.list_logs: gapic_v1.method.wrap_method( 

199 self.list_logs, 

200 default_retry=retries.Retry( 

201 initial=0.1, 

202 maximum=60.0, 

203 multiplier=1.3, 

204 predicate=retries.if_exception_type( 

205 core_exceptions.DeadlineExceeded, 

206 core_exceptions.InternalServerError, 

207 core_exceptions.ServiceUnavailable, 

208 ), 

209 deadline=60.0, 

210 ), 

211 default_timeout=60.0, 

212 client_info=client_info, 

213 ), 

214 self.tail_log_entries: gapic_v1.method.wrap_method( 

215 self.tail_log_entries, 

216 default_retry=retries.Retry( 

217 initial=0.1, 

218 maximum=60.0, 

219 multiplier=1.3, 

220 predicate=retries.if_exception_type( 

221 core_exceptions.DeadlineExceeded, 

222 core_exceptions.InternalServerError, 

223 core_exceptions.ServiceUnavailable, 

224 ), 

225 deadline=3600.0, 

226 ), 

227 default_timeout=3600.0, 

228 client_info=client_info, 

229 ), 

230 } 

231 

232 def close(self): 

233 """Closes resources associated with the transport. 

234 

235 .. warning:: 

236 Only call this method if the transport is NOT shared 

237 with other clients - this may cause errors in other clients! 

238 """ 

239 raise NotImplementedError() 

240 

241 @property 

242 def delete_log( 

243 self, 

244 ) -> Callable[ 

245 [logging.DeleteLogRequest], Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]] 

246 ]: 

247 raise NotImplementedError() 

248 

249 @property 

250 def write_log_entries( 

251 self, 

252 ) -> Callable[ 

253 [logging.WriteLogEntriesRequest], 

254 Union[ 

255 logging.WriteLogEntriesResponse, Awaitable[logging.WriteLogEntriesResponse] 

256 ], 

257 ]: 

258 raise NotImplementedError() 

259 

260 @property 

261 def list_log_entries( 

262 self, 

263 ) -> Callable[ 

264 [logging.ListLogEntriesRequest], 

265 Union[ 

266 logging.ListLogEntriesResponse, Awaitable[logging.ListLogEntriesResponse] 

267 ], 

268 ]: 

269 raise NotImplementedError() 

270 

271 @property 

272 def list_monitored_resource_descriptors( 

273 self, 

274 ) -> Callable[ 

275 [logging.ListMonitoredResourceDescriptorsRequest], 

276 Union[ 

277 logging.ListMonitoredResourceDescriptorsResponse, 

278 Awaitable[logging.ListMonitoredResourceDescriptorsResponse], 

279 ], 

280 ]: 

281 raise NotImplementedError() 

282 

283 @property 

284 def list_logs( 

285 self, 

286 ) -> Callable[ 

287 [logging.ListLogsRequest], 

288 Union[logging.ListLogsResponse, Awaitable[logging.ListLogsResponse]], 

289 ]: 

290 raise NotImplementedError() 

291 

292 @property 

293 def tail_log_entries( 

294 self, 

295 ) -> Callable[ 

296 [logging.TailLogEntriesRequest], 

297 Union[ 

298 logging.TailLogEntriesResponse, Awaitable[logging.TailLogEntriesResponse] 

299 ], 

300 ]: 

301 raise NotImplementedError() 

302 

303 @property 

304 def list_operations( 

305 self, 

306 ) -> Callable[ 

307 [operations_pb2.ListOperationsRequest], 

308 Union[ 

309 operations_pb2.ListOperationsResponse, 

310 Awaitable[operations_pb2.ListOperationsResponse], 

311 ], 

312 ]: 

313 raise NotImplementedError() 

314 

315 @property 

316 def get_operation( 

317 self, 

318 ) -> Callable[ 

319 [operations_pb2.GetOperationRequest], 

320 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], 

321 ]: 

322 raise NotImplementedError() 

323 

324 @property 

325 def cancel_operation( 

326 self, 

327 ) -> Callable[[operations_pb2.CancelOperationRequest], None,]: 

328 raise NotImplementedError() 

329 

330 @property 

331 def kind(self) -> str: 

332 raise NotImplementedError() 

333 

334 

335__all__ = ("LoggingServiceV2Transport",)