Skip to content

Commit 8b62607

Browse files
Allow JSON matching API endpoint for reset job (#582)
Co-authored-by: Jan van der Vegt <[email protected]>
1 parent 7019590 commit 8b62607

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

databricks_cli/jobs/cli.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def create_cli(api_client, json_file, json, version):
6161

6262

6363
@click.command(context_settings=CONTEXT_SETTINGS)
64-
@click.option('--job-id', required=True, type=JobIdClickType(), help=JobIdClickType.help)
64+
@click.option('--job-id', default=None, type=JobIdClickType(), help=JobIdClickType.help)
6565
@click.option('--json-file', default=None, type=click.Path(),
6666
help='File containing partial JSON request to POST to /api/2.*/jobs/reset. '
6767
'For more, read full help message.')
@@ -87,10 +87,25 @@ def reset_cli(api_client, json_file, json, job_id, version):
8787
with open(json_file, 'r') as f:
8888
json = f.read()
8989
deser_json = json_loads(json)
90+
# If the payload is defined using the API definition rather than the CLI
91+
# extract the settings data.
92+
new_settings = deser_json['new_settings'] if (
93+
'new_settings' in deser_json) else deser_json
94+
95+
# If job id is not defined in the call we fall back to checking
96+
# the JSON for a job_id property
97+
if job_id is None:
98+
if 'job_id' in deser_json:
99+
job_id = deser_json['job_id']
100+
else:
101+
raise RuntimeError(
102+
'Either --job-id or a root-level json key "job_id" should be provided')
103+
90104
request_body = {
91105
'job_id': job_id,
92-
'new_settings': deser_json
106+
'new_settings': new_settings
93107
}
108+
94109
JobsApi(api_client).reset_job(request_body, version=version)
95110

96111

tests/jobs/test_cli.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,33 @@ def test_create_cli_json_file(jobs_api_mock, tmpdir):
7474

7575
RESET_JSON = '{"job_name": "test_job"}'
7676

77+
CORRECT_REQUEST_PAYLOAD = {'job_id': 1, 'new_settings': json.loads(RESET_JSON)}
78+
7779

7880
@provide_conf
7981
def test_reset_cli_json(jobs_api_mock):
8082
runner = CliRunner()
8183
runner.invoke(cli.reset_cli, ['--json', RESET_JSON, '--job-id', 1])
82-
assert jobs_api_mock.reset_job.call_args[0][0] == {
83-
'job_id': 1,
84-
'new_settings': json.loads(RESET_JSON)
85-
}
84+
assert jobs_api_mock.reset_job.call_args[0][0] == CORRECT_REQUEST_PAYLOAD
85+
86+
87+
RESET_JSON_NEW_SETTINGS = '{ "new_settings": %s, "job_id": 5 }' % RESET_JSON
88+
89+
90+
@provide_conf
91+
def test_reset_cli_json_new_settings(jobs_api_mock):
92+
runner = CliRunner()
93+
runner.invoke(cli.reset_cli, [
94+
'--json', RESET_JSON_NEW_SETTINGS, '--job-id', 1])
95+
assert jobs_api_mock.reset_job.call_args[0][0] == CORRECT_REQUEST_PAYLOAD
96+
97+
CORRECT_PAYLOAD_NEW_ID = {'job_id': 5, 'new_settings': json.loads(RESET_JSON)}
98+
99+
@provide_conf
100+
def test_reset_cli_json_new_settings_no_job_id(jobs_api_mock):
101+
runner = CliRunner()
102+
runner.invoke(cli.reset_cli, ['--json', RESET_JSON_NEW_SETTINGS])
103+
assert jobs_api_mock.reset_job.call_args[0][0] == CORRECT_PAYLOAD_NEW_ID
86104

87105

88106
LIST_RETURN = {

0 commit comments

Comments
 (0)