Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle 404s for deactivated users when exporting exposures #96

Merged
merged 4 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions dbtmetabase/metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,17 @@ def increase_indent(self, flow=False, indentless=False):
creator_email = exposure["creator"]["email"]
creator_name = exposure["creator"]["common_name"]
elif "creator_id" in exposure:
creator = self.api("get", f"/api/user/{exposure['creator_id']}")
creator_email = creator["email"]
creator_name = creator["common_name"]
# If a metabase user is deactivated, the API returns a 404
try:
creator = self.api("get", f"/api/user/{exposure['creator_id']}")
except requests.exceptions.HTTPError as error:
if error.response.status_code == 404:
creator = {}
else:
raise

creator_email = creator.get("email")
creator_name = creator.get("common_name")

# No spaces allowed in model names in dbt docs DAG / No duplicate model names
exposure_name = exposure_name.replace(" ", "_")
Expand Down Expand Up @@ -799,6 +807,7 @@ def _build_exposure(
)

# Output exposure

return {
"name": name,
"description": description,
Expand All @@ -807,7 +816,7 @@ def _build_exposure(
"maturity": "medium",
"owner": {
"name": creator_name,
"email": creator_email,
"email": creator_email or "",
},
"depends_on": [
refable_models[exposure.upper()]
Expand Down Expand Up @@ -855,12 +864,12 @@ def api(
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
if "password" in kwargs["json"]:
if "json" in kwargs and "password" in kwargs["json"]:
gouline marked this conversation as resolved.
Show resolved Hide resolved
logger().error("HTTP request failed. Response: %s", response.text)
else:
logger().error(
"HTTP request failed. Payload: %s. Response: %s",
kwargs["json"],
kwargs.get("json"),
response.text,
)
raise
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/exposure/baseline_test_exposures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ exposures:
url: http://localhost:3000/card/3
maturity: medium
owner:
name: Alexander Butler
email: [email protected]
name: null
email: ''
depends_on:
- ref('customers')
2 changes: 1 addition & 1 deletion tests/fixtures/mock_api/api/card/3.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"description": null, "archived": false, "collection_position": null, "table_id": 7, "result_metadata": [{"name": "count", "display_name": "Count", "base_type": "type/BigInteger", "semantic_type": "type/Quantity", "field_ref": ["aggregation", 0], "fingerprint": {"global": {"distinct-count": 1, "nil%": 0.0}, "type": {"type/Number": {"min": 100.0, "q1": 100.0, "q3": 100.0, "max": 100.0, "sd": null, "avg": 100.0}}}}], "creator": {"email": "[email protected]", "first_name": "Alexander", "last_login": "2021-07-21T19:25:28.6083Z", "is_qbnewb": false, "is_superuser": true, "id": 1, "last_name": "Butler", "date_joined": "2021-07-21T05:38:53.637091Z", "common_name": "Alexander Butler"}, "can_write": true, "database_id": 2, "enable_embedding": false, "collection_id": 3, "query_type": "query", "name": "Total customers", "dashboard_count": 1, "creator_id": 1, "updated_at": "2021-07-21T08:01:41.996Z", "made_public_by_id": null, "embedding_params": null, "cache_ttl": null, "dataset_query": {"type": "query", "database": 2, "query": {"source-table": 7, "aggregation": [["count"]]}}, "id": 3, "display": "scalar", "last-edit-info": {"id": 1, "email": "[email protected]", "first_name": "Alexander", "last_name": "Butler", "timestamp": "2021-07-21T08:01:37.449936Z"}, "visualization_settings": {"graph.series_labels": ["number"], "graph.metrics": ["count"], "graph.dimensions": []}, "collection": {"authority_level": null, "description": "Automatically generated cards.", "archived": false, "slug": "a_look_at_your_customers_table", "color": "#f9d45c", "name": "A look at your customers table", "personal_owner_id": null, "id": 3, "location": "/2/", "namespace": null}, "created_at": "2021-07-21T08:01:37.434243Z", "public_uuid": null}
{"description": null, "archived": false, "collection_position": null, "table_id": 7, "result_metadata": [{"name": "count", "display_name": "Count", "base_type": "type/BigInteger", "semantic_type": "type/Quantity", "field_ref": ["aggregation", 0], "fingerprint": {"global": {"distinct-count": 1, "nil%": 0.0}, "type": {"type/Number": {"min": 100.0, "q1": 100.0, "q3": 100.0, "max": 100.0, "sd": null, "avg": 100.0}}}}], "can_write": true, "database_id": 2, "enable_embedding": false, "collection_id": 3, "query_type": "query", "name": "Total customers", "dashboard_count": 1, "creator_id": 2, "updated_at": "2021-07-21T08:01:41.996Z", "made_public_by_id": null, "embedding_params": null, "cache_ttl": null, "dataset_query": {"type": "query", "database": 2, "query": {"source-table": 7, "aggregation": [["count"]]}}, "id": 3, "display": "scalar", "last-edit-info": {"id": 1, "email": "[email protected]", "first_name": "Alexander", "last_name": "Butler", "timestamp": "2021-07-21T08:01:37.449936Z"}, "visualization_settings": {"graph.series_labels": ["number"], "graph.metrics": ["count"], "graph.dimensions": []}, "collection": {"authority_level": null, "description": "Automatically generated cards.", "archived": false, "slug": "a_look_at_your_customers_table", "color": "#f9d45c", "name": "A look at your customers table", "personal_owner_id": null, "id": 3, "location": "/2/", "namespace": null}, "created_at": "2021-07-21T08:01:37.434243Z", "public_uuid": null}
gouline marked this conversation as resolved.
Show resolved Hide resolved