Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py: 43%

91 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 warnings 

17from typing import Callable, Dict, Optional, Sequence, Tuple, Union 

18 

19from google.api_core import grpc_helpers 

20from google.api_core import gapic_v1 

21import google.auth # type: ignore 

22from google.auth import credentials as ga_credentials # type: ignore 

23from google.auth.transport.grpc import SslCredentials # type: ignore 

24 

25import grpc # type: ignore 

26 

27from google.cloud.logging_v2.types import logging_metrics 

28from google.longrunning import operations_pb2 # type: ignore 

29from google.protobuf import empty_pb2 # type: ignore 

30from .base import MetricsServiceV2Transport, DEFAULT_CLIENT_INFO 

31 

32 

33class MetricsServiceV2GrpcTransport(MetricsServiceV2Transport): 

34 """gRPC backend transport for MetricsServiceV2. 

35 

36 Service for configuring logs-based metrics. 

37 

38 This class defines the same methods as the primary client, so the 

39 primary client can load the underlying transport implementation 

40 and call it. 

41 

42 It sends protocol buffers over the wire using gRPC (which is built on 

43 top of HTTP/2); the ``grpcio`` package must be installed. 

44 """ 

45 

46 _stubs: Dict[str, Callable] 

47 

48 def __init__( 

49 self, 

50 *, 

51 host: str = "logging.googleapis.com", 

52 credentials: Optional[ga_credentials.Credentials] = None, 

53 credentials_file: Optional[str] = None, 

54 scopes: Optional[Sequence[str]] = None, 

55 channel: Optional[grpc.Channel] = None, 

56 api_mtls_endpoint: Optional[str] = None, 

57 client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, 

58 ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, 

59 client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, 

60 quota_project_id: Optional[str] = None, 

61 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

62 always_use_jwt_access: Optional[bool] = False, 

63 api_audience: Optional[str] = None, 

64 ) -> None: 

65 """Instantiate the transport. 

66 

67 Args: 

68 host (Optional[str]): 

69 The hostname to connect to. 

70 credentials (Optional[google.auth.credentials.Credentials]): The 

71 authorization credentials to attach to requests. These 

72 credentials identify the application to the service; if none 

73 are specified, the client will attempt to ascertain the 

74 credentials from the environment. 

75 This argument is ignored if ``channel`` is provided. 

76 credentials_file (Optional[str]): A file with credentials that can 

77 be loaded with :func:`google.auth.load_credentials_from_file`. 

78 This argument is ignored if ``channel`` is provided. 

79 scopes (Optional(Sequence[str])): A list of scopes. This argument is 

80 ignored if ``channel`` is provided. 

81 channel (Optional[grpc.Channel]): A ``Channel`` instance through 

82 which to make calls. 

83 api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. 

84 If provided, it overrides the ``host`` argument and tries to create 

85 a mutual TLS channel with client SSL credentials from 

86 ``client_cert_source`` or application default SSL credentials. 

87 client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): 

88 Deprecated. A callback to provide client SSL certificate bytes and 

89 private key bytes, both in PEM format. It is ignored if 

90 ``api_mtls_endpoint`` is None. 

91 ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials 

92 for the grpc channel. It is ignored if ``channel`` is provided. 

93 client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): 

94 A callback to provide client certificate bytes and private key bytes, 

95 both in PEM format. It is used to configure a mutual TLS channel. It is 

96 ignored if ``channel`` or ``ssl_channel_credentials`` is provided. 

97 quota_project_id (Optional[str]): An optional project to use for billing 

98 and quota. 

99 client_info (google.api_core.gapic_v1.client_info.ClientInfo): 

100 The client info used to send a user-agent string along with 

101 API requests. If ``None``, then default info will be used. 

102 Generally, you only need to set this if you're developing 

103 your own client library. 

104 always_use_jwt_access (Optional[bool]): Whether self signed JWT should 

105 be used for service account credentials. 

106 

107 Raises: 

108 google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport 

109 creation failed for any reason. 

110 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` 

111 and ``credentials_file`` are passed. 

112 """ 

113 self._grpc_channel = None 

114 self._ssl_channel_credentials = ssl_channel_credentials 

115 self._stubs: Dict[str, Callable] = {} 

116 

117 if api_mtls_endpoint: 

118 warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) 

119 if client_cert_source: 

120 warnings.warn("client_cert_source is deprecated", DeprecationWarning) 

121 

122 if channel: 

123 # Ignore credentials if a channel was passed. 

124 credentials = False 

125 # If a channel was explicitly provided, set it. 

126 self._grpc_channel = channel 

127 self._ssl_channel_credentials = None 

128 

129 else: 

130 if api_mtls_endpoint: 

131 host = api_mtls_endpoint 

132 

133 # Create SSL credentials with client_cert_source or application 

134 # default SSL credentials. 

135 if client_cert_source: 

136 cert, key = client_cert_source() 

137 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

138 certificate_chain=cert, private_key=key 

139 ) 

140 else: 

141 self._ssl_channel_credentials = SslCredentials().ssl_credentials 

142 

143 else: 

144 if client_cert_source_for_mtls and not ssl_channel_credentials: 

145 cert, key = client_cert_source_for_mtls() 

146 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

147 certificate_chain=cert, private_key=key 

148 ) 

149 

150 # The base transport sets the host, credentials and scopes 

151 super().__init__( 

152 host=host, 

153 credentials=credentials, 

154 credentials_file=credentials_file, 

155 scopes=scopes, 

156 quota_project_id=quota_project_id, 

157 client_info=client_info, 

158 always_use_jwt_access=always_use_jwt_access, 

159 api_audience=api_audience, 

160 ) 

161 

162 if not self._grpc_channel: 

163 self._grpc_channel = type(self).create_channel( 

164 self._host, 

165 # use the credentials which are saved 

166 credentials=self._credentials, 

167 # Set ``credentials_file`` to ``None`` here as 

168 # the credentials that we saved earlier should be used. 

169 credentials_file=None, 

170 scopes=self._scopes, 

171 ssl_credentials=self._ssl_channel_credentials, 

172 quota_project_id=quota_project_id, 

173 options=[ 

174 ("grpc.max_send_message_length", -1), 

175 ("grpc.max_receive_message_length", -1), 

176 ], 

177 ) 

178 

179 # Wrap messages. This must be done after self._grpc_channel exists 

180 self._prep_wrapped_messages(client_info) 

181 

182 @classmethod 

183 def create_channel( 

184 cls, 

185 host: str = "logging.googleapis.com", 

186 credentials: Optional[ga_credentials.Credentials] = None, 

187 credentials_file: Optional[str] = None, 

188 scopes: Optional[Sequence[str]] = None, 

189 quota_project_id: Optional[str] = None, 

190 **kwargs, 

191 ) -> grpc.Channel: 

192 """Create and return a gRPC channel object. 

193 Args: 

194 host (Optional[str]): The host for the channel to use. 

195 credentials (Optional[~.Credentials]): The 

196 authorization credentials to attach to requests. These 

197 credentials identify this application to the service. If 

198 none are specified, the client will attempt to ascertain 

199 the credentials from the environment. 

200 credentials_file (Optional[str]): A file with credentials that can 

201 be loaded with :func:`google.auth.load_credentials_from_file`. 

202 This argument is mutually exclusive with credentials. 

203 scopes (Optional[Sequence[str]]): A optional list of scopes needed for this 

204 service. These are only used when credentials are not specified and 

205 are passed to :func:`google.auth.default`. 

206 quota_project_id (Optional[str]): An optional project to use for billing 

207 and quota. 

208 kwargs (Optional[dict]): Keyword arguments, which are passed to the 

209 channel creation. 

210 Returns: 

211 grpc.Channel: A gRPC channel object. 

212 

213 Raises: 

214 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` 

215 and ``credentials_file`` are passed. 

216 """ 

217 

218 return grpc_helpers.create_channel( 

219 host, 

220 credentials=credentials, 

221 credentials_file=credentials_file, 

222 quota_project_id=quota_project_id, 

223 default_scopes=cls.AUTH_SCOPES, 

224 scopes=scopes, 

225 default_host=cls.DEFAULT_HOST, 

226 **kwargs, 

227 ) 

228 

229 @property 

230 def grpc_channel(self) -> grpc.Channel: 

231 """Return the channel designed to connect to this service.""" 

232 return self._grpc_channel 

233 

234 @property 

235 def list_log_metrics( 

236 self, 

237 ) -> Callable[ 

238 [logging_metrics.ListLogMetricsRequest], logging_metrics.ListLogMetricsResponse 

239 ]: 

240 r"""Return a callable for the list log metrics method over gRPC. 

241 

242 Lists logs-based metrics. 

243 

244 Returns: 

245 Callable[[~.ListLogMetricsRequest], 

246 ~.ListLogMetricsResponse]: 

247 A function that, when called, will call the underlying RPC 

248 on the server. 

249 """ 

250 # Generate a "stub function" on-the-fly which will actually make 

251 # the request. 

252 # gRPC handles serialization and deserialization, so we just need 

253 # to pass in the functions for each. 

254 if "list_log_metrics" not in self._stubs: 

255 self._stubs["list_log_metrics"] = self.grpc_channel.unary_unary( 

256 "/google.logging.v2.MetricsServiceV2/ListLogMetrics", 

257 request_serializer=logging_metrics.ListLogMetricsRequest.serialize, 

258 response_deserializer=logging_metrics.ListLogMetricsResponse.deserialize, 

259 ) 

260 return self._stubs["list_log_metrics"] 

261 

262 @property 

263 def get_log_metric( 

264 self, 

265 ) -> Callable[[logging_metrics.GetLogMetricRequest], logging_metrics.LogMetric]: 

266 r"""Return a callable for the get log metric method over gRPC. 

267 

268 Gets a logs-based metric. 

269 

270 Returns: 

271 Callable[[~.GetLogMetricRequest], 

272 ~.LogMetric]: 

273 A function that, when called, will call the underlying RPC 

274 on the server. 

275 """ 

276 # Generate a "stub function" on-the-fly which will actually make 

277 # the request. 

278 # gRPC handles serialization and deserialization, so we just need 

279 # to pass in the functions for each. 

280 if "get_log_metric" not in self._stubs: 

281 self._stubs["get_log_metric"] = self.grpc_channel.unary_unary( 

282 "/google.logging.v2.MetricsServiceV2/GetLogMetric", 

283 request_serializer=logging_metrics.GetLogMetricRequest.serialize, 

284 response_deserializer=logging_metrics.LogMetric.deserialize, 

285 ) 

286 return self._stubs["get_log_metric"] 

287 

288 @property 

289 def create_log_metric( 

290 self, 

291 ) -> Callable[[logging_metrics.CreateLogMetricRequest], logging_metrics.LogMetric]: 

292 r"""Return a callable for the create log metric method over gRPC. 

293 

294 Creates a logs-based metric. 

295 

296 Returns: 

297 Callable[[~.CreateLogMetricRequest], 

298 ~.LogMetric]: 

299 A function that, when called, will call the underlying RPC 

300 on the server. 

301 """ 

302 # Generate a "stub function" on-the-fly which will actually make 

303 # the request. 

304 # gRPC handles serialization and deserialization, so we just need 

305 # to pass in the functions for each. 

306 if "create_log_metric" not in self._stubs: 

307 self._stubs["create_log_metric"] = self.grpc_channel.unary_unary( 

308 "/google.logging.v2.MetricsServiceV2/CreateLogMetric", 

309 request_serializer=logging_metrics.CreateLogMetricRequest.serialize, 

310 response_deserializer=logging_metrics.LogMetric.deserialize, 

311 ) 

312 return self._stubs["create_log_metric"] 

313 

314 @property 

315 def update_log_metric( 

316 self, 

317 ) -> Callable[[logging_metrics.UpdateLogMetricRequest], logging_metrics.LogMetric]: 

318 r"""Return a callable for the update log metric method over gRPC. 

319 

320 Creates or updates a logs-based metric. 

321 

322 Returns: 

323 Callable[[~.UpdateLogMetricRequest], 

324 ~.LogMetric]: 

325 A function that, when called, will call the underlying RPC 

326 on the server. 

327 """ 

328 # Generate a "stub function" on-the-fly which will actually make 

329 # the request. 

330 # gRPC handles serialization and deserialization, so we just need 

331 # to pass in the functions for each. 

332 if "update_log_metric" not in self._stubs: 

333 self._stubs["update_log_metric"] = self.grpc_channel.unary_unary( 

334 "/google.logging.v2.MetricsServiceV2/UpdateLogMetric", 

335 request_serializer=logging_metrics.UpdateLogMetricRequest.serialize, 

336 response_deserializer=logging_metrics.LogMetric.deserialize, 

337 ) 

338 return self._stubs["update_log_metric"] 

339 

340 @property 

341 def delete_log_metric( 

342 self, 

343 ) -> Callable[[logging_metrics.DeleteLogMetricRequest], empty_pb2.Empty]: 

344 r"""Return a callable for the delete log metric method over gRPC. 

345 

346 Deletes a logs-based metric. 

347 

348 Returns: 

349 Callable[[~.DeleteLogMetricRequest], 

350 ~.Empty]: 

351 A function that, when called, will call the underlying RPC 

352 on the server. 

353 """ 

354 # Generate a "stub function" on-the-fly which will actually make 

355 # the request. 

356 # gRPC handles serialization and deserialization, so we just need 

357 # to pass in the functions for each. 

358 if "delete_log_metric" not in self._stubs: 

359 self._stubs["delete_log_metric"] = self.grpc_channel.unary_unary( 

360 "/google.logging.v2.MetricsServiceV2/DeleteLogMetric", 

361 request_serializer=logging_metrics.DeleteLogMetricRequest.serialize, 

362 response_deserializer=empty_pb2.Empty.FromString, 

363 ) 

364 return self._stubs["delete_log_metric"] 

365 

366 def close(self): 

367 self.grpc_channel.close() 

368 

369 @property 

370 def cancel_operation( 

371 self, 

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

373 r"""Return a callable for the cancel_operation method over gRPC.""" 

374 # Generate a "stub function" on-the-fly which will actually make 

375 # the request. 

376 # gRPC handles serialization and deserialization, so we just need 

377 # to pass in the functions for each. 

378 if "cancel_operation" not in self._stubs: 

379 self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( 

380 "/google.longrunning.Operations/CancelOperation", 

381 request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, 

382 response_deserializer=None, 

383 ) 

384 return self._stubs["cancel_operation"] 

385 

386 @property 

387 def get_operation( 

388 self, 

389 ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: 

390 r"""Return a callable for the get_operation method over gRPC.""" 

391 # Generate a "stub function" on-the-fly which will actually make 

392 # the request. 

393 # gRPC handles serialization and deserialization, so we just need 

394 # to pass in the functions for each. 

395 if "get_operation" not in self._stubs: 

396 self._stubs["get_operation"] = self.grpc_channel.unary_unary( 

397 "/google.longrunning.Operations/GetOperation", 

398 request_serializer=operations_pb2.GetOperationRequest.SerializeToString, 

399 response_deserializer=operations_pb2.Operation.FromString, 

400 ) 

401 return self._stubs["get_operation"] 

402 

403 @property 

404 def list_operations( 

405 self, 

406 ) -> Callable[ 

407 [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse 

408 ]: 

409 r"""Return a callable for the list_operations method over gRPC.""" 

410 # Generate a "stub function" on-the-fly which will actually make 

411 # the request. 

412 # gRPC handles serialization and deserialization, so we just need 

413 # to pass in the functions for each. 

414 if "list_operations" not in self._stubs: 

415 self._stubs["list_operations"] = self.grpc_channel.unary_unary( 

416 "/google.longrunning.Operations/ListOperations", 

417 request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, 

418 response_deserializer=operations_pb2.ListOperationsResponse.FromString, 

419 ) 

420 return self._stubs["list_operations"] 

421 

422 @property 

423 def kind(self) -> str: 

424 return "grpc" 

425 

426 

427__all__ = ("MetricsServiceV2GrpcTransport",)