From bf0ec44f4b702a558bd1804a00dcb43e08a89504 Mon Sep 17 00:00:00 2001 From: Mayur Singal <39544459+ulixius9@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:19:20 +0530 Subject: [PATCH] Fix #15678: Accomodate Metabase API changes (#15692) --- .../source/dashboard/metabase/client.py | 47 +++++++++++++++---- .../source/dashboard/metabase/connection.py | 3 +- .../source/dashboard/metabase/metadata.py | 2 +- .../source/dashboard/metabase/models.py | 2 +- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/ingestion/src/metadata/ingestion/source/dashboard/metabase/client.py b/ingestion/src/metadata/ingestion/source/dashboard/metabase/client.py index 9a0bf81d83f..9de61684480 100644 --- a/ingestion/src/metadata/ingestion/source/dashboard/metabase/client.py +++ b/ingestion/src/metadata/ingestion/source/dashboard/metabase/client.py @@ -85,18 +85,49 @@ class MetabaseClient: ) self.client = REST(client_config) - def get_dashboards_list(self) -> List[MetabaseDashboard]: + def get_dashboards_list( + self, collections: List[MetabaseCollection] + ) -> List[MetabaseDashboard]: """ Get List of all dashboards """ - try: - resp_dashboards = self.client.get("/dashboard") + dashboards = [] + for collection in collections or []: + try: + resp_dashboards = self.client.get( + f"/collection/{collection.id}/items?models=dashboard" + ) + if resp_dashboards: + dashboard_list = MetabaseDashboardList(**resp_dashboards) + dashboards.extend(dashboard_list.data) + except Exception: + logger.debug(traceback.format_exc()) + logger.warning("Failed to fetch the dashboard list") + return dashboards + + def get_dashboards_list_test_conn( + self, collections: List[MetabaseCollection] + ) -> List[MetabaseDashboard]: + """ + Get List of all dashboards + """ + for collection in collections or []: + resp_dashboards = self.client.get( + f"/collection/{collection.id}/items?models=dashboard" + ) if resp_dashboards: - dashboard_list = MetabaseDashboardList(dashboards=resp_dashboards) - return dashboard_list.dashboards - except Exception: - logger.debug(traceback.format_exc()) - logger.warning("Failed to fetch the dashboard list") + dashboard_list = MetabaseDashboardList(**resp_dashboards) + return dashboard_list.data + return [] + + def get_collections_list_test_conn(self) -> List[MetabaseCollection]: + """ + Get List of all collections + """ + resp_collections = self.client.get("/collection") + if resp_collections: + collection_list = MetabaseCollectionList(collections=resp_collections) + return collection_list.collections return [] def get_collections_list(self) -> List[MetabaseCollection]: diff --git a/ingestion/src/metadata/ingestion/source/dashboard/metabase/connection.py b/ingestion/src/metadata/ingestion/source/dashboard/metabase/connection.py index 0f51e177bd2..1d8000ea439 100644 --- a/ingestion/src/metadata/ingestion/source/dashboard/metabase/connection.py +++ b/ingestion/src/metadata/ingestion/source/dashboard/metabase/connection.py @@ -44,7 +44,8 @@ def test_connection( """ def custom_executor(): - return client.get_dashboards_list() + collections = client.get_collections_list_test_conn() + return client.get_dashboards_list_test_conn(collections) test_fn = {"GetDashboards": custom_executor} diff --git a/ingestion/src/metadata/ingestion/source/dashboard/metabase/metadata.py b/ingestion/src/metadata/ingestion/source/dashboard/metabase/metadata.py index 7011b877709..49fb5fe1325 100644 --- a/ingestion/src/metadata/ingestion/source/dashboard/metabase/metadata.py +++ b/ingestion/src/metadata/ingestion/source/dashboard/metabase/metadata.py @@ -94,7 +94,7 @@ class MetabaseSource(DashboardServiceSource): """ Get List of all dashboards """ - return self.client.get_dashboards_list() + return self.client.get_dashboards_list(self.collections) def get_dashboard_name(self, dashboard: MetabaseDashboard) -> str: """ diff --git a/ingestion/src/metadata/ingestion/source/dashboard/metabase/models.py b/ingestion/src/metadata/ingestion/source/dashboard/metabase/models.py index fce0d030adc..1f3d0498bff 100644 --- a/ingestion/src/metadata/ingestion/source/dashboard/metabase/models.py +++ b/ingestion/src/metadata/ingestion/source/dashboard/metabase/models.py @@ -37,7 +37,7 @@ class MetabaseCollection(BaseModel): class MetabaseDashboardList(BaseModel): - dashboards: Optional[List[MetabaseDashboard]] + data: Optional[List[MetabaseDashboard]] class MetabaseCollectionList(BaseModel):