Skip to content

Commit 1c97780

Browse files
authored
Let page name adhere to naming restrictions (#370)
Let page names to adhere to the ([new](databrickslabs/ucx#3789)) naming convention: Resource names should only contain alphanumeric characters (a-z, A-Z, 0-9), hyphens (-), or underscores (_) Resolves #369
1 parent f465a16 commit 1c97780

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/databricks/labs/lsql/dashboards.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"Resource names should only contain alphanumeric characters (a-z, A-Z, 0-9), hyphens (-), or underscores (_)"
6161
)
6262
_VALID_RESOURCE_NAME_PATTERN = re.compile("^[A-Za-z0-9_-]+$")
63+
_CLEAN_RESOURCE_NAME_PATTERN = re.compile("[^A-Za-z0-9_-]+")
6364

6465

6566
def _is_valid_resource_name(name: str) -> bool:
@@ -70,6 +71,14 @@ def _is_valid_resource_name(name: str) -> bool:
7071
return _VALID_RESOURCE_NAME_PATTERN.match(name) is not None
7172

7273

74+
def _clean_resource_name(name: str) -> str:
75+
"""Clean a resource name to become valid.
76+
77+
See :func:_is_valid_resource_name for the definition of a valid resource name.
78+
"""
79+
return _CLEAN_RESOURCE_NAME_PATTERN.sub("_", name)
80+
81+
7382
class BaseHandler:
7483
"""Base file handler.
7584
@@ -998,7 +1007,7 @@ def as_lakeview(self) -> Dashboard:
9981007
datasets = self.get_datasets()
9991008
layouts = self._get_layouts()
10001009
page = Page(
1001-
name=self.display_name,
1010+
name=_clean_resource_name(self.display_name),
10021011
display_name=self.display_name,
10031012
layout=layouts,
10041013
)

tests/integration/test_dashboards.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_dashboard_deploys_dashboard_with_display_name(ws, make_dashboard, tmp_p
160160
dashboards = Dashboards(ws)
161161
sdk_dashboard = make_dashboard(display_name="Counter")
162162

163-
(tmp_path / "dashboard.yml").write_text("display_name: Counter")
163+
(tmp_path / "dashboard.yml").write_text("display_name: 'My custom counter'")
164164
(tmp_path / "counter.sql").write_text("SELECT 102132 AS count")
165165
dashboard_metadata = DashboardMetadata.from_path(tmp_path)
166166

tests/unit/test_dashboards.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,17 @@ def test_dashboard_metadata_as_lakeview_with_custom_first_page_name(tmp_path):
659659
assert page.display_name == "Custom"
660660

661661

662+
def test_dashboard_metadata_as_lakeview_cleans_page_name(tmp_path):
663+
"""The page name is not allowed to have special characters."""
664+
(tmp_path / "dashboard.yml").write_text("display_name: 'name with spaces'")
665+
dashboard_metadata = DashboardMetadata.from_path(tmp_path)
666+
667+
dashboard = dashboard_metadata.as_lakeview()
668+
669+
page = dashboard.pages[0]
670+
assert " " not in page.name
671+
672+
662673
@pytest.mark.parametrize("dashboard_content", ["missing_display_name: true", "invalid:\nyml"])
663674
def test_dashboard_metadata_handles_invalid_dashboard_yml(tmp_path, dashboard_content):
664675
queries_path = tmp_path / "queries"

0 commit comments

Comments
 (0)