Package: bigquery-view-analyzer
BigQuery View AnalyzerDescriptionbigquery-view-analyzeris a command-line tool for visualizing dependencies and managing permissions between BigQuery views.To authorize a view, permissions must be granted at a dataset level for every view/table referenced in the view definition. This requirement cascades down to every view that's referenced by the parent view, they too must have permissions granted for every view/table they reference - and so on. This can quickly become difficult to manage if you have many nested views across multiple datasets and/or projects.bigquery-view-analyzerautomatically resolves these dependencies and applies the relevant permissions to all views and datasets referenced by the parent view.Installation$pipinstallbigquery-view-analyzerUsage$bqva--helpExample: CLIGiven the above datasets and tables in BigQuery, to authorizebqva-demo:dataset_4.shared_view, the following views would need to be authorized with each of the following datasets:Authorized views fordataset_1bqva-demo:dataset_3.view_a_b_c_dAuthorized views fordataset_2bqva-demo:dataset_3.view_a_b_c_dbqva-demo:dataset_1.view_cAuthorized views fordataset_3bqva-demo:dataset_2.view_dbqva-demo:dataset_4.shared_viewYou can easily visualize the above view hierarchy using thebqva treecommand.# View dependency tree and authorization status for 'bqva-demo:dataset_4.shared_view'$bqvatree--status--no-key--view"bqva-demo:dataset_4.shared_view"bqva-demo:dataset_4.shared_view
└──bqva-demo:dataset_3.view_a_b_c_d(⨯)├──bqva-demo:dataset_1.table_a(⨯)├──bqva-demo:dataset_1.table_b(⨯)├──bqva-demo:dataset_1.view_c(⨯)│└──bqva-demo:dataset_2.table_c(⨯)└──bqva-demo:dataset_2.view_d(⨯)└──bqva-demo:dataset_3.table_d(⨯)Permissions can be applied automatically to all datasets referenced by the parent view using thebqva authorizecommand.# Apply all permissions required by 'bqva-demo:dataset_4.shared_view'$bqvaauthorize--view"bqva-demo:dataset_4.shared_view"bqva-demo:dataset_4.shared_view
└──bqva-demo:dataset_3.view_a_b_c_d(✓)├──bqva-demo:dataset_1.table_a(✓)├──bqva-demo:dataset_1.table_b(✓)├──bqva-demo:dataset_1.view_c(✓)│└──bqva-demo:dataset_2.table_c(✓)└──bqva-demo:dataset_2.view_d(✓)└──bqva-demo:dataset_3.table_d(✓)If you want to revoke permissions for a view, you can do that too!# Revoke all permissions granted to 'bqva-demo:dataset_4.shared_view'$bqvarevoke--view"bqva-demo:dataset_4.shared_view"bqva-demo:dataset_4.shared_view
└──bqva-demo:dataset_3.view_a_b_c_d(⨯)├──bqva-demo:dataset_1.table_a(⨯)├──bqva-demo:dataset_1.table_b(⨯)├──bqva-demo:dataset_1.view_c(⨯)│└──bqva-demo:dataset_2.table_c(⨯)└──bqva-demo:dataset_2.view_d(⨯)└──bqva-demo:dataset_3.table_d(⨯)Example: Python libraryYou can import the library within a Python project to programatically apply permissions to multiple datasets.frombqvaimportViewAnalyzerfromgoogle.cloudimportbigqueryclient=bigquery.Client()defauth_views(datasets=[],**kwargs):# get all datasets by default if none providediflen(datasets)==0:datasets=client.list_datasets(max_results=1)fordatasetindatasets:dataset=client.dataset(dataset)tables=client.list_tables(dataset.dataset_id)fortableintables:iftable.table_type=="VIEW":view=ViewAnalyzer(project_id=table.project,dataset_id=table.dataset_id,view_id=table.table_id,)view.apply_permissions()print(f"Authorised view:{table.project}.{table.dataset_id}.{table.table_id}")auth_views(["dataset_a","dataset_b"])