From 7aab1c3ed1d17d1cc436f6b623c340f7184d1686 Mon Sep 17 00:00:00 2001 From: Fabian Reisegger Date: Thu, 25 Jan 2024 11:22:20 +0100 Subject: [PATCH] Add scheduled-jobs edit command --- CHANGES.rst | 2 + croud/__main__.py | 33 ++++++++++++- croud/scheduledjobs/commands.py | 23 +++++++++ tests/commands/test_scheduled_jobs.py | 71 ++++++++++++++++++++++++++- 4 files changed, 127 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 9c720bec..1301a1c8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,8 @@ Changes for croud Unreleased ========== +- Added the ``scheduled-jobs edit`` command. + - Added support for scheduling sql jobs with the ``scheduled-jobs`` commands. 1.10.1 - 2024/01/11 diff --git a/croud/__main__.py b/croud/__main__.py index ea891a07..bb7b63f7 100644 --- a/croud/__main__.py +++ b/croud/__main__.py @@ -112,6 +112,7 @@ from croud.scheduledjobs.commands import ( create_scheduled_job, delete_scheduled_job, + edit_scheduled_job, get_scheduled_job_log, get_scheduled_jobs, ) @@ -1496,7 +1497,7 @@ help="The sql statement the job should run." ), Argument( - "--enabled", type=bool, required=True, + "--enabled", type=str, required=True, help="Enable or disable the job." ) ], @@ -1539,6 +1540,36 @@ ), ], "resolver": delete_scheduled_job, + }, + "edit": { + "help": "Edit specified scheduled sql job.", + "extra_args": [ + Argument( + "--job-id", type=str, required=True, + help="The id of the job to edit." + ), + Argument( + "--cluster-id", type=str, required=True, + help="The cluster id where the job was created." + ), + Argument( + "--name", type=str, required=True, + help="The name of the sql job." + ), + Argument( + "--sql", type=str, required=True, + help="The sql statement of the sql job." + ), + Argument( + "--cron", type=str, required=True, + help="Cron schedule of the sql job." + ), + Argument( + "--enabled", type=str, required=True, + help="Enable or disable the sql job." + ), + ], + "resolver": edit_scheduled_job, } } }, diff --git a/croud/scheduledjobs/commands.py b/croud/scheduledjobs/commands.py index 949a3ee3..77c5f7d6 100644 --- a/croud/scheduledjobs/commands.py +++ b/croud/scheduledjobs/commands.py @@ -96,6 +96,29 @@ def delete_scheduled_job(args: Namespace) -> None: ) +@grand_central_jwt_token +def edit_scheduled_job(args: Namespace) -> None: + body = { + "name": args.name, + "cron": args.cron, + "sql": args.sql, + "enabled": args.enabled, + } + + client = _get_gc_client(args) + + data, errors = client.put(f"/api/scheduled-jobs/{args.job_id}", body=body) + print_response( + data=data, + errors=errors, + keys=["name", "id", "sql", "cron", "enabled"], + output_fmt=get_output_format(args), + ) + + if errors or not data: + return + + def _get_gc_client(args: Namespace) -> Client: client = Client.from_args(args) cluster, _ = client.get(f"/api/v2/clusters/{args.cluster_id}/") diff --git a/tests/commands/test_scheduled_jobs.py b/tests/commands/test_scheduled_jobs.py index 591f5de0..4569ea02 100644 --- a/tests/commands/test_scheduled_jobs.py +++ b/tests/commands/test_scheduled_jobs.py @@ -63,7 +63,7 @@ def mock_call(*args, **kwargs): "name": "test-job", "cron": "1 1 * * *", "sql": "CREATE TABLE test (id TEXT)", - "enabled": True, + "enabled": "True", }, any_times=True, ) @@ -213,3 +213,72 @@ def mock_call(*args, **kwargs): f"/api/scheduled-jobs/{job_id}", any_times=True, ) + + +@mock.patch.object(Client, "request", return_value=({}, None)) +def test_edit_scheduled_job(mock_request): + job_id = gen_uuid() + + def mock_call(*args, **kwargs): + if args[0] == RequestMethod.GET and "/jwt/" in args[1]: + return { + "token": "xyz", + "expiry": "01.02.2024", + }, None + if args[0] == RequestMethod.GET: + return {"fqdn": "my.cluster.cloud", "name": "mycluster"}, None + if args[0] == RequestMethod.PUT: + return { + "name": "test-job-edit", + "id": gen_uuid(), + "cron": "2 2 * * *", + "sql": "CREATE TABLE test (id TEXT)", + "enabled": False, + "next_run_time": "02.02.2024", + }, None + return None, None + + mock_request.side_effect = mock_call + + cluster_id = gen_uuid() + call_command( + "croud", + "scheduled-jobs", + "edit", + "--job-id", + job_id, + "--cluster-id", + cluster_id, + "--name", + "test-job-edit", + "--cron", + "2 2 * * *", + "--sql", + "CREATE TABLE test (id TEXT)", + "--enabled", + "False", + ) + assert_rest( + mock_request, + RequestMethod.GET, + f"/api/v2/clusters/{cluster_id}/jwt/", + any_times=True, + ) + assert_rest( + mock_request, + RequestMethod.GET, + f"/api/v2/clusters/{cluster_id}/", + any_times=True, + ) + assert_rest( + mock_request, + RequestMethod.PUT, + f"/api/scheduled-jobs/{job_id}", + body={ + "name": "test-job-edit", + "cron": "2 2 * * *", + "sql": "CREATE TABLE test (id TEXT)", + "enabled": "False", + }, + any_times=True, + )