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

Artifacts upload #917

Merged
merged 14 commits into from
Feb 23, 2024
Merged
Changes from 1 commit
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
107 changes: 60 additions & 47 deletions dispatcher/backend/src/routes/schedules/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,60 +225,56 @@ def get(self, schedule_name: str, token: AccessToken.Payload, session: so.Sessio

return jsonify(schedule)

@authenticate
@require_perm("schedules", "update")
@dbsession
def patch(
self, schedule_name: str, token: AccessToken.Payload, session: so.Session
):
"""Update all properties of a schedule but _id and most_recent_task"""
def _get_and_validate_patch(self, schedule, request):
"""Check patch request is valid

Current schedule is needed to check validity since the patch contains only
changes and some combination of fields are needed under some conditions, e.g.
you cannot change the task_name (offliner) without changing flags and image name
"""
update = UpdateSchema().load(request.get_json())
raise_if(not request.get_json(), ValidationError, "Update can't be empty")

# ensure we test flags according to new task_name if present
if (
"task_name" in update
and update["task_name"] != schedule.config["task_name"]
):
raise_if(
"flags" not in update,
ValidationError,
"Can't update offliner without updating flags",
)
raise_if(
"image" not in update or "name" not in update["image"],
ValidationError,
"Image name must be updated when offliner is changed",
)

schedule = dbm.Schedule.get(session, schedule_name, ScheduleNotFound)
flags_schema = ScheduleConfigSchema.get_offliner_schema(update["task_name"])
else:
flags_schema = ScheduleConfigSchema.get_offliner_schema(
schedule.config["task_name"]
)

try:
update = UpdateSchema().load(request.get_json())
raise_if(not request.get_json(), ValidationError, "Update can't be empty")

# ensure we test flags according to new task_name if present
if (
"task_name" in update
and update["task_name"] != schedule.config["task_name"]
):
raise_if(
"flags" not in update,
ValidationError,
"Can't update offliner without updating flags",
)
raise_if(
"image" not in update or "name" not in update["image"],
ValidationError,
"Image name must be updated when offliner is changed",
)
if "flags" in update:
flags_schema().load(update["flags"])

flags_schema = ScheduleConfigSchema.get_offliner_schema(
update["task_name"]
)
if "image" in update and "name" in update["image"]:
if "task_name" in update:
future_task_name = update["task_name"]
else:
flags_schema = ScheduleConfigSchema.get_offliner_schema(
schedule.config["task_name"]
)

if "flags" in update:
flags_schema().load(update["flags"])
future_task_name = schedule.config["task_name"]

if "image" in update and "name" in update["image"]:
if "task_name" in update:
future_task_name = update["task_name"]
else:
future_task_name = schedule.config["task_name"]
if Offliner.get_image_prefix(future_task_name) + update["image"][
"name"
] != Offliner.get_image_name(future_task_name):
raise ValidationError("Image name must match selected offliner")

if Offliner.get_image_prefix(future_task_name) + update["image"][
"name"
] != Offliner.get_image_name(future_task_name):
raise ValidationError("Image name must match selected offliner")
except ValidationError as e:
raise InvalidRequestJSON(e.messages)
return update

def _apply_patch_to_schedule(self, schedule, update):
"""Apply the patch update to the schedule"""
config_keys = [
"task_name",
"warehouse_path",
Expand Down Expand Up @@ -306,6 +302,23 @@ def patch(
# not allowed (yet) in UpdateSchema, only config keys can be set to null
setattr(schedule, key, value)

@authenticate
@require_perm("schedules", "update")
@dbsession
def patch(
self, schedule_name: str, token: AccessToken.Payload, session: so.Session
):
"""Update all properties of a schedule but _id and most_recent_task"""

schedule = dbm.Schedule.get(session, schedule_name, ScheduleNotFound)

try:
update = self._get_and_validate_patch(schedule=schedule, request=request)
rgaudin marked this conversation as resolved.
Show resolved Hide resolved
except ValidationError as e:
raise InvalidRequestJSON(e.messages)

self._apply_patch_to_schedule(schedule=schedule, update=update)

try:
session.flush()
except IntegrityError:
Expand Down
Loading