|
| 1 | +# Copyright 2025 Google LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# -*- coding: utf-8 -*- |
| 16 | +# Copyright 2025 Google LLC |
| 17 | +# |
| 18 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 | +# you may not use this file except in compliance with the License. |
| 20 | +# You may obtain a copy of the License at |
| 21 | +# |
| 22 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | +# |
| 24 | +# Unless required by applicable law or agreed to in writing, software |
| 25 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 26 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | +# See the License for the specific language governing permissions and |
| 28 | +# limitations under the License. |
| 29 | + |
| 30 | +import copy |
| 31 | +import functools |
| 32 | + |
| 33 | +from typing import Callable, Optional, Sequence, Tuple, Union |
| 34 | +from google.api_core import gapic_v1 |
| 35 | +from google.api_core import retry as retries |
| 36 | + |
| 37 | +try: |
| 38 | + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] |
| 39 | +except AttributeError: # pragma: NO COVER |
| 40 | + OptionalRetry = Union[retries.Retry, object, None] # type: ignore |
| 41 | + |
| 42 | +from google.api_core import client_options as client_options_lib |
| 43 | +from google.auth import credentials as ga_credentials # type: ignore |
| 44 | + |
| 45 | +from google.cloud.bigtable.admin_v2.types import bigtable_table_admin |
| 46 | + |
| 47 | +from google.cloud.bigtable.admin_v2.services.bigtable_table_admin import ( |
| 48 | + async_client as base_client, |
| 49 | +) |
| 50 | +from google.cloud.bigtable.admin_v2.services.bigtable_table_admin.transports.base import ( |
| 51 | + BigtableTableAdminTransport, |
| 52 | +) |
| 53 | +from google.cloud.bigtable.admin_v2.overlay.types import async_consistency |
| 54 | + |
| 55 | +from google.cloud.bigtable.gapic_version import __version__ as bigtable_version |
| 56 | + |
| 57 | + |
| 58 | +DEFAULT_CLIENT_INFO = copy.copy(base_client.DEFAULT_CLIENT_INFO) |
| 59 | +DEFAULT_CLIENT_INFO.client_library_version = f"{bigtable_version}-admin-overlay-async" |
| 60 | + |
| 61 | +class BigtableTableAdminAsyncClient(base_client.BaseBigtableTableAdminAsyncClient): |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + *, |
| 65 | + credentials: Optional[ga_credentials.Credentials] = None, |
| 66 | + transport: Optional[ |
| 67 | + Union[ |
| 68 | + str, |
| 69 | + BigtableTableAdminTransport, |
| 70 | + Callable[..., BigtableTableAdminTransport], |
| 71 | + ] |
| 72 | + ] = "grpc_asyncio", |
| 73 | + client_options: Optional[client_options_lib.ClientOptions] = None, |
| 74 | + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, |
| 75 | + ) -> None: |
| 76 | + """Instantiates the Bigtable table admin async client. |
| 77 | +
|
| 78 | + Args: |
| 79 | + credentials (Optional[google.auth.credentials.Credentials]): The |
| 80 | + authorization credentials to attach to requests. These |
| 81 | + credentials identify the application to the service; if none |
| 82 | + are specified, the client will attempt to ascertain the |
| 83 | + credentials from the environment. |
| 84 | + transport (Optional[Union[str,BigtableTableAdminTransport,Callable[..., BigtableTableAdminTransport]]]): |
| 85 | + The transport to use, or a Callable that constructs and returns a new transport to use. |
| 86 | + If a Callable is given, it will be called with the same set of initialization |
| 87 | + arguments as used in the BigtableTableAdminTransport constructor. |
| 88 | + If set to None, a transport is chosen automatically. |
| 89 | + client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): |
| 90 | + Custom options for the client. |
| 91 | +
|
| 92 | + 1. The ``api_endpoint`` property can be used to override the |
| 93 | + default endpoint provided by the client when ``transport`` is |
| 94 | + not explicitly provided. Only if this property is not set and |
| 95 | + ``transport`` was not explicitly provided, the endpoint is |
| 96 | + determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment |
| 97 | + variable, which have one of the following values: |
| 98 | + "always" (always use the default mTLS endpoint), "never" (always |
| 99 | + use the default regular endpoint) and "auto" (auto-switch to the |
| 100 | + default mTLS endpoint if client certificate is present; this is |
| 101 | + the default value). |
| 102 | +
|
| 103 | + 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable |
| 104 | + is "true", then the ``client_cert_source`` property can be used |
| 105 | + to provide a client certificate for mTLS transport. If |
| 106 | + not provided, the default SSL client certificate will be used if |
| 107 | + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not |
| 108 | + set, no client certificate will be used. |
| 109 | +
|
| 110 | + 3. The ``universe_domain`` property can be used to override the |
| 111 | + default "googleapis.com" universe. Note that ``api_endpoint`` |
| 112 | + property still takes precedence; and ``universe_domain`` is |
| 113 | + currently not supported for mTLS. |
| 114 | +
|
| 115 | + client_info (google.api_core.gapic_v1.client_info.ClientInfo): |
| 116 | + The client info used to send a user-agent string along with |
| 117 | + API requests. If ``None``, then default info will be used. |
| 118 | + Generally, you only need to set this if you're developing |
| 119 | + your own client library. |
| 120 | +
|
| 121 | + Raises: |
| 122 | + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport |
| 123 | + creation failed for any reason. |
| 124 | + """ |
| 125 | + super(BigtableTableAdminAsyncClient, self).__init__( |
| 126 | + credentials=credentials, |
| 127 | + transport=transport, |
| 128 | + client_options=client_options, |
| 129 | + client_info=client_info, |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | + async def wait_for_consistency( |
| 134 | + self, |
| 135 | + request: Optional[ |
| 136 | + Union[bigtable_table_admin.CheckConsistencyRequest, dict] |
| 137 | + ] = None, |
| 138 | + *, |
| 139 | + name: Optional[str] = None, |
| 140 | + consistency_token: Optional[str] = None, |
| 141 | + retry: OptionalRetry = gapic_v1.method.DEFAULT, |
| 142 | + timeout: Union[float, object] = gapic_v1.method.DEFAULT, |
| 143 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), |
| 144 | + ) -> async_consistency.AsyncCheckConsistencyPollingFuture: |
| 145 | + r"""Creates a polling future that periodically checks replication |
| 146 | + consistency based on a consistency token, that is, if replication |
| 147 | + has caught up based on the conditions specified in the token and |
| 148 | + the check request. The future will stop checking once the underlying |
| 149 | + :meth:`check_consistency` request involving that token returns True. |
| 150 | +
|
| 151 | + .. code-block:: python |
| 152 | +
|
| 153 | + # This snippet should be regarded as a code template only. |
| 154 | + # It will require modifications to work: |
| 155 | + # - It may require correct/in-range values for request initialization. |
| 156 | + # - It may require specifying regional endpoints when creating the service |
| 157 | + # client as shown in: |
| 158 | + # https://googleapis.dev/python/google-api-core/latest/client_options.html |
| 159 | + from google.cloud.bigtable import admin_v2 |
| 160 | +
|
| 161 | + def sample_wait_for_consistency(): |
| 162 | + # Create a client |
| 163 | + client = admin_v2.BigtableTableAdminClient() |
| 164 | +
|
| 165 | + # Initialize request argument(s) |
| 166 | + request = admin_v2.CheckConsistencyRequest( |
| 167 | + name="name_value", |
| 168 | + consistency_token="consistency_token_value", |
| 169 | + ) |
| 170 | +
|
| 171 | + # Make the request |
| 172 | + future = client.wait_for_consistency(request=request) |
| 173 | +
|
| 174 | + # Wait for the table to become consistent |
| 175 | + print("Waiting for operation to complete...") |
| 176 | +
|
| 177 | + response = future.result() |
| 178 | +
|
| 179 | + # Handle the response |
| 180 | + print(response) |
| 181 | +
|
| 182 | + Args: |
| 183 | + request (Union[google.cloud.bigtable.admin_v2.types.CheckConsistencyRequest, dict]): |
| 184 | + The request object. Request message for |
| 185 | + [google.bigtable.admin.v2.BigtableTableAdmin.CheckConsistency][google.bigtable.admin.v2.BigtableTableAdmin.CheckConsistency] |
| 186 | + name (str): |
| 187 | + Required. The unique name of the Table for which to |
| 188 | + check replication consistency. Values are of the form |
| 189 | + ``projects/{project}/instances/{instance}/tables/{table}``. |
| 190 | +
|
| 191 | + This corresponds to the ``name`` field |
| 192 | + on the ``request`` instance; if ``request`` is provided, this |
| 193 | + should not be set. |
| 194 | + consistency_token (str): |
| 195 | + Required. The token created using |
| 196 | + GenerateConsistencyToken for the Table. |
| 197 | +
|
| 198 | + This corresponds to the ``consistency_token`` field |
| 199 | + on the ``request`` instance; if ``request`` is provided, this |
| 200 | + should not be set. |
| 201 | + retry (google.api_core.retry.Retry): Designation of what errors, if any, |
| 202 | + should be retried. |
| 203 | + timeout (float): The timeout for this request. |
| 204 | + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be |
| 205 | + sent along with the request as metadata. Normally, each value must be of type `str`, |
| 206 | + but for metadata keys ending with the suffix `-bin`, the corresponding values must |
| 207 | + be of type `bytes`. |
| 208 | +
|
| 209 | + Returns: |
| 210 | + google.cloud.bigtable.admin_v2.overlay.types.async_consistency.AsyncCheckConsistencyPollingFuture: |
| 211 | + An object representing a polling future. |
| 212 | +
|
| 213 | + The result type for the operation will be `bool`, and will return True when the |
| 214 | + consistency check involving the given consistency token returns True. |
| 215 | + """ |
| 216 | + api_call = functools.partial( |
| 217 | + self.check_consistency, |
| 218 | + request, |
| 219 | + name=name, |
| 220 | + consistency_token=consistency_token, |
| 221 | + timeout=timeout, |
| 222 | + metadata=metadata, |
| 223 | + ) |
| 224 | + |
| 225 | + return async_consistency.AsyncCheckConsistencyPollingFuture( |
| 226 | + api_call, |
| 227 | + check_consistency_call_retry=retry |
| 228 | + ) |
0 commit comments