Semaphore/api/projects/backupRestore.go

52 lines
1.1 KiB
Go
Raw Normal View History

2024-01-20 23:46:43 +01:00
package projects
import (
"net/http"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
projectService "github.com/ansible-semaphore/semaphore/services/project"
"github.com/gorilla/context"
log "github.com/sirupsen/logrus"
2024-01-20 23:46:43 +01:00
)
func GetBackup(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
store := helpers.Store(r)
backup, err := projectService.GetBackup(project.ID, store)
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, backup)
}
func Restore(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "user").(*db.User)
2024-01-20 23:46:43 +01:00
var backup projectService.BackupFormat
2024-02-04 13:46:27 +01:00
var p *db.Project
var err error
2024-01-20 23:46:43 +01:00
if !helpers.Bind(w, r, &backup) {
helpers.WriteJSON(w, http.StatusBadRequest, backup)
return
}
2024-02-04 13:46:27 +01:00
store := helpers.Store(r)
if err = backup.Verify(); err != nil {
log.Error(err)
helpers.WriteError(w, err)
2024-02-04 13:46:27 +01:00
return
}
if p, err = backup.Restore(*user, store); err != nil {
2024-02-04 13:46:27 +01:00
log.Error(err)
helpers.WriteError(w, err)
2024-01-20 23:46:43 +01:00
return
}
2024-02-04 13:46:27 +01:00
helpers.WriteJSON(w, http.StatusOK, p)
2024-01-20 23:46:43 +01:00
}