Skip to content

Commit 20d1b42

Browse files
authored
Use all account-level groups with matching names to workspace-level groups in case no explicit configuration (#277)
Fixes #272 Fixes #236
1 parent dfa350a commit 20d1b42

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/databricks/labs/ucx/install.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ def _configure(self):
187187
warehouse_id = new_warehouse.id
188188

189189
selected_groups = self._question(
190-
"Comma-separated list of workspace group names to migrate (empty means all)", default="<ALL>"
190+
"Comma-separated list of workspace group names to migrate. If not specified, we'll wse all "
191+
"account-level groups with matching names to workspace-level groups.",
192+
default="<ALL>",
191193
)
192194
backup_group_prefix = self._question("Backup prefix", default="db-temp-")
193195
log_level = self._question("Log level", default="INFO").upper()

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _list_workspace_groups(self) -> list[iam.Group]:
7070

7171
def _list_account_groups(self) -> list[iam.Group]:
7272
# TODO: we should avoid using this method, as it's not documented
73-
# unfortunately, there's no other way to consistently get the list of account groups
73+
# get account-level groups even if they're not (yet) assigned to a workspace
7474
logger.debug("Listing account groups...")
7575
account_groups = [
7676
iam.Group.from_dict(r)
@@ -160,19 +160,25 @@ def prepare_groups_in_environment(self):
160160
"Preparing groups in the current environment. At this step we'll verify that all groups "
161161
"exist and are of the correct type. If some temporary groups are missing, they'll be created"
162162
)
163-
if self.config.selected:
163+
group_names = self.config.selected
164+
if group_names:
164165
logger.info("Using the provided group listing")
165166

166-
for g in self.config.selected:
167+
for g in group_names:
167168
assert g not in self.SYSTEM_GROUPS, f"Cannot migrate system group {g}"
168169
assert self._get_group(g, "workspace"), f"Group {g} not found on the workspace level"
169170
assert self._get_group(g, "account"), f"Group {g} not found on the account level"
170171

171-
self._set_migration_groups(self.config.selected)
172-
else:
173-
logger.info("No group listing provided, all available workspace-level groups will be used")
174-
available_group_names = [g.display_name for g in self._workspace_groups]
175-
self._set_migration_groups(groups_names=available_group_names)
172+
if not group_names:
173+
logger.info(
174+
"No group listing provided, all available workspace-level groups that have an account-level "
175+
"group with the same name will be used"
176+
)
177+
ws_group_names = {_.display_name for _ in self._workspace_groups}
178+
ac_group_names = {_.display_name for _ in self._account_groups}
179+
group_names = list(ws_group_names.intersection(ac_group_names))
180+
181+
self._set_migration_groups(group_names)
176182
logger.info("Environment prepared successfully")
177183

178184
@property

tests/integration/workspace_access/test_groups.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ def test_prepare_environment(ws, make_ucx_group):
1919
assert _ws_members == _backup_members
2020

2121

22+
def test_prepare_environment_no_groups_selected(ws, make_ucx_group, make_group, make_acc_group):
23+
make_group()
24+
make_acc_group()
25+
for_test = [make_ucx_group(), make_ucx_group()]
26+
27+
group_manager = GroupManager(ws, GroupsConfig(auto=True))
28+
group_manager.prepare_groups_in_environment()
29+
30+
group_migration_state = group_manager.migration_groups_provider
31+
for _info in group_migration_state.groups:
32+
_ws = ws.groups.get(id=_info.workspace.id)
33+
_backup = ws.groups.get(id=_info.backup.id)
34+
# https://github.com/databricks/databricks-sdk-py/pull/361 may fix the NPE gotcha with empty members
35+
_ws_members = sorted([m.value for m in _ws.members])
36+
_backup_members = sorted([m.value for m in _backup.members])
37+
assert _ws_members == _backup_members
38+
39+
for g, _ in for_test:
40+
assert group_migration_state.get_by_workspace_group_name(g.display_name) is not None
41+
42+
2243
def test_group_listing(ws: WorkspaceClient, make_ucx_group):
2344
ws_group, acc_group = make_ucx_group()
2445
manager = GroupManager(ws, GroupsConfig(selected=[ws_group.display_name]))

0 commit comments

Comments
 (0)