Skip to content

Commit

Permalink
feat(ingest/metabase): Allow to configure a different url for links
Browse files Browse the repository at this point in the history
  • Loading branch information
ilpianista committed Feb 2, 2024
1 parent f7f0b14 commit 6b4d643
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
21 changes: 16 additions & 5 deletions metadata-ingestion/src/datahub/ingestion/source/metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dateutil.parser as dp
import pydantic
import requests
from pydantic import Field, validator
from pydantic import Field, root_validator, validator
from requests.models import HTTPError

import datahub.emitter.mce_builder as builder
Expand Down Expand Up @@ -53,6 +53,10 @@ class MetabaseConfig(DatasetLineageProviderConfigBase):
# See the Metabase /api/session endpoint for details
# https://www.metabase.com/docs/latest/api-documentation.html#post-apisession
connect_uri: str = Field(default="localhost:3000", description="Metabase host URL.")
display_uri: Optional[str] = Field(
default=None,
description="optional URL to use in links (if `connect_uri` is only for ingestion)",
)
username: Optional[str] = Field(default=None, description="Metabase username.")
password: Optional[pydantic.SecretStr] = Field(
default=None, description="Metabase password."
Expand All @@ -76,10 +80,17 @@ class MetabaseConfig(DatasetLineageProviderConfigBase):
description="Default schema name to use when schema is not provided in an SQL query",
)

@validator("connect_uri")
@validator("connect_uri", "display_uri")
def remove_trailing_slash(cls, v):
return config_clean.remove_trailing_slashes(v)

@root_validator(skip_on_failure=True)
def default_display_uri_to_connect_uri(cls, values):
base = values.get("display_uri")
if base is None:
values["display_uri"] = values.get("connect_uri")
return values


@platform_name("Metabase")
@config_class(MetabaseConfig)
Expand Down Expand Up @@ -239,7 +250,7 @@ def construct_dashboard_from_api_data(
self, dashboard_info: dict
) -> Optional[DashboardSnapshot]:
dashboard_id = dashboard_info.get("id", "")
dashboard_url = f"{self.config.connect_uri}/api/dashboard/{dashboard_id}"
dashboard_url = f"{self.config.display_uri}/api/dashboard/{dashboard_id}"
try:
dashboard_response = self.session.get(dashboard_url)
dashboard_response.raise_for_status()
Expand Down Expand Up @@ -297,7 +308,7 @@ def construct_dashboard_from_api_data(

@lru_cache(maxsize=None)
def _get_ownership(self, creator_id: int) -> Optional[OwnershipClass]:
user_info_url = f"{self.config.connect_uri}/api/user/{creator_id}"
user_info_url = f"{self.config.display_uri}/api/user/{creator_id}"
try:
user_info_response = self.session.get(user_info_url)
user_info_response.raise_for_status()
Expand Down Expand Up @@ -362,7 +373,7 @@ def get_card_details_by_id(self, card_id: Union[int, str]) -> dict:
:param int datasource_id: Numeric datasource ID received from Metabase API
:return: dict with info or empty dict
"""
card_url = f"{self.config.connect_uri}/api/card/{card_id}"
card_url = f"{self.config.display_uri}/api/card/{card_id}"
try:
card_response = self.session.get(card_url)
card_response.raise_for_status()
Expand Down
9 changes: 9 additions & 0 deletions metadata-ingestion/tests/unit/test_metabase_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ def test_get_platform_instance():

# database_id_to_instance_map is missing, platform_instance_map is defined and key missing
assert metabase.get_platform_instance("missing-platform", 999) is None


def test_set_display_uri():
display_uri = "some_host:1234"

config = MetabaseConfig.parse_obj({"display_uri": display_uri})

assert config.connect_uri == "localhost:3000"
assert config.display_uri == display_uri

0 comments on commit 6b4d643

Please sign in to comment.