Skip to content

Commit 3b54920

Browse files
authored
Let validate-external-locations command run as collection (#2649)
## Changes Let `validate-external-locations` command to run as collection ### Linked issues Resolves #2607 ### Functionality - [x] modified existing command: `databricks labs ucx validate-external-locations` ### Tests - [x] manually tested - [x] added unit tests - [ ] ~added integration tests~ : Covering after #2507
1 parent ee024cd commit 3b54920

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

labs.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ commands:
7676
description: boolean flag to indicate to run the cmd as a collection. Default is False.
7777

7878
- name: validate-external-locations
79-
description: validates and provides mapping to external table to external location and shared generation tf scripts
79+
description: |
80+
Validates external locations and provides Terraform script that maps external locations to external table.
81+
flags:
82+
- name: run-as-collection
83+
description: Run the command for the collection of workspaces with ucx installed. Default is False.
8084

8185
- name: repair-run
8286
description: Repair Run the Failed Job

src/databricks/labs/ucx/cli.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,24 @@ def create_table_mapping(
171171

172172

173173
@ucx.command
174-
def validate_external_locations(w: WorkspaceClient, prompts: Prompts):
174+
def validate_external_locations(
175+
w: WorkspaceClient,
176+
prompts: Prompts,
177+
ctx: WorkspaceContext | None = None,
178+
run_as_collection: bool = False,
179+
a: AccountClient | None = None,
180+
):
175181
"""validates and provides mapping to external table to external location and shared generation tf scripts"""
176-
ctx = WorkspaceContext(w)
177-
path = ctx.external_locations.save_as_terraform_definitions_on_workspace(ctx.installation)
178-
if path and prompts.confirm(f"external_locations.tf file written to {path}. Do you want to open it?"):
179-
webbrowser.open(f"{w.config.host}/#workspace{path}")
182+
if ctx:
183+
workspace_contexts = [ctx]
184+
else:
185+
workspace_contexts = _get_workspace_contexts(w, a, run_as_collection)
186+
for workspace_context in workspace_contexts:
187+
path = workspace_context.external_locations.save_as_terraform_definitions_on_workspace(
188+
workspace_context.installation
189+
)
190+
if path and prompts.confirm(f"external_locations.tf file written to {path}. Do you want to open it?"):
191+
webbrowser.open(f"{w.config.host}/#workspace{path}")
180192

181193

182194
@ucx.command

tests/unit/test_cli.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,23 @@ def test_create_table_mapping_raises_value_error_because_no_tables_found(ws, acc
278278
create_table_mapping(ws, ctx, False, acc_client)
279279

280280

281-
def test_validate_external_locations(ws):
282-
validate_external_locations(ws, MockPrompts({}))
283-
281+
def test_validate_external_locations(ws) -> None:
282+
validate_external_locations(ws, MockPrompts({}), ctx=WorkspaceContext(ws))
284283
ws.statement_execution.execute_statement.assert_called()
285284

286285

286+
def test_validate_external_locations_runs_as_collection(workspace_clients, acc_client) -> None:
287+
validate_external_locations(
288+
workspace_clients[0],
289+
MockPrompts({}),
290+
run_as_collection=True,
291+
a=acc_client,
292+
)
293+
294+
for workspace_client in workspace_clients:
295+
workspace_client.statement_execution.execute_statement.assert_called()
296+
297+
287298
def test_ensure_assessment_run(ws, acc_client):
288299
ws.jobs.wait_get_run_job_terminated_or_skipped.return_value = Run(
289300
state=RunState(result_state=RunResultState.SUCCESS), start_time=0, end_time=1000, run_duration=1000

0 commit comments

Comments
 (0)