2018-11-05 20:58:15 +00:00
|
|
|
package schedules
|
|
|
|
|
|
|
|
import (
|
2018-11-06 09:49:48 +00:00
|
|
|
"errors"
|
2018-11-05 20:58:15 +00:00
|
|
|
"net/http"
|
2018-11-07 04:19:10 +00:00
|
|
|
"strconv"
|
2018-11-05 20:58:15 +00:00
|
|
|
|
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
|
|
|
"github.com/portainer/portainer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (handler *Handler) scheduleDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
scheduleID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid schedule identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:49:48 +00:00
|
|
|
schedule, err := handler.ScheduleService.Schedule(portainer.ScheduleID(scheduleID))
|
|
|
|
if err == portainer.ErrObjectNotFound {
|
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a schedule with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a schedule with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if schedule.JobType == portainer.SnapshotJobType || schedule.JobType == portainer.EndpointSyncJobType {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Cannot remove system schedules", errors.New("Cannot remove system schedule")}
|
|
|
|
}
|
2018-11-05 20:58:15 +00:00
|
|
|
|
2018-11-07 04:19:10 +00:00
|
|
|
scheduleFolder := handler.FileService.GetScheduleFolder(strconv.Itoa(scheduleID))
|
2018-11-05 20:58:15 +00:00
|
|
|
err = handler.FileService.RemoveDirectory(scheduleFolder)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the files associated to the schedule on the filesystem", err}
|
|
|
|
}
|
|
|
|
|
2018-11-13 01:39:26 +00:00
|
|
|
handler.JobScheduler.UnscheduleJob(schedule.ID)
|
|
|
|
|
2018-11-05 20:58:15 +00:00
|
|
|
err = handler.ScheduleService.DeleteSchedule(portainer.ScheduleID(scheduleID))
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the schedule from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|