Skip to content

Commit 18b005b

Browse files
authored
Fixed edge case for RegexSubStrategy (#1561)
1 parent 0217486 commit 18b005b

File tree

2 files changed

+97
-30
lines changed

2 files changed

+97
-30
lines changed

src/databricks/labs/ucx/workspace_access/groups.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ def generate_migrated_groups(self):
205205
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}"
206206
account_group = self.account_groups_in_account.get(group.display_name)
207207
if not account_group:
208-
logger.info(f"Couldn't find a matching account group for {group.display_name} group")
208+
logger.info(
209+
f"Couldn't find a matching account group for {group.display_name} group using name matching"
210+
)
209211
continue
210212
yield MigratedGroup(
211213
id_in_workspace=group.id,
@@ -241,21 +243,19 @@ def generate_migrated_groups(self):
241243
for group in workspace_groups.values():
242244
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}"
243245
account_group = account_groups_by_id.get(group.external_id)
244-
if account_group:
245-
yield MigratedGroup(
246-
id_in_workspace=group.id,
247-
name_in_workspace=group.display_name,
248-
name_in_account=account_group.display_name,
249-
temporary_name=temporary_name,
250-
external_id=account_group.external_id,
251-
members=json.dumps([gg.as_dict() for gg in group.members]) if group.members else None,
252-
roles=json.dumps([gg.as_dict() for gg in group.roles]) if group.roles else None,
253-
entitlements=(
254-
json.dumps([gg.as_dict() for gg in group.entitlements]) if group.entitlements else None
255-
),
256-
)
257-
else:
246+
if not account_group:
258247
logger.info(f"Couldn't find a matching account group for {group.display_name} group with external_id")
248+
continue
249+
yield MigratedGroup(
250+
id_in_workspace=group.id,
251+
name_in_workspace=group.display_name,
252+
name_in_account=account_group.display_name,
253+
temporary_name=temporary_name,
254+
external_id=account_group.external_id,
255+
members=json.dumps([gg.as_dict() for gg in group.members]) if group.members else None,
256+
roles=json.dumps([gg.as_dict() for gg in group.roles]) if group.roles else None,
257+
entitlements=(json.dumps([gg.as_dict() for gg in group.entitlements]) if group.entitlements else None),
258+
)
259259

260260

261261
class RegexSubStrategy(GroupMigrationStrategy):
@@ -285,12 +285,18 @@ def generate_migrated_groups(self):
285285
name_in_account = self._safe_sub(
286286
group.display_name, self.workspace_group_regex, self.workspace_group_replace
287287
)
288+
account_group = self.account_groups_in_account.get(name_in_account)
289+
if not account_group:
290+
logger.info(
291+
f"Couldn't find a matching account group for {group.display_name} group with regex substitution"
292+
)
293+
continue
288294
yield MigratedGroup(
289295
id_in_workspace=group.id,
290296
name_in_workspace=group.display_name,
291297
name_in_account=name_in_account,
292298
temporary_name=temporary_name,
293-
external_id=self.account_groups_in_account[name_in_account].external_id,
299+
external_id=account_group.external_id,
294300
members=json.dumps([gg.as_dict() for gg in group.members]) if group.members else None,
295301
roles=json.dumps([gg.as_dict() for gg in group.roles]) if group.roles else None,
296302
entitlements=json.dumps([gg.as_dict() for gg in group.entitlements]) if group.entitlements else None,
@@ -329,21 +335,23 @@ def generate_migrated_groups(self):
329335
for group_match, ws_group in workspace_groups_by_match.items():
330336
temporary_name = f"{self.renamed_groups_prefix}{ws_group.display_name}"
331337
account_group = account_groups_by_match.get(group_match)
332-
if account_group:
333-
yield MigratedGroup(
334-
id_in_workspace=ws_group.id,
335-
name_in_workspace=ws_group.display_name,
336-
name_in_account=account_group.display_name,
337-
temporary_name=temporary_name,
338-
external_id=account_group.external_id,
339-
members=json.dumps([gg.as_dict() for gg in ws_group.members]) if ws_group.members else None,
340-
roles=json.dumps([gg.as_dict() for gg in ws_group.roles]) if ws_group.roles else None,
341-
entitlements=(
342-
json.dumps([gg.as_dict() for gg in ws_group.entitlements]) if ws_group.entitlements else None
343-
),
338+
if not account_group:
339+
logger.info(
340+
f"Couldn't find a matching account group for {ws_group.display_name} group with regex matching"
344341
)
345-
else:
346-
logger.info(f"Couldn't find a match for group {ws_group.display_name}")
342+
continue
343+
yield MigratedGroup(
344+
id_in_workspace=ws_group.id,
345+
name_in_workspace=ws_group.display_name,
346+
name_in_account=account_group.display_name,
347+
temporary_name=temporary_name,
348+
external_id=account_group.external_id,
349+
members=json.dumps([gg.as_dict() for gg in ws_group.members]) if ws_group.members else None,
350+
roles=json.dumps([gg.as_dict() for gg in ws_group.roles]) if ws_group.roles else None,
351+
entitlements=(
352+
json.dumps([gg.as_dict() for gg in ws_group.entitlements]) if ws_group.entitlements else None
353+
),
354+
)
347355

348356

349357
class GroupManager(CrawlerBase[MigratedGroup]):

tests/unit/workspace_access/test_groups.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
from unittest.mock import create_autospec
34

45
import pytest
@@ -636,6 +637,26 @@ def test_snapshot_with_group_matched_by_prefix():
636637
]
637638

638639

640+
def test_snapshot_with_group_matched_by_prefix_not_found(caplog):
641+
caplog.set_level(logging.INFO)
642+
backend = MockBackend()
643+
wsclient = create_autospec(WorkspaceClient)
644+
group = Group(
645+
id="1",
646+
display_name="de_(1234)",
647+
meta=ResourceMeta(resource_type="WorkspaceGroup"),
648+
)
649+
wsclient.groups.list.return_value = [group]
650+
wsclient.groups.get.return_value = group
651+
wsclient.api_client.do.return_value = {
652+
"Resources": [],
653+
}
654+
GroupManager(
655+
backend, wsclient, inventory_database="inv", workspace_group_regex="^", workspace_group_replace="px_"
656+
).snapshot()
657+
assert "Couldn't find a matching account group for de_(1234) group with regex substitution" in caplog.text
658+
659+
639660
def test_snapshot_with_group_matched_by_subset():
640661
backend = MockBackend()
641662
wsclient = create_autospec(WorkspaceClient)
@@ -675,6 +696,26 @@ def test_snapshot_with_group_matched_by_subset():
675696
]
676697

677698

699+
def test_snapshot_with_group_matched_by_subset_not_found(caplog):
700+
caplog.set_level(logging.INFO)
701+
backend = MockBackend()
702+
wsclient = create_autospec(WorkspaceClient)
703+
group = Group(
704+
id="1",
705+
display_name="de_(1234)",
706+
meta=ResourceMeta(resource_type="WorkspaceGroup"),
707+
)
708+
wsclient.groups.list.return_value = [group]
709+
wsclient.groups.get.return_value = group
710+
wsclient.api_client.do.return_value = {
711+
"Resources": [],
712+
}
713+
GroupManager(
714+
backend, wsclient, inventory_database="inv", workspace_group_regex=r"\(([1-9]+)\)", account_group_regex="[1-9]+"
715+
).snapshot()
716+
assert "Couldn't find a matching account group for de_(1234) group with regex matching" in caplog.text
717+
718+
678719
def test_snapshot_with_group_matched_by_external_id():
679720
backend = MockBackend()
680721
wsclient = create_autospec(WorkspaceClient)
@@ -712,6 +753,24 @@ def test_snapshot_with_group_matched_by_external_id():
712753
]
713754

714755

756+
def test_snapshot_with_group_matched_by_external_id_not_found(caplog):
757+
caplog.set_level(logging.INFO)
758+
backend = MockBackend()
759+
wsclient = create_autospec(WorkspaceClient)
760+
group = Group(
761+
id="1",
762+
display_name="de_(1234)",
763+
meta=ResourceMeta(resource_type="WorkspaceGroup"),
764+
)
765+
wsclient.groups.list.return_value = [group]
766+
wsclient.groups.get.return_value = group
767+
wsclient.api_client.do.return_value = {
768+
"Resources": [],
769+
}
770+
GroupManager(backend, wsclient, inventory_database="inv", external_id_match=True).snapshot()
771+
assert "Couldn't find a matching account group for de_(1234) group with external_id" in caplog.text
772+
773+
715774
def test_configure_include_groups():
716775
configure_groups = ConfigureGroups(
717776
MockPrompts(

0 commit comments

Comments
 (0)