Skip to content

Commit 0b474d5

Browse files
authored
Added ability to run create-missing-principals command as collection (#2675)
This PR enables create-missing-principals command to run as a collection. Linked issues #1782 Resolves #2606 ### Functionality - [ ] modified existing command: `databricks labs ucx ...` ### Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [ ] added unit tests - [ ] verified on staging environment (screenshot attached)
1 parent 2cfb899 commit 0b474d5

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

labs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ commands:
157157
description: (Optional) IAM policy Name to be specified for the UC roles. (default:UC_POLICY)
158158
- name: single-role
159159
description: (Optional) Create a single role for all S3 locations. (default:False)
160+
- name: run-as-collection
161+
description: (Optional) boolean flag to indicate to run the cmd as a collection. Default is False.
160162

161163
- name: delete-missing-principals
162164
description: For AWS, this command identifies all the UC roles that are created through the create-missing-principals

src/databricks/labs/ucx/cli.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ def create_missing_principals(
371371
w: WorkspaceClient,
372372
prompts: Prompts,
373373
ctx: WorkspaceContext | None = None,
374+
run_as_collection: bool = False,
375+
a: AccountClient | None = None,
374376
single_role: bool = False,
375377
role_name="UC_ROLE",
376378
policy_name="UC_POLICY",
@@ -380,11 +382,17 @@ def create_missing_principals(
380382
For AWS, this command identifies all the S3 locations that are missing a UC compatible role and creates them.
381383
By default, it will create a role per S3 location. Set the optional single_role parameter to True to create a single role for all S3 locations.
382384
"""
383-
if not ctx:
384-
ctx = WorkspaceContext(w, named_parameters)
385-
if ctx.is_aws:
386-
return ctx.iam_role_creation.run(prompts, single_role=single_role, role_name=role_name, policy_name=policy_name)
387-
raise ValueError("Unsupported cloud provider")
385+
workspace_contexts = _get_workspace_contexts(w, a, run_as_collection, **named_parameters)
386+
if ctx:
387+
workspace_contexts = [ctx]
388+
if w.config.is_aws:
389+
for workspace_ctx in workspace_contexts:
390+
logger.info(f"Running cmd for workspace {workspace_ctx.workspace_client.get_workspace_id()}")
391+
workspace_ctx.iam_role_creation.run(
392+
prompts, single_role=single_role, role_name=role_name, policy_name=policy_name
393+
)
394+
else:
395+
raise ValueError("Unsupported cloud provider")
388396

389397

390398
@ucx.command

tests/unit/test_cli.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -966,27 +966,30 @@ def test_migrate_tables_calls_external_tables_ctas_job_run_now(ws) -> None:
966966
ws.jobs.wait_get_run_job_terminated_or_skipped.call_count = 2
967967

968968

969-
def test_create_missing_principal_aws(ws):
969+
def test_create_missing_principal_aws(ws, acc_client):
970+
ws.config.is_aws = True
970971
aws_resource_permissions = create_autospec(AWSResourcePermissions)
971972
ctx = WorkspaceContext(ws).replace(is_aws=True, is_azure=False, aws_resource_permissions=aws_resource_permissions)
972973
prompts = MockPrompts({'.*': 'yes'})
973-
create_missing_principals(ws, prompts=prompts, ctx=ctx)
974+
create_missing_principals(ws, prompts=prompts, ctx=ctx, a=acc_client)
974975
aws_resource_permissions.create_uc_roles.assert_called_once()
975976

976977

977-
def test_create_missing_principal_aws_not_approved(ws):
978+
def test_create_missing_principal_aws_not_approved(ws, acc_client):
979+
ws.config.is_aws = True
978980
aws_resource_permissions = create_autospec(AWSResourcePermissions)
979981
ctx = WorkspaceContext(ws).replace(is_aws=True, is_azure=False, aws_resource_permissions=aws_resource_permissions)
980982
prompts = MockPrompts({'.*': 'No'})
981-
create_missing_principals(ws, prompts=prompts, ctx=ctx)
983+
create_missing_principals(ws, prompts=prompts, ctx=ctx, a=acc_client)
982984
aws_resource_permissions.create_uc_roles.assert_not_called()
983985

984986

985-
def test_create_missing_principal_azure(ws, caplog):
987+
def test_create_missing_principal_azure(ws, caplog, acc_client):
988+
ws.config.is_aws = False
986989
ctx = WorkspaceContext(ws).replace(is_aws=False, is_azure=True)
987990
prompts = MockPrompts({'.*': 'yes'})
988991
with pytest.raises(ValueError) as failure:
989-
create_missing_principals(ws, prompts=prompts, ctx=ctx)
992+
create_missing_principals(ws, prompts=prompts, ctx=ctx, a=acc_client)
990993
assert str(failure.value) == "Unsupported cloud provider"
991994

992995

0 commit comments

Comments
 (0)