From 090023387812f9601b53ea19c515cfdd6d795aba Mon Sep 17 00:00:00 2001 From: Dino Wernli Date: Tue, 27 Dec 2022 17:39:01 +0100 Subject: [PATCH] Make use of the new name filtering in calls to list jobs. --- databricks_cli/jobs/api.py | 5 +++-- databricks_cli/jobs/cli.py | 11 +++++++---- databricks_cli/sdk/service.py | 4 +++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/databricks_cli/jobs/api.py b/databricks_cli/jobs/api.py index 6b554c79..39fec1ff 100644 --- a/databricks_cli/jobs/api.py +++ b/databricks_cli/jobs/api.py @@ -32,9 +32,9 @@ def create_job(self, json, headers=None, version=None): version=version) def list_jobs(self, job_type=None, expand_tasks=None, offset=None, limit=None, headers=None, - version=None): + version=None, name_filter=None): resp = self.client.list_jobs(job_type=job_type, expand_tasks=expand_tasks, offset=offset, - limit=limit, headers=headers, version=version) + limit=limit, headers=headers, version=version, name_filter=name_filter) if 'jobs' not in resp: resp['jobs'] = [] return resp @@ -56,6 +56,7 @@ def run_now(self, job_id, jar_params, notebook_params, python_params, spark_subm idempotency_token, headers=headers, version=version) def _list_jobs_by_name(self, name, headers=None): + #TODO DONOTMERGE use the more efficient filter-by-name mechanism if API at least 2.1 jobs = self.list_jobs(headers=headers)['jobs'] result = list(filter(lambda job: job['settings']['name'] == name, jobs)) return result diff --git a/databricks_cli/jobs/cli.py b/databricks_cli/jobs/cli.py index 095d0183..117bbf3a 100644 --- a/databricks_cli/jobs/cli.py +++ b/databricks_cli/jobs/cli.py @@ -130,12 +130,14 @@ def _jobs_to_table(jobs_json): @click.option('--all', '_all', is_flag=True, help='Lists all jobs by executing sequential calls to the API ' + '(only available in API 2.1).') +@click.option('--name', 'name_filter', default=None, type=str, + help='If provided, only returns jobs that match the supplied name (only available in API 2.1).') @api_version_option @debug_option @profile_option @eat_exceptions @provide_api_client -def list_cli(api_client, output, job_type, version, expand_tasks, offset, limit, _all): +def list_cli(api_client, output, job_type, version, expand_tasks, offset, limit, _all, name_filter): """ Lists the jobs in the Databricks Job Service. @@ -151,9 +153,9 @@ def list_cli(api_client, output, job_type, version, expand_tasks, offset, limit, """ check_version(api_client, version) api_version = version or api_client.jobs_api_version - if api_version != '2.1' and (expand_tasks or offset or limit or _all): + if api_version != '2.1' and (expand_tasks or offset or limit or _all or name_filter): click.echo(click.style('ERROR', fg='red') + ': the options --expand-tasks, ' + - '--offset, --limit, and --all are only available in API 2.1', err=True) + '--offset, --limit, --all, and --name are only available in API 2.1', err=True) return jobs_api = JobsApi(api_client) has_more = True @@ -163,7 +165,8 @@ def list_cli(api_client, output, job_type, version, expand_tasks, offset, limit, limit = 20 while has_more: jobs_json = jobs_api.list_jobs(job_type=job_type, expand_tasks=expand_tasks, - offset=offset, limit=limit, version=version) + offset=offset, limit=limit, version=version, + name_filter=name_filter) jobs += jobs_json['jobs'] if 'jobs' in jobs_json else [] has_more = jobs_json.get('has_more', False) and _all if has_more: diff --git a/databricks_cli/sdk/service.py b/databricks_cli/sdk/service.py index 9e76f40d..0c6e6430 100755 --- a/databricks_cli/sdk/service.py +++ b/databricks_cli/sdk/service.py @@ -287,7 +287,7 @@ def get_job(self, job_id, headers=None, version=None): ) def list_jobs( - self, job_type=None, expand_tasks=None, limit=None, offset=None, headers=None, version=None + self, job_type=None, expand_tasks=None, limit=None, offset=None, headers=None, version=None, name_filter=None ): _data = {} if job_type is not None: @@ -298,6 +298,8 @@ def list_jobs( _data['limit'] = limit if offset is not None: _data['offset'] = offset + if name_filter is not None: + _data['name'] = name_filter return self.client.perform_query( 'GET', '/jobs/list', data=_data, headers=headers, version=version )