Skip to content

Commit e516998

Browse files
authored
Remove exception and added proper logging for groups in the list that… (#357)
Closes #312 Closes #313
1 parent 40919a7 commit e516998

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,43 +161,59 @@ def prepare_groups_in_environment(self):
161161
"exist and are of the correct type. If some temporary groups are missing, they'll be created"
162162
)
163163
group_names = self.config.selected
164+
valid_group_names = []
164165
if group_names:
165166
logger.info("Using the provided group listing")
166167

167168
for g in group_names:
168-
assert g not in self.SYSTEM_GROUPS, f"Cannot migrate system group {g}"
169-
assert self._get_group(g, "workspace"), f"Group {g} not found on the workspace level"
170-
assert self._get_group(g, "account"), f"Group {g} not found on the account level"
171-
172-
if not group_names:
169+
if g in self.SYSTEM_GROUPS:
170+
logger.info(f"Cannot migrate system group {g}. {g} will be skipped.")
171+
continue
172+
if not self._get_group(g, "workspace"):
173+
logger.info(f"Group {g} not found on the workspace level. {g} will be skipped.")
174+
continue
175+
if not self._get_group(g, "account"):
176+
logger.info(
177+
f"Group {g} not found on the account level. {g} will be skipped. You can add {g} "
178+
f"to the account and rerun the job."
179+
)
180+
continue
181+
valid_group_names.append(g)
182+
else:
173183
logger.info(
174184
"No group listing provided, all available workspace-level groups that have an account-level "
175185
"group with the same name will be used"
176186
)
177187
ws_group_names = {_.display_name for _ in self._workspace_groups}
178188
ac_group_names = {_.display_name for _ in self._account_groups}
179-
group_names = list(ws_group_names.intersection(ac_group_names))
189+
valid_group_names = list(ws_group_names.intersection(ac_group_names))
180190

181-
self._set_migration_groups(group_names)
191+
self._set_migration_groups(valid_group_names)
182192
logger.info("Environment prepared successfully")
183193

184194
def has_groups(self) -> bool:
185195
return len(self._migration_state.groups) > 0
186196

187197
@property
188198
def migration_groups_provider(self) -> GroupMigrationState:
189-
assert len(self._migration_state.groups) > 0, "Migration groups were not loaded or initialized"
199+
if len(self._migration_state.groups) == 0:
200+
logger.info("No groups were loaded or initialized, nothing to do")
190201
return self._migration_state
191202

192203
def replace_workspace_groups_with_account_groups(self):
193204
logger.info("Replacing the workspace groups with account-level groups")
205+
if len(self._migration_state.groups) == 0:
206+
logger.info("No groups were loaded or initialized, nothing to do")
207+
return
194208
ThreadedExecution.gather(
195209
"groups: workspace -> account",
196210
[partial(self._replace_group, migration_info) for migration_info in self.migration_groups_provider.groups],
197211
)
198212
logger.info("Workspace groups were successfully replaced with account-level groups")
199213

200214
def delete_backup_groups(self):
215+
if len(self._migration_state.groups) == 0:
216+
return
201217
logger.info(
202218
f"Deleting the workspace-level backup groups. "
203219
f"In total, {len(self.migration_groups_provider.groups)} group(s) to be deleted"

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def inventorize_permissions(self):
3838

3939
def apply_group_permissions(self, migration_state: GroupMigrationState, destination: Literal["backup", "account"]):
4040
# list shall be sorted prior to using group by
41+
if len(migration_state.groups) == 0:
42+
logger.info("No valid groups selected, nothing to do.")
43+
return
4144
items = sorted(self._load_all(), key=lambda i: i.object_type)
4245
logger.info(
4346
f"Applying the permissions to {destination} groups. "

tests/unit/workspace_access/test_groups.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_prepare_groups_in_environment_with_multiple_groups_in_conf_should_retur
216216
assert compare(manager._migration_state.groups, [ds_group_info, de_group_info])
217217

218218

219-
def test_prepare_groups_in_environment_should_throw_when_account_group_doesnt_exist():
219+
def test_prepare_groups_in_environment_should_not_throw_when_account_group_doesnt_exist():
220220
de_group = Group(display_name="de", meta=ResourceMeta(resource_type="WorkspaceGroup"))
221221

222222
client = Mock()
@@ -227,9 +227,8 @@ def test_prepare_groups_in_environment_should_throw_when_account_group_doesnt_ex
227227
group_conf = GroupsConfig(selected=["de"], backup_group_prefix="dbr_backup_")
228228
manager = GroupManager(client, group_conf)
229229

230-
with pytest.raises(AssertionError) as e_info:
231-
manager.prepare_groups_in_environment()
232-
assert str(e_info.value) == "Group de not found on the account level"
230+
manager.prepare_groups_in_environment()
231+
assert len(manager.migration_groups_provider.groups) == 0
233232

234233

235234
def test_prepare_groups_in_environment_should_throw_when_workspace_group_doesnt_exist():
@@ -332,6 +331,44 @@ def test_replace_workspace_groups_with_account_groups_should_call_delete_and_do(
332331
)
333332

334333

334+
def test_system_groups():
335+
client = Mock()
336+
test_ws_group = Group(display_name="admins", meta=ResourceMeta(resource_type="WorkspaceGroup"))
337+
test_acc_group = Group(display_name="admins", meta=ResourceMeta(resource_type="Group"))
338+
backup_group_id = "100"
339+
client.groups.list.return_value = [test_ws_group]
340+
client.groups.create.return_value = Group(
341+
display_name="dbr_backup_de", meta=ResourceMeta(resource_type="WorkspaceGroup"), id=backup_group_id
342+
)
343+
client.api_client.do.return_value = {
344+
"Resources": [g.as_dict() for g in [test_acc_group]],
345+
}
346+
347+
group_conf = GroupsConfig(backup_group_prefix="dbr_backup_", selected=["admins"])
348+
manager = GroupManager(client, group_conf)
349+
manager.prepare_groups_in_environment()
350+
assert len(manager._migration_state.groups) == 0
351+
352+
353+
def test_workspace_only_groups():
354+
client = Mock()
355+
test_ws_group = Group(display_name="ws_group", meta=ResourceMeta(resource_type="WorkspaceGroup"))
356+
test_acc_group = Group(display_name="acc_group", meta=ResourceMeta(resource_type="Group"))
357+
backup_group_id = "100"
358+
client.groups.list.return_value = [test_ws_group, test_acc_group]
359+
client.groups.create.return_value = Group(
360+
display_name="dbr_backup_de", meta=ResourceMeta(resource_type="WorkspaceGroup"), id=backup_group_id
361+
)
362+
client.api_client.do.return_value = {
363+
"Resources": [g.as_dict() for g in [test_acc_group]],
364+
}
365+
366+
group_conf = GroupsConfig(backup_group_prefix="dbr_backup_", selected=["ws_group"])
367+
manager = GroupManager(client, group_conf)
368+
manager.prepare_groups_in_environment()
369+
assert len(manager._migration_state.groups) == 0
370+
371+
335372
def test_delete_backup_groups():
336373
client = Mock()
337374

tests/unit/workspace_access/test_manager.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33

44
import pytest
55
from databricks.sdk.service import iam
6+
from databricks.sdk.service.iam import Group, ResourceMeta
67

78
from databricks.labs.ucx.mixins.sql import Row
9+
from databricks.labs.ucx.workspace_access.groups import (
10+
GroupMigrationState,
11+
MigrationGroupInfo,
12+
)
813
from databricks.labs.ucx.workspace_access.manager import PermissionManager, Permissions
914

1015
from ..framework.mocks import MockBackend
@@ -141,7 +146,16 @@ def test_manager_apply(mocker):
141146
"cluster-policies": mock_applier,
142147
},
143148
)
144-
pm.apply_group_permissions(MagicMock(), "backup")
149+
group_migration_state: GroupMigrationState = MagicMock()
150+
group_migration_state.groups = [
151+
MigrationGroupInfo(
152+
Group(display_name="group", meta=ResourceMeta(resource_type="WorkspaceGroup")),
153+
Group(display_name="group_backup", meta=ResourceMeta(resource_type="WorkspaceGroup")),
154+
Group(display_name="group", meta=ResourceMeta(resource_type="AccountGroup")),
155+
)
156+
]
157+
158+
pm.apply_group_permissions(group_migration_state, "backup")
145159

146160
assert {"test2 test2 backup", "test test backup"} == applied_items
147161

@@ -155,5 +169,4 @@ def test_unregistered_support():
155169
}
156170
)
157171
pm = PermissionManager(b, "test", [], {})
158-
with pytest.raises(ValueError):
159-
pm.apply_group_permissions(migration_state=MagicMock(), destination="backup")
172+
pm.apply_group_permissions(migration_state=MagicMock(), destination="backup")

0 commit comments

Comments
 (0)