Skip to content

Commit 8771190

Browse files
authored
[SC-71389] Better exceptions for Pipelines CLI (#365)
Add validation on missing pipeline id and throw ValueError instead of RuntimeError.
1 parent 5da224c commit 8771190

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

databricks_cli/pipelines/cli.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def deploy_cli(api_client, spec_arg, spec, allow_duplicate_names, pipeline_id):
8888
databricks pipelines deploy --pipeline-id 1234 --spec example.json
8989
"""
9090
if bool(spec_arg) == bool(spec):
91-
raise RuntimeError('The spec should be provided either by an option or argument')
91+
raise ValueError('The spec should be provided either by an option or argument')
9292
src = spec_arg if bool(spec_arg) else spec
9393
spec_obj = _read_spec(src)
9494
if not pipeline_id and 'id' not in spec_obj:
@@ -254,7 +254,7 @@ def _read_spec(src):
254254
except json_parse_exception as e:
255255
error_and_quit("Invalid JSON provided in spec\n{}".format(e))
256256
else:
257-
raise RuntimeError('The provided file extension for the spec is not supported')
257+
raise ValueError('The provided file extension for the spec is not supported')
258258

259259

260260
def _get_pipeline_url(api_client, pipeline_id):
@@ -271,27 +271,11 @@ def _write_spec(src, spec):
271271
f.write(data)
272272

273273

274-
def _get_pipeline_id(spec_arg, spec, pipeline_id):
275-
"""
276-
Ensures that the user has either specified a spec (either through argument or option) or a
277-
pipeline ID directly, and returns the pipeline id to use.
278-
"""
279-
# Only one out of spec/pipeline_id/spec_arg should be supplied
280-
if bool(spec_arg) + bool(spec) + bool(pipeline_id) != 1:
281-
raise RuntimeError('Either spec should be provided as an argument '
282-
'or option, or the pipeline-id should be provided')
283-
if bool(spec_arg) or bool(spec):
284-
src = spec_arg if bool(spec_arg) else spec
285-
pipeline_id = _read_spec(src)["id"]
286-
_validate_pipeline_id(pipeline_id)
287-
return pipeline_id
288-
289-
290274
def _validate_pipeline_id(pipeline_id):
291275
"""
292-
Checks if the pipeline_id only contain -, _ and alphanumeric characters
276+
Checks if the pipeline_id is not empty and only contains -, _ and alphanumeric characters
293277
"""
294-
if len(pipeline_id) == 0:
278+
if pipeline_id is None or len(pipeline_id) == 0:
295279
error_and_quit(u'Empty pipeline id provided')
296280
if not set(pipeline_id) <= PIPELINE_ID_PERMITTED_CHARACTERS:
297281
message = u'Pipeline id {} has invalid character(s)\n'.format(pipeline_id)

tests/pipelines/test_cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ def test_deploy_update_delete_cli_correct_spec_extensions(pipelines_api_mock, tm
185185
pipelines_api_mock.reset_mock()
186186

187187

188+
@provide_conf
189+
def test_deploy_with_invalid_spec_extension(pipelines_api_mock):
190+
pipelines_api_mock.deploy = mock.Mock()
191+
result = CliRunner().invoke(cli.deploy_cli, ['--spec', 'spec.invalid'])
192+
assert result.exit_code == 1
193+
assert "ValueError: The provided file extension for the spec is not " \
194+
"supported" in result.stdout
195+
assert pipelines_api_mock.deploy.call_count == 0
196+
197+
188198
@provide_conf
189199
def test_cli_id(pipelines_api_mock):
190200
for command in [cli.reset_cli, cli.stop_cli, cli.run_cli]:
@@ -310,3 +320,12 @@ def test_deploy_pipeline_conflicting_ids(pipelines_api_mock, tmpdir):
310320
assert "ValueError: The ID provided in --pipeline_id 'fake' is different from the id " \
311321
"provided in the spec '123'." in result.stdout
312322
assert pipelines_api_mock.deploy.call_count == 0
323+
324+
325+
@provide_conf
326+
def test_deploy_with_missing_spec(pipelines_api_mock):
327+
pipelines_api_mock.deploy = mock.Mock()
328+
result = CliRunner().invoke(cli.deploy_cli, [])
329+
assert result.exit_code == 1
330+
assert "ValueError: The spec should be provided" in result.stdout
331+
assert pipelines_api_mock.deploy.call_count == 0

0 commit comments

Comments
 (0)