|
| 1 | +#### |
| 2 | +# This script demonstrates how to use the Tableau Server Client |
| 3 | +# to read database and table assets. It explores the read-only |
| 4 | +# functions that the Server API supports on databases and tables. |
| 5 | +# |
| 6 | +# With no flags set, this sample will query all databases, pick one |
| 7 | +# database and print its details, list the tables in that database, |
| 8 | +# then populate and list the columns of one table. Adding the |
| 9 | +# --permissions flag also prints the permissions on the database |
| 10 | +# and table. |
| 11 | +# |
| 12 | +# Databases and tables are external assets. Your site must be |
| 13 | +# licensed with Data Management for the server to return them. |
| 14 | +#### |
| 15 | + |
| 16 | +import argparse |
| 17 | +import logging |
| 18 | + |
| 19 | +import tableauserverclient as TSC |
| 20 | + |
| 21 | +from _shared import add_common_arguments, build_auth, resolve_credentials |
| 22 | + |
| 23 | +# databases.populate_tables is not enabled in TSC, so we use the Metadata API |
| 24 | +# to find which tables belong to a database, then read them over REST. |
| 25 | +DATABASE_TABLES_QUERY = """ |
| 26 | +query tablesInDatabase($luid: String) { |
| 27 | + databases(filter: {luid: $luid}) { |
| 28 | + name |
| 29 | + tables { |
| 30 | + luid |
| 31 | + name |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +""" |
| 36 | + |
| 37 | + |
| 38 | +def print_permissions(item): |
| 39 | + for rule in item.permissions: |
| 40 | + print(f" {rule.grantee.tag_name} {rule.grantee.id}: {rule.capabilities}") |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + parser = argparse.ArgumentParser(description="Explore database and table functions supported by the Server API.") |
| 45 | + add_common_arguments(parser) |
| 46 | + # Options specific to this sample |
| 47 | + parser.add_argument("--database-name", help="name of the database to explore (defaults to the first one found)") |
| 48 | + parser.add_argument("--permissions", help="also print database and table permissions", action="store_true") |
| 49 | + |
| 50 | + args = parser.parse_args() |
| 51 | + |
| 52 | + resolve_credentials(args) |
| 53 | + logging.basicConfig(level=getattr(logging, args.logging_level.upper())) |
| 54 | + |
| 55 | + tableau_auth = build_auth(args) |
| 56 | + server = TSC.Server(args.server, use_server_version=True) |
| 57 | + with server.auth.sign_in(tableau_auth): |
| 58 | + # Gets all database items. `.get()` returns only one page; use |
| 59 | + # TSC.Pager to iterate every database on the site. |
| 60 | + first_page, pagination_item = server.databases.get() |
| 61 | + print(f"\nThere are {pagination_item.total_available} databases on site: ") |
| 62 | + all_databases = list(TSC.Pager(server.databases)) |
| 63 | + print([database.name for database in all_databases]) |
| 64 | + |
| 65 | + if args.database_name: |
| 66 | + matches = [database for database in all_databases if database.name == args.database_name] |
| 67 | + else: |
| 68 | + matches = all_databases |
| 69 | + if not matches: |
| 70 | + print("\nNo matching database found.") |
| 71 | + return |
| 72 | + |
| 73 | + sample_database = server.databases.get_by_id(matches[0].id) |
| 74 | + print(f"\nDetails for database {sample_database.name}:") |
| 75 | + print(f" id: {sample_database.id}") |
| 76 | + print(f" connection type: {sample_database.connection_type}") |
| 77 | + print(f" host: {sample_database.host_name}:{sample_database.port}") |
| 78 | + print(f" embedded: {sample_database.embedded}") |
| 79 | + print(f" certified: {sample_database.certified}") |
| 80 | + print(f" description: {sample_database.description}") |
| 81 | + print(f" contact id: {sample_database.contact_id}") |
| 82 | + |
| 83 | + if args.permissions: |
| 84 | + server.databases.populate_permissions(sample_database) |
| 85 | + print(f"\nPermissions for database {sample_database.name}:") |
| 86 | + print_permissions(sample_database) |
| 87 | + |
| 88 | + result = server.metadata.query(DATABASE_TABLES_QUERY, {"luid": sample_database.id}) |
| 89 | + if result.get("errors"): |
| 90 | + print(f"\nMetadata API returned errors: {result['errors']}") |
| 91 | + databases_data = (result.get("data") or {}).get("databases") or [] |
| 92 | + table_refs = databases_data[0]["tables"] if databases_data else [] |
| 93 | + print(f"\nThere are {len(table_refs)} tables in database {sample_database.name}: ") |
| 94 | + print([table_ref["name"] for table_ref in table_refs]) |
| 95 | + |
| 96 | + if not table_refs: |
| 97 | + return |
| 98 | + |
| 99 | + sample_table = server.tables.get_by_id(table_refs[0]["luid"]) |
| 100 | + print(f"\nDetails for table {sample_table.name}:") |
| 101 | + print(f" id: {sample_table.id}") |
| 102 | + print(f" schema: {sample_table.schema}") |
| 103 | + print(f" certified: {sample_table.certified}") |
| 104 | + print(f" description: {sample_table.description}") |
| 105 | + |
| 106 | + # `columns` is a Pager, so this iterates every page of columns. |
| 107 | + server.tables.populate_columns(sample_table) |
| 108 | + print(f"\nColumns in table {sample_table.name}: ") |
| 109 | + for column in sample_table.columns: |
| 110 | + # ColumnItem.remote_type raises AttributeError when the server omits remoteType. |
| 111 | + print(f" {column.name} ({getattr(column, 'remote_type', None)})") |
| 112 | + |
| 113 | + if args.permissions: |
| 114 | + server.tables.populate_permissions(sample_table) |
| 115 | + print(f"\nPermissions for table {sample_table.name}:") |
| 116 | + print_permissions(sample_table) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments