Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scheduled-jobs edit command #484

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion croud/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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."
)
],
Expand Down Expand Up @@ -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,
}
}
},
Expand Down
23 changes: 23 additions & 0 deletions croud/scheduledjobs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}/")
Expand Down
71 changes: 70 additions & 1 deletion tests/commands/test_scheduled_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Loading