Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/resourcemanager_v3/services/tag_keys/transports/grpc_asyncio.py: 42%

106 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-06 06:03 +0000

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# 

16from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union 

17import warnings 

18 

19from google.api_core import gapic_v1, grpc_helpers_async, operations_v1 

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

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

22from google.iam.v1 import iam_policy_pb2 # type: ignore 

23from google.iam.v1 import policy_pb2 # type: ignore 

24from google.longrunning import operations_pb2 # type: ignore 

25import grpc # type: ignore 

26from grpc.experimental import aio # type: ignore 

27 

28from google.cloud.resourcemanager_v3.types import tag_keys 

29 

30from .base import DEFAULT_CLIENT_INFO, TagKeysTransport 

31from .grpc import TagKeysGrpcTransport 

32 

33 

34class TagKeysGrpcAsyncIOTransport(TagKeysTransport): 

35 """gRPC AsyncIO backend transport for TagKeys. 

36 

37 Allow users to create and manage tag keys. 

38 

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

40 primary client can load the underlying transport implementation 

41 and call it. 

42 

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

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

45 """ 

46 

47 _grpc_channel: aio.Channel 

48 _stubs: Dict[str, Callable] = {} 

49 

50 @classmethod 

51 def create_channel( 

52 cls, 

53 host: str = "cloudresourcemanager.googleapis.com", 

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 **kwargs, 

59 ) -> aio.Channel: 

60 """Create and return a gRPC AsyncIO channel object. 

61 Args: 

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

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

64 authorization credentials to attach to requests. These 

65 credentials identify this application to the service. If 

66 none are specified, the client will attempt to ascertain 

67 the credentials from the environment. 

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

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

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

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

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

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

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

75 and quota. 

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

77 channel creation. 

78 Returns: 

79 aio.Channel: A gRPC AsyncIO channel object. 

80 """ 

81 

82 return grpc_helpers_async.create_channel( 

83 host, 

84 credentials=credentials, 

85 credentials_file=credentials_file, 

86 quota_project_id=quota_project_id, 

87 default_scopes=cls.AUTH_SCOPES, 

88 scopes=scopes, 

89 default_host=cls.DEFAULT_HOST, 

90 **kwargs, 

91 ) 

92 

93 def __init__( 

94 self, 

95 *, 

96 host: str = "cloudresourcemanager.googleapis.com", 

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

98 credentials_file: Optional[str] = None, 

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

100 channel: Optional[aio.Channel] = None, 

101 api_mtls_endpoint: Optional[str] = None, 

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

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

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

105 quota_project_id: Optional[str] = None, 

106 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

107 always_use_jwt_access: Optional[bool] = False, 

108 api_audience: Optional[str] = None, 

109 ) -> None: 

110 """Instantiate the transport. 

111 

112 Args: 

113 host (Optional[str]): 

114 The hostname to connect to. 

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

116 authorization credentials to attach to requests. These 

117 credentials identify the application to the service; if none 

118 are specified, the client will attempt to ascertain the 

119 credentials from the environment. 

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

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

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

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

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

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

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

127 channel (Optional[aio.Channel]): A ``Channel`` instance through 

128 which to make calls. 

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

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

131 a mutual TLS channel with client SSL credentials from 

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

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

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

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

136 ``api_mtls_endpoint`` is None. 

137 ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials 

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

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

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

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

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

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

144 and quota. 

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

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

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

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

149 your own client library. 

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

151 be used for service account credentials. 

152 

153 Raises: 

154 google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport 

155 creation failed for any reason. 

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

157 and ``credentials_file`` are passed. 

158 """ 

159 self._grpc_channel = None 

160 self._ssl_channel_credentials = ssl_channel_credentials 

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

162 self._operations_client: Optional[operations_v1.OperationsAsyncClient] = None 

163 

164 if api_mtls_endpoint: 

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

166 if client_cert_source: 

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

168 

169 if channel: 

170 # Ignore credentials if a channel was passed. 

171 credentials = False 

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

173 self._grpc_channel = channel 

174 self._ssl_channel_credentials = None 

175 else: 

176 if api_mtls_endpoint: 

177 host = api_mtls_endpoint 

178 

179 # Create SSL credentials with client_cert_source or application 

180 # default SSL credentials. 

181 if client_cert_source: 

182 cert, key = client_cert_source() 

183 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

184 certificate_chain=cert, private_key=key 

185 ) 

186 else: 

187 self._ssl_channel_credentials = SslCredentials().ssl_credentials 

188 

189 else: 

190 if client_cert_source_for_mtls and not ssl_channel_credentials: 

191 cert, key = client_cert_source_for_mtls() 

192 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

193 certificate_chain=cert, private_key=key 

194 ) 

195 

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

197 super().__init__( 

198 host=host, 

199 credentials=credentials, 

200 credentials_file=credentials_file, 

201 scopes=scopes, 

202 quota_project_id=quota_project_id, 

203 client_info=client_info, 

204 always_use_jwt_access=always_use_jwt_access, 

205 api_audience=api_audience, 

206 ) 

207 

208 if not self._grpc_channel: 

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

210 self._host, 

211 # use the credentials which are saved 

212 credentials=self._credentials, 

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

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

215 credentials_file=None, 

216 scopes=self._scopes, 

217 ssl_credentials=self._ssl_channel_credentials, 

218 quota_project_id=quota_project_id, 

219 options=[ 

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

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

222 ], 

223 ) 

224 

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

226 self._prep_wrapped_messages(client_info) 

227 

228 @property 

229 def grpc_channel(self) -> aio.Channel: 

230 """Create the channel designed to connect to this service. 

231 

232 This property caches on the instance; repeated calls return 

233 the same channel. 

234 """ 

235 # Return the channel from cache. 

236 return self._grpc_channel 

237 

238 @property 

239 def operations_client(self) -> operations_v1.OperationsAsyncClient: 

240 """Create the client designed to process long-running operations. 

241 

242 This property caches on the instance; repeated calls return the same 

243 client. 

244 """ 

245 # Quick check: Only create a new client if we do not already have one. 

246 if self._operations_client is None: 

247 self._operations_client = operations_v1.OperationsAsyncClient( 

248 self.grpc_channel 

249 ) 

250 

251 # Return the client from cache. 

252 return self._operations_client 

253 

254 @property 

255 def list_tag_keys( 

256 self, 

257 ) -> Callable[ 

258 [tag_keys.ListTagKeysRequest], Awaitable[tag_keys.ListTagKeysResponse] 

259 ]: 

260 r"""Return a callable for the list tag keys method over gRPC. 

261 

262 Lists all TagKeys for a parent resource. 

263 

264 Returns: 

265 Callable[[~.ListTagKeysRequest], 

266 Awaitable[~.ListTagKeysResponse]]: 

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

268 on the server. 

269 """ 

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

271 # the request. 

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

273 # to pass in the functions for each. 

274 if "list_tag_keys" not in self._stubs: 

275 self._stubs["list_tag_keys"] = self.grpc_channel.unary_unary( 

276 "/google.cloud.resourcemanager.v3.TagKeys/ListTagKeys", 

277 request_serializer=tag_keys.ListTagKeysRequest.serialize, 

278 response_deserializer=tag_keys.ListTagKeysResponse.deserialize, 

279 ) 

280 return self._stubs["list_tag_keys"] 

281 

282 @property 

283 def get_tag_key( 

284 self, 

285 ) -> Callable[[tag_keys.GetTagKeyRequest], Awaitable[tag_keys.TagKey]]: 

286 r"""Return a callable for the get tag key method over gRPC. 

287 

288 Retrieves a TagKey. This method will return 

289 ``PERMISSION_DENIED`` if the key does not exist or the user does 

290 not have permission to view it. 

291 

292 Returns: 

293 Callable[[~.GetTagKeyRequest], 

294 Awaitable[~.TagKey]]: 

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

296 on the server. 

297 """ 

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

299 # the request. 

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

301 # to pass in the functions for each. 

302 if "get_tag_key" not in self._stubs: 

303 self._stubs["get_tag_key"] = self.grpc_channel.unary_unary( 

304 "/google.cloud.resourcemanager.v3.TagKeys/GetTagKey", 

305 request_serializer=tag_keys.GetTagKeyRequest.serialize, 

306 response_deserializer=tag_keys.TagKey.deserialize, 

307 ) 

308 return self._stubs["get_tag_key"] 

309 

310 @property 

311 def get_namespaced_tag_key( 

312 self, 

313 ) -> Callable[[tag_keys.GetNamespacedTagKeyRequest], Awaitable[tag_keys.TagKey]]: 

314 r"""Return a callable for the get namespaced tag key method over gRPC. 

315 

316 Retrieves a TagKey by its namespaced name. This method will 

317 return ``PERMISSION_DENIED`` if the key does not exist or the 

318 user does not have permission to view it. 

319 

320 Returns: 

321 Callable[[~.GetNamespacedTagKeyRequest], 

322 Awaitable[~.TagKey]]: 

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

324 on the server. 

325 """ 

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

327 # the request. 

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

329 # to pass in the functions for each. 

330 if "get_namespaced_tag_key" not in self._stubs: 

331 self._stubs["get_namespaced_tag_key"] = self.grpc_channel.unary_unary( 

332 "/google.cloud.resourcemanager.v3.TagKeys/GetNamespacedTagKey", 

333 request_serializer=tag_keys.GetNamespacedTagKeyRequest.serialize, 

334 response_deserializer=tag_keys.TagKey.deserialize, 

335 ) 

336 return self._stubs["get_namespaced_tag_key"] 

337 

338 @property 

339 def create_tag_key( 

340 self, 

341 ) -> Callable[[tag_keys.CreateTagKeyRequest], Awaitable[operations_pb2.Operation]]: 

342 r"""Return a callable for the create tag key method over gRPC. 

343 

344 Creates a new TagKey. If another request with the 

345 same parameters is sent while the original request is in 

346 process, the second request will receive an error. A 

347 maximum of 1000 TagKeys can exist under a parent at any 

348 given time. 

349 

350 Returns: 

351 Callable[[~.CreateTagKeyRequest], 

352 Awaitable[~.Operation]]: 

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

354 on the server. 

355 """ 

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

357 # the request. 

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

359 # to pass in the functions for each. 

360 if "create_tag_key" not in self._stubs: 

361 self._stubs["create_tag_key"] = self.grpc_channel.unary_unary( 

362 "/google.cloud.resourcemanager.v3.TagKeys/CreateTagKey", 

363 request_serializer=tag_keys.CreateTagKeyRequest.serialize, 

364 response_deserializer=operations_pb2.Operation.FromString, 

365 ) 

366 return self._stubs["create_tag_key"] 

367 

368 @property 

369 def update_tag_key( 

370 self, 

371 ) -> Callable[[tag_keys.UpdateTagKeyRequest], Awaitable[operations_pb2.Operation]]: 

372 r"""Return a callable for the update tag key method over gRPC. 

373 

374 Updates the attributes of the TagKey resource. 

375 

376 Returns: 

377 Callable[[~.UpdateTagKeyRequest], 

378 Awaitable[~.Operation]]: 

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

380 on the server. 

381 """ 

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

383 # the request. 

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

385 # to pass in the functions for each. 

386 if "update_tag_key" not in self._stubs: 

387 self._stubs["update_tag_key"] = self.grpc_channel.unary_unary( 

388 "/google.cloud.resourcemanager.v3.TagKeys/UpdateTagKey", 

389 request_serializer=tag_keys.UpdateTagKeyRequest.serialize, 

390 response_deserializer=operations_pb2.Operation.FromString, 

391 ) 

392 return self._stubs["update_tag_key"] 

393 

394 @property 

395 def delete_tag_key( 

396 self, 

397 ) -> Callable[[tag_keys.DeleteTagKeyRequest], Awaitable[operations_pb2.Operation]]: 

398 r"""Return a callable for the delete tag key method over gRPC. 

399 

400 Deletes a TagKey. The TagKey cannot be deleted if it 

401 has any child TagValues. 

402 

403 Returns: 

404 Callable[[~.DeleteTagKeyRequest], 

405 Awaitable[~.Operation]]: 

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

407 on the server. 

408 """ 

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

410 # the request. 

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

412 # to pass in the functions for each. 

413 if "delete_tag_key" not in self._stubs: 

414 self._stubs["delete_tag_key"] = self.grpc_channel.unary_unary( 

415 "/google.cloud.resourcemanager.v3.TagKeys/DeleteTagKey", 

416 request_serializer=tag_keys.DeleteTagKeyRequest.serialize, 

417 response_deserializer=operations_pb2.Operation.FromString, 

418 ) 

419 return self._stubs["delete_tag_key"] 

420 

421 @property 

422 def get_iam_policy( 

423 self, 

424 ) -> Callable[[iam_policy_pb2.GetIamPolicyRequest], Awaitable[policy_pb2.Policy]]: 

425 r"""Return a callable for the get iam policy method over gRPC. 

426 

427 Gets the access control policy for a TagKey. The returned policy 

428 may be empty if no such policy or resource exists. The 

429 ``resource`` field should be the TagKey's resource name. For 

430 example, "tagKeys/1234". The caller must have 

431 ``cloudresourcemanager.googleapis.com/tagKeys.getIamPolicy`` 

432 permission on the specified TagKey. 

433 

434 Returns: 

435 Callable[[~.GetIamPolicyRequest], 

436 Awaitable[~.Policy]]: 

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

438 on the server. 

439 """ 

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

441 # the request. 

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

443 # to pass in the functions for each. 

444 if "get_iam_policy" not in self._stubs: 

445 self._stubs["get_iam_policy"] = self.grpc_channel.unary_unary( 

446 "/google.cloud.resourcemanager.v3.TagKeys/GetIamPolicy", 

447 request_serializer=iam_policy_pb2.GetIamPolicyRequest.SerializeToString, 

448 response_deserializer=policy_pb2.Policy.FromString, 

449 ) 

450 return self._stubs["get_iam_policy"] 

451 

452 @property 

453 def set_iam_policy( 

454 self, 

455 ) -> Callable[[iam_policy_pb2.SetIamPolicyRequest], Awaitable[policy_pb2.Policy]]: 

456 r"""Return a callable for the set iam policy method over gRPC. 

457 

458 Sets the access control policy on a TagKey, replacing any 

459 existing policy. The ``resource`` field should be the TagKey's 

460 resource name. For example, "tagKeys/1234". The caller must have 

461 ``resourcemanager.tagKeys.setIamPolicy`` permission on the 

462 identified tagValue. 

463 

464 Returns: 

465 Callable[[~.SetIamPolicyRequest], 

466 Awaitable[~.Policy]]: 

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

468 on the server. 

469 """ 

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

471 # the request. 

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

473 # to pass in the functions for each. 

474 if "set_iam_policy" not in self._stubs: 

475 self._stubs["set_iam_policy"] = self.grpc_channel.unary_unary( 

476 "/google.cloud.resourcemanager.v3.TagKeys/SetIamPolicy", 

477 request_serializer=iam_policy_pb2.SetIamPolicyRequest.SerializeToString, 

478 response_deserializer=policy_pb2.Policy.FromString, 

479 ) 

480 return self._stubs["set_iam_policy"] 

481 

482 @property 

483 def test_iam_permissions( 

484 self, 

485 ) -> Callable[ 

486 [iam_policy_pb2.TestIamPermissionsRequest], 

487 Awaitable[iam_policy_pb2.TestIamPermissionsResponse], 

488 ]: 

489 r"""Return a callable for the test iam permissions method over gRPC. 

490 

491 Returns permissions that a caller has on the specified TagKey. 

492 The ``resource`` field should be the TagKey's resource name. For 

493 example, "tagKeys/1234". 

494 

495 There are no permissions required for making this API call. 

496 

497 Returns: 

498 Callable[[~.TestIamPermissionsRequest], 

499 Awaitable[~.TestIamPermissionsResponse]]: 

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

501 on the server. 

502 """ 

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

504 # the request. 

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

506 # to pass in the functions for each. 

507 if "test_iam_permissions" not in self._stubs: 

508 self._stubs["test_iam_permissions"] = self.grpc_channel.unary_unary( 

509 "/google.cloud.resourcemanager.v3.TagKeys/TestIamPermissions", 

510 request_serializer=iam_policy_pb2.TestIamPermissionsRequest.SerializeToString, 

511 response_deserializer=iam_policy_pb2.TestIamPermissionsResponse.FromString, 

512 ) 

513 return self._stubs["test_iam_permissions"] 

514 

515 def close(self): 

516 return self.grpc_channel.close() 

517 

518 @property 

519 def get_operation( 

520 self, 

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

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

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

524 # the request. 

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

526 # to pass in the functions for each. 

527 if "get_operation" not in self._stubs: 

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

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

530 request_serializer=operations_pb2.GetOperationRequest.SerializeToString, 

531 response_deserializer=operations_pb2.Operation.FromString, 

532 ) 

533 return self._stubs["get_operation"] 

534 

535 

536__all__ = ("TagKeysGrpcAsyncIOTransport",)