Skip to content

Commit 61b6715

Browse files
authored
Let migrate-acls command run as collection (#2664)
## Changes Let `migrate-acls` command to run as collection ### Linked issues Resolves #2611 ### Functionality - [x] modified existing command: `databricks labs ucx migrate-acls` ### Tests - [x] manually tested - [x] added unit tests - [ ] ~added integration tests~ : Covering after #2507
1 parent aeb09a8 commit 61b6715

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

labs.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,14 @@ commands:
258258

259259
- name: migrate-acls
260260
description: |
261-
Migrate ACLs from legacy metastore to UC metastore.
261+
Migrate access control lists from legacy metastore to UC metastore.
262262
flags:
263263
- name: target-catalog
264264
description: (Optional) Target catalog to migrate ACLs to. Used for HMS-FED ACLs migration.
265265
- name: hms-fed
266266
description: (Optional) Migrate HMS-FED ACLs. If not provided, HMS ACLs will be migrated for migrated tables.
267+
- name: run-as-collection
268+
description: (Optional) Run the command for the collection of workspaces with ucx installed. Default is False.
267269

268270
- name: migrate-dbsql-dashboards
269271
description: Migrate DBSQL dashboards by replacing legacy HMS tables in DBSQL queries with the corresponding new UC tables.

src/databricks/labs/ucx/cli.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,24 @@ def migrate_tables(
572572

573573

574574
@ucx.command
575-
def migrate_acls(w: WorkspaceClient, *, ctx: WorkspaceContext | None = None, **named_parameters):
575+
def migrate_acls(
576+
w: WorkspaceClient,
577+
*,
578+
ctx: WorkspaceContext | None = None,
579+
run_as_collection: bool = False,
580+
a: AccountClient | None = None,
581+
**named_parameters,
582+
):
576583
"""
577584
Migrate the ACLs for migrated tables and view. Can work with hms federation or other table migration scenarios.
578585
"""
579-
if ctx is None:
580-
ctx = WorkspaceContext(w)
581-
ctx.acl_migrator.migrate_acls(
582-
target_catalog=named_parameters.get("target_catalog"),
583-
hms_fed=named_parameters.get("hms_fed", False),
584-
)
586+
if ctx:
587+
workspace_contexts = [ctx]
588+
else:
589+
workspace_contexts = _get_workspace_contexts(w, a, run_as_collection, **named_parameters)
590+
target_catalog, hms_fed = named_parameters.get("target_catalog"), named_parameters.get("hms_fed", False)
591+
for workspace_context in workspace_contexts:
592+
workspace_context.acl_migrator.migrate_acls(target_catalog=target_catalog, hms_fed=hms_fed)
585593

586594

587595
@ucx.command

tests/unit/test_cli.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
join_collection,
4040
logs,
4141
manual_workspace_info,
42+
migrate_acls,
4243
migrate_credentials,
4344
migrate_dbsql_dashboards,
4445
migrate_local_code,
@@ -100,6 +101,12 @@ def create_workspace_client_mock(workspace_id: int) -> WorkspaceClient:
100101
}
101102
}
102103
),
104+
'/Users/foo/.ucx/workspaces.json': json.dumps(
105+
[
106+
{'workspace_id': 123, 'workspace_name': '123'},
107+
{'workspace_id': 456, 'workspace_name': '456'},
108+
]
109+
),
103110
"/Users/foo/.ucx/uc_roles_access.csv": "role_arn,resource_type,privilege,resource_path\n"
104111
"arn:aws:iam::123456789012:role/role_name,s3,READ_FILES,s3://labsawsbucket/",
105112
"/Users/foo/.ucx/azure_storage_account_info.csv": "prefix,client_id,principal,privilege,type,directory_id\ntest,test,test,test,Application,test",
@@ -265,9 +272,9 @@ def test_manual_workspace_info(ws):
265272
manual_workspace_info(ws, prompts)
266273

267274

268-
def test_create_table_mapping(ws, acc_client):
275+
def test_create_table_mapping_raises_value_error_because_no_tables_found(ws, acc_client) -> None:
269276
ctx = WorkspaceContext(ws)
270-
with pytest.raises(ValueError, match='databricks labs ucx sync-workspace-info'):
277+
with pytest.raises(ValueError, match="No tables found. .*"):
271278
create_table_mapping(ws, ctx, False, acc_client)
272279

273280

@@ -434,6 +441,23 @@ def test_save_storage_and_principal_gcp(ws):
434441
principal_prefix_access(ws, ctx=ctx)
435442

436443

444+
@pytest.mark.parametrize("run_as_collection", [True, False])
445+
def test_migrate_acls_calls_workspace_id(
446+
run_as_collection,
447+
workspace_clients,
448+
acc_client,
449+
) -> None:
450+
if not run_as_collection:
451+
workspace_clients = [workspace_clients[0]]
452+
migrate_acls(
453+
workspace_clients[0],
454+
run_as_collection=run_as_collection,
455+
a=acc_client,
456+
)
457+
for workspace_client in workspace_clients:
458+
workspace_client.get_workspace_id.assert_called()
459+
460+
437461
def test_migrate_credentials_azure(ws, acc_client):
438462
ws.config.is_azure = True
439463
ws.workspace.upload.return_value = "test"

0 commit comments

Comments
 (0)