|
| 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +import logging |
| 18 | + |
| 19 | +from google.auth.credentials import Credentials |
| 20 | + |
| 21 | +from . import client |
| 22 | + |
| 23 | + |
| 24 | +def list_instances(project_id: str, credentials: Credentials) -> dict: |
| 25 | + """List Bigtable instance ids in a Google Cloud project. |
| 26 | +
|
| 27 | + Args: |
| 28 | + project_id (str): The Google Cloud project id. |
| 29 | + credentials (Credentials): The credentials to use for the request. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + dict: Dictionary with a list of the Bigtable instance ids present in the project. |
| 33 | + """ |
| 34 | + try: |
| 35 | + bt_client = client.get_bigtable_admin_client( |
| 36 | + project=project_id, credentials=credentials |
| 37 | + ) |
| 38 | + (instances_list, failed_locations_list) = bt_client.list_instances() |
| 39 | + if failed_locations_list: |
| 40 | + logging.warning( |
| 41 | + "Failed to list instances from the following locations: %s", |
| 42 | + failed_locations_list, |
| 43 | + ) |
| 44 | + instance_ids = [instance.instance_id for instance in instances_list] |
| 45 | + return {"status": "SUCCESS", "results": instance_ids} |
| 46 | + except Exception as ex: |
| 47 | + return { |
| 48 | + "status": "ERROR", |
| 49 | + "error_details": str(ex), |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +def get_instance_info( |
| 54 | + project_id: str, instance_id: str, credentials: Credentials |
| 55 | +) -> dict: |
| 56 | + """Get metadata information about a Bigtable instance. |
| 57 | +
|
| 58 | + Args: |
| 59 | + project_id (str): The Google Cloud project id containing the instance. |
| 60 | + instance_id (str): The Bigtable instance id. |
| 61 | + credentials (Credentials): The credentials to use for the request. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + dict: Dictionary representing the properties of the instance. |
| 65 | + """ |
| 66 | + try: |
| 67 | + bt_client = client.get_bigtable_admin_client( |
| 68 | + project=project_id, credentials=credentials |
| 69 | + ) |
| 70 | + instance = bt_client.instance(instance_id) |
| 71 | + instance.reload() |
| 72 | + instance_info = { |
| 73 | + "project_id": project_id, |
| 74 | + "instance_id": instance.instance_id, |
| 75 | + "display_name": instance.display_name, |
| 76 | + "state": instance.state, |
| 77 | + "type": instance.type_, |
| 78 | + "labels": instance.labels, |
| 79 | + } |
| 80 | + return {"status": "SUCCESS", "results": instance_info} |
| 81 | + except Exception as ex: |
| 82 | + return { |
| 83 | + "status": "ERROR", |
| 84 | + "error_details": str(ex), |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +def list_tables( |
| 89 | + project_id: str, instance_id: str, credentials: Credentials |
| 90 | +) -> dict: |
| 91 | + """List table ids in a Bigtable instance. |
| 92 | +
|
| 93 | + Args: |
| 94 | + project_id (str): The Google Cloud project id containing the instance. |
| 95 | + instance_id (str): The Bigtable instance id. |
| 96 | + credentials (Credentials): The credentials to use for the request. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + dict: Dictionary with a list of the tables ids present in the instance. |
| 100 | + """ |
| 101 | + try: |
| 102 | + bt_client = client.get_bigtable_admin_client( |
| 103 | + project=project_id, credentials=credentials |
| 104 | + ) |
| 105 | + instance = bt_client.instance(instance_id) |
| 106 | + tables = instance.list_tables() |
| 107 | + table_ids = [table.table_id for table in tables] |
| 108 | + return {"status": "SUCCESS", "results": table_ids} |
| 109 | + except Exception as ex: |
| 110 | + return { |
| 111 | + "status": "ERROR", |
| 112 | + "error_details": str(ex), |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | +def get_table_info( |
| 117 | + project_id: str, instance_id: str, table_id: str, credentials: Credentials |
| 118 | +) -> dict: |
| 119 | + """Get metadata information about a Bigtable table. |
| 120 | +
|
| 121 | + Args: |
| 122 | + project_id (str): The Google Cloud project id containing the instance. |
| 123 | + instance_id (str): The Bigtable instance id containing the table. |
| 124 | + table_id (str): The Bigtable table id. |
| 125 | + credentials (Credentials): The credentials to use for the request. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + dict: Dictionary representing the properties of the table. |
| 129 | + """ |
| 130 | + try: |
| 131 | + bt_client = client.get_bigtable_admin_client( |
| 132 | + project=project_id, credentials=credentials |
| 133 | + ) |
| 134 | + instance = bt_client.instance(instance_id) |
| 135 | + table = instance.table(table_id) |
| 136 | + column_families = table.list_column_families() |
| 137 | + table_info = { |
| 138 | + "project_id": project_id, |
| 139 | + "instance_id": instance.instance_id, |
| 140 | + "table_id": table.table_id, |
| 141 | + "column_families": list(column_families.keys()), |
| 142 | + } |
| 143 | + return {"status": "SUCCESS", "results": table_info} |
| 144 | + except Exception as ex: |
| 145 | + return { |
| 146 | + "status": "ERROR", |
| 147 | + "error_details": str(ex), |
| 148 | + } |
0 commit comments