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_values
29
30from .base import DEFAULT_CLIENT_INFO, TagValuesTransport
31from .grpc import TagValuesGrpcTransport
32
33
34class TagValuesGrpcAsyncIOTransport(TagValuesTransport):
35 """gRPC AsyncIO backend transport for TagValues.
36
37 Allow users to create and manage tag values.
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_values(
256 self,
257 ) -> Callable[
258 [tag_values.ListTagValuesRequest], Awaitable[tag_values.ListTagValuesResponse]
259 ]:
260 r"""Return a callable for the list tag values method over gRPC.
261
262 Lists all TagValues for a specific TagKey.
263
264 Returns:
265 Callable[[~.ListTagValuesRequest],
266 Awaitable[~.ListTagValuesResponse]]:
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_values" not in self._stubs:
275 self._stubs["list_tag_values"] = self.grpc_channel.unary_unary(
276 "/google.cloud.resourcemanager.v3.TagValues/ListTagValues",
277 request_serializer=tag_values.ListTagValuesRequest.serialize,
278 response_deserializer=tag_values.ListTagValuesResponse.deserialize,
279 )
280 return self._stubs["list_tag_values"]
281
282 @property
283 def get_tag_value(
284 self,
285 ) -> Callable[[tag_values.GetTagValueRequest], Awaitable[tag_values.TagValue]]:
286 r"""Return a callable for the get tag value method over gRPC.
287
288 Retrieves a TagValue. This method will return
289 ``PERMISSION_DENIED`` if the value does not exist or the user
290 does not have permission to view it.
291
292 Returns:
293 Callable[[~.GetTagValueRequest],
294 Awaitable[~.TagValue]]:
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_value" not in self._stubs:
303 self._stubs["get_tag_value"] = self.grpc_channel.unary_unary(
304 "/google.cloud.resourcemanager.v3.TagValues/GetTagValue",
305 request_serializer=tag_values.GetTagValueRequest.serialize,
306 response_deserializer=tag_values.TagValue.deserialize,
307 )
308 return self._stubs["get_tag_value"]
309
310 @property
311 def get_namespaced_tag_value(
312 self,
313 ) -> Callable[
314 [tag_values.GetNamespacedTagValueRequest], Awaitable[tag_values.TagValue]
315 ]:
316 r"""Return a callable for the get namespaced tag value method over gRPC.
317
318 Retrieves a TagValue by its namespaced name. This method will
319 return ``PERMISSION_DENIED`` if the value does not exist or the
320 user does not have permission to view it.
321
322 Returns:
323 Callable[[~.GetNamespacedTagValueRequest],
324 Awaitable[~.TagValue]]:
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 "get_namespaced_tag_value" not in self._stubs:
333 self._stubs["get_namespaced_tag_value"] = self.grpc_channel.unary_unary(
334 "/google.cloud.resourcemanager.v3.TagValues/GetNamespacedTagValue",
335 request_serializer=tag_values.GetNamespacedTagValueRequest.serialize,
336 response_deserializer=tag_values.TagValue.deserialize,
337 )
338 return self._stubs["get_namespaced_tag_value"]
339
340 @property
341 def create_tag_value(
342 self,
343 ) -> Callable[
344 [tag_values.CreateTagValueRequest], Awaitable[operations_pb2.Operation]
345 ]:
346 r"""Return a callable for the create tag value method over gRPC.
347
348 Creates a TagValue as a child of the specified
349 TagKey. If a another request with the same parameters is
350 sent while the original request is in process the second
351 request will receive an error. A maximum of 1000
352 TagValues can exist under a TagKey at any given time.
353
354 Returns:
355 Callable[[~.CreateTagValueRequest],
356 Awaitable[~.Operation]]:
357 A function that, when called, will call the underlying RPC
358 on the server.
359 """
360 # Generate a "stub function" on-the-fly which will actually make
361 # the request.
362 # gRPC handles serialization and deserialization, so we just need
363 # to pass in the functions for each.
364 if "create_tag_value" not in self._stubs:
365 self._stubs["create_tag_value"] = self.grpc_channel.unary_unary(
366 "/google.cloud.resourcemanager.v3.TagValues/CreateTagValue",
367 request_serializer=tag_values.CreateTagValueRequest.serialize,
368 response_deserializer=operations_pb2.Operation.FromString,
369 )
370 return self._stubs["create_tag_value"]
371
372 @property
373 def update_tag_value(
374 self,
375 ) -> Callable[
376 [tag_values.UpdateTagValueRequest], Awaitable[operations_pb2.Operation]
377 ]:
378 r"""Return a callable for the update tag value method over gRPC.
379
380 Updates the attributes of the TagValue resource.
381
382 Returns:
383 Callable[[~.UpdateTagValueRequest],
384 Awaitable[~.Operation]]:
385 A function that, when called, will call the underlying RPC
386 on the server.
387 """
388 # Generate a "stub function" on-the-fly which will actually make
389 # the request.
390 # gRPC handles serialization and deserialization, so we just need
391 # to pass in the functions for each.
392 if "update_tag_value" not in self._stubs:
393 self._stubs["update_tag_value"] = self.grpc_channel.unary_unary(
394 "/google.cloud.resourcemanager.v3.TagValues/UpdateTagValue",
395 request_serializer=tag_values.UpdateTagValueRequest.serialize,
396 response_deserializer=operations_pb2.Operation.FromString,
397 )
398 return self._stubs["update_tag_value"]
399
400 @property
401 def delete_tag_value(
402 self,
403 ) -> Callable[
404 [tag_values.DeleteTagValueRequest], Awaitable[operations_pb2.Operation]
405 ]:
406 r"""Return a callable for the delete tag value method over gRPC.
407
408 Deletes a TagValue. The TagValue cannot have any
409 bindings when it is deleted.
410
411 Returns:
412 Callable[[~.DeleteTagValueRequest],
413 Awaitable[~.Operation]]:
414 A function that, when called, will call the underlying RPC
415 on the server.
416 """
417 # Generate a "stub function" on-the-fly which will actually make
418 # the request.
419 # gRPC handles serialization and deserialization, so we just need
420 # to pass in the functions for each.
421 if "delete_tag_value" not in self._stubs:
422 self._stubs["delete_tag_value"] = self.grpc_channel.unary_unary(
423 "/google.cloud.resourcemanager.v3.TagValues/DeleteTagValue",
424 request_serializer=tag_values.DeleteTagValueRequest.serialize,
425 response_deserializer=operations_pb2.Operation.FromString,
426 )
427 return self._stubs["delete_tag_value"]
428
429 @property
430 def get_iam_policy(
431 self,
432 ) -> Callable[[iam_policy_pb2.GetIamPolicyRequest], Awaitable[policy_pb2.Policy]]:
433 r"""Return a callable for the get iam policy method over gRPC.
434
435 Gets the access control policy for a TagValue. The returned
436 policy may be empty if no such policy or resource exists. The
437 ``resource`` field should be the TagValue's resource name. For
438 example: ``tagValues/1234``. The caller must have the
439 ``cloudresourcemanager.googleapis.com/tagValues.getIamPolicy``
440 permission on the identified TagValue to get the access control
441 policy.
442
443 Returns:
444 Callable[[~.GetIamPolicyRequest],
445 Awaitable[~.Policy]]:
446 A function that, when called, will call the underlying RPC
447 on the server.
448 """
449 # Generate a "stub function" on-the-fly which will actually make
450 # the request.
451 # gRPC handles serialization and deserialization, so we just need
452 # to pass in the functions for each.
453 if "get_iam_policy" not in self._stubs:
454 self._stubs["get_iam_policy"] = self.grpc_channel.unary_unary(
455 "/google.cloud.resourcemanager.v3.TagValues/GetIamPolicy",
456 request_serializer=iam_policy_pb2.GetIamPolicyRequest.SerializeToString,
457 response_deserializer=policy_pb2.Policy.FromString,
458 )
459 return self._stubs["get_iam_policy"]
460
461 @property
462 def set_iam_policy(
463 self,
464 ) -> Callable[[iam_policy_pb2.SetIamPolicyRequest], Awaitable[policy_pb2.Policy]]:
465 r"""Return a callable for the set iam policy method over gRPC.
466
467 Sets the access control policy on a TagValue, replacing any
468 existing policy. The ``resource`` field should be the TagValue's
469 resource name. For example: ``tagValues/1234``. The caller must
470 have ``resourcemanager.tagValues.setIamPolicy`` permission on
471 the identified tagValue.
472
473 Returns:
474 Callable[[~.SetIamPolicyRequest],
475 Awaitable[~.Policy]]:
476 A function that, when called, will call the underlying RPC
477 on the server.
478 """
479 # Generate a "stub function" on-the-fly which will actually make
480 # the request.
481 # gRPC handles serialization and deserialization, so we just need
482 # to pass in the functions for each.
483 if "set_iam_policy" not in self._stubs:
484 self._stubs["set_iam_policy"] = self.grpc_channel.unary_unary(
485 "/google.cloud.resourcemanager.v3.TagValues/SetIamPolicy",
486 request_serializer=iam_policy_pb2.SetIamPolicyRequest.SerializeToString,
487 response_deserializer=policy_pb2.Policy.FromString,
488 )
489 return self._stubs["set_iam_policy"]
490
491 @property
492 def test_iam_permissions(
493 self,
494 ) -> Callable[
495 [iam_policy_pb2.TestIamPermissionsRequest],
496 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
497 ]:
498 r"""Return a callable for the test iam permissions method over gRPC.
499
500 Returns permissions that a caller has on the specified TagValue.
501 The ``resource`` field should be the TagValue's resource name.
502 For example: ``tagValues/1234``.
503
504 There are no permissions required for making this API call.
505
506 Returns:
507 Callable[[~.TestIamPermissionsRequest],
508 Awaitable[~.TestIamPermissionsResponse]]:
509 A function that, when called, will call the underlying RPC
510 on the server.
511 """
512 # Generate a "stub function" on-the-fly which will actually make
513 # the request.
514 # gRPC handles serialization and deserialization, so we just need
515 # to pass in the functions for each.
516 if "test_iam_permissions" not in self._stubs:
517 self._stubs["test_iam_permissions"] = self.grpc_channel.unary_unary(
518 "/google.cloud.resourcemanager.v3.TagValues/TestIamPermissions",
519 request_serializer=iam_policy_pb2.TestIamPermissionsRequest.SerializeToString,
520 response_deserializer=iam_policy_pb2.TestIamPermissionsResponse.FromString,
521 )
522 return self._stubs["test_iam_permissions"]
523
524 def close(self):
525 return self.grpc_channel.close()
526
527 @property
528 def get_operation(
529 self,
530 ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]:
531 r"""Return a callable for the get_operation method over gRPC."""
532 # Generate a "stub function" on-the-fly which will actually make
533 # the request.
534 # gRPC handles serialization and deserialization, so we just need
535 # to pass in the functions for each.
536 if "get_operation" not in self._stubs:
537 self._stubs["get_operation"] = self.grpc_channel.unary_unary(
538 "/google.longrunning.Operations/GetOperation",
539 request_serializer=operations_pb2.GetOperationRequest.SerializeToString,
540 response_deserializer=operations_pb2.Operation.FromString,
541 )
542 return self._stubs["get_operation"]
543
544
545__all__ = ("TagValuesGrpcAsyncIOTransport",)