diff --git a/docs/bigquery_storage_v1alpha/metastore_partition_service.rst b/docs/bigquery_storage_v1alpha/metastore_partition_service.rst new file mode 100644 index 00000000..94af0211 --- /dev/null +++ b/docs/bigquery_storage_v1alpha/metastore_partition_service.rst @@ -0,0 +1,6 @@ +MetastorePartitionService +------------------------------------------- + +.. automodule:: google.cloud.bigquery_storage_v1alpha.services.metastore_partition_service + :members: + :inherited-members: diff --git a/docs/bigquery_storage_v1alpha/services_.rst b/docs/bigquery_storage_v1alpha/services_.rst new file mode 100644 index 00000000..df517182 --- /dev/null +++ b/docs/bigquery_storage_v1alpha/services_.rst @@ -0,0 +1,6 @@ +Services for Google Cloud Bigquery Storage v1alpha API +====================================================== +.. toctree:: + :maxdepth: 2 + + metastore_partition_service diff --git a/docs/bigquery_storage_v1alpha/types_.rst b/docs/bigquery_storage_v1alpha/types_.rst new file mode 100644 index 00000000..4a91fc13 --- /dev/null +++ b/docs/bigquery_storage_v1alpha/types_.rst @@ -0,0 +1,6 @@ +Types for Google Cloud Bigquery Storage v1alpha API +=================================================== + +.. automodule:: google.cloud.bigquery_storage_v1alpha.types + :members: + :show-inheritance: diff --git a/docs/index.rst b/docs/index.rst index 843f1b34..c0d66165 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -24,6 +24,8 @@ API Reference bigquery_storage_v1beta2/library bigquery_storage_v1beta2/services_ bigquery_storage_v1beta2/types_ + bigquery_storage_v1alpha/services_ + bigquery_storage_v1alpha/types_ Migration Guide diff --git a/google/cloud/bigquery_storage_v1alpha/gapic_metadata.json b/google/cloud/bigquery_storage_v1alpha/gapic_metadata.json new file mode 100644 index 00000000..39f3429f --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/gapic_metadata.json @@ -0,0 +1,73 @@ + { + "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", + "language": "python", + "libraryPackage": "google.cloud.bigquery_storage_v1alpha", + "protoPackage": "google.cloud.bigquery.storage.v1alpha", + "schema": "1.0", + "services": { + "MetastorePartitionService": { + "clients": { + "grpc": { + "libraryClient": "MetastorePartitionServiceClient", + "rpcs": { + "BatchCreateMetastorePartitions": { + "methods": [ + "batch_create_metastore_partitions" + ] + }, + "BatchDeleteMetastorePartitions": { + "methods": [ + "batch_delete_metastore_partitions" + ] + }, + "BatchUpdateMetastorePartitions": { + "methods": [ + "batch_update_metastore_partitions" + ] + }, + "ListMetastorePartitions": { + "methods": [ + "list_metastore_partitions" + ] + }, + "StreamMetastorePartitions": { + "methods": [ + "stream_metastore_partitions" + ] + } + } + }, + "grpc-async": { + "libraryClient": "MetastorePartitionServiceAsyncClient", + "rpcs": { + "BatchCreateMetastorePartitions": { + "methods": [ + "batch_create_metastore_partitions" + ] + }, + "BatchDeleteMetastorePartitions": { + "methods": [ + "batch_delete_metastore_partitions" + ] + }, + "BatchUpdateMetastorePartitions": { + "methods": [ + "batch_update_metastore_partitions" + ] + }, + "ListMetastorePartitions": { + "methods": [ + "list_metastore_partitions" + ] + }, + "StreamMetastorePartitions": { + "methods": [ + "stream_metastore_partitions" + ] + } + } + } + } + } + } +} diff --git a/google/cloud/bigquery_storage_v1alpha/gapic_version.py b/google/cloud/bigquery_storage_v1alpha/gapic_version.py new file mode 100644 index 00000000..e5fa8f60 --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/gapic_version.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +__version__ = "2.25.0" # {x-release-please-version} diff --git a/google/cloud/bigquery_storage_v1alpha/py.typed b/google/cloud/bigquery_storage_v1alpha/py.typed new file mode 100644 index 00000000..e71b4749 --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/py.typed @@ -0,0 +1,2 @@ +# Marker file for PEP 561. +# The google-cloud-bigquery-storage package uses inline types. diff --git a/google/cloud/bigquery_storage_v1alpha/services/__init__.py b/google/cloud/bigquery_storage_v1alpha/services/__init__.py new file mode 100644 index 00000000..8f6cf068 --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/__init__.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/__init__.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/__init__.py new file mode 100644 index 00000000..d529a87e --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from .async_client import MetastorePartitionServiceAsyncClient +from .client import MetastorePartitionServiceClient + +__all__ = ( + "MetastorePartitionServiceClient", + "MetastorePartitionServiceAsyncClient", +) diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/async_client.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/async_client.py new file mode 100644 index 00000000..e297d7ac --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/async_client.py @@ -0,0 +1,769 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from collections import OrderedDict +import re +from typing import ( + AsyncIterable, + AsyncIterator, + Awaitable, + Callable, + Dict, + Mapping, + MutableMapping, + MutableSequence, + Optional, + Sequence, + Tuple, + Type, + Union, +) + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1 +from google.api_core import retry_async as retries +from google.api_core.client_options import ClientOptions +from google.auth import credentials as ga_credentials # type: ignore +from google.oauth2 import service_account # type: ignore + +from google.cloud.bigquery_storage_v1alpha import gapic_version as package_version + +try: + OptionalRetry = Union[retries.AsyncRetry, gapic_v1.method._MethodDefault, None] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.AsyncRetry, object, None] # type: ignore + +from google.cloud.bigquery_storage_v1alpha.types import metastore_partition, partition + +from .client import MetastorePartitionServiceClient +from .transports.base import DEFAULT_CLIENT_INFO, MetastorePartitionServiceTransport +from .transports.grpc_asyncio import MetastorePartitionServiceGrpcAsyncIOTransport + + +class MetastorePartitionServiceAsyncClient: + """BigQuery Metastore Partition Service API. + This service is used for managing metastore partitions in + BigQuery metastore. The service supports only batch operations + for write. + """ + + _client: MetastorePartitionServiceClient + + # Copy defaults from the synchronous client for use here. + # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead. + DEFAULT_ENDPOINT = MetastorePartitionServiceClient.DEFAULT_ENDPOINT + DEFAULT_MTLS_ENDPOINT = MetastorePartitionServiceClient.DEFAULT_MTLS_ENDPOINT + _DEFAULT_ENDPOINT_TEMPLATE = ( + MetastorePartitionServiceClient._DEFAULT_ENDPOINT_TEMPLATE + ) + _DEFAULT_UNIVERSE = MetastorePartitionServiceClient._DEFAULT_UNIVERSE + + read_stream_path = staticmethod(MetastorePartitionServiceClient.read_stream_path) + parse_read_stream_path = staticmethod( + MetastorePartitionServiceClient.parse_read_stream_path + ) + table_path = staticmethod(MetastorePartitionServiceClient.table_path) + parse_table_path = staticmethod(MetastorePartitionServiceClient.parse_table_path) + common_billing_account_path = staticmethod( + MetastorePartitionServiceClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + MetastorePartitionServiceClient.parse_common_billing_account_path + ) + common_folder_path = staticmethod( + MetastorePartitionServiceClient.common_folder_path + ) + parse_common_folder_path = staticmethod( + MetastorePartitionServiceClient.parse_common_folder_path + ) + common_organization_path = staticmethod( + MetastorePartitionServiceClient.common_organization_path + ) + parse_common_organization_path = staticmethod( + MetastorePartitionServiceClient.parse_common_organization_path + ) + common_project_path = staticmethod( + MetastorePartitionServiceClient.common_project_path + ) + parse_common_project_path = staticmethod( + MetastorePartitionServiceClient.parse_common_project_path + ) + common_location_path = staticmethod( + MetastorePartitionServiceClient.common_location_path + ) + parse_common_location_path = staticmethod( + MetastorePartitionServiceClient.parse_common_location_path + ) + + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials + info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + MetastorePartitionServiceAsyncClient: The constructed client. + """ + return MetastorePartitionServiceClient.from_service_account_info.__func__(MetastorePartitionServiceAsyncClient, info, *args, **kwargs) # type: ignore + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + MetastorePartitionServiceAsyncClient: The constructed client. + """ + return MetastorePartitionServiceClient.from_service_account_file.__func__(MetastorePartitionServiceAsyncClient, filename, *args, **kwargs) # type: ignore + + from_service_account_json = from_service_account_file + + @classmethod + def get_mtls_endpoint_and_cert_source( + cls, client_options: Optional[ClientOptions] = None + ): + """Return the API endpoint and client cert source for mutual TLS. + + The client cert source is determined in the following order: + (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the + client cert source is None. + (2) if `client_options.client_cert_source` is provided, use the provided one; if the + default client cert source exists, use the default one; otherwise the client cert + source is None. + + The API endpoint is determined in the following order: + (1) if `client_options.api_endpoint` if provided, use the provided one. + (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the + default mTLS endpoint; if the environment variable is "never", use the default API + endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise + use the default API endpoint. + + More details can be found at https://google.aip.dev/auth/4114. + + Args: + client_options (google.api_core.client_options.ClientOptions): Custom options for the + client. Only the `api_endpoint` and `client_cert_source` properties may be used + in this method. + + Returns: + Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the + client cert source to use. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If any errors happen. + """ + return MetastorePartitionServiceClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore + + @property + def transport(self) -> MetastorePartitionServiceTransport: + """Returns the transport used by the client instance. + + Returns: + MetastorePartitionServiceTransport: The transport used by the client instance. + """ + return self._client.transport + + @property + def api_endpoint(self): + """Return the API endpoint used by the client instance. + + Returns: + str: The API endpoint used by the client instance. + """ + return self._client._api_endpoint + + @property + def universe_domain(self) -> str: + """Return the universe domain used by the client instance. + + Returns: + str: The universe domain used + by the client instance. + """ + return self._client._universe_domain + + get_transport_class = MetastorePartitionServiceClient.get_transport_class + + def __init__( + self, + *, + credentials: Optional[ga_credentials.Credentials] = None, + transport: Optional[ + Union[ + str, + MetastorePartitionServiceTransport, + Callable[..., MetastorePartitionServiceTransport], + ] + ] = "grpc_asyncio", + client_options: Optional[ClientOptions] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiates the metastore partition service async client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Optional[Union[str,MetastorePartitionServiceTransport,Callable[..., MetastorePartitionServiceTransport]]]): + The transport to use, or a Callable that constructs and returns a new transport to use. + If a Callable is given, it will be called with the same set of initialization + arguments as used in the MetastorePartitionServiceTransport constructor. + If set to None, a transport is chosen automatically. + client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): + Custom options for the client. + + 1. The ``api_endpoint`` property can be used to override the + default endpoint provided by the client when ``transport`` is + not explicitly provided. Only if this property is not set and + ``transport`` was not explicitly provided, the endpoint is + determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment + variable, which have one of the following values: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto-switch to the + default mTLS endpoint if client certificate is present; this is + the default value). + + 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide a client certificate for mTLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + + 3. The ``universe_domain`` property can be used to override the + default "googleapis.com" universe. Note that ``api_endpoint`` + property still takes precedence; and ``universe_domain`` is + currently not supported for mTLS. + + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + """ + self._client = MetastorePartitionServiceClient( + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, + ) + + async def batch_create_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.BatchCreateMetastorePartitionsRequest, dict] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> metastore_partition.BatchCreateMetastorePartitionsResponse: + r"""Adds metastore partitions to a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + async def sample_batch_create_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.CreateMetastorePartitionRequest() + requests.parent = "parent_value" + requests.metastore_partition.values = ['values_value1', 'values_value2'] + + request = bigquery_storage_v1alpha.BatchCreateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = await client.batch_create_metastore_partitions(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsRequest, dict]]): + The request object. Request message for + BatchCreateMetastorePartitions. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsResponse: + Response message for + BatchCreateMetastorePartitions. + + """ + # Create or coerce a protobuf request object. + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, metastore_partition.BatchCreateMetastorePartitionsRequest + ): + request = metastore_partition.BatchCreateMetastorePartitionsRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.batch_create_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def batch_delete_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.BatchDeleteMetastorePartitionsRequest, dict] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> None: + r"""Deletes metastore partitions from a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + async def sample_batch_delete_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + partition_values = bigquery_storage_v1alpha.MetastorePartitionValues() + partition_values.values = ['values_value1', 'values_value2'] + + request = bigquery_storage_v1alpha.BatchDeleteMetastorePartitionsRequest( + parent="parent_value", + partition_values=partition_values, + ) + + # Make the request + await client.batch_delete_metastore_partitions(request=request) + + Args: + request (Optional[Union[google.cloud.bigquery_storage_v1alpha.types.BatchDeleteMetastorePartitionsRequest, dict]]): + The request object. Request message for + BatchDeleteMetastorePartitions. The + MetastorePartition is uniquely + identified by values, which is an + ordered list. Hence, there is no + separate name or partition id field. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + # Create or coerce a protobuf request object. + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, metastore_partition.BatchDeleteMetastorePartitionsRequest + ): + request = metastore_partition.BatchDeleteMetastorePartitionsRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.batch_delete_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def batch_update_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.BatchUpdateMetastorePartitionsRequest, dict] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> metastore_partition.BatchUpdateMetastorePartitionsResponse: + r"""Updates metastore partitions in a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + async def sample_batch_update_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.UpdateMetastorePartitionRequest() + requests.metastore_partition.values = ['values_value1', 'values_value2'] + + request = bigquery_storage_v1alpha.BatchUpdateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = await client.batch_update_metastore_partitions(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsRequest, dict]]): + The request object. Request message for + BatchUpdateMetastorePartitions. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsResponse: + Response message for + BatchUpdateMetastorePartitions. + + """ + # Create or coerce a protobuf request object. + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, metastore_partition.BatchUpdateMetastorePartitionsRequest + ): + request = metastore_partition.BatchUpdateMetastorePartitionsRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.batch_update_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.ListMetastorePartitionsRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> metastore_partition.ListMetastorePartitionsResponse: + r"""Gets metastore partitions from a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + async def sample_list_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.ListMetastorePartitionsRequest( + parent="parent_value", + ) + + # Make the request + response = await client.list_metastore_partitions(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsRequest, dict]]): + The request object. Request message for + ListMetastorePartitions. + parent (:class:`str`): + Required. Reference to the table to + which these metastore partitions belong, + in the format of + projects/{project}/locations/{location}/datasets/{dataset}/tables/{table}. + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsResponse: + Response message for + ListMetastorePartitions. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, metastore_partition.ListMetastorePartitionsRequest): + request = metastore_partition.ListMetastorePartitionsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.list_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + def stream_metastore_partitions( + self, + requests: Optional[ + AsyncIterator[metastore_partition.StreamMetastorePartitionsRequest] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> Awaitable[ + AsyncIterable[metastore_partition.StreamMetastorePartitionsResponse] + ]: + r"""This is a bi-di streaming rpc method that allows the + client to send a stream of partitions and commit all of + them atomically at the end. If the commit is successful, + the server will return a response and close the stream. + If the commit fails (due to duplicate partitions or + other reason), the server will close the stream with an + error. This method is only available via the gRPC API + (not REST). + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + async def sample_stream_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.StreamMetastorePartitionsRequest( + parent="parent_value", + ) + + # This method expects an iterator which contains + # 'bigquery_storage_v1alpha.StreamMetastorePartitionsRequest' objects + # Here we create a generator that yields a single `request` for + # demonstrative purposes. + requests = [request] + + def request_generator(): + for request in requests: + yield request + + # Make the request + stream = await client.stream_metastore_partitions(requests=request_generator()) + + # Handle the response + async for response in stream: + print(response) + + Args: + requests (AsyncIterator[`google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsRequest`]): + The request object AsyncIterator. The top-level message sent by the client to the + [Partitions.StreamMetastorePartitions][] method. Follows + the default gRPC streaming maximum size of 4 MB. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + AsyncIterable[google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsResponse]: + This is the response message sent by the server + to the client for the + [Partitions.StreamMetastorePartitions][] method when + the commit is successful. Server will close the + stream after sending this message. + + """ + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.stream_metastore_partitions + ] + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = rpc( + requests, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def __aenter__(self) -> "MetastorePartitionServiceAsyncClient": + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=package_version.__version__ +) + + +__all__ = ("MetastorePartitionServiceAsyncClient",) diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/client.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/client.py new file mode 100644 index 00000000..3825a22b --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/client.py @@ -0,0 +1,1210 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from collections import OrderedDict +import os +import re +from typing import ( + Callable, + Dict, + Iterable, + Iterator, + Mapping, + MutableMapping, + MutableSequence, + Optional, + Sequence, + Tuple, + Type, + Union, + cast, +) +import warnings + +from google.api_core import client_options as client_options_lib +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1 +from google.api_core import retry as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.exceptions import MutualTLSChannelError # type: ignore +from google.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.oauth2 import service_account # type: ignore + +from google.cloud.bigquery_storage_v1alpha import gapic_version as package_version + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object, None] # type: ignore + +from google.cloud.bigquery_storage_v1alpha.types import metastore_partition, partition + +from .transports.base import DEFAULT_CLIENT_INFO, MetastorePartitionServiceTransport +from .transports.grpc import MetastorePartitionServiceGrpcTransport +from .transports.grpc_asyncio import MetastorePartitionServiceGrpcAsyncIOTransport + + +class MetastorePartitionServiceClientMeta(type): + """Metaclass for the MetastorePartitionService client. + + This provides class-level methods for building and retrieving + support objects (e.g. transport) without polluting the client instance + objects. + """ + + _transport_registry = ( + OrderedDict() + ) # type: Dict[str, Type[MetastorePartitionServiceTransport]] + _transport_registry["grpc"] = MetastorePartitionServiceGrpcTransport + _transport_registry["grpc_asyncio"] = MetastorePartitionServiceGrpcAsyncIOTransport + + def get_transport_class( + cls, + label: Optional[str] = None, + ) -> Type[MetastorePartitionServiceTransport]: + """Returns an appropriate transport class. + + Args: + label: The name of the desired transport. If none is + provided, then the first transport in the registry is used. + + Returns: + The transport class to use. + """ + # If a specific transport is requested, return that one. + if label: + return cls._transport_registry[label] + + # No transport is requested; return the default (that is, the first one + # in the dictionary). + return next(iter(cls._transport_registry.values())) + + +class MetastorePartitionServiceClient(metaclass=MetastorePartitionServiceClientMeta): + """BigQuery Metastore Partition Service API. + This service is used for managing metastore partitions in + BigQuery metastore. The service supports only batch operations + for write. + """ + + @staticmethod + def _get_default_mtls_endpoint(api_endpoint): + """Converts api endpoint to mTLS endpoint. + + Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to + "*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively. + Args: + api_endpoint (Optional[str]): the api endpoint to convert. + Returns: + str: converted mTLS api endpoint. + """ + if not api_endpoint: + return api_endpoint + + mtls_endpoint_re = re.compile( + r"(?P[^.]+)(?P\.mtls)?(?P\.sandbox)?(?P\.googleapis\.com)?" + ) + + m = mtls_endpoint_re.match(api_endpoint) + name, mtls, sandbox, googledomain = m.groups() + if mtls or not googledomain: + return api_endpoint + + if sandbox: + return api_endpoint.replace( + "sandbox.googleapis.com", "mtls.sandbox.googleapis.com" + ) + + return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com") + + # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead. + DEFAULT_ENDPOINT = "bigquerystorage.googleapis.com" + DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__( # type: ignore + DEFAULT_ENDPOINT + ) + + _DEFAULT_ENDPOINT_TEMPLATE = "bigquerystorage.{UNIVERSE_DOMAIN}" + _DEFAULT_UNIVERSE = "googleapis.com" + + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials + info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + MetastorePartitionServiceClient: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_info(info) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + MetastorePartitionServiceClient: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_file(filename) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + + from_service_account_json = from_service_account_file + + @property + def transport(self) -> MetastorePartitionServiceTransport: + """Returns the transport used by the client instance. + + Returns: + MetastorePartitionServiceTransport: The transport used by the client + instance. + """ + return self._transport + + @staticmethod + def read_stream_path( + project: str, + location: str, + session: str, + stream: str, + ) -> str: + """Returns a fully-qualified read_stream string.""" + return "projects/{project}/locations/{location}/sessions/{session}/streams/{stream}".format( + project=project, + location=location, + session=session, + stream=stream, + ) + + @staticmethod + def parse_read_stream_path(path: str) -> Dict[str, str]: + """Parses a read_stream path into its component segments.""" + m = re.match( + r"^projects/(?P.+?)/locations/(?P.+?)/sessions/(?P.+?)/streams/(?P.+?)$", + path, + ) + return m.groupdict() if m else {} + + @staticmethod + def table_path( + project: str, + dataset: str, + table: str, + ) -> str: + """Returns a fully-qualified table string.""" + return "projects/{project}/datasets/{dataset}/tables/{table}".format( + project=project, + dataset=dataset, + table=table, + ) + + @staticmethod + def parse_table_path(path: str) -> Dict[str, str]: + """Parses a table path into its component segments.""" + m = re.match( + r"^projects/(?P.+?)/datasets/(?P.+?)/tables/(?P.+?)$", + path, + ) + return m.groupdict() if m else {} + + @staticmethod + def common_billing_account_path( + billing_account: str, + ) -> str: + """Returns a fully-qualified billing_account string.""" + return "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + + @staticmethod + def parse_common_billing_account_path(path: str) -> Dict[str, str]: + """Parse a billing_account path into its component segments.""" + m = re.match(r"^billingAccounts/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_folder_path( + folder: str, + ) -> str: + """Returns a fully-qualified folder string.""" + return "folders/{folder}".format( + folder=folder, + ) + + @staticmethod + def parse_common_folder_path(path: str) -> Dict[str, str]: + """Parse a folder path into its component segments.""" + m = re.match(r"^folders/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_organization_path( + organization: str, + ) -> str: + """Returns a fully-qualified organization string.""" + return "organizations/{organization}".format( + organization=organization, + ) + + @staticmethod + def parse_common_organization_path(path: str) -> Dict[str, str]: + """Parse a organization path into its component segments.""" + m = re.match(r"^organizations/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_project_path( + project: str, + ) -> str: + """Returns a fully-qualified project string.""" + return "projects/{project}".format( + project=project, + ) + + @staticmethod + def parse_common_project_path(path: str) -> Dict[str, str]: + """Parse a project path into its component segments.""" + m = re.match(r"^projects/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_location_path( + project: str, + location: str, + ) -> str: + """Returns a fully-qualified location string.""" + return "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + + @staticmethod + def parse_common_location_path(path: str) -> Dict[str, str]: + """Parse a location path into its component segments.""" + m = re.match(r"^projects/(?P.+?)/locations/(?P.+?)$", path) + return m.groupdict() if m else {} + + @classmethod + def get_mtls_endpoint_and_cert_source( + cls, client_options: Optional[client_options_lib.ClientOptions] = None + ): + """Deprecated. Return the API endpoint and client cert source for mutual TLS. + + The client cert source is determined in the following order: + (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the + client cert source is None. + (2) if `client_options.client_cert_source` is provided, use the provided one; if the + default client cert source exists, use the default one; otherwise the client cert + source is None. + + The API endpoint is determined in the following order: + (1) if `client_options.api_endpoint` if provided, use the provided one. + (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the + default mTLS endpoint; if the environment variable is "never", use the default API + endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise + use the default API endpoint. + + More details can be found at https://google.aip.dev/auth/4114. + + Args: + client_options (google.api_core.client_options.ClientOptions): Custom options for the + client. Only the `api_endpoint` and `client_cert_source` properties may be used + in this method. + + Returns: + Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the + client cert source to use. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If any errors happen. + """ + + warnings.warn( + "get_mtls_endpoint_and_cert_source is deprecated. Use the api_endpoint property instead.", + DeprecationWarning, + ) + if client_options is None: + client_options = client_options_lib.ClientOptions() + use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") + use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") + if use_client_cert not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" + ) + if use_mtls_endpoint not in ("auto", "never", "always"): + raise MutualTLSChannelError( + "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" + ) + + # Figure out the client cert source to use. + client_cert_source = None + if use_client_cert == "true": + if client_options.client_cert_source: + client_cert_source = client_options.client_cert_source + elif mtls.has_default_client_cert_source(): + client_cert_source = mtls.default_client_cert_source() + + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + elif use_mtls_endpoint == "always" or ( + use_mtls_endpoint == "auto" and client_cert_source + ): + api_endpoint = cls.DEFAULT_MTLS_ENDPOINT + else: + api_endpoint = cls.DEFAULT_ENDPOINT + + return api_endpoint, client_cert_source + + @staticmethod + def _read_environment_variables(): + """Returns the environment variables used by the client. + + Returns: + Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE, + GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables. + + Raises: + ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not + any of ["true", "false"]. + google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT + is not any of ["auto", "never", "always"]. + """ + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() + universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") + if use_client_cert not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" + ) + if use_mtls_endpoint not in ("auto", "never", "always"): + raise MutualTLSChannelError( + "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" + ) + return use_client_cert == "true", use_mtls_endpoint, universe_domain_env + + @staticmethod + def _get_client_cert_source(provided_cert_source, use_cert_flag): + """Return the client cert source to be used by the client. + + Args: + provided_cert_source (bytes): The client certificate source provided. + use_cert_flag (bool): A flag indicating whether to use the client certificate. + + Returns: + bytes or None: The client cert source to be used by the client. + """ + client_cert_source = None + if use_cert_flag: + if provided_cert_source: + client_cert_source = provided_cert_source + elif mtls.has_default_client_cert_source(): + client_cert_source = mtls.default_client_cert_source() + return client_cert_source + + @staticmethod + def _get_api_endpoint( + api_override, client_cert_source, universe_domain, use_mtls_endpoint + ): + """Return the API endpoint used by the client. + + Args: + api_override (str): The API endpoint override. If specified, this is always + the return value of this function and the other arguments are not used. + client_cert_source (bytes): The client certificate source used by the client. + universe_domain (str): The universe domain used by the client. + use_mtls_endpoint (str): How to use the mTLS endpoint, which depends also on the other parameters. + Possible values are "always", "auto", or "never". + + Returns: + str: The API endpoint to be used by the client. + """ + if api_override is not None: + api_endpoint = api_override + elif use_mtls_endpoint == "always" or ( + use_mtls_endpoint == "auto" and client_cert_source + ): + _default_universe = MetastorePartitionServiceClient._DEFAULT_UNIVERSE + if universe_domain != _default_universe: + raise MutualTLSChannelError( + f"mTLS is not supported in any universe other than {_default_universe}." + ) + api_endpoint = MetastorePartitionServiceClient.DEFAULT_MTLS_ENDPOINT + else: + api_endpoint = ( + MetastorePartitionServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=universe_domain + ) + ) + return api_endpoint + + @staticmethod + def _get_universe_domain( + client_universe_domain: Optional[str], universe_domain_env: Optional[str] + ) -> str: + """Return the universe domain used by the client. + + Args: + client_universe_domain (Optional[str]): The universe domain configured via the client options. + universe_domain_env (Optional[str]): The universe domain configured via the "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable. + + Returns: + str: The universe domain to be used by the client. + + Raises: + ValueError: If the universe domain is an empty string. + """ + universe_domain = MetastorePartitionServiceClient._DEFAULT_UNIVERSE + if client_universe_domain is not None: + universe_domain = client_universe_domain + elif universe_domain_env is not None: + universe_domain = universe_domain_env + if len(universe_domain.strip()) == 0: + raise ValueError("Universe Domain cannot be an empty string.") + return universe_domain + + @staticmethod + def _compare_universes( + client_universe: str, credentials: ga_credentials.Credentials + ) -> bool: + """Returns True iff the universe domains used by the client and credentials match. + + Args: + client_universe (str): The universe domain configured via the client options. + credentials (ga_credentials.Credentials): The credentials being used in the client. + + Returns: + bool: True iff client_universe matches the universe in credentials. + + Raises: + ValueError: when client_universe does not match the universe in credentials. + """ + + default_universe = MetastorePartitionServiceClient._DEFAULT_UNIVERSE + credentials_universe = getattr(credentials, "universe_domain", default_universe) + + if client_universe != credentials_universe: + raise ValueError( + "The configured universe domain " + f"({client_universe}) does not match the universe domain " + f"found in the credentials ({credentials_universe}). " + "If you haven't configured the universe domain explicitly, " + f"`{default_universe}` is the default." + ) + return True + + def _validate_universe_domain(self): + """Validates client's and credentials' universe domains are consistent. + + Returns: + bool: True iff the configured universe domain is valid. + + Raises: + ValueError: If the configured universe domain is not valid. + """ + self._is_universe_domain_valid = ( + self._is_universe_domain_valid + or MetastorePartitionServiceClient._compare_universes( + self.universe_domain, self.transport._credentials + ) + ) + return self._is_universe_domain_valid + + @property + def api_endpoint(self): + """Return the API endpoint used by the client instance. + + Returns: + str: The API endpoint used by the client instance. + """ + return self._api_endpoint + + @property + def universe_domain(self) -> str: + """Return the universe domain used by the client instance. + + Returns: + str: The universe domain used by the client instance. + """ + return self._universe_domain + + def __init__( + self, + *, + credentials: Optional[ga_credentials.Credentials] = None, + transport: Optional[ + Union[ + str, + MetastorePartitionServiceTransport, + Callable[..., MetastorePartitionServiceTransport], + ] + ] = None, + client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiates the metastore partition service client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Optional[Union[str,MetastorePartitionServiceTransport,Callable[..., MetastorePartitionServiceTransport]]]): + The transport to use, or a Callable that constructs and returns a new transport. + If a Callable is given, it will be called with the same set of initialization + arguments as used in the MetastorePartitionServiceTransport constructor. + If set to None, a transport is chosen automatically. + client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): + Custom options for the client. + + 1. The ``api_endpoint`` property can be used to override the + default endpoint provided by the client when ``transport`` is + not explicitly provided. Only if this property is not set and + ``transport`` was not explicitly provided, the endpoint is + determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment + variable, which have one of the following values: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto-switch to the + default mTLS endpoint if client certificate is present; this is + the default value). + + 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide a client certificate for mTLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + + 3. The ``universe_domain`` property can be used to override the + default "googleapis.com" universe. Note that the ``api_endpoint`` + property still takes precedence; and ``universe_domain`` is + currently not supported for mTLS. + + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + """ + self._client_options = client_options + if isinstance(self._client_options, dict): + self._client_options = client_options_lib.from_dict(self._client_options) + if self._client_options is None: + self._client_options = client_options_lib.ClientOptions() + self._client_options = cast( + client_options_lib.ClientOptions, self._client_options + ) + + universe_domain_opt = getattr(self._client_options, "universe_domain", None) + + ( + self._use_client_cert, + self._use_mtls_endpoint, + self._universe_domain_env, + ) = MetastorePartitionServiceClient._read_environment_variables() + self._client_cert_source = ( + MetastorePartitionServiceClient._get_client_cert_source( + self._client_options.client_cert_source, self._use_client_cert + ) + ) + self._universe_domain = MetastorePartitionServiceClient._get_universe_domain( + universe_domain_opt, self._universe_domain_env + ) + self._api_endpoint = None # updated below, depending on `transport` + + # Initialize the universe domain validation. + self._is_universe_domain_valid = False + + api_key_value = getattr(self._client_options, "api_key", None) + if api_key_value and credentials: + raise ValueError( + "client_options.api_key and credentials are mutually exclusive" + ) + + # Save or instantiate the transport. + # Ordinarily, we provide the transport, but allowing a custom transport + # instance provides an extensibility point for unusual situations. + transport_provided = isinstance(transport, MetastorePartitionServiceTransport) + if transport_provided: + # transport is a MetastorePartitionServiceTransport instance. + if credentials or self._client_options.credentials_file or api_key_value: + raise ValueError( + "When providing a transport instance, " + "provide its credentials directly." + ) + if self._client_options.scopes: + raise ValueError( + "When providing a transport instance, provide its scopes " + "directly." + ) + self._transport = cast(MetastorePartitionServiceTransport, transport) + self._api_endpoint = self._transport.host + + self._api_endpoint = ( + self._api_endpoint + or MetastorePartitionServiceClient._get_api_endpoint( + self._client_options.api_endpoint, + self._client_cert_source, + self._universe_domain, + self._use_mtls_endpoint, + ) + ) + + if not transport_provided: + import google.auth._default # type: ignore + + if api_key_value and hasattr( + google.auth._default, "get_api_key_credentials" + ): + credentials = google.auth._default.get_api_key_credentials( + api_key_value + ) + + transport_init: Union[ + Type[MetastorePartitionServiceTransport], + Callable[..., MetastorePartitionServiceTransport], + ] = ( + MetastorePartitionServiceClient.get_transport_class(transport) + if isinstance(transport, str) or transport is None + else cast(Callable[..., MetastorePartitionServiceTransport], transport) + ) + # initialize with the provided callable or the passed in class + self._transport = transport_init( + credentials=credentials, + credentials_file=self._client_options.credentials_file, + host=self._api_endpoint, + scopes=self._client_options.scopes, + client_cert_source_for_mtls=self._client_cert_source, + quota_project_id=self._client_options.quota_project_id, + client_info=client_info, + always_use_jwt_access=True, + api_audience=self._client_options.api_audience, + ) + + def batch_create_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.BatchCreateMetastorePartitionsRequest, dict] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> metastore_partition.BatchCreateMetastorePartitionsResponse: + r"""Adds metastore partitions to a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + def sample_batch_create_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.CreateMetastorePartitionRequest() + requests.parent = "parent_value" + requests.metastore_partition.values = ['values_value1', 'values_value2'] + + request = bigquery_storage_v1alpha.BatchCreateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = client.batch_create_metastore_partitions(request=request) + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsRequest, dict]): + The request object. Request message for + BatchCreateMetastorePartitions. + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsResponse: + Response message for + BatchCreateMetastorePartitions. + + """ + # Create or coerce a protobuf request object. + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, metastore_partition.BatchCreateMetastorePartitionsRequest + ): + request = metastore_partition.BatchCreateMetastorePartitionsRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.batch_create_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._validate_universe_domain() + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + def batch_delete_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.BatchDeleteMetastorePartitionsRequest, dict] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> None: + r"""Deletes metastore partitions from a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + def sample_batch_delete_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + partition_values = bigquery_storage_v1alpha.MetastorePartitionValues() + partition_values.values = ['values_value1', 'values_value2'] + + request = bigquery_storage_v1alpha.BatchDeleteMetastorePartitionsRequest( + parent="parent_value", + partition_values=partition_values, + ) + + # Make the request + client.batch_delete_metastore_partitions(request=request) + + Args: + request (Union[google.cloud.bigquery_storage_v1alpha.types.BatchDeleteMetastorePartitionsRequest, dict]): + The request object. Request message for + BatchDeleteMetastorePartitions. The + MetastorePartition is uniquely + identified by values, which is an + ordered list. Hence, there is no + separate name or partition id field. + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + # Create or coerce a protobuf request object. + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, metastore_partition.BatchDeleteMetastorePartitionsRequest + ): + request = metastore_partition.BatchDeleteMetastorePartitionsRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.batch_delete_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._validate_universe_domain() + + # Send the request. + rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + def batch_update_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.BatchUpdateMetastorePartitionsRequest, dict] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> metastore_partition.BatchUpdateMetastorePartitionsResponse: + r"""Updates metastore partitions in a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + def sample_batch_update_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.UpdateMetastorePartitionRequest() + requests.metastore_partition.values = ['values_value1', 'values_value2'] + + request = bigquery_storage_v1alpha.BatchUpdateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = client.batch_update_metastore_partitions(request=request) + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsRequest, dict]): + The request object. Request message for + BatchUpdateMetastorePartitions. + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsResponse: + Response message for + BatchUpdateMetastorePartitions. + + """ + # Create or coerce a protobuf request object. + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, metastore_partition.BatchUpdateMetastorePartitionsRequest + ): + request = metastore_partition.BatchUpdateMetastorePartitionsRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.batch_update_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._validate_universe_domain() + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + def list_metastore_partitions( + self, + request: Optional[ + Union[metastore_partition.ListMetastorePartitionsRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> metastore_partition.ListMetastorePartitionsResponse: + r"""Gets metastore partitions from a table. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + def sample_list_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.ListMetastorePartitionsRequest( + parent="parent_value", + ) + + # Make the request + response = client.list_metastore_partitions(request=request) + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsRequest, dict]): + The request object. Request message for + ListMetastorePartitions. + parent (str): + Required. Reference to the table to + which these metastore partitions belong, + in the format of + projects/{project}/locations/{location}/datasets/{dataset}/tables/{table}. + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsResponse: + Response message for + ListMetastorePartitions. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, metastore_partition.ListMetastorePartitionsRequest): + request = metastore_partition.ListMetastorePartitionsRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.list_metastore_partitions + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._validate_universe_domain() + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + def stream_metastore_partitions( + self, + requests: Optional[ + Iterator[metastore_partition.StreamMetastorePartitionsRequest] + ] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> Iterable[metastore_partition.StreamMetastorePartitionsResponse]: + r"""This is a bi-di streaming rpc method that allows the + client to send a stream of partitions and commit all of + them atomically at the end. If the commit is successful, + the server will return a response and close the stream. + If the commit fails (due to duplicate partitions or + other reason), the server will close the stream with an + error. This method is only available via the gRPC API + (not REST). + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import bigquery_storage_v1alpha + + def sample_stream_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.StreamMetastorePartitionsRequest( + parent="parent_value", + ) + + # This method expects an iterator which contains + # 'bigquery_storage_v1alpha.StreamMetastorePartitionsRequest' objects + # Here we create a generator that yields a single `request` for + # demonstrative purposes. + requests = [request] + + def request_generator(): + for request in requests: + yield request + + # Make the request + stream = client.stream_metastore_partitions(requests=request_generator()) + + # Handle the response + for response in stream: + print(response) + + Args: + requests (Iterator[google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsRequest]): + The request object iterator. The top-level message sent by the client to the + [Partitions.StreamMetastorePartitions][] method. Follows + the default gRPC streaming maximum size of 4 MB. + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + Iterable[google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsResponse]: + This is the response message sent by the server + to the client for the + [Partitions.StreamMetastorePartitions][] method when + the commit is successful. Server will close the + stream after sending this message. + + """ + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.stream_metastore_partitions + ] + + # Validate the universe domain. + self._validate_universe_domain() + + # Send the request. + response = rpc( + requests, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + def __enter__(self) -> "MetastorePartitionServiceClient": + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + self.transport.close() + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=package_version.__version__ +) + + +__all__ = ("MetastorePartitionServiceClient",) diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/__init__.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/__init__.py new file mode 100644 index 00000000..d7c94ea1 --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/__init__.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from collections import OrderedDict +from typing import Dict, Type + +from .base import MetastorePartitionServiceTransport +from .grpc import MetastorePartitionServiceGrpcTransport +from .grpc_asyncio import MetastorePartitionServiceGrpcAsyncIOTransport + +# Compile a registry of transports. +_transport_registry = ( + OrderedDict() +) # type: Dict[str, Type[MetastorePartitionServiceTransport]] +_transport_registry["grpc"] = MetastorePartitionServiceGrpcTransport +_transport_registry["grpc_asyncio"] = MetastorePartitionServiceGrpcAsyncIOTransport + +__all__ = ( + "MetastorePartitionServiceTransport", + "MetastorePartitionServiceGrpcTransport", + "MetastorePartitionServiceGrpcAsyncIOTransport", +) diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/base.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/base.py new file mode 100644 index 00000000..2c973180 --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/base.py @@ -0,0 +1,269 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import abc +from typing import Awaitable, Callable, Dict, Optional, Sequence, Union + +import google.api_core +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1 +from google.api_core import retry as retries +import google.auth # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.oauth2 import service_account # type: ignore +from google.protobuf import empty_pb2 # type: ignore + +from google.cloud.bigquery_storage_v1alpha import gapic_version as package_version +from google.cloud.bigquery_storage_v1alpha.types import metastore_partition + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=package_version.__version__ +) + + +class MetastorePartitionServiceTransport(abc.ABC): + """Abstract transport class for MetastorePartitionService.""" + + AUTH_SCOPES = ( + "https://www.googleapis.com/auth/bigquery", + "https://www.googleapis.com/auth/cloud-platform", + ) + + DEFAULT_HOST: str = "bigquerystorage.googleapis.com" + + def __init__( + self, + *, + host: str = DEFAULT_HOST, + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + **kwargs, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'bigquerystorage.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scopes (Optional[Sequence[str]]): A list of scopes. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + """ + + scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES} + + # Save the scopes. + self._scopes = scopes + if not hasattr(self, "_ignore_credentials"): + self._ignore_credentials: bool = False + + # If no credentials are provided, then determine the appropriate + # defaults. + if credentials and credentials_file: + raise core_exceptions.DuplicateCredentialArgs( + "'credentials_file' and 'credentials' are mutually exclusive" + ) + + if credentials_file is not None: + credentials, _ = google.auth.load_credentials_from_file( + credentials_file, **scopes_kwargs, quota_project_id=quota_project_id + ) + elif credentials is None and not self._ignore_credentials: + credentials, _ = google.auth.default( + **scopes_kwargs, quota_project_id=quota_project_id + ) + # Don't apply audience if the credentials file passed from user. + if hasattr(credentials, "with_gdch_audience"): + credentials = credentials.with_gdch_audience( + api_audience if api_audience else host + ) + + # If the credentials are service account credentials, then always try to use self signed JWT. + if ( + always_use_jwt_access + and isinstance(credentials, service_account.Credentials) + and hasattr(service_account.Credentials, "with_always_use_jwt_access") + ): + credentials = credentials.with_always_use_jwt_access(True) + + # Save the credentials. + self._credentials = credentials + + # Save the hostname. Default to port 443 (HTTPS) if none is specified. + if ":" not in host: + host += ":443" + self._host = host + + @property + def host(self): + return self._host + + def _prep_wrapped_messages(self, client_info): + # Precompute the wrapped methods. + self._wrapped_methods = { + self.batch_create_metastore_partitions: gapic_v1.method.wrap_method( + self.batch_create_metastore_partitions, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.batch_delete_metastore_partitions: gapic_v1.method.wrap_method( + self.batch_delete_metastore_partitions, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.batch_update_metastore_partitions: gapic_v1.method.wrap_method( + self.batch_update_metastore_partitions, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.list_metastore_partitions: gapic_v1.method.wrap_method( + self.list_metastore_partitions, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.stream_metastore_partitions: gapic_v1.method.wrap_method( + self.stream_metastore_partitions, + default_timeout=120.0, + client_info=client_info, + ), + } + + def close(self): + """Closes resources associated with the transport. + + .. warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ + raise NotImplementedError() + + @property + def batch_create_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchCreateMetastorePartitionsRequest], + Union[ + metastore_partition.BatchCreateMetastorePartitionsResponse, + Awaitable[metastore_partition.BatchCreateMetastorePartitionsResponse], + ], + ]: + raise NotImplementedError() + + @property + def batch_delete_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchDeleteMetastorePartitionsRequest], + Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]], + ]: + raise NotImplementedError() + + @property + def batch_update_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchUpdateMetastorePartitionsRequest], + Union[ + metastore_partition.BatchUpdateMetastorePartitionsResponse, + Awaitable[metastore_partition.BatchUpdateMetastorePartitionsResponse], + ], + ]: + raise NotImplementedError() + + @property + def list_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.ListMetastorePartitionsRequest], + Union[ + metastore_partition.ListMetastorePartitionsResponse, + Awaitable[metastore_partition.ListMetastorePartitionsResponse], + ], + ]: + raise NotImplementedError() + + @property + def stream_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.StreamMetastorePartitionsRequest], + Union[ + metastore_partition.StreamMetastorePartitionsResponse, + Awaitable[metastore_partition.StreamMetastorePartitionsResponse], + ], + ]: + raise NotImplementedError() + + @property + def kind(self) -> str: + raise NotImplementedError() + + +__all__ = ("MetastorePartitionServiceTransport",) diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc.py new file mode 100644 index 00000000..12207255 --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc.py @@ -0,0 +1,411 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from typing import Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import gapic_v1, grpc_helpers +import google.auth # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.protobuf import empty_pb2 # type: ignore +import grpc # type: ignore + +from google.cloud.bigquery_storage_v1alpha.types import metastore_partition + +from .base import DEFAULT_CLIENT_INFO, MetastorePartitionServiceTransport + + +class MetastorePartitionServiceGrpcTransport(MetastorePartitionServiceTransport): + """gRPC backend transport for MetastorePartitionService. + + BigQuery Metastore Partition Service API. + This service is used for managing metastore partitions in + BigQuery metastore. The service supports only batch operations + for write. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _stubs: Dict[str, Callable] + + def __init__( + self, + *, + host: str = "bigquerystorage.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'bigquerystorage.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if a ``channel`` instance is provided. + channel (Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create + a mutual TLS channel with client SSL credentials from + ``client_cert_source`` or application default SSL credentials. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, grpc.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Wrap messages. This must be done after self._grpc_channel exists + self._prep_wrapped_messages(client_info) + + @classmethod + def create_channel( + cls, + host: str = "bigquerystorage.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> grpc.Channel: + """Create and return a gRPC channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + grpc.Channel: A gRPC channel object. + + Raises: + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + + return grpc_helpers.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + @property + def grpc_channel(self) -> grpc.Channel: + """Return the channel designed to connect to this service.""" + return self._grpc_channel + + @property + def batch_create_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchCreateMetastorePartitionsRequest], + metastore_partition.BatchCreateMetastorePartitionsResponse, + ]: + r"""Return a callable for the batch create metastore + partitions method over gRPC. + + Adds metastore partitions to a table. + + Returns: + Callable[[~.BatchCreateMetastorePartitionsRequest], + ~.BatchCreateMetastorePartitionsResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "batch_create_metastore_partitions" not in self._stubs: + self._stubs[ + "batch_create_metastore_partitions" + ] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/BatchCreateMetastorePartitions", + request_serializer=metastore_partition.BatchCreateMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.BatchCreateMetastorePartitionsResponse.deserialize, + ) + return self._stubs["batch_create_metastore_partitions"] + + @property + def batch_delete_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchDeleteMetastorePartitionsRequest], empty_pb2.Empty + ]: + r"""Return a callable for the batch delete metastore + partitions method over gRPC. + + Deletes metastore partitions from a table. + + Returns: + Callable[[~.BatchDeleteMetastorePartitionsRequest], + ~.Empty]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "batch_delete_metastore_partitions" not in self._stubs: + self._stubs[ + "batch_delete_metastore_partitions" + ] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/BatchDeleteMetastorePartitions", + request_serializer=metastore_partition.BatchDeleteMetastorePartitionsRequest.serialize, + response_deserializer=empty_pb2.Empty.FromString, + ) + return self._stubs["batch_delete_metastore_partitions"] + + @property + def batch_update_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchUpdateMetastorePartitionsRequest], + metastore_partition.BatchUpdateMetastorePartitionsResponse, + ]: + r"""Return a callable for the batch update metastore + partitions method over gRPC. + + Updates metastore partitions in a table. + + Returns: + Callable[[~.BatchUpdateMetastorePartitionsRequest], + ~.BatchUpdateMetastorePartitionsResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "batch_update_metastore_partitions" not in self._stubs: + self._stubs[ + "batch_update_metastore_partitions" + ] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/BatchUpdateMetastorePartitions", + request_serializer=metastore_partition.BatchUpdateMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.BatchUpdateMetastorePartitionsResponse.deserialize, + ) + return self._stubs["batch_update_metastore_partitions"] + + @property + def list_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.ListMetastorePartitionsRequest], + metastore_partition.ListMetastorePartitionsResponse, + ]: + r"""Return a callable for the list metastore partitions method over gRPC. + + Gets metastore partitions from a table. + + Returns: + Callable[[~.ListMetastorePartitionsRequest], + ~.ListMetastorePartitionsResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "list_metastore_partitions" not in self._stubs: + self._stubs["list_metastore_partitions"] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/ListMetastorePartitions", + request_serializer=metastore_partition.ListMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.ListMetastorePartitionsResponse.deserialize, + ) + return self._stubs["list_metastore_partitions"] + + @property + def stream_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.StreamMetastorePartitionsRequest], + metastore_partition.StreamMetastorePartitionsResponse, + ]: + r"""Return a callable for the stream metastore partitions method over gRPC. + + This is a bi-di streaming rpc method that allows the + client to send a stream of partitions and commit all of + them atomically at the end. If the commit is successful, + the server will return a response and close the stream. + If the commit fails (due to duplicate partitions or + other reason), the server will close the stream with an + error. This method is only available via the gRPC API + (not REST). + + Returns: + Callable[[~.StreamMetastorePartitionsRequest], + ~.StreamMetastorePartitionsResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "stream_metastore_partitions" not in self._stubs: + self._stubs[ + "stream_metastore_partitions" + ] = self.grpc_channel.stream_stream( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/StreamMetastorePartitions", + request_serializer=metastore_partition.StreamMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.StreamMetastorePartitionsResponse.deserialize, + ) + return self._stubs["stream_metastore_partitions"] + + def close(self): + self.grpc_channel.close() + + @property + def kind(self) -> str: + return "grpc" + + +__all__ = ("MetastorePartitionServiceGrpcTransport",) diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc_asyncio.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc_asyncio.py new file mode 100644 index 00000000..f1a4e316 --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc_asyncio.py @@ -0,0 +1,478 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1, grpc_helpers_async +from google.api_core import retry_async as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.protobuf import empty_pb2 # type: ignore +import grpc # type: ignore +from grpc.experimental import aio # type: ignore + +from google.cloud.bigquery_storage_v1alpha.types import metastore_partition + +from .base import DEFAULT_CLIENT_INFO, MetastorePartitionServiceTransport +from .grpc import MetastorePartitionServiceGrpcTransport + + +class MetastorePartitionServiceGrpcAsyncIOTransport(MetastorePartitionServiceTransport): + """gRPC AsyncIO backend transport for MetastorePartitionService. + + BigQuery Metastore Partition Service API. + This service is used for managing metastore partitions in + BigQuery metastore. The service supports only batch operations + for write. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _grpc_channel: aio.Channel + _stubs: Dict[str, Callable] = {} + + @classmethod + def create_channel( + cls, + host: str = "bigquerystorage.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> aio.Channel: + """Create and return a gRPC AsyncIO channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + aio.Channel: A gRPC AsyncIO channel object. + """ + + return grpc_helpers_async.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + def __init__( + self, + *, + host: str = "bigquerystorage.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[aio.Channel, Callable[..., aio.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'bigquerystorage.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + channel (Optional[Union[aio.Channel, Callable[..., aio.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create + a mutual TLS channel with client SSL credentials from + ``client_cert_source`` or application default SSL credentials. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, aio.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Wrap messages. This must be done after self._grpc_channel exists + self._prep_wrapped_messages(client_info) + + @property + def grpc_channel(self) -> aio.Channel: + """Create the channel designed to connect to this service. + + This property caches on the instance; repeated calls return + the same channel. + """ + # Return the channel from cache. + return self._grpc_channel + + @property + def batch_create_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchCreateMetastorePartitionsRequest], + Awaitable[metastore_partition.BatchCreateMetastorePartitionsResponse], + ]: + r"""Return a callable for the batch create metastore + partitions method over gRPC. + + Adds metastore partitions to a table. + + Returns: + Callable[[~.BatchCreateMetastorePartitionsRequest], + Awaitable[~.BatchCreateMetastorePartitionsResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "batch_create_metastore_partitions" not in self._stubs: + self._stubs[ + "batch_create_metastore_partitions" + ] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/BatchCreateMetastorePartitions", + request_serializer=metastore_partition.BatchCreateMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.BatchCreateMetastorePartitionsResponse.deserialize, + ) + return self._stubs["batch_create_metastore_partitions"] + + @property + def batch_delete_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchDeleteMetastorePartitionsRequest], + Awaitable[empty_pb2.Empty], + ]: + r"""Return a callable for the batch delete metastore + partitions method over gRPC. + + Deletes metastore partitions from a table. + + Returns: + Callable[[~.BatchDeleteMetastorePartitionsRequest], + Awaitable[~.Empty]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "batch_delete_metastore_partitions" not in self._stubs: + self._stubs[ + "batch_delete_metastore_partitions" + ] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/BatchDeleteMetastorePartitions", + request_serializer=metastore_partition.BatchDeleteMetastorePartitionsRequest.serialize, + response_deserializer=empty_pb2.Empty.FromString, + ) + return self._stubs["batch_delete_metastore_partitions"] + + @property + def batch_update_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.BatchUpdateMetastorePartitionsRequest], + Awaitable[metastore_partition.BatchUpdateMetastorePartitionsResponse], + ]: + r"""Return a callable for the batch update metastore + partitions method over gRPC. + + Updates metastore partitions in a table. + + Returns: + Callable[[~.BatchUpdateMetastorePartitionsRequest], + Awaitable[~.BatchUpdateMetastorePartitionsResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "batch_update_metastore_partitions" not in self._stubs: + self._stubs[ + "batch_update_metastore_partitions" + ] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/BatchUpdateMetastorePartitions", + request_serializer=metastore_partition.BatchUpdateMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.BatchUpdateMetastorePartitionsResponse.deserialize, + ) + return self._stubs["batch_update_metastore_partitions"] + + @property + def list_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.ListMetastorePartitionsRequest], + Awaitable[metastore_partition.ListMetastorePartitionsResponse], + ]: + r"""Return a callable for the list metastore partitions method over gRPC. + + Gets metastore partitions from a table. + + Returns: + Callable[[~.ListMetastorePartitionsRequest], + Awaitable[~.ListMetastorePartitionsResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "list_metastore_partitions" not in self._stubs: + self._stubs["list_metastore_partitions"] = self.grpc_channel.unary_unary( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/ListMetastorePartitions", + request_serializer=metastore_partition.ListMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.ListMetastorePartitionsResponse.deserialize, + ) + return self._stubs["list_metastore_partitions"] + + @property + def stream_metastore_partitions( + self, + ) -> Callable[ + [metastore_partition.StreamMetastorePartitionsRequest], + Awaitable[metastore_partition.StreamMetastorePartitionsResponse], + ]: + r"""Return a callable for the stream metastore partitions method over gRPC. + + This is a bi-di streaming rpc method that allows the + client to send a stream of partitions and commit all of + them atomically at the end. If the commit is successful, + the server will return a response and close the stream. + If the commit fails (due to duplicate partitions or + other reason), the server will close the stream with an + error. This method is only available via the gRPC API + (not REST). + + Returns: + Callable[[~.StreamMetastorePartitionsRequest], + Awaitable[~.StreamMetastorePartitionsResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "stream_metastore_partitions" not in self._stubs: + self._stubs[ + "stream_metastore_partitions" + ] = self.grpc_channel.stream_stream( + "/google.cloud.bigquery.storage.v1alpha.MetastorePartitionService/StreamMetastorePartitions", + request_serializer=metastore_partition.StreamMetastorePartitionsRequest.serialize, + response_deserializer=metastore_partition.StreamMetastorePartitionsResponse.deserialize, + ) + return self._stubs["stream_metastore_partitions"] + + def _prep_wrapped_messages(self, client_info): + """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" + self._wrapped_methods = { + self.batch_create_metastore_partitions: gapic_v1.method_async.wrap_method( + self.batch_create_metastore_partitions, + default_retry=retries.AsyncRetry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.batch_delete_metastore_partitions: gapic_v1.method_async.wrap_method( + self.batch_delete_metastore_partitions, + default_retry=retries.AsyncRetry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.batch_update_metastore_partitions: gapic_v1.method_async.wrap_method( + self.batch_update_metastore_partitions, + default_retry=retries.AsyncRetry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.list_metastore_partitions: gapic_v1.method_async.wrap_method( + self.list_metastore_partitions, + default_retry=retries.AsyncRetry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + core_exceptions.ServiceUnavailable, + ), + deadline=120.0, + ), + default_timeout=120.0, + client_info=client_info, + ), + self.stream_metastore_partitions: gapic_v1.method_async.wrap_method( + self.stream_metastore_partitions, + default_timeout=120.0, + client_info=client_info, + ), + } + + def close(self): + return self.grpc_channel.close() + + +__all__ = ("MetastorePartitionServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/bigquery_storage_v1alpha/types/__init__.py b/google/cloud/bigquery_storage_v1alpha/types/__init__.py new file mode 100644 index 00000000..0531692a --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/types/__init__.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from .metastore_partition import ( + BatchCreateMetastorePartitionsRequest, + BatchCreateMetastorePartitionsResponse, + BatchDeleteMetastorePartitionsRequest, + BatchSizeTooLargeError, + BatchUpdateMetastorePartitionsRequest, + BatchUpdateMetastorePartitionsResponse, + CreateMetastorePartitionRequest, + ListMetastorePartitionsRequest, + ListMetastorePartitionsResponse, + StreamMetastorePartitionsRequest, + StreamMetastorePartitionsResponse, + UpdateMetastorePartitionRequest, +) +from .partition import ( + FieldSchema, + MetastorePartition, + MetastorePartitionList, + MetastorePartitionValues, + ReadStream, + SerDeInfo, + StorageDescriptor, + StreamList, +) + +__all__ = ( + "BatchCreateMetastorePartitionsRequest", + "BatchCreateMetastorePartitionsResponse", + "BatchDeleteMetastorePartitionsRequest", + "BatchSizeTooLargeError", + "BatchUpdateMetastorePartitionsRequest", + "BatchUpdateMetastorePartitionsResponse", + "CreateMetastorePartitionRequest", + "ListMetastorePartitionsRequest", + "ListMetastorePartitionsResponse", + "StreamMetastorePartitionsRequest", + "StreamMetastorePartitionsResponse", + "UpdateMetastorePartitionRequest", + "FieldSchema", + "MetastorePartition", + "MetastorePartitionList", + "MetastorePartitionValues", + "ReadStream", + "SerDeInfo", + "StorageDescriptor", + "StreamList", +) diff --git a/google/cloud/bigquery_storage_v1alpha/types/metastore_partition.py b/google/cloud/bigquery_storage_v1alpha/types/metastore_partition.py new file mode 100644 index 00000000..e265c26d --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/types/metastore_partition.py @@ -0,0 +1,381 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from __future__ import annotations + +from typing import MutableMapping, MutableSequence + +from google.protobuf import field_mask_pb2 # type: ignore +import proto # type: ignore + +from google.cloud.bigquery_storage_v1alpha.types import partition + +__protobuf__ = proto.module( + package="google.cloud.bigquery.storage.v1alpha", + manifest={ + "CreateMetastorePartitionRequest", + "BatchCreateMetastorePartitionsRequest", + "BatchCreateMetastorePartitionsResponse", + "BatchDeleteMetastorePartitionsRequest", + "UpdateMetastorePartitionRequest", + "BatchUpdateMetastorePartitionsRequest", + "BatchUpdateMetastorePartitionsResponse", + "ListMetastorePartitionsRequest", + "ListMetastorePartitionsResponse", + "StreamMetastorePartitionsRequest", + "StreamMetastorePartitionsResponse", + "BatchSizeTooLargeError", + }, +) + + +class CreateMetastorePartitionRequest(proto.Message): + r"""Request message for CreateMetastorePartition. The + MetastorePartition is uniquely identified by values, which is an + ordered list. Hence, there is no separate name or partition id + field. + + Attributes: + parent (str): + Required. Reference to the table to where the + metastore partition to be added, in the format + of + projects/{project}/databases/{databases}/tables/{table}. + metastore_partition (google.cloud.bigquery_storage_v1alpha.types.MetastorePartition): + Required. The metastore partition to be + added. + """ + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + metastore_partition: partition.MetastorePartition = proto.Field( + proto.MESSAGE, + number=2, + message=partition.MetastorePartition, + ) + + +class BatchCreateMetastorePartitionsRequest(proto.Message): + r"""Request message for BatchCreateMetastorePartitions. + + Attributes: + parent (str): + Required. Reference to the table to where the + metastore partitions to be added, in the format + of + projects/{project}/locations/{location}/datasets/{dataset}/tables/{table}. + requests (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.CreateMetastorePartitionRequest]): + Required. Requests to add metastore + partitions to the table. + skip_existing_partitions (bool): + Optional. Mimics the ifNotExists flag in IMetaStoreClient + add_partitions(..). If the flag is set to false, the server + will return ALREADY_EXISTS if any partition already exists. + If the flag is set to true, the server will skip existing + partitions and insert only the non-existing partitions. + """ + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + requests: MutableSequence["CreateMetastorePartitionRequest"] = proto.RepeatedField( + proto.MESSAGE, + number=2, + message="CreateMetastorePartitionRequest", + ) + skip_existing_partitions: bool = proto.Field( + proto.BOOL, + number=3, + ) + + +class BatchCreateMetastorePartitionsResponse(proto.Message): + r"""Response message for BatchCreateMetastorePartitions. + + Attributes: + partitions (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.MetastorePartition]): + The list of metastore partitions that have + been created. + """ + + partitions: MutableSequence[partition.MetastorePartition] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message=partition.MetastorePartition, + ) + + +class BatchDeleteMetastorePartitionsRequest(proto.Message): + r"""Request message for BatchDeleteMetastorePartitions. The + MetastorePartition is uniquely identified by values, which is an + ordered list. Hence, there is no separate name or partition id + field. + + Attributes: + parent (str): + Required. Reference to the table to which + these metastore partitions belong, in the format + of + projects/{project}/locations/{location}/datasets/{dataset}/tables/{table}. + partition_values (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.MetastorePartitionValues]): + Required. The list of metastore partitions + (identified by its values) to be deleted. A + maximum of 100 partitions can be deleted in a + batch. + """ + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + partition_values: MutableSequence[ + partition.MetastorePartitionValues + ] = proto.RepeatedField( + proto.MESSAGE, + number=2, + message=partition.MetastorePartitionValues, + ) + + +class UpdateMetastorePartitionRequest(proto.Message): + r"""Request message for UpdateMetastorePartition. + + Attributes: + metastore_partition (google.cloud.bigquery_storage_v1alpha.types.MetastorePartition): + Required. The metastore partition to be + updated. + update_mask (google.protobuf.field_mask_pb2.FieldMask): + Optional. The list of fields to update. + """ + + metastore_partition: partition.MetastorePartition = proto.Field( + proto.MESSAGE, + number=1, + message=partition.MetastorePartition, + ) + update_mask: field_mask_pb2.FieldMask = proto.Field( + proto.MESSAGE, + number=2, + message=field_mask_pb2.FieldMask, + ) + + +class BatchUpdateMetastorePartitionsRequest(proto.Message): + r"""Request message for BatchUpdateMetastorePartitions. + + Attributes: + parent (str): + Required. Reference to the table to which + these metastore partitions belong, in the format + of + projects/{project}/locations/{location}/datasets/{dataset}/tables/{table}. + requests (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.UpdateMetastorePartitionRequest]): + Required. Requests to update metastore + partitions in the table. + """ + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + requests: MutableSequence["UpdateMetastorePartitionRequest"] = proto.RepeatedField( + proto.MESSAGE, + number=2, + message="UpdateMetastorePartitionRequest", + ) + + +class BatchUpdateMetastorePartitionsResponse(proto.Message): + r"""Response message for BatchUpdateMetastorePartitions. + + Attributes: + partitions (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.MetastorePartition]): + The list of metastore partitions that have + been updated. + """ + + partitions: MutableSequence[partition.MetastorePartition] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message=partition.MetastorePartition, + ) + + +class ListMetastorePartitionsRequest(proto.Message): + r"""Request message for ListMetastorePartitions. + + Attributes: + parent (str): + Required. Reference to the table to which + these metastore partitions belong, in the format + of + projects/{project}/locations/{location}/datasets/{dataset}/tables/{table}. + filter (str): + Optional. SQL text filtering statement, similar to a WHERE + clause in a query. Only supports single-row expressions. + Aggregate functions are not supported. + + Examples: "int_field > 5" "date_field = CAST('2014-9-27' as + DATE)" "nullable_field is not NULL" "st_equals(geo_field, + st_geofromtext("POINT(2, 2)"))" "numeric_field BETWEEN 1.0 + AND 5.0" Restricted to a maximum length for 1 MB. + """ + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + filter: str = proto.Field( + proto.STRING, + number=2, + ) + + +class ListMetastorePartitionsResponse(proto.Message): + r"""Response message for ListMetastorePartitions. + + This message has `oneof`_ fields (mutually exclusive fields). + For each oneof, at most one member field can be set at the same time. + Setting any member of the oneof automatically clears all other + members. + + .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields + + Attributes: + partitions (google.cloud.bigquery_storage_v1alpha.types.MetastorePartitionList): + The list of partitions. + + This field is a member of `oneof`_ ``response``. + streams (google.cloud.bigquery_storage_v1alpha.types.StreamList): + The list of streams. + + This field is a member of `oneof`_ ``response``. + """ + + partitions: partition.MetastorePartitionList = proto.Field( + proto.MESSAGE, + number=1, + oneof="response", + message=partition.MetastorePartitionList, + ) + streams: partition.StreamList = proto.Field( + proto.MESSAGE, + number=2, + oneof="response", + message=partition.StreamList, + ) + + +class StreamMetastorePartitionsRequest(proto.Message): + r"""The top-level message sent by the client to the + [Partitions.StreamMetastorePartitions][] method. Follows the default + gRPC streaming maximum size of 4 MB. + + Attributes: + parent (str): + Required. Reference to the table to where the + partition to be added, in the format of + projects/{project}/locations/{location}/datasets/{dataset}/tables/{table}. + metastore_partitions (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.MetastorePartition]): + Optional. A list of metastore partitions to + be added to the table. + skip_existing_partitions (bool): + Optional. Mimics the ifNotExists flag in IMetaStoreClient + add_partitions(..). If the flag is set to false, the server + will return ALREADY_EXISTS on commit if any partition + already exists. If the flag is set to true: + + 1) the server will skip existing partitions insert only the + non-existing partitions as part of the commit. + 2) The client must set the ``skip_existing_partitions`` + field to true for all requests in the stream. + """ + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + metastore_partitions: MutableSequence[ + partition.MetastorePartition + ] = proto.RepeatedField( + proto.MESSAGE, + number=2, + message=partition.MetastorePartition, + ) + skip_existing_partitions: bool = proto.Field( + proto.BOOL, + number=3, + ) + + +class StreamMetastorePartitionsResponse(proto.Message): + r"""This is the response message sent by the server to the client for + the [Partitions.StreamMetastorePartitions][] method when the commit + is successful. Server will close the stream after sending this + message. + + Attributes: + total_partitions_streamed_count (int): + Total count of partitions streamed by the + client during the lifetime of the stream. This + is only set in the final response message before + closing the stream. + total_partitions_inserted_count (int): + Total count of partitions inserted by the + server during the lifetime of the stream. This + is only set in the final response message before + closing the stream. + """ + + total_partitions_streamed_count: int = proto.Field( + proto.INT64, + number=2, + ) + total_partitions_inserted_count: int = proto.Field( + proto.INT64, + number=3, + ) + + +class BatchSizeTooLargeError(proto.Message): + r"""Structured custom error message for batch size too large + error. The error can be attached as error details in the + returned rpc Status for more structured error handling in the + client. + + Attributes: + max_batch_size (int): + The maximum number of items that are + supported in a single batch. This is returned as + a hint to the client to adjust the batch size. + error_message (str): + Optional. The error message that is returned + to the client. + """ + + max_batch_size: int = proto.Field( + proto.INT64, + number=1, + ) + error_message: str = proto.Field( + proto.STRING, + number=2, + ) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/bigquery_storage_v1alpha/types/partition.py b/google/cloud/bigquery_storage_v1alpha/types/partition.py new file mode 100644 index 00000000..041787dc --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/types/partition.py @@ -0,0 +1,254 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from __future__ import annotations + +from typing import MutableMapping, MutableSequence + +from google.protobuf import timestamp_pb2 # type: ignore +import proto # type: ignore + +__protobuf__ = proto.module( + package="google.cloud.bigquery.storage.v1alpha", + manifest={ + "FieldSchema", + "StorageDescriptor", + "SerDeInfo", + "MetastorePartition", + "MetastorePartitionList", + "ReadStream", + "StreamList", + "MetastorePartitionValues", + }, +) + + +class FieldSchema(proto.Message): + r"""Schema description of a metastore partition column. + + Attributes: + name (str): + Required. The name of the column. + The maximum length of the name is 1024 + characters + type_ (str): + Required. The type of the metastore partition + column. Maximum allowed length is 1024 + characters. + """ + + name: str = proto.Field( + proto.STRING, + number=1, + ) + type_: str = proto.Field( + proto.STRING, + number=2, + ) + + +class StorageDescriptor(proto.Message): + r"""Contains information about the physical storage of the data + in the metastore partition. + + Attributes: + location_uri (str): + Optional. The physical location of the metastore partition + (e.g. + ``gs://spark-dataproc-data/pangea-data/case_sensitive/`` or + ``gs://spark-dataproc-data/pangea-data/*``). + input_format (str): + Optional. Specifies the fully qualified class + name of the InputFormat (e.g. + "org.apache.hadoop.hive.ql.io.orc.OrcInputFormat"). + The maximum length is 128 characters. + output_format (str): + Optional. Specifies the fully qualified class + name of the OutputFormat (e.g. + "org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat"). + The maximum length is 128 characters. + serde_info (google.cloud.bigquery_storage_v1alpha.types.SerDeInfo): + Optional. Serializer and deserializer + information. + """ + + location_uri: str = proto.Field( + proto.STRING, + number=1, + ) + input_format: str = proto.Field( + proto.STRING, + number=2, + ) + output_format: str = proto.Field( + proto.STRING, + number=3, + ) + serde_info: "SerDeInfo" = proto.Field( + proto.MESSAGE, + number=4, + message="SerDeInfo", + ) + + +class SerDeInfo(proto.Message): + r"""Serializer and deserializer information. + + Attributes: + name (str): + Optional. Name of the SerDe. + The maximum length is 256 characters. + serialization_library (str): + Required. Specifies a fully-qualified class + name of the serialization library that is + responsible for the translation of data between + table representation and the underlying + low-level input and output format structures. + The maximum length is 256 characters. + parameters (MutableMapping[str, str]): + Optional. Key-value pairs that define the + initialization parameters for the serialization + library. Maximum size 10 Kib. + """ + + name: str = proto.Field( + proto.STRING, + number=1, + ) + serialization_library: str = proto.Field( + proto.STRING, + number=2, + ) + parameters: MutableMapping[str, str] = proto.MapField( + proto.STRING, + proto.STRING, + number=3, + ) + + +class MetastorePartition(proto.Message): + r"""Information about a Hive partition. + + Attributes: + values (MutableSequence[str]): + Required. Represents the values of the + partition keys, where each value corresponds to + a specific partition key in the order in which + the keys are defined. Each value is limited to + 1024 characters. + create_time (google.protobuf.timestamp_pb2.Timestamp): + Output only. The creation time of the + partition. + storage_descriptor (google.cloud.bigquery_storage_v1alpha.types.StorageDescriptor): + Optional. Contains information about the + physical storage of the data in the partition. + parameters (MutableMapping[str, str]): + Optional. Additional parameters or metadata + associated with the partition. Maximum size 10 + KiB. + fields (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.FieldSchema]): + Optional. List of columns. + """ + + values: MutableSequence[str] = proto.RepeatedField( + proto.STRING, + number=1, + ) + create_time: timestamp_pb2.Timestamp = proto.Field( + proto.MESSAGE, + number=2, + message=timestamp_pb2.Timestamp, + ) + storage_descriptor: "StorageDescriptor" = proto.Field( + proto.MESSAGE, + number=3, + message="StorageDescriptor", + ) + parameters: MutableMapping[str, str] = proto.MapField( + proto.STRING, + proto.STRING, + number=4, + ) + fields: MutableSequence["FieldSchema"] = proto.RepeatedField( + proto.MESSAGE, + number=5, + message="FieldSchema", + ) + + +class MetastorePartitionList(proto.Message): + r"""List of metastore partitions. + + Attributes: + partitions (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.MetastorePartition]): + Required. List of partitions. + """ + + partitions: MutableSequence["MetastorePartition"] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message="MetastorePartition", + ) + + +class ReadStream(proto.Message): + r"""Information about a single stream that is used to read + partitions. + + Attributes: + name (str): + Output only. Identifier. Name of the stream, in the form + ``projects/{project_id}/locations/{location}/sessions/{session_id}/streams/{stream_id}``. + """ + + name: str = proto.Field( + proto.STRING, + number=1, + ) + + +class StreamList(proto.Message): + r"""List of streams. + + Attributes: + streams (MutableSequence[google.cloud.bigquery_storage_v1alpha.types.ReadStream]): + Output only. List of streams. + """ + + streams: MutableSequence["ReadStream"] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message="ReadStream", + ) + + +class MetastorePartitionValues(proto.Message): + r"""Represents the values of a metastore partition. + + Attributes: + values (MutableSequence[str]): + Required. The values of the partition keys, + where each value corresponds to a specific + partition key in the order in which the keys are + defined. + """ + + values: MutableSequence[str] = proto.RepeatedField( + proto.STRING, + number=1, + ) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/owlbot.py b/owlbot.py index 96d70ea4..dba1ff61 100644 --- a/owlbot.py +++ b/owlbot.py @@ -34,78 +34,79 @@ shutil.rmtree("samples/generated_samples", ignore_errors=True) clean_up_generated_samples = False - # We don't want the generated client to be accessible through - # "google.cloud.bigquery_storage", replace it with the hand written client that - # wraps it. - assert 1 == s.replace( - library / "google/cloud/bigquery_storage/__init__.py", - f"from google\\.cloud\\.bigquery_storage_{library.name}\\.services.big_query_read.client import", - f"from google.cloud.bigquery_storage_{library.name} import", - ) - - # We also don't want to expose the async client just yet, at least not until - # it is wrapped in its own manual client class. - assert 1 == s.replace( - library / "google/cloud/bigquery_storage/__init__.py", - ( - f"from google\\.cloud\\.bigquery_storage_{library.name}\\.services.big_query_read.async_client " - r"import BigQueryReadAsyncClient\n" - ), - "", - ) - assert 1 == s.replace( - library / "google/cloud/bigquery_storage/__init__.py", - r"""["']BigQueryReadAsyncClient["'],\n""", - "", - ) - - assert 1 == s.replace( - library / "google/cloud/bigquery_storage/__init__.py", - r"""["']ArrowRecordBatch["']""", - ('"__version__",\n' ' "types",\n' " \\g<0>"), - ) - - # We want types to be accessible through the "main" library - assert 1 == s.replace( - library / "google/cloud/bigquery_storage/__init__.py", - f"from google\\.cloud\\.bigquery_storage_{library.name}\\.types\\.arrow import ArrowRecordBatch", - ( - f"from google.cloud.bigquery_storage_{library.name} import gapic_types as types\n" - "\\g<0>" - ), - ) - - # The DataFormat enum is not exposed in bigquery_storage_v1/types, add it there. - assert 1 == s.replace( - library / f"google/cloud/bigquery_storage_{library.name}*/types/__init__.py", - r"from \.stream import \(", - "\\g<0>\n DataFormat,", - ) - assert 1 == s.replace( - library / f"google/cloud/bigquery_storage_{library.name}*/types/__init__.py", - r"""["']ReadSession["']""", - '"DataFormat",\n \\g<0>', - ) - - # Expose handwritten classes AppendRowsStream and ReadRowsStream here. - assert 1 == s.replace( - library / "google/cloud/bigquery_storage/__init__.py", - f"from google.cloud.bigquery_storage_{library.name} import BigQueryReadClient", - ( - f"from google.cloud.bigquery_storage_{library.name}.writer import AppendRowsStream\n" - "\\g<0>\n" - f"from google.cloud.bigquery_storage_{library.name}.reader import ReadRowsStream\n" - ), - ) - assert 1 == s.replace( - library / f"google/cloud/bigquery_storage/__init__.py", - r"""["']ReadSession["']""", - ( - '"AppendRowsStream",\n' - ' "ReadRowsStream",\n' - ' \\g<0>' - ), - ) + if library.name != "v1alpha": + # We don't want the generated client to be accessible through + # "google.cloud.bigquery_storage", replace it with the hand written client that + # wraps it. + assert 1 == s.replace( + library / "google/cloud/bigquery_storage/__init__.py", + f"from google\\.cloud\\.bigquery_storage_{library.name}\\.services.big_query_read.client import", + f"from google.cloud.bigquery_storage_{library.name} import", + ) + + # We also don't want to expose the async client just yet, at least not until + # it is wrapped in its own manual client class. + assert 1 == s.replace( + library / "google/cloud/bigquery_storage/__init__.py", + ( + f"from google\\.cloud\\.bigquery_storage_{library.name}\\.services.big_query_read.async_client " + r"import BigQueryReadAsyncClient\n" + ), + "", + ) + assert 1 == s.replace( + library / "google/cloud/bigquery_storage/__init__.py", + r"""["']BigQueryReadAsyncClient["'],\n""", + "", + ) + + assert 1 == s.replace( + library / "google/cloud/bigquery_storage/__init__.py", + r"""["']ArrowRecordBatch["']""", + ('"__version__",\n' ' "types",\n' " \\g<0>"), + ) + + # We want types to be accessible through the "main" library + assert 1 == s.replace( + library / "google/cloud/bigquery_storage/__init__.py", + f"from google\\.cloud\\.bigquery_storage_{library.name}\\.types\\.arrow import ArrowRecordBatch", + ( + f"from google.cloud.bigquery_storage_{library.name} import gapic_types as types\n" + "\\g<0>" + ), + ) + + # The DataFormat enum is not exposed in bigquery_storage_v1/types, add it there. + assert 1 == s.replace( + library / f"google/cloud/bigquery_storage_{library.name}*/types/__init__.py", + r"from \.stream import \(", + "\\g<0>\n DataFormat,", + ) + assert 1 == s.replace( + library / f"google/cloud/bigquery_storage_{library.name}*/types/__init__.py", + r"""["']ReadSession["']""", + '"DataFormat",\n \\g<0>', + ) + + # Expose handwritten classes AppendRowsStream and ReadRowsStream here. + assert 1 == s.replace( + library / "google/cloud/bigquery_storage/__init__.py", + f"from google.cloud.bigquery_storage_{library.name} import BigQueryReadClient", + ( + f"from google.cloud.bigquery_storage_{library.name}.writer import AppendRowsStream\n" + "\\g<0>\n" + f"from google.cloud.bigquery_storage_{library.name}.reader import ReadRowsStream\n" + ), + ) + assert 1 == s.replace( + library / f"google/cloud/bigquery_storage/__init__.py", + r"""["']ReadSession["']""", + ( + '"AppendRowsStream",\n' + ' "ReadRowsStream",\n' + ' \\g<0>' + ), + ) s.move( [library], diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_async.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_async.py new file mode 100644 index 00000000..2840025e --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_async.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for BatchCreateMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchCreateMetastorePartitions_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +async def sample_batch_create_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.CreateMetastorePartitionRequest() + requests.parent = "parent_value" + requests.metastore_partition.values = ["values_value1", "values_value2"] + + request = bigquery_storage_v1alpha.BatchCreateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = await client.batch_create_metastore_partitions(request=request) + + # Handle the response + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchCreateMetastorePartitions_async] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_sync.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_sync.py new file mode 100644 index 00000000..fabe4a00 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_sync.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for BatchCreateMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchCreateMetastorePartitions_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +def sample_batch_create_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.CreateMetastorePartitionRequest() + requests.parent = "parent_value" + requests.metastore_partition.values = ["values_value1", "values_value2"] + + request = bigquery_storage_v1alpha.BatchCreateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = client.batch_create_metastore_partitions(request=request) + + # Handle the response + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchCreateMetastorePartitions_sync] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_async.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_async.py new file mode 100644 index 00000000..231e2997 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_async.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for BatchDeleteMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchDeleteMetastorePartitions_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +async def sample_batch_delete_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + partition_values = bigquery_storage_v1alpha.MetastorePartitionValues() + partition_values.values = ["values_value1", "values_value2"] + + request = bigquery_storage_v1alpha.BatchDeleteMetastorePartitionsRequest( + parent="parent_value", + partition_values=partition_values, + ) + + # Make the request + await client.batch_delete_metastore_partitions(request=request) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchDeleteMetastorePartitions_async] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_sync.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_sync.py new file mode 100644 index 00000000..74242022 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_sync.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for BatchDeleteMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchDeleteMetastorePartitions_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +def sample_batch_delete_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + partition_values = bigquery_storage_v1alpha.MetastorePartitionValues() + partition_values.values = ["values_value1", "values_value2"] + + request = bigquery_storage_v1alpha.BatchDeleteMetastorePartitionsRequest( + parent="parent_value", + partition_values=partition_values, + ) + + # Make the request + client.batch_delete_metastore_partitions(request=request) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchDeleteMetastorePartitions_sync] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_async.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_async.py new file mode 100644 index 00000000..c1608f77 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for BatchUpdateMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchUpdateMetastorePartitions_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +async def sample_batch_update_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.UpdateMetastorePartitionRequest() + requests.metastore_partition.values = ["values_value1", "values_value2"] + + request = bigquery_storage_v1alpha.BatchUpdateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = await client.batch_update_metastore_partitions(request=request) + + # Handle the response + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchUpdateMetastorePartitions_async] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_sync.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_sync.py new file mode 100644 index 00000000..8f2f2005 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_sync.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for BatchUpdateMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchUpdateMetastorePartitions_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +def sample_batch_update_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + requests = bigquery_storage_v1alpha.UpdateMetastorePartitionRequest() + requests.metastore_partition.values = ["values_value1", "values_value2"] + + request = bigquery_storage_v1alpha.BatchUpdateMetastorePartitionsRequest( + parent="parent_value", + requests=requests, + ) + + # Make the request + response = client.batch_update_metastore_partitions(request=request) + + # Handle the response + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchUpdateMetastorePartitions_sync] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_async.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_async.py new file mode 100644 index 00000000..344a5253 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_ListMetastorePartitions_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +async def sample_list_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.ListMetastorePartitionsRequest( + parent="parent_value", + ) + + # Make the request + response = await client.list_metastore_partitions(request=request) + + # Handle the response + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_ListMetastorePartitions_async] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_sync.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_sync.py new file mode 100644 index 00000000..09cb3122 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_sync.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_ListMetastorePartitions_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +def sample_list_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.ListMetastorePartitionsRequest( + parent="parent_value", + ) + + # Make the request + response = client.list_metastore_partitions(request=request) + + # Handle the response + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_ListMetastorePartitions_sync] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_async.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_async.py new file mode 100644 index 00000000..310b0c6c --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_async.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for StreamMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_StreamMetastorePartitions_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +async def sample_stream_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.StreamMetastorePartitionsRequest( + parent="parent_value", + ) + + # This method expects an iterator which contains + # 'bigquery_storage_v1alpha.StreamMetastorePartitionsRequest' objects + # Here we create a generator that yields a single `request` for + # demonstrative purposes. + requests = [request] + + def request_generator(): + for request in requests: + yield request + + # Make the request + stream = await client.stream_metastore_partitions(requests=request_generator()) + + # Handle the response + async for response in stream: + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_StreamMetastorePartitions_async] diff --git a/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_sync.py b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_sync.py new file mode 100644 index 00000000..4e4b85f3 --- /dev/null +++ b/samples/generated_samples/bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_sync.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for StreamMetastorePartitions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-bigquery-storage + + +# [START bigquerystorage_v1alpha_generated_MetastorePartitionService_StreamMetastorePartitions_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import bigquery_storage_v1alpha + + +def sample_stream_metastore_partitions(): + # Create a client + client = bigquery_storage_v1alpha.MetastorePartitionServiceClient() + + # Initialize request argument(s) + request = bigquery_storage_v1alpha.StreamMetastorePartitionsRequest( + parent="parent_value", + ) + + # This method expects an iterator which contains + # 'bigquery_storage_v1alpha.StreamMetastorePartitionsRequest' objects + # Here we create a generator that yields a single `request` for + # demonstrative purposes. + requests = [request] + + def request_generator(): + for request in requests: + yield request + + # Make the request + stream = client.stream_metastore_partitions(requests=request_generator()) + + # Handle the response + for response in stream: + print(response) + + +# [END bigquerystorage_v1alpha_generated_MetastorePartitionService_StreamMetastorePartitions_sync] diff --git a/samples/generated_samples/snippet_metadata_google.cloud.bigquery.storage.v1alpha.json b/samples/generated_samples/snippet_metadata_google.cloud.bigquery.storage.v1alpha.json new file mode 100644 index 00000000..3c29286e --- /dev/null +++ b/samples/generated_samples/snippet_metadata_google.cloud.bigquery.storage.v1alpha.json @@ -0,0 +1,782 @@ +{ + "clientLibrary": { + "apis": [ + { + "id": "google.cloud.bigquery.storage.v1alpha", + "version": "v1alpha" + } + ], + "language": "PYTHON", + "name": "google-cloud-bigquery-storage", + "version": "0.1.0" + }, + "snippets": [ + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient", + "shortName": "MetastorePartitionServiceAsyncClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient.batch_create_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.BatchCreateMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "BatchCreateMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsResponse", + "shortName": "batch_create_metastore_partitions" + }, + "description": "Sample for BatchCreateMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchCreateMetastorePartitions_async", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient", + "shortName": "MetastorePartitionServiceClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient.batch_create_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.BatchCreateMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "BatchCreateMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.bigquery_storage_v1alpha.types.BatchCreateMetastorePartitionsResponse", + "shortName": "batch_create_metastore_partitions" + }, + "description": "Sample for BatchCreateMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchCreateMetastorePartitions_sync", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_create_metastore_partitions_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient", + "shortName": "MetastorePartitionServiceAsyncClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient.batch_delete_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.BatchDeleteMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "BatchDeleteMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.BatchDeleteMetastorePartitionsRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "shortName": "batch_delete_metastore_partitions" + }, + "description": "Sample for BatchDeleteMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchDeleteMetastorePartitions_async", + "segments": [ + { + "end": 53, + "start": 27, + "type": "FULL" + }, + { + "end": 53, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 49, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 50, + "type": "REQUEST_EXECUTION" + }, + { + "end": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient", + "shortName": "MetastorePartitionServiceClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient.batch_delete_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.BatchDeleteMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "BatchDeleteMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.BatchDeleteMetastorePartitionsRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "shortName": "batch_delete_metastore_partitions" + }, + "description": "Sample for BatchDeleteMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchDeleteMetastorePartitions_sync", + "segments": [ + { + "end": 53, + "start": 27, + "type": "FULL" + }, + { + "end": 53, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 49, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 50, + "type": "REQUEST_EXECUTION" + }, + { + "end": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_delete_metastore_partitions_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient", + "shortName": "MetastorePartitionServiceAsyncClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient.batch_update_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.BatchUpdateMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "BatchUpdateMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsResponse", + "shortName": "batch_update_metastore_partitions" + }, + "description": "Sample for BatchUpdateMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchUpdateMetastorePartitions_async", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 49, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 50, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient", + "shortName": "MetastorePartitionServiceClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient.batch_update_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.BatchUpdateMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "BatchUpdateMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.bigquery_storage_v1alpha.types.BatchUpdateMetastorePartitionsResponse", + "shortName": "batch_update_metastore_partitions" + }, + "description": "Sample for BatchUpdateMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_BatchUpdateMetastorePartitions_sync", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 49, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 50, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_batch_update_metastore_partitions_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient", + "shortName": "MetastorePartitionServiceAsyncClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient.list_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.ListMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "ListMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsResponse", + "shortName": "list_metastore_partitions" + }, + "description": "Sample for ListMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_ListMetastorePartitions_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient", + "shortName": "MetastorePartitionServiceClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient.list_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.ListMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "ListMetastorePartitions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.bigquery_storage_v1alpha.types.ListMetastorePartitionsResponse", + "shortName": "list_metastore_partitions" + }, + "description": "Sample for ListMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_ListMetastorePartitions_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_list_metastore_partitions_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient", + "shortName": "MetastorePartitionServiceAsyncClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceAsyncClient.stream_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.StreamMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "StreamMetastorePartitions" + }, + "parameters": [ + { + "name": "requests", + "type": "Iterator[google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsRequest]" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "Iterable[google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsResponse]", + "shortName": "stream_metastore_partitions" + }, + "description": "Sample for StreamMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_StreamMetastorePartitions_async", + "segments": [ + { + "end": 62, + "start": 27, + "type": "FULL" + }, + { + "end": 62, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 55, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 58, + "start": 56, + "type": "REQUEST_EXECUTION" + }, + { + "end": 63, + "start": 59, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient", + "shortName": "MetastorePartitionServiceClient" + }, + "fullName": "google.cloud.bigquery_storage_v1alpha.MetastorePartitionServiceClient.stream_metastore_partitions", + "method": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService.StreamMetastorePartitions", + "service": { + "fullName": "google.cloud.bigquery.storage.v1alpha.MetastorePartitionService", + "shortName": "MetastorePartitionService" + }, + "shortName": "StreamMetastorePartitions" + }, + "parameters": [ + { + "name": "requests", + "type": "Iterator[google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsRequest]" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "Iterable[google.cloud.bigquery_storage_v1alpha.types.StreamMetastorePartitionsResponse]", + "shortName": "stream_metastore_partitions" + }, + "description": "Sample for StreamMetastorePartitions", + "file": "bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "bigquerystorage_v1alpha_generated_MetastorePartitionService_StreamMetastorePartitions_sync", + "segments": [ + { + "end": 62, + "start": 27, + "type": "FULL" + }, + { + "end": 62, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 55, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 58, + "start": 56, + "type": "REQUEST_EXECUTION" + }, + { + "end": 63, + "start": 59, + "type": "RESPONSE_HANDLING" + } + ], + "title": "bigquerystorage_v1alpha_generated_metastore_partition_service_stream_metastore_partitions_sync.py" + } + ] +} diff --git a/scripts/fixup_bigquery_storage_v1alpha_keywords.py b/scripts/fixup_bigquery_storage_v1alpha_keywords.py new file mode 100644 index 00000000..5a01e5af --- /dev/null +++ b/scripts/fixup_bigquery_storage_v1alpha_keywords.py @@ -0,0 +1,180 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import argparse +import os +import libcst as cst +import pathlib +import sys +from typing import (Any, Callable, Dict, List, Sequence, Tuple) + + +def partition( + predicate: Callable[[Any], bool], + iterator: Sequence[Any] +) -> Tuple[List[Any], List[Any]]: + """A stable, out-of-place partition.""" + results = ([], []) + + for i in iterator: + results[int(predicate(i))].append(i) + + # Returns trueList, falseList + return results[1], results[0] + + +class bigquery_storageCallTransformer(cst.CSTTransformer): + CTRL_PARAMS: Tuple[str] = ('retry', 'timeout', 'metadata') + METHOD_TO_PARAMS: Dict[str, Tuple[str]] = { + 'batch_create_metastore_partitions': ('parent', 'requests', 'skip_existing_partitions', ), + 'batch_delete_metastore_partitions': ('parent', 'partition_values', ), + 'batch_update_metastore_partitions': ('parent', 'requests', ), + 'list_metastore_partitions': ('parent', 'filter', ), + 'stream_metastore_partitions': ('parent', 'metastore_partitions', 'skip_existing_partitions', ), + } + + def leave_Call(self, original: cst.Call, updated: cst.Call) -> cst.CSTNode: + try: + key = original.func.attr.value + kword_params = self.METHOD_TO_PARAMS[key] + except (AttributeError, KeyError): + # Either not a method from the API or too convoluted to be sure. + return updated + + # If the existing code is valid, keyword args come after positional args. + # Therefore, all positional args must map to the first parameters. + args, kwargs = partition(lambda a: not bool(a.keyword), updated.args) + if any(k.keyword.value == "request" for k in kwargs): + # We've already fixed this file, don't fix it again. + return updated + + kwargs, ctrl_kwargs = partition( + lambda a: a.keyword.value not in self.CTRL_PARAMS, + kwargs + ) + + args, ctrl_args = args[:len(kword_params)], args[len(kword_params):] + ctrl_kwargs.extend(cst.Arg(value=a.value, keyword=cst.Name(value=ctrl)) + for a, ctrl in zip(ctrl_args, self.CTRL_PARAMS)) + + request_arg = cst.Arg( + value=cst.Dict([ + cst.DictElement( + cst.SimpleString("'{}'".format(name)), +cst.Element(value=arg.value) + ) + # Note: the args + kwargs looks silly, but keep in mind that + # the control parameters had to be stripped out, and that + # those could have been passed positionally or by keyword. + for name, arg in zip(kword_params, args + kwargs)]), + keyword=cst.Name("request") + ) + + return updated.with_changes( + args=[request_arg] + ctrl_kwargs + ) + + +def fix_files( + in_dir: pathlib.Path, + out_dir: pathlib.Path, + *, + transformer=bigquery_storageCallTransformer(), +): + """Duplicate the input dir to the output dir, fixing file method calls. + + Preconditions: + * in_dir is a real directory + * out_dir is a real, empty directory + """ + pyfile_gen = ( + pathlib.Path(os.path.join(root, f)) + for root, _, files in os.walk(in_dir) + for f in files if os.path.splitext(f)[1] == ".py" + ) + + for fpath in pyfile_gen: + with open(fpath, 'r') as f: + src = f.read() + + # Parse the code and insert method call fixes. + tree = cst.parse_module(src) + updated = tree.visit(transformer) + + # Create the path and directory structure for the new file. + updated_path = out_dir.joinpath(fpath.relative_to(in_dir)) + updated_path.parent.mkdir(parents=True, exist_ok=True) + + # Generate the updated source file at the corresponding path. + with open(updated_path, 'w') as f: + f.write(updated.code) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description="""Fix up source that uses the bigquery_storage client library. + +The existing sources are NOT overwritten but are copied to output_dir with changes made. + +Note: This tool operates at a best-effort level at converting positional + parameters in client method calls to keyword based parameters. + Cases where it WILL FAIL include + A) * or ** expansion in a method call. + B) Calls via function or method alias (includes free function calls) + C) Indirect or dispatched calls (e.g. the method is looked up dynamically) + + These all constitute false negatives. The tool will also detect false + positives when an API method shares a name with another method. +""") + parser.add_argument( + '-d', + '--input-directory', + required=True, + dest='input_dir', + help='the input directory to walk for python files to fix up', + ) + parser.add_argument( + '-o', + '--output-directory', + required=True, + dest='output_dir', + help='the directory to output files fixed via un-flattening', + ) + args = parser.parse_args() + input_dir = pathlib.Path(args.input_dir) + output_dir = pathlib.Path(args.output_dir) + if not input_dir.is_dir(): + print( + f"input directory '{input_dir}' does not exist or is not a directory", + file=sys.stderr, + ) + sys.exit(-1) + + if not output_dir.is_dir(): + print( + f"output directory '{output_dir}' does not exist or is not a directory", + file=sys.stderr, + ) + sys.exit(-1) + + if os.listdir(output_dir): + print( + f"output directory '{output_dir}' is not empty", + file=sys.stderr, + ) + sys.exit(-1) + + fix_files(input_dir, output_dir) diff --git a/tests/unit/gapic/bigquery_storage_v1alpha/__init__.py b/tests/unit/gapic/bigquery_storage_v1alpha/__init__.py new file mode 100644 index 00000000..8f6cf068 --- /dev/null +++ b/tests/unit/gapic/bigquery_storage_v1alpha/__init__.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# diff --git a/tests/unit/gapic/bigquery_storage_v1alpha/test_metastore_partition_service.py b/tests/unit/gapic/bigquery_storage_v1alpha/test_metastore_partition_service.py new file mode 100644 index 00000000..1dd722d3 --- /dev/null +++ b/tests/unit/gapic/bigquery_storage_v1alpha/test_metastore_partition_service.py @@ -0,0 +1,3362 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import os + +# try/except added for compatibility with python < 3.8 +try: + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock + +import math + +from google.api_core import gapic_v1, grpc_helpers, grpc_helpers_async, path_template +from google.api_core import api_core_version, client_options +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +import google.auth +from google.auth import credentials as ga_credentials +from google.auth.exceptions import MutualTLSChannelError +from google.oauth2 import service_account +from google.protobuf import field_mask_pb2 # type: ignore +from google.protobuf import timestamp_pb2 # type: ignore +import grpc +from grpc.experimental import aio +from proto.marshal.rules import wrappers +from proto.marshal.rules.dates import DurationRule, TimestampRule +import pytest + +from google.cloud.bigquery_storage_v1alpha.services.metastore_partition_service import ( + MetastorePartitionServiceAsyncClient, + MetastorePartitionServiceClient, + transports, +) +from google.cloud.bigquery_storage_v1alpha.types import metastore_partition, partition + + +def client_cert_source_callback(): + return b"cert bytes", b"key bytes" + + +# If default endpoint is localhost, then default mtls endpoint will be the same. +# This method modifies the default endpoint so the client can produce a different +# mtls endpoint for endpoint testing purposes. +def modify_default_endpoint(client): + return ( + "foo.googleapis.com" + if ("localhost" in client.DEFAULT_ENDPOINT) + else client.DEFAULT_ENDPOINT + ) + + +# If default endpoint template is localhost, then default mtls endpoint will be the same. +# This method modifies the default endpoint template so the client can produce a different +# mtls endpoint for endpoint testing purposes. +def modify_default_endpoint_template(client): + return ( + "test.{UNIVERSE_DOMAIN}" + if ("localhost" in client._DEFAULT_ENDPOINT_TEMPLATE) + else client._DEFAULT_ENDPOINT_TEMPLATE + ) + + +def test__get_default_mtls_endpoint(): + api_endpoint = "example.googleapis.com" + api_mtls_endpoint = "example.mtls.googleapis.com" + sandbox_endpoint = "example.sandbox.googleapis.com" + sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com" + non_googleapi = "api.example.com" + + assert MetastorePartitionServiceClient._get_default_mtls_endpoint(None) is None + assert ( + MetastorePartitionServiceClient._get_default_mtls_endpoint(api_endpoint) + == api_mtls_endpoint + ) + assert ( + MetastorePartitionServiceClient._get_default_mtls_endpoint(api_mtls_endpoint) + == api_mtls_endpoint + ) + assert ( + MetastorePartitionServiceClient._get_default_mtls_endpoint(sandbox_endpoint) + == sandbox_mtls_endpoint + ) + assert ( + MetastorePartitionServiceClient._get_default_mtls_endpoint( + sandbox_mtls_endpoint + ) + == sandbox_mtls_endpoint + ) + assert ( + MetastorePartitionServiceClient._get_default_mtls_endpoint(non_googleapi) + == non_googleapi + ) + + +def test__read_environment_variables(): + assert MetastorePartitionServiceClient._read_environment_variables() == ( + False, + "auto", + None, + ) + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert MetastorePartitionServiceClient._read_environment_variables() == ( + True, + "auto", + None, + ) + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert MetastorePartitionServiceClient._read_environment_variables() == ( + False, + "auto", + None, + ) + + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError) as excinfo: + MetastorePartitionServiceClient._read_environment_variables() + assert ( + str(excinfo.value) + == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" + ) + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + assert MetastorePartitionServiceClient._read_environment_variables() == ( + False, + "never", + None, + ) + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + assert MetastorePartitionServiceClient._read_environment_variables() == ( + False, + "always", + None, + ) + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}): + assert MetastorePartitionServiceClient._read_environment_variables() == ( + False, + "auto", + None, + ) + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError) as excinfo: + MetastorePartitionServiceClient._read_environment_variables() + assert ( + str(excinfo.value) + == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" + ) + + with mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}): + assert MetastorePartitionServiceClient._read_environment_variables() == ( + False, + "auto", + "foo.com", + ) + + +def test__get_client_cert_source(): + mock_provided_cert_source = mock.Mock() + mock_default_cert_source = mock.Mock() + + assert MetastorePartitionServiceClient._get_client_cert_source(None, False) is None + assert ( + MetastorePartitionServiceClient._get_client_cert_source( + mock_provided_cert_source, False + ) + is None + ) + assert ( + MetastorePartitionServiceClient._get_client_cert_source( + mock_provided_cert_source, True + ) + == mock_provided_cert_source + ) + + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", return_value=True + ): + with mock.patch( + "google.auth.transport.mtls.default_client_cert_source", + return_value=mock_default_cert_source, + ): + assert ( + MetastorePartitionServiceClient._get_client_cert_source(None, True) + is mock_default_cert_source + ) + assert ( + MetastorePartitionServiceClient._get_client_cert_source( + mock_provided_cert_source, "true" + ) + is mock_provided_cert_source + ) + + +@mock.patch.object( + MetastorePartitionServiceClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceClient), +) +@mock.patch.object( + MetastorePartitionServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceAsyncClient), +) +def test__get_api_endpoint(): + api_override = "foo.com" + mock_client_cert_source = mock.Mock() + default_universe = MetastorePartitionServiceClient._DEFAULT_UNIVERSE + default_endpoint = ( + MetastorePartitionServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=default_universe + ) + ) + mock_universe = "bar.com" + mock_endpoint = MetastorePartitionServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=mock_universe + ) + + assert ( + MetastorePartitionServiceClient._get_api_endpoint( + api_override, mock_client_cert_source, default_universe, "always" + ) + == api_override + ) + assert ( + MetastorePartitionServiceClient._get_api_endpoint( + None, mock_client_cert_source, default_universe, "auto" + ) + == MetastorePartitionServiceClient.DEFAULT_MTLS_ENDPOINT + ) + assert ( + MetastorePartitionServiceClient._get_api_endpoint( + None, None, default_universe, "auto" + ) + == default_endpoint + ) + assert ( + MetastorePartitionServiceClient._get_api_endpoint( + None, None, default_universe, "always" + ) + == MetastorePartitionServiceClient.DEFAULT_MTLS_ENDPOINT + ) + assert ( + MetastorePartitionServiceClient._get_api_endpoint( + None, mock_client_cert_source, default_universe, "always" + ) + == MetastorePartitionServiceClient.DEFAULT_MTLS_ENDPOINT + ) + assert ( + MetastorePartitionServiceClient._get_api_endpoint( + None, None, mock_universe, "never" + ) + == mock_endpoint + ) + assert ( + MetastorePartitionServiceClient._get_api_endpoint( + None, None, default_universe, "never" + ) + == default_endpoint + ) + + with pytest.raises(MutualTLSChannelError) as excinfo: + MetastorePartitionServiceClient._get_api_endpoint( + None, mock_client_cert_source, mock_universe, "auto" + ) + assert ( + str(excinfo.value) + == "mTLS is not supported in any universe other than googleapis.com." + ) + + +def test__get_universe_domain(): + client_universe_domain = "foo.com" + universe_domain_env = "bar.com" + + assert ( + MetastorePartitionServiceClient._get_universe_domain( + client_universe_domain, universe_domain_env + ) + == client_universe_domain + ) + assert ( + MetastorePartitionServiceClient._get_universe_domain(None, universe_domain_env) + == universe_domain_env + ) + assert ( + MetastorePartitionServiceClient._get_universe_domain(None, None) + == MetastorePartitionServiceClient._DEFAULT_UNIVERSE + ) + + with pytest.raises(ValueError) as excinfo: + MetastorePartitionServiceClient._get_universe_domain("", None) + assert str(excinfo.value) == "Universe Domain cannot be an empty string." + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + "grpc", + ), + ], +) +def test__validate_universe_domain(client_class, transport_class, transport_name): + client = client_class( + transport=transport_class(credentials=ga_credentials.AnonymousCredentials()) + ) + assert client._validate_universe_domain() == True + + # Test the case when universe is already validated. + assert client._validate_universe_domain() == True + + if transport_name == "grpc": + # Test the case where credentials are provided by the + # `local_channel_credentials`. The default universes in both match. + channel = grpc.secure_channel( + "http://localhost/", grpc.local_channel_credentials() + ) + client = client_class(transport=transport_class(channel=channel)) + assert client._validate_universe_domain() == True + + # Test the case where credentials do not exist: e.g. a transport is provided + # with no credentials. Validation should still succeed because there is no + # mismatch with non-existent credentials. + channel = grpc.secure_channel( + "http://localhost/", grpc.local_channel_credentials() + ) + transport = transport_class(channel=channel) + transport._credentials = None + client = client_class(transport=transport) + assert client._validate_universe_domain() == True + + # TODO: This is needed to cater for older versions of google-auth + # Make this test unconditional once the minimum supported version of + # google-auth becomes 2.23.0 or higher. + google_auth_major, google_auth_minor = [ + int(part) for part in google.auth.__version__.split(".")[0:2] + ] + if google_auth_major > 2 or (google_auth_major == 2 and google_auth_minor >= 23): + credentials = ga_credentials.AnonymousCredentials() + credentials._universe_domain = "foo.com" + # Test the case when there is a universe mismatch from the credentials. + client = client_class(transport=transport_class(credentials=credentials)) + with pytest.raises(ValueError) as excinfo: + client._validate_universe_domain() + assert ( + str(excinfo.value) + == "The configured universe domain (googleapis.com) does not match the universe domain found in the credentials (foo.com). If you haven't configured the universe domain explicitly, `googleapis.com` is the default." + ) + + # Test the case when there is a universe mismatch from the client. + # + # TODO: Make this test unconditional once the minimum supported version of + # google-api-core becomes 2.15.0 or higher. + api_core_major, api_core_minor = [ + int(part) for part in api_core_version.__version__.split(".")[0:2] + ] + if api_core_major > 2 or (api_core_major == 2 and api_core_minor >= 15): + client = client_class( + client_options={"universe_domain": "bar.com"}, + transport=transport_class( + credentials=ga_credentials.AnonymousCredentials(), + ), + ) + with pytest.raises(ValueError) as excinfo: + client._validate_universe_domain() + assert ( + str(excinfo.value) + == "The configured universe domain (bar.com) does not match the universe domain found in the credentials (googleapis.com). If you haven't configured the universe domain explicitly, `googleapis.com` is the default." + ) + + # Test that ValueError is raised if universe_domain is provided via client options and credentials is None + with pytest.raises(ValueError): + client._compare_universes("foo.bar", None) + + +@pytest.mark.parametrize( + "client_class,transport_name", + [ + (MetastorePartitionServiceClient, "grpc"), + (MetastorePartitionServiceAsyncClient, "grpc_asyncio"), + ], +) +def test_metastore_partition_service_client_from_service_account_info( + client_class, transport_name +): + creds = ga_credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_info" + ) as factory: + factory.return_value = creds + info = {"valid": True} + client = client_class.from_service_account_info(info, transport=transport_name) + assert client.transport._credentials == creds + assert isinstance(client, client_class) + + assert client.transport._host == ("bigquerystorage.googleapis.com:443") + + +@pytest.mark.parametrize( + "transport_class,transport_name", + [ + (transports.MetastorePartitionServiceGrpcTransport, "grpc"), + (transports.MetastorePartitionServiceGrpcAsyncIOTransport, "grpc_asyncio"), + ], +) +def test_metastore_partition_service_client_service_account_always_use_jwt( + transport_class, transport_name +): + with mock.patch.object( + service_account.Credentials, "with_always_use_jwt_access", create=True + ) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=True) + use_jwt.assert_called_once_with(True) + + with mock.patch.object( + service_account.Credentials, "with_always_use_jwt_access", create=True + ) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + + +@pytest.mark.parametrize( + "client_class,transport_name", + [ + (MetastorePartitionServiceClient, "grpc"), + (MetastorePartitionServiceAsyncClient, "grpc_asyncio"), + ], +) +def test_metastore_partition_service_client_from_service_account_file( + client_class, transport_name +): + creds = ga_credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_file" + ) as factory: + factory.return_value = creds + client = client_class.from_service_account_file( + "dummy/file/path.json", transport=transport_name + ) + assert client.transport._credentials == creds + assert isinstance(client, client_class) + + client = client_class.from_service_account_json( + "dummy/file/path.json", transport=transport_name + ) + assert client.transport._credentials == creds + assert isinstance(client, client_class) + + assert client.transport._host == ("bigquerystorage.googleapis.com:443") + + +def test_metastore_partition_service_client_get_transport_class(): + transport = MetastorePartitionServiceClient.get_transport_class() + available_transports = [ + transports.MetastorePartitionServiceGrpcTransport, + ] + assert transport in available_transports + + transport = MetastorePartitionServiceClient.get_transport_class("grpc") + assert transport == transports.MetastorePartitionServiceGrpcTransport + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + "grpc", + ), + ( + MetastorePartitionServiceAsyncClient, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +@mock.patch.object( + MetastorePartitionServiceClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceClient), +) +@mock.patch.object( + MetastorePartitionServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceAsyncClient), +) +def test_metastore_partition_service_client_client_options( + client_class, transport_class, transport_name +): + # Check that if channel is provided we won't create a new one. + with mock.patch.object( + MetastorePartitionServiceClient, "get_transport_class" + ) as gtc: + transport = transport_class(credentials=ga_credentials.AnonymousCredentials()) + client = client_class(transport=transport) + gtc.assert_not_called() + + # Check that if channel is provided via str we will create a new one. + with mock.patch.object( + MetastorePartitionServiceClient, "get_transport_class" + ) as gtc: + client = client_class(transport=transport_name) + gtc.assert_called() + + # Check the case api_endpoint is provided. + options = client_options.ClientOptions(api_endpoint="squid.clam.whelk") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(transport=transport_name, client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "never". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "always". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_MTLS_ENDPOINT, + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has + # unsupported value. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError) as excinfo: + client = client_class(transport=transport_name) + assert ( + str(excinfo.value) + == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" + ) + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError) as excinfo: + client = client_class(transport=transport_name) + assert ( + str(excinfo.value) + == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" + ) + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + # Check the case api_endpoint is provided + options = client_options.ClientOptions( + api_audience="https://language.googleapis.com" + ) + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience="https://language.googleapis.com", + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + "grpc", + "true", + ), + ( + MetastorePartitionServiceAsyncClient, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + "grpc", + "false", + ), + ( + MetastorePartitionServiceAsyncClient, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + MetastorePartitionServiceClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceClient), +) +@mock.patch.object( + MetastorePartitionServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceAsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_metastore_partition_service_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. + + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + options = client_options.ClientOptions( + client_cert_source=client_cert_source_callback + ) + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + + if use_client_cert_env == "false": + expected_client_cert_source = None + expected_host = client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ) + else: + expected_client_cert_source = client_cert_source_callback + expected_host = client.DEFAULT_MTLS_ENDPOINT + + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + client_cert_source_for_mtls=expected_client_cert_source, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=True, + ): + with mock.patch( + "google.auth.transport.mtls.default_client_cert_source", + return_value=client_cert_source_callback, + ): + if use_client_cert_env == "false": + expected_host = client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ) + expected_client_cert_source = None + else: + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_client_cert_source = client_cert_source_callback + + patched.return_value = None + client = client_class(transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + client_cert_source_for_mtls=expected_client_cert_source, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=False, + ): + patched.return_value = None + client = client_class(transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class", + [MetastorePartitionServiceClient, MetastorePartitionServiceAsyncClient], +) +@mock.patch.object( + MetastorePartitionServiceClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(MetastorePartitionServiceClient), +) +@mock.patch.object( + MetastorePartitionServiceAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(MetastorePartitionServiceAsyncClient), +) +def test_metastore_partition_service_client_get_mtls_endpoint_and_cert_source( + client_class, +): + mock_client_cert_source = mock.Mock() + + # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + mock_api_endpoint = "foo" + options = client_options.ClientOptions( + client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint + ) + api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source( + options + ) + assert api_endpoint == mock_api_endpoint + assert cert_source == mock_client_cert_source + + # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + mock_client_cert_source = mock.Mock() + mock_api_endpoint = "foo" + options = client_options.ClientOptions( + client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint + ) + api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source( + options + ) + assert api_endpoint == mock_api_endpoint + assert cert_source is None + + # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source() + assert api_endpoint == client_class.DEFAULT_ENDPOINT + assert cert_source is None + + # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source() + assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT + assert cert_source is None + + # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=False, + ): + api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source() + assert api_endpoint == client_class.DEFAULT_ENDPOINT + assert cert_source is None + + # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=True, + ): + with mock.patch( + "google.auth.transport.mtls.default_client_cert_source", + return_value=mock_client_cert_source, + ): + ( + api_endpoint, + cert_source, + ) = client_class.get_mtls_endpoint_and_cert_source() + assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT + assert cert_source == mock_client_cert_source + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has + # unsupported value. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError) as excinfo: + client_class.get_mtls_endpoint_and_cert_source() + + assert ( + str(excinfo.value) + == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" + ) + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError) as excinfo: + client_class.get_mtls_endpoint_and_cert_source() + + assert ( + str(excinfo.value) + == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" + ) + + +@pytest.mark.parametrize( + "client_class", + [MetastorePartitionServiceClient, MetastorePartitionServiceAsyncClient], +) +@mock.patch.object( + MetastorePartitionServiceClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceClient), +) +@mock.patch.object( + MetastorePartitionServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(MetastorePartitionServiceAsyncClient), +) +def test_metastore_partition_service_client_client_api_endpoint(client_class): + mock_client_cert_source = client_cert_source_callback + api_override = "foo.com" + default_universe = MetastorePartitionServiceClient._DEFAULT_UNIVERSE + default_endpoint = ( + MetastorePartitionServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=default_universe + ) + ) + mock_universe = "bar.com" + mock_endpoint = MetastorePartitionServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=mock_universe + ) + + # If ClientOptions.api_endpoint is set and GOOGLE_API_USE_CLIENT_CERTIFICATE="true", + # use ClientOptions.api_endpoint as the api endpoint regardless. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ): + options = client_options.ClientOptions( + client_cert_source=mock_client_cert_source, api_endpoint=api_override + ) + client = client_class( + client_options=options, + credentials=ga_credentials.AnonymousCredentials(), + ) + assert client.api_endpoint == api_override + + # If ClientOptions.api_endpoint is not set and GOOGLE_API_USE_MTLS_ENDPOINT="never", + # use the _DEFAULT_ENDPOINT_TEMPLATE populated with GDU as the api endpoint. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + client = client_class(credentials=ga_credentials.AnonymousCredentials()) + assert client.api_endpoint == default_endpoint + + # If ClientOptions.api_endpoint is not set and GOOGLE_API_USE_MTLS_ENDPOINT="always", + # use the DEFAULT_MTLS_ENDPOINT as the api endpoint. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + client = client_class(credentials=ga_credentials.AnonymousCredentials()) + assert client.api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT + + # If ClientOptions.api_endpoint is not set, GOOGLE_API_USE_MTLS_ENDPOINT="auto" (default), + # GOOGLE_API_USE_CLIENT_CERTIFICATE="false" (default), default cert source doesn't exist, + # and ClientOptions.universe_domain="bar.com", + # use the _DEFAULT_ENDPOINT_TEMPLATE populated with universe domain as the api endpoint. + options = client_options.ClientOptions() + universe_exists = hasattr(options, "universe_domain") + if universe_exists: + options = client_options.ClientOptions(universe_domain=mock_universe) + client = client_class( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + else: + client = client_class( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + assert client.api_endpoint == ( + mock_endpoint if universe_exists else default_endpoint + ) + assert client.universe_domain == ( + mock_universe if universe_exists else default_universe + ) + + # If ClientOptions does not have a universe domain attribute and GOOGLE_API_USE_MTLS_ENDPOINT="never", + # use the _DEFAULT_ENDPOINT_TEMPLATE populated with GDU as the api endpoint. + options = client_options.ClientOptions() + if hasattr(options, "universe_domain"): + delattr(options, "universe_domain") + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + client = client_class( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + assert client.api_endpoint == default_endpoint + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + "grpc", + ), + ( + MetastorePartitionServiceAsyncClient, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +def test_metastore_partition_service_client_client_options_scopes( + client_class, transport_class, transport_name +): + # Check the case scopes are provided. + options = client_options.ClientOptions( + scopes=["1", "2"], + ) + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=["1", "2"], + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + MetastorePartitionServiceAsyncClient, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_metastore_partition_service_client_client_options_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +def test_metastore_partition_service_client_client_options_from_dict(): + with mock.patch( + "google.cloud.bigquery_storage_v1alpha.services.metastore_partition_service.transports.MetastorePartitionServiceGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = MetastorePartitionServiceClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + MetastorePartitionServiceAsyncClient, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_metastore_partition_service_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "bigquerystorage.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=( + "https://www.googleapis.com/auth/bigquery", + "https://www.googleapis.com/auth/cloud-platform", + ), + scopes=None, + default_host="bigquerystorage.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + metastore_partition.BatchCreateMetastorePartitionsRequest, + dict, + ], +) +def test_batch_create_metastore_partitions(request_type, transport: str = "grpc"): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = metastore_partition.BatchCreateMetastorePartitionsResponse() + response = client.batch_create_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = metastore_partition.BatchCreateMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, metastore_partition.BatchCreateMetastorePartitionsResponse + ) + + +def test_batch_create_metastore_partitions_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.batch_create_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchCreateMetastorePartitionsRequest() + + +def test_batch_create_metastore_partitions_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = metastore_partition.BatchCreateMetastorePartitionsRequest( + parent="parent_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.batch_create_metastore_partitions(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchCreateMetastorePartitionsRequest( + parent="parent_value", + ) + + +def test_batch_create_metastore_partitions_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.batch_create_metastore_partitions + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.batch_create_metastore_partitions + ] = mock_rpc + request = {} + client.batch_create_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.batch_create_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_batch_create_metastore_partitions_empty_call_async(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchCreateMetastorePartitionsResponse() + ) + response = await client.batch_create_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchCreateMetastorePartitionsRequest() + + +@pytest.mark.asyncio +async def test_batch_create_metastore_partitions_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.batch_create_metastore_partitions + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.batch_create_metastore_partitions + ] = mock_rpc + + request = {} + await client.batch_create_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.batch_create_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_batch_create_metastore_partitions_async( + transport: str = "grpc_asyncio", + request_type=metastore_partition.BatchCreateMetastorePartitionsRequest, +): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchCreateMetastorePartitionsResponse() + ) + response = await client.batch_create_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = metastore_partition.BatchCreateMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, metastore_partition.BatchCreateMetastorePartitionsResponse + ) + + +@pytest.mark.asyncio +async def test_batch_create_metastore_partitions_async_from_dict(): + await test_batch_create_metastore_partitions_async(request_type=dict) + + +def test_batch_create_metastore_partitions_field_headers(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.BatchCreateMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + call.return_value = metastore_partition.BatchCreateMetastorePartitionsResponse() + client.batch_create_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_batch_create_metastore_partitions_field_headers_async(): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.BatchCreateMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchCreateMetastorePartitionsResponse() + ) + await client.batch_create_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.parametrize( + "request_type", + [ + metastore_partition.BatchDeleteMetastorePartitionsRequest, + dict, + ], +) +def test_batch_delete_metastore_partitions(request_type, transport: str = "grpc"): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.batch_delete_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = metastore_partition.BatchDeleteMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_batch_delete_metastore_partitions_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.batch_delete_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchDeleteMetastorePartitionsRequest() + + +def test_batch_delete_metastore_partitions_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = metastore_partition.BatchDeleteMetastorePartitionsRequest( + parent="parent_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.batch_delete_metastore_partitions(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchDeleteMetastorePartitionsRequest( + parent="parent_value", + ) + + +def test_batch_delete_metastore_partitions_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.batch_delete_metastore_partitions + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.batch_delete_metastore_partitions + ] = mock_rpc + request = {} + client.batch_delete_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.batch_delete_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_batch_delete_metastore_partitions_empty_call_async(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.batch_delete_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchDeleteMetastorePartitionsRequest() + + +@pytest.mark.asyncio +async def test_batch_delete_metastore_partitions_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.batch_delete_metastore_partitions + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.batch_delete_metastore_partitions + ] = mock_rpc + + request = {} + await client.batch_delete_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.batch_delete_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_batch_delete_metastore_partitions_async( + transport: str = "grpc_asyncio", + request_type=metastore_partition.BatchDeleteMetastorePartitionsRequest, +): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.batch_delete_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = metastore_partition.BatchDeleteMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_batch_delete_metastore_partitions_async_from_dict(): + await test_batch_delete_metastore_partitions_async(request_type=dict) + + +def test_batch_delete_metastore_partitions_field_headers(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.BatchDeleteMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + call.return_value = None + client.batch_delete_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_batch_delete_metastore_partitions_field_headers_async(): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.BatchDeleteMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.batch_delete_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.parametrize( + "request_type", + [ + metastore_partition.BatchUpdateMetastorePartitionsRequest, + dict, + ], +) +def test_batch_update_metastore_partitions(request_type, transport: str = "grpc"): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = metastore_partition.BatchUpdateMetastorePartitionsResponse() + response = client.batch_update_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = metastore_partition.BatchUpdateMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, metastore_partition.BatchUpdateMetastorePartitionsResponse + ) + + +def test_batch_update_metastore_partitions_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.batch_update_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchUpdateMetastorePartitionsRequest() + + +def test_batch_update_metastore_partitions_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = metastore_partition.BatchUpdateMetastorePartitionsRequest( + parent="parent_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.batch_update_metastore_partitions(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchUpdateMetastorePartitionsRequest( + parent="parent_value", + ) + + +def test_batch_update_metastore_partitions_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.batch_update_metastore_partitions + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.batch_update_metastore_partitions + ] = mock_rpc + request = {} + client.batch_update_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.batch_update_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_batch_update_metastore_partitions_empty_call_async(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchUpdateMetastorePartitionsResponse() + ) + response = await client.batch_update_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.BatchUpdateMetastorePartitionsRequest() + + +@pytest.mark.asyncio +async def test_batch_update_metastore_partitions_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.batch_update_metastore_partitions + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.batch_update_metastore_partitions + ] = mock_rpc + + request = {} + await client.batch_update_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.batch_update_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_batch_update_metastore_partitions_async( + transport: str = "grpc_asyncio", + request_type=metastore_partition.BatchUpdateMetastorePartitionsRequest, +): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchUpdateMetastorePartitionsResponse() + ) + response = await client.batch_update_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = metastore_partition.BatchUpdateMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, metastore_partition.BatchUpdateMetastorePartitionsResponse + ) + + +@pytest.mark.asyncio +async def test_batch_update_metastore_partitions_async_from_dict(): + await test_batch_update_metastore_partitions_async(request_type=dict) + + +def test_batch_update_metastore_partitions_field_headers(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.BatchUpdateMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + call.return_value = metastore_partition.BatchUpdateMetastorePartitionsResponse() + client.batch_update_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_batch_update_metastore_partitions_field_headers_async(): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.BatchUpdateMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchUpdateMetastorePartitionsResponse() + ) + await client.batch_update_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.parametrize( + "request_type", + [ + metastore_partition.ListMetastorePartitionsRequest, + dict, + ], +) +def test_list_metastore_partitions(request_type, transport: str = "grpc"): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = metastore_partition.ListMetastorePartitionsResponse() + response = client.list_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = metastore_partition.ListMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, metastore_partition.ListMetastorePartitionsResponse) + + +def test_list_metastore_partitions_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.ListMetastorePartitionsRequest() + + +def test_list_metastore_partitions_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = metastore_partition.ListMetastorePartitionsRequest( + parent="parent_value", + filter="filter_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_metastore_partitions(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.ListMetastorePartitionsRequest( + parent="parent_value", + filter="filter_value", + ) + + +def test_list_metastore_partitions_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_metastore_partitions + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_metastore_partitions + ] = mock_rpc + request = {} + client.list_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_metastore_partitions_empty_call_async(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.ListMetastorePartitionsResponse() + ) + response = await client.list_metastore_partitions() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == metastore_partition.ListMetastorePartitionsRequest() + + +@pytest.mark.asyncio +async def test_list_metastore_partitions_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.list_metastore_partitions + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_metastore_partitions + ] = mock_rpc + + request = {} + await client.list_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.list_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_metastore_partitions_async( + transport: str = "grpc_asyncio", + request_type=metastore_partition.ListMetastorePartitionsRequest, +): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.ListMetastorePartitionsResponse() + ) + response = await client.list_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = metastore_partition.ListMetastorePartitionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, metastore_partition.ListMetastorePartitionsResponse) + + +@pytest.mark.asyncio +async def test_list_metastore_partitions_async_from_dict(): + await test_list_metastore_partitions_async(request_type=dict) + + +def test_list_metastore_partitions_field_headers(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.ListMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + call.return_value = metastore_partition.ListMetastorePartitionsResponse() + client.list_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_metastore_partitions_field_headers_async(): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = metastore_partition.ListMetastorePartitionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.ListMetastorePartitionsResponse() + ) + await client.list_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_metastore_partitions_flattened(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = metastore_partition.ListMetastorePartitionsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_metastore_partitions( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +def test_list_metastore_partitions_flattened_error(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_metastore_partitions( + metastore_partition.ListMetastorePartitionsRequest(), + parent="parent_value", + ) + + +@pytest.mark.asyncio +async def test_list_metastore_partitions_flattened_async(): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = metastore_partition.ListMetastorePartitionsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.ListMetastorePartitionsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_metastore_partitions( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_metastore_partitions_flattened_error_async(): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_metastore_partitions( + metastore_partition.ListMetastorePartitionsRequest(), + parent="parent_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + metastore_partition.StreamMetastorePartitionsRequest, + dict, + ], +) +def test_stream_metastore_partitions(request_type, transport: str = "grpc"): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + requests = [request] + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.stream_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = iter( + [metastore_partition.StreamMetastorePartitionsResponse()] + ) + response = client.stream_metastore_partitions(iter(requests)) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert next(args[0]) == request + + # Establish that the response is the type that we expect. + for message in response: + assert isinstance( + message, metastore_partition.StreamMetastorePartitionsResponse + ) + + +def test_stream_metastore_partitions_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.stream_metastore_partitions + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.stream_metastore_partitions + ] = mock_rpc + request = [{}] + client.stream_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.stream_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_stream_metastore_partitions_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.stream_metastore_partitions + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.stream_metastore_partitions + ] = mock_rpc + + request = [{}] + await client.stream_metastore_partitions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.stream_metastore_partitions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_stream_metastore_partitions_async( + transport: str = "grpc_asyncio", + request_type=metastore_partition.StreamMetastorePartitionsRequest, +): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + requests = [request] + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.stream_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = mock.Mock(aio.StreamStreamCall, autospec=True) + call.return_value.read = mock.AsyncMock( + side_effect=[metastore_partition.StreamMetastorePartitionsResponse()] + ) + response = await client.stream_metastore_partitions(iter(requests)) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert next(args[0]) == request + + # Establish that the response is the type that we expect. + message = await response.read() + assert isinstance(message, metastore_partition.StreamMetastorePartitionsResponse) + + +@pytest.mark.asyncio +async def test_stream_metastore_partitions_async_from_dict(): + await test_stream_metastore_partitions_async(request_type=dict) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.MetastorePartitionServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.MetastorePartitionServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = MetastorePartitionServiceClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.MetastorePartitionServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = MetastorePartitionServiceClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = MetastorePartitionServiceClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.MetastorePartitionServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = MetastorePartitionServiceClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.MetastorePartitionServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = MetastorePartitionServiceClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.MetastorePartitionServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.MetastorePartitionServiceGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.MetastorePartitionServiceGrpcTransport, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + ], +) +def test_transport_kind(transport_name): + transport = MetastorePartitionServiceClient.get_transport_class(transport_name)( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert transport.kind == transport_name + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.MetastorePartitionServiceGrpcTransport, + ) + + +def test_metastore_partition_service_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.MetastorePartitionServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_metastore_partition_service_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.bigquery_storage_v1alpha.services.metastore_partition_service.transports.MetastorePartitionServiceTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.MetastorePartitionServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "batch_create_metastore_partitions", + "batch_delete_metastore_partitions", + "batch_update_metastore_partitions", + "list_metastore_partitions", + "stream_metastore_partitions", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_metastore_partition_service_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.bigquery_storage_v1alpha.services.metastore_partition_service.transports.MetastorePartitionServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.MetastorePartitionServiceTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=( + "https://www.googleapis.com/auth/bigquery", + "https://www.googleapis.com/auth/cloud-platform", + ), + quota_project_id="octopus", + ) + + +def test_metastore_partition_service_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.bigquery_storage_v1alpha.services.metastore_partition_service.transports.MetastorePartitionServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.MetastorePartitionServiceTransport() + adc.assert_called_once() + + +def test_metastore_partition_service_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + MetastorePartitionServiceClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=( + "https://www.googleapis.com/auth/bigquery", + "https://www.googleapis.com/auth/cloud-platform", + ), + quota_project_id=None, + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.MetastorePartitionServiceGrpcTransport, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + ], +) +def test_metastore_partition_service_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=( + "https://www.googleapis.com/auth/bigquery", + "https://www.googleapis.com/auth/cloud-platform", + ), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.MetastorePartitionServiceGrpcTransport, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + ], +) +def test_metastore_partition_service_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.MetastorePartitionServiceGrpcTransport, grpc_helpers), + (transports.MetastorePartitionServiceGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_metastore_partition_service_transport_create_channel( + transport_class, grpc_helpers +): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "bigquerystorage.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=( + "https://www.googleapis.com/auth/bigquery", + "https://www.googleapis.com/auth/cloud-platform", + ), + scopes=["1", "2"], + default_host="bigquerystorage.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.MetastorePartitionServiceGrpcTransport, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + ], +) +def test_metastore_partition_service_grpc_transport_client_cert_source_for_mtls( + transport_class, +): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + ], +) +def test_metastore_partition_service_host_no_port(transport_name): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="bigquerystorage.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ("bigquerystorage.googleapis.com:443") + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + ], +) +def test_metastore_partition_service_host_with_port(transport_name): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="bigquerystorage.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ("bigquerystorage.googleapis.com:8000") + + +def test_metastore_partition_service_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.MetastorePartitionServiceGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_metastore_partition_service_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.MetastorePartitionServiceGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.MetastorePartitionServiceGrpcTransport, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + ], +) +def test_metastore_partition_service_transport_channel_mtls_with_client_cert_source( + transport_class, +): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.MetastorePartitionServiceGrpcTransport, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + ], +) +def test_metastore_partition_service_transport_channel_mtls_with_adc(transport_class): + mock_ssl_cred = mock.Mock() + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel" + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_read_stream_path(): + project = "squid" + location = "clam" + session = "whelk" + stream = "octopus" + expected = "projects/{project}/locations/{location}/sessions/{session}/streams/{stream}".format( + project=project, + location=location, + session=session, + stream=stream, + ) + actual = MetastorePartitionServiceClient.read_stream_path( + project, location, session, stream + ) + assert expected == actual + + +def test_parse_read_stream_path(): + expected = { + "project": "oyster", + "location": "nudibranch", + "session": "cuttlefish", + "stream": "mussel", + } + path = MetastorePartitionServiceClient.read_stream_path(**expected) + + # Check that the path construction is reversible. + actual = MetastorePartitionServiceClient.parse_read_stream_path(path) + assert expected == actual + + +def test_table_path(): + project = "winkle" + dataset = "nautilus" + table = "scallop" + expected = "projects/{project}/datasets/{dataset}/tables/{table}".format( + project=project, + dataset=dataset, + table=table, + ) + actual = MetastorePartitionServiceClient.table_path(project, dataset, table) + assert expected == actual + + +def test_parse_table_path(): + expected = { + "project": "abalone", + "dataset": "squid", + "table": "clam", + } + path = MetastorePartitionServiceClient.table_path(**expected) + + # Check that the path construction is reversible. + actual = MetastorePartitionServiceClient.parse_table_path(path) + assert expected == actual + + +def test_common_billing_account_path(): + billing_account = "whelk" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + actual = MetastorePartitionServiceClient.common_billing_account_path( + billing_account + ) + assert expected == actual + + +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "octopus", + } + path = MetastorePartitionServiceClient.common_billing_account_path(**expected) + + # Check that the path construction is reversible. + actual = MetastorePartitionServiceClient.parse_common_billing_account_path(path) + assert expected == actual + + +def test_common_folder_path(): + folder = "oyster" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = MetastorePartitionServiceClient.common_folder_path(folder) + assert expected == actual + + +def test_parse_common_folder_path(): + expected = { + "folder": "nudibranch", + } + path = MetastorePartitionServiceClient.common_folder_path(**expected) + + # Check that the path construction is reversible. + actual = MetastorePartitionServiceClient.parse_common_folder_path(path) + assert expected == actual + + +def test_common_organization_path(): + organization = "cuttlefish" + expected = "organizations/{organization}".format( + organization=organization, + ) + actual = MetastorePartitionServiceClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "mussel", + } + path = MetastorePartitionServiceClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = MetastorePartitionServiceClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "winkle" + expected = "projects/{project}".format( + project=project, + ) + actual = MetastorePartitionServiceClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "nautilus", + } + path = MetastorePartitionServiceClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = MetastorePartitionServiceClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "scallop" + location = "abalone" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + actual = MetastorePartitionServiceClient.common_location_path(project, location) + assert expected == actual + + +def test_parse_common_location_path(): + expected = { + "project": "squid", + "location": "clam", + } + path = MetastorePartitionServiceClient.common_location_path(**expected) + + # Check that the path construction is reversible. + actual = MetastorePartitionServiceClient.parse_common_location_path(path) + assert expected == actual + + +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() + + with mock.patch.object( + transports.MetastorePartitionServiceTransport, "_prep_wrapped_messages" + ) as prep: + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.MetastorePartitionServiceTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = MetastorePartitionServiceClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = MetastorePartitionServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object( + type(getattr(client.transport, "grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + +def test_transport_close(): + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() + + +@pytest.mark.parametrize( + "client_class,transport_class", + [ + ( + MetastorePartitionServiceClient, + transports.MetastorePartitionServiceGrpcTransport, + ), + ( + MetastorePartitionServiceAsyncClient, + transports.MetastorePartitionServiceGrpcAsyncIOTransport, + ), + ], +) +def test_api_key_credentials(client_class, transport_class): + with mock.patch.object( + google.auth._default, "get_api_key_credentials", create=True + ) as get_api_key_credentials: + mock_cred = mock.Mock() + get_api_key_credentials.return_value = mock_cred + options = client_options.ClientOptions() + options.api_key = "api_key" + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=mock_cred, + credentials_file=None, + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + )